forked from nhibernate/fluent-nhibernate
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced new way to configure the automapper.
The automapper can (and should) now be configured by supplying a configuration instance -- an implementation of IAutomappingConfiguration -- which specifies how the automapper should behave when mapping the domain. All configuration options from the Setup action have been migrated, along with the Where method. The Setup-based usage is still available (although depreciated), but you can't mix-and-match the types; if a configuration instance is supplied the Setup and Where methods will throw. The automapper has also been modified to act on all members of a class, ra than just properties. The default is still to use properties, but the ShouldMap(Member) method can be overridden to use fields or any combination.
- Loading branch information
Showing
68 changed files
with
2,532 additions
and
1,685 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/FluentNHibernate.Specs/Automapping/AutoMapBackwardsCompatibilitySpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using FluentNHibernate.Automapping; | ||
using FluentNHibernate.Specs.Automapping.Fixtures; | ||
using Machine.Specifications; | ||
|
||
namespace FluentNHibernate.Specs.Automapping | ||
{ | ||
// just verify that the obsolete methods still work until we bin them entirely | ||
|
||
public class when_using_automap_obsolete_where_method_for_assembly_of : AutoMapObsoleteSpec | ||
{ | ||
Because of = () => | ||
AutoMap.AssemblyOf<EntityUsingPrivateFields>(where_clause) | ||
.BuildMappings(); | ||
|
||
It should_use_the_where_clause_provided = () => | ||
was_called.ShouldBeTrue(); | ||
} | ||
|
||
public class when_using_automap_obsolete_where_method_for_an_assembly : AutoMapObsoleteSpec | ||
{ | ||
Because of = () => | ||
AutoMap.Assembly(typeof(EntityUsingPrivateFields).Assembly, where_clause) | ||
.BuildMappings(); | ||
|
||
It should_use_the_where_clause_provided = () => | ||
was_called.ShouldBeTrue(); | ||
} | ||
|
||
public class when_using_automap_obsolete_where_method_for_a_source : AutoMapObsoleteSpec | ||
{ | ||
Because of = () => | ||
AutoMap.Source(new StubTypeSource(typeof(EntityUsingPrivateFields)), where_clause) | ||
.BuildMappings(); | ||
|
||
It should_use_the_where_clause_provided = () => | ||
was_called.ShouldBeTrue(); | ||
} | ||
|
||
public abstract class AutoMapObsoleteSpec | ||
{ | ||
protected static Func<Type, bool> where_clause = x => !(was_called = true); | ||
protected static bool was_called; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/FluentNHibernate.Specs/Automapping/AutoPersistenceModelSpecs.Conventions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Xml; | ||
using FluentNHibernate.Automapping; | ||
using FluentNHibernate.Conventions; | ||
using FluentNHibernate.Conventions.Instances; | ||
using FluentNHibernate.Specs.Automapping.Fixtures; | ||
using Machine.Specifications; | ||
|
||
namespace FluentNHibernate.Specs.Automapping | ||
{ | ||
public class when_the_automapper_maps_with_a_property_convention | ||
{ | ||
Establish context = () => | ||
mapper = AutoMap.Source(new StubTypeSource(typeof(Entity))) | ||
.Conventions.Add<XXAppenderPropertyConvention>(); | ||
|
||
Because of = () => | ||
xml = mapper.BuildMappingFor<Entity>().ToXml(); | ||
|
||
It should_apply_the_convention_to_any_properties = () => | ||
xml.Element("class/property[@name='One']/column").HasAttribute("name", "OneXX"); | ||
|
||
static AutoPersistenceModel mapper; | ||
static XmlDocument xml; | ||
|
||
class XXAppenderPropertyConvention : IPropertyConvention | ||
{ | ||
public void Apply(IPropertyInstance instance) | ||
{ | ||
instance.Column(instance.Name + "XX"); | ||
} | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/FluentNHibernate.Specs/Automapping/AutoPersistenceModelSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Xml; | ||
using FluentNHibernate.Automapping; | ||
using FluentNHibernate.Cfg; | ||
using FluentNHibernate.Cfg.Db; | ||
using FluentNHibernate.Specs.Automapping.Fixtures; | ||
using Machine.Specifications; | ||
|
||
namespace FluentNHibernate.Specs.Automapping | ||
{ | ||
public class when_the_automapper_is_told_to_map_a_single_type | ||
{ | ||
Because of = () => | ||
xml = AutoMap.Source(new StubTypeSource(typeof(Entity))) | ||
.BuildMappingFor<Entity>() | ||
.ToXml(); | ||
|
||
It should_only_return_one_class_in_the_xml = () => | ||
{ | ||
xml.Element("class").ShouldExist(); | ||
xml.Element("class[2]").ShouldNotExist(); | ||
}; | ||
|
||
It should_map_the_id = () => | ||
xml.Element("class/id").ShouldExist(); | ||
|
||
It should_map_properties = () => | ||
xml.Element("class/property[@name='One']").ShouldExist(); | ||
|
||
It should_map_enum_properties = () => | ||
xml.Element("class/property[@name='Enum']").ShouldExist(); | ||
|
||
It should_map_references = () => | ||
xml.Element("class/many-to-one[@name='Parent']").ShouldExist(); | ||
|
||
It should_map_collections = () => | ||
xml.Element("class/bag[@name='Children']").ShouldExist(); | ||
|
||
static XmlDocument xml; | ||
} | ||
|
||
public class when_the_automapper_is_ran_to_completion | ||
{ | ||
Establish context = () => | ||
setup = Fluently.Configure() | ||
.Database(SQLiteConfiguration.Standard.InMemory) | ||
.Mappings(x => x.AutoMappings.Add(AutoMap.Source(new StubTypeSource(typeof(Entity))))); | ||
|
||
Because of = () => | ||
ex = Catch.Exception(() => setup.BuildConfiguration()); | ||
|
||
It should_generate_xml_that_is_accepted_by_the_nhibernate_schema_validation = () => | ||
ex.ShouldBeNull(); | ||
|
||
static FluentConfiguration setup; | ||
static Exception ex; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/FluentNHibernate.Specs/Automapping/Fixtures/AutomappingSpecExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Xml; | ||
using FluentNHibernate.Automapping; | ||
using FluentNHibernate.MappingModel; | ||
using FluentNHibernate.MappingModel.ClassBased; | ||
using FluentNHibernate.MappingModel.Output; | ||
using Machine.Specifications; | ||
|
||
namespace FluentNHibernate.Specs.Automapping.Fixtures | ||
{ | ||
public static class AutomappingSpecExtensions | ||
{ | ||
public static ClassMapping BuildMappingFor<T>(this AutoPersistenceModel model) | ||
{ | ||
return model.BuildMappings() | ||
.SelectMany(x => x.Classes) | ||
.FirstOrDefault(x => x.Type == typeof(T)); | ||
} | ||
|
||
public static XmlDocument ToXml(this ClassMapping mapping) | ||
{ | ||
var hbm = new HibernateMapping(); | ||
|
||
hbm.AddClass(mapping); | ||
|
||
return new MappingXmlSerializer() | ||
.Serialize(hbm); | ||
} | ||
} | ||
|
||
public static class XmlTestExtensions | ||
{ | ||
public static XmlElementTester Element(this XmlDocument doc, string path) | ||
{ | ||
return new XmlElementTester(doc, path); | ||
} | ||
|
||
public class XmlElementTester | ||
{ | ||
readonly XmlDocument doc; | ||
string currentPath; | ||
XmlElement currentElement; | ||
|
||
public XmlElementTester(XmlDocument doc, string path) | ||
{ | ||
currentElement = (XmlElement)doc.DocumentElement.SelectSingleNode(path); | ||
this.doc = doc; | ||
currentPath = path; | ||
} | ||
|
||
public XmlElementTester ShouldExist() | ||
{ | ||
if (currentElement == null) | ||
throw new SpecificationException(string.Format("Should exist at {0} but does not.", currentPath)); | ||
|
||
return this; | ||
} | ||
|
||
public XmlElementTester ShouldNotExist() | ||
{ | ||
if (currentElement != null) | ||
throw new SpecificationException(string.Format("Should not exist at {0} but does.", currentPath)); | ||
|
||
return this; | ||
} | ||
|
||
public XmlElementTester HasAttribute(string name) | ||
{ | ||
if (!currentElement.HasAttribute(name)) | ||
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} but does not.", name, currentPath)); | ||
|
||
return this; | ||
} | ||
|
||
public XmlElementTester HasAttribute(string name, string value) | ||
{ | ||
ShouldExist(); | ||
HasAttribute(name); | ||
|
||
var actual = currentElement.GetAttribute(name); | ||
|
||
if (!actual.Equals(value)) | ||
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} with value of {2} but was {3}", name, currentPath, value, actual)); | ||
|
||
return this; | ||
} | ||
|
||
public XmlElementTester HasAttribute(string name, Func<string, bool> predicate) | ||
{ | ||
ShouldExist(); | ||
HasAttribute(name); | ||
|
||
var actual = currentElement.GetAttribute(name); | ||
|
||
if (!predicate(actual)) | ||
throw new SpecificationException(string.Format("Should have attribute named {0} at {1} with value matching predicate but does not.", name, currentPath)); | ||
|
||
return this; | ||
} | ||
|
||
public XmlElementTester DoesntHaveAttribute(string name) | ||
{ | ||
ShouldExist(); | ||
|
||
if (currentElement.HasAttribute(name)) | ||
throw new SpecificationException(string.Format("Should not have attribute named {0} at {1} but does.", name, currentPath)); | ||
|
||
return this; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.