public void ComponentMappingJustOnceDemo() { var mapper = new ModelMapper(); mapper.Component <Name>(comp => { comp.Property(name => name.First); comp.Property(name => name.Last); }); mapper.Component <Address>(comp => { comp.Property(address => address.CivicNumber); comp.Property(address => address.Street); }); mapper.Class <Person1>(cm => { cm.Id(person => person.Id, map => map.Generator(Generators.HighLow)); cm.Property(person => person.Test); cm.Component(person => person.Name, comp => { }); cm.Component(person => person.Address, comp => { }); }); var hbmMapping = mapper.CompileMappingForAllExplicitAddedEntities(); var hbmClass = hbmMapping.RootClasses[0]; var hbmComponents = hbmClass.Properties.OfType <HbmComponent>(); hbmComponents.Should().Have.Count.EqualTo(2); hbmComponents.Select(x => x.Name).Should().Have.SameValuesAs("Name", "Address"); }
public void ComponentMappingJustOnceDemo() { var mapper = new ModelMapper(); mapper.Component<Name>(comp => { comp.Property(name => name.First); comp.Property(name => name.Last); }); mapper.Component<Address>(comp => { comp.Property(address => address.CivicNumber); comp.Property(address => address.Street); }); mapper.Class<Person1>(cm => { cm.Id(person => person.Id, map => map.Generator(Generators.HighLow)); cm.Property(person => person.Test); cm.Component(person => person.Name, comp => { }); cm.Component(person => person.Address, comp => { }); }); var hbmMapping = mapper.CompileMappingForAllExplicitAddedEntities(); var hbmClass = hbmMapping.RootClasses[0]; var hbmComponents = hbmClass.Properties.OfType<HbmComponent>(); hbmComponents.Should().Have.Count.EqualTo(2); hbmComponents.Select(x => x.Name).Should().Have.SameValuesAs("Name","Address"); }
public void WhenMapPropertiesInTheInheritedThenMapInBase() { // without ignoring MyClass as root-class I will try to map all properties using the inherited class. // NH have to recognize the case and, following Object-Relational-Mapping rules, map those properties in the base class. // Where needed, using the SimpleModelInspector, the user can revert this behavior checking the DeclaringType and ReflectedType of the persistent member. var mapper = new ModelMapper(); mapper.Class <MyClass>(mc => mc.Id(x => x.Id)); mapper.JoinedSubclass <Inherited>(mc => { mc.Property(x => x.Simple, map => map.Access(Accessor.Field)); mc.Property(x => x.ComplexType, map => map.Access(Accessor.Field)); mc.Bag(x => x.Bag, y => y.Access(Accessor.Field)); mc.IdBag(x => x.IdBag, y => y.Access(Accessor.Field)); mc.List(x => x.List, y => y.Access(Accessor.Field)); mc.Set(x => x.Set, y => y.Access(Accessor.Field)); mc.Map(x => x.Map, y => y.Access(Accessor.Field)); mc.OneToOne(x => x.OneToOne, y => y.Access(Accessor.Field)); mc.ManyToOne(x => x.ManyToOne, y => y.Access(Accessor.Field)); mc.Any(x => x.Any, typeof(int), y => y.Access(Accessor.Field)); mc.Component(x => x.DynamicCompo, new { A = 2 }, y => y.Access(Accessor.Field)); mc.Component(x => x.Compo, y => { y.Access(Accessor.Field); y.Property(c => c.Something); }); }); var mappings = mapper.CompileMappingForAllExplicitAddedEntities(); var hbmClass = mappings.RootClasses[0]; var hbmJoinedSubclass = mappings.JoinedSubclasses[0]; hbmClass.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Simple", "ComplexType", "Bag", "IdBag", "List", "Set", "Map", "Compo", "OneToOne", "ManyToOne", "Any", "DynamicCompo"); hbmClass.Properties.Select(p => p.Access).All(x => x.Satisfy(access => access.Contains("field."))); hbmJoinedSubclass.Properties.Should().Be.Empty(); }
public void WhenMapPropertiesInTheInheritedUsingMemberNameThenMapInBase() { // without ignoring MyClass as root-class I will try to map all properties using the inherited class. // NH have to recognize the case and, following Object-Relational-Mapping rules, map those properties in the base class. var mapper = new ModelMapper(); mapper.Class <MyClass>(mc => mc.Id(x => x.Id)); mapper.JoinedSubclass <Inherited>(mc => { mc.Property("Simple", map => map.Access(Accessor.Field)); mc.Property("ComplexType", map => map.Access(Accessor.Field)); mc.Bag <string>("Bag", y => y.Access(Accessor.Field)); mc.IdBag <MyCompo>("IdBag", y => y.Access(Accessor.Field)); mc.List <string>("List", y => y.Access(Accessor.Field)); mc.Set <string>("Set", y => y.Access(Accessor.Field)); mc.Map <int, string>("Map", y => y.Access(Accessor.Field)); mc.OneToOne <Related>("OneToOne", y => y.Access(Accessor.Field)); mc.ManyToOne <Related>("ManyToOne", y => y.Access(Accessor.Field)); mc.Any <object>("Any", typeof(int), y => y.Access(Accessor.Field)); mc.Component("DynamicCompo", new { A = 2 }, y => y.Access(Accessor.Field)); mc.Component <MyCompo>("Compo", y => { y.Access(Accessor.Field); y.Property(c => c.Something); }); }); var mappings = mapper.CompileMappingForAllExplicitAddedEntities(); var hbmClass = mappings.RootClasses[0]; var hbmJoinedSubclass = mappings.JoinedSubclasses[0]; hbmClass.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Simple", "ComplexType", "Bag", "IdBag", "List", "Set", "Map", "Compo", "OneToOne", "ManyToOne", "Any", "DynamicCompo"); hbmClass.Properties.Select(p => p.Access).All(x => x.Satisfy(access => access.Contains("field."))); hbmJoinedSubclass.Properties.Should().Be.Empty(); }
protected override HbmMapping GetMappings() { // The impl/mapping of the bidirectional one-to-many sucks but was provided as is var mapper = new ModelMapper(); mapper.BeforeMapClass += (i, t, cm) => cm.Id(map => { map.Column((t.Name + "Id").ToUpperInvariant()); map.Generator(Generators.HighLow, g => g.Params(new { max_lo = "1000" })); }); mapper.Class <Customer>(ca => { ca.Lazy(false); ca.Id(x => x.Id, m => { }); ca.NaturalId(x => x.Property(c => c.Name, p => p.NotNullable(true))); ca.Property(x => x.Address, p => p.Lazy(true)); ca.Set(c => c.Orders, c => { c.Key(x => x.Column("CUSTOMERID")); c.Inverse(true); c.Cascade(Mapping.ByCode.Cascade.All); }, c => c.OneToMany()); }); mapper.Class <Order>(cm => { cm.Id(x => x.Id, m => { }); cm.Property(x => x.Date); cm.ManyToOne(x => x.Customer, map => map.Column("CUSTOMERID")); }); return(mapper.CompileMappingForAllExplicitAddedEntities()); }
protected override HbmMapping GetMappings() { // The impl/mapping of the bidirectional one-to-many sucks but was provided as is var mapper = new ModelMapper(); mapper.BeforeMapClass += (i, t, cm) => cm.Id(map => { map.Column((t.Name + "Id").ToUpperInvariant()); map.Generator(Generators.HighLow, g => g.Params(new { max_lo = "1000" })); }); mapper.Class<Customer>(ca => { ca.Lazy(false); ca.Id(x => x.Id, m => { }); ca.NaturalId(x => x.Property(c => c.Name, p => p.NotNullable(true))); ca.Property(x => x.Address, p => p.Lazy(true)); ca.Set(c => c.Orders, c => { c.Key(x => x.Column("CUSTOMERID")); c.Inverse(true); c.Cascade(Mapping.ByCode.Cascade.All); }, c => c.OneToMany()); }); mapper.Class<Order>(cm => { cm.Id(x => x.Id, m => { }); cm.Property(x => x.Date); cm.ManyToOne(x => x.Customer, map => map.Column("CUSTOMERID")); }); return mapper.CompileMappingForAllExplicitAddedEntities(); }
public void WhenDuplicateClassDoesNotDuplicateMapping() { var mapper = new ModelMapper(); mapper.Class <MyClass>(ca => { ca.Id(x => x.Id, map => { map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 })); }); ca.Property(x => x.Something); }); mapper.Class <MyClass>(ca => { ca.Id(x => x.Id, map => { map.Column("MyClassId"); }); ca.Property(x => x.Something, map => map.Length(150)); }); var hbmMapping = mapper.CompileMappingForAllExplicitAddedEntities(); ModelIsWellFormed(hbmMapping); }
public void WhenRegisterClassMappingThroughTypeThenGetMapping() { var mapper = new ModelMapper(); mapper.AddMapping(typeof(MyClassMap)); var hbmMapping = mapper.CompileMappingForAllExplicitAddedEntities(); ModelIsWellFormed(hbmMapping); }
protected virtual void AddDefaultMapping(NHibernate.Cfg.Configuration cfg) { ModelMapper mm = new ModelMapper(); mm.Class <ContentItem>(ContentItemCustomization); mm.Class <ContentDetail>(ContentDetailCustomization); mm.Class <DetailCollection>(DetailCollectionCustomization); mm.Class <AuthorizedRole>(AuthorizedRoleCustomization); var compiledMapping = mm.CompileMappingForAllExplicitAddedEntities(); var debugXml = compiledMapping.AsString(); cfg.AddDeserializedMapping(compiledMapping, "N2"); }
public void ApplyTo(Configuration configuration) { var mapper = new ModelMapper(); mapper.Class<Node>(node => { node.Id(x => x.Id, map => { map.Column("NodeId"); map.Generator(Generators.GuidComb); }); node.Property(x => x.Name, map => { map.Length(50); map.Column("NodeName"); }); node.Bag(x => x.Connections, bag => { bag.Key(key => { key.Column("StartNodeId"); key.ForeignKey("FK_RelatedNode_Node_StartNodeId"); }); bag.Table("Connection"); bag.Cascade(Cascade.All.Include(Cascade.Remove)); bag.Fetch(CollectionFetchMode.Subselect); }); }); mapper.Component<Connection>(connection => { connection.Parent(x => x.Start); connection.ManyToOne(x => x.End, manyToOne => { manyToOne.Column("EndNodeId"); manyToOne.ForeignKey("FK_RelatedNode_Node_EndNodeId"); manyToOne.Cascade(Cascade.Persist); manyToOne.NotNullable(true); }); connection.Component(x => x.Quality, c => {}); }); mapper.Component<ConnectionQuality>(connectionQuality => { connectionQuality.Property(x => x.UploadMbps); connectionQuality.Property(x => x.DownloadMbps); }); configuration.AddMapping(mapper.CompileMappingForAllExplicitAddedEntities()); }
public void ApplyTo(Configuration configuration) { var mapper = new ModelMapper(); mapper.Class<Thing>(node => { node.Id(x => x.Id, map => { map.Column("TestModel1Id"); map.Generator(Generators.GuidComb); }); node.Property(x => x.StringProperty, map => map.Length(50)); node.Property(x => x.BoolProperty); node.Property(x => x.DateProperty); node.Property(x => x.IntProperty); node.Property(x => x.DecimalProperty); }); configuration.AddMapping(mapper.CompileMappingForAllExplicitAddedEntities()); }
public void WhenMapHiloToDifferentSchemaThanClassThenIdHasTheMappedSchema() { var mapper = new ModelMapper(); mapper.Class<MyClass>(cm => { cm.Schema("aSchema"); cm.Id(x => x.Id, idm => idm.Generator(Generators.HighLow, gm => gm.Params(new { table = "hilosequences", schema="gSchema" }))); }); var conf = new Configuration(); conf.DataBaseIntegration(x=> x.Dialect<MsSql2008Dialect>()); conf.AddDeserializedMapping(mapper.CompileMappingForAllExplicitAddedEntities(), "wholeDomain"); var mappings = conf.CreateMappings(Dialect.Dialect.GetDialect()); var pc = mappings.GetClass(typeof(MyClass).FullName); ((SimpleValue)pc.Identifier).IdentifierGeneratorProperties["schema"].Should().Be("gSchema"); }
private HbmMapping GetMappingWithParentInCompo() { var mapper = new ModelMapper(); mapper.Class<MyClass>(x => { x.Id(c => c.Id); x.Component(c => c.Compo); x.Bag(c => c.Compos, cm => { }); }); mapper.Component<MyCompo>(x => { x.Parent(c => c.AManyToOne); x.Component(c => c.NestedCompo); }); mapper.Component<MyNestedCompo>(x => { x.Component(c => c.Owner); x.Property(c => c.Something); }); return mapper.CompileMappingForAllExplicitAddedEntities(); }
public void WhenMapPropertiesInTheBaseJumpedClassUsingMemberNameThenMapInInherited() { // ignoring MyClass and using Inherited, as root-class, I will try to map all properties using the base class. // NH have to recognize the case and map those properties in the inherited. var inspector = new SimpleModelInspector(); inspector.IsEntity((type, declared) => type == typeof(Inherited)); inspector.IsRootEntity((type, declared) => type == typeof(Inherited)); var mapper = new ModelMapper(inspector); mapper.Class <MyClass>(mc => { mc.Id(x => x.Id); mc.Property("Simple", map => map.Access(Accessor.Field)); mc.Property("ComplexType", map => map.Access(Accessor.Field)); mc.Bag <string>("Bag", y => y.Access(Accessor.Field)); mc.IdBag <MyCompo>("IdBag", y => y.Access(Accessor.Field)); mc.List <string>("List", y => y.Access(Accessor.Field)); mc.Set <string>("Set", y => y.Access(Accessor.Field)); mc.Map <int, string>("Map", y => y.Access(Accessor.Field)); mc.OneToOne <Related>("OneToOne", y => y.Access(Accessor.Field)); mc.ManyToOne <Related>("ManyToOne", y => y.Access(Accessor.Field)); mc.Any <object>("Any", typeof(int), y => y.Access(Accessor.Field)); mc.Component("DynamicCompo", new { A = 2 }, y => y.Access(Accessor.Field)); mc.Component <MyCompo>("Compo", y => { y.Access(Accessor.Field); y.Property(c => c.Something); }); }); mapper.Class <Inherited>(mc => { }); HbmMapping mappings = mapper.CompileMappingForAllExplicitAddedEntities(); HbmClass hbmClass = mappings.RootClasses[0]; mappings.JoinedSubclasses.Should().Be.Empty(); hbmClass.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Simple", "ComplexType", "Bag", "IdBag", "List", "Set", "Map", "Compo", "OneToOne", "ManyToOne", "Any", "DynamicCompo"); hbmClass.Properties.Select(p => p.Access).All(x => x.Satisfy(access => access.Contains("field."))); }
private HbmMapping GetMappingWithParentInCompo() { var mapper = new ModelMapper(); mapper.Class <MyClass>(x => { x.Id(c => c.Id); x.Component(c => c.Compo); x.Bag(c => c.Compos, cm => { }); }); mapper.Component <MyCompo>(x => { x.Parent(c => c.AManyToOne); x.Component(c => c.NestedCompo); }); mapper.Component <MyNestedCompo>(x => { x.Component(c => c.Owner); x.Property(c => c.Something); }); return(mapper.CompileMappingForAllExplicitAddedEntities()); }
public void WhenMapHiloToDifferentSchemaThanClassThenIdHasTheMappedSchema() { var mapper = new ModelMapper(); mapper.Class <MyClass>(cm => { cm.Schema("aSchema"); cm.Id(x => x.Id, idm => idm.Generator(Generators.HighLow, gm => gm.Params(new { table = "hilosequences", schema = "gSchema" }))); }); var conf = new Configuration(); conf.DataBaseIntegration(x => x.Dialect <MsSql2008Dialect>()); conf.AddDeserializedMapping(mapper.CompileMappingForAllExplicitAddedEntities(), "wholeDomain"); var mappings = conf.CreateMappings(Dialect.Dialect.GetDialect()); var pc = mappings.GetClass(typeof(MyClass).FullName); ((SimpleValue)pc.Identifier).IdentifierGeneratorProperties["schema"].Should().Be("gSchema"); }
protected override HbmMapping GetMappings() { var mapper = new ModelMapper(); mapper.BeforeMapClass += (mi, t, cm) => cm.Id(im => im.Generator(Generators.HighLow)); mapper.Class <Animal>(mc => { mc.Id(x => x.Id); mc.Discriminator(dm => dm.Column("kind")); mc.Property(x => x.Description); }); mapper.Subclass <Reptile>(mc => { mc.Property(x => x.BodyTemperature); }); mapper.Subclass <Human>(mc => { mc.Property(x => x.Name); mc.Property(x => x.NickName); mc.Property(x => x.Birthdate, pm => pm.Type(NHibernateUtil.Date)); }); mapper.AddMapping <FamilyMap <Reptile> >(); mapper.AddMapping <FamilyMap <Human> >(); var mappings = mapper.CompileMappingForAllExplicitAddedEntities(); return(mappings); }
public void WhenMapPropertiesInTheInheritedUsingMemberNameThenMapInBase() { // without ignoring MyClass as root-class I will try to map all properties using the inherited class. // NH have to recognize the case and, following Object-Relational-Mapping rules, map those properties in the base class. var mapper = new ModelMapper(); mapper.Class<MyClass>(mc => mc.Id(x => x.Id)); mapper.JoinedSubclass<Inherited>(mc => { mc.Property("Simple", map => map.Access(Accessor.Field)); mc.Property("ComplexType", map => map.Access(Accessor.Field)); mc.Bag<string>("Bag", y => y.Access(Accessor.Field)); mc.IdBag<MyCompo>("IdBag", y => y.Access(Accessor.Field)); mc.List<string>("List", y => y.Access(Accessor.Field)); mc.Set<string>("Set", y => y.Access(Accessor.Field)); mc.Map<int, string>("Map", y => y.Access(Accessor.Field)); mc.OneToOne<Related>("OneToOne", y => y.Access(Accessor.Field)); mc.ManyToOne<Related>("ManyToOne", y => y.Access(Accessor.Field)); mc.Any<object>("Any", typeof(int), y => y.Access(Accessor.Field)); mc.Component("DynamicCompo", new { A = 2 }, y => y.Access(Accessor.Field)); mc.Component<MyCompo>("Compo", y => { y.Access(Accessor.Field); y.Property(c => c.Something); }); }); var mappings = mapper.CompileMappingForAllExplicitAddedEntities(); var hbmClass = mappings.RootClasses[0]; var hbmJoinedSubclass = mappings.JoinedSubclasses[0]; hbmClass.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Simple", "ComplexType", "Bag", "IdBag", "List", "Set", "Map", "Compo", "OneToOne", "ManyToOne", "Any", "DynamicCompo"); hbmClass.Properties.Select(p => p.Access).All(x => x.Satisfy(access => access.Contains("field."))); hbmJoinedSubclass.Properties.Should().Be.Empty(); }
public void WhenMapPropertiesInTheInheritedThenMapInBase() { // without ignoring MyClass as root-class I will try to map all properties using the inherited class. // NH have to recognize the case and, following Object-Relational-Mapping rules, map those properties in the base class. // Where needed, using the SimpleModelInspector, the user can revert this behavior checking the DeclaringType and ReflectedType of the persistent member. var mapper = new ModelMapper(); mapper.Class<MyClass>(mc => mc.Id(x => x.Id)); mapper.JoinedSubclass<Inherited>(mc => { mc.Property(x => x.Simple, map => map.Access(Accessor.Field)); mc.Property(x => x.ComplexType, map => map.Access(Accessor.Field)); mc.Bag(x => x.Bag, y => y.Access(Accessor.Field)); mc.IdBag(x => x.IdBag, y => y.Access(Accessor.Field)); mc.List(x => x.List, y => y.Access(Accessor.Field)); mc.Set(x => x.Set, y => y.Access(Accessor.Field)); mc.Map(x => x.Map, y => y.Access(Accessor.Field)); mc.OneToOne(x => x.OneToOne, y => y.Access(Accessor.Field)); mc.ManyToOne(x => x.ManyToOne, y => y.Access(Accessor.Field)); mc.Any(x => x.Any, typeof(int), y => y.Access(Accessor.Field)); mc.Component(x => x.DynamicCompo, new { A=2 }, y => y.Access(Accessor.Field)); mc.Component(x => x.Compo, y => { y.Access(Accessor.Field); y.Property(c => c.Something); }); }); var mappings = mapper.CompileMappingForAllExplicitAddedEntities(); var hbmClass = mappings.RootClasses[0]; var hbmJoinedSubclass = mappings.JoinedSubclasses[0]; hbmClass.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Simple", "ComplexType", "Bag", "IdBag", "List", "Set", "Map", "Compo", "OneToOne", "ManyToOne", "Any", "DynamicCompo"); hbmClass.Properties.Select(p => p.Access).All(x=> x.Satisfy(access=> access.Contains("field."))); hbmJoinedSubclass.Properties.Should().Be.Empty(); }
public void WhenMapPropertiesInTheBaseJumpedClassUsingMemberNameThenMapInInherited() { // ignoring MyClass and using Inherited, as root-class, I will try to map all properties using the base class. // NH have to recognize the case and map those properties in the inherited. var inspector = new SimpleModelInspector(); inspector.IsEntity((type, declared) => type == typeof(Inherited)); inspector.IsRootEntity((type, declared) => type == typeof(Inherited)); var mapper = new ModelMapper(inspector); mapper.Class<MyClass>(mc => { mc.Id(x => x.Id); mc.Property("Simple", map => map.Access(Accessor.Field)); mc.Property("ComplexType", map => map.Access(Accessor.Field)); mc.Bag<string>("Bag", y => y.Access(Accessor.Field)); mc.IdBag<MyCompo>("IdBag", y => y.Access(Accessor.Field)); mc.List<string>("List", y => y.Access(Accessor.Field)); mc.Set<string>("Set", y => y.Access(Accessor.Field)); mc.Map<int, string>("Map", y => y.Access(Accessor.Field)); mc.OneToOne<Related>("OneToOne", y => y.Access(Accessor.Field)); mc.ManyToOne<Related>("ManyToOne", y => y.Access(Accessor.Field)); mc.Any<object>("Any", typeof (int), y => y.Access(Accessor.Field)); mc.Component("DynamicCompo", new {A = 2}, y => y.Access(Accessor.Field)); mc.Component<MyCompo>("Compo", y => { y.Access(Accessor.Field); y.Property(c => c.Something); }); }); mapper.Class<Inherited>(mc => { }); HbmMapping mappings = mapper.CompileMappingForAllExplicitAddedEntities(); HbmClass hbmClass = mappings.RootClasses[0]; mappings.JoinedSubclasses.Should().Be.Empty(); hbmClass.Properties.Select(p => p.Name).Should().Have.SameValuesAs("Simple", "ComplexType", "Bag", "IdBag", "List", "Set", "Map", "Compo", "OneToOne", "ManyToOne", "Any", "DynamicCompo"); hbmClass.Properties.Select(p => p.Access).All(x => x.Satisfy(access => access.Contains("field."))); }
protected virtual void AddDefaultMapping(NHibernate.Cfg.Configuration cfg) { ModelMapper mm = new ModelMapper(); mm.Class<ContentItem>(ContentItemCustomization); mm.Class<ContentDetail>(ContentDetailCustomization); mm.Class<DetailCollection>(DetailCollectionCustomization); mm.Class<AuthorizedRole>(AuthorizedRoleCustomization); var compiledMapping = mm.CompileMappingForAllExplicitAddedEntities(); var debugXml = compiledMapping.AsString(); cfg.AddDeserializedMapping(compiledMapping, "N2"); }
public void WhenDuplicateClassDoesNotDuplicateMapping() { var mapper = new ModelMapper(); mapper.Class<MyClass>(ca => { ca.Id(x => x.Id, map => { map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 })); }); ca.Property(x => x.Something); }); mapper.Class<MyClass>(ca => { ca.Id(x => x.Id, map => { map.Column("MyClassId"); }); ca.Property(x => x.Something, map => map.Length(150)); }); var hbmMapping = mapper.CompileMappingForAllExplicitAddedEntities(); ModelIsWellFormed(hbmMapping); }