private static HbmMapping GetMapping() { var orm = new ObjectRelationalMapper(); var mapper = new Mapper(orm, new CoolPatternsAppliersHolder(orm)); orm.TablePerClassHierarchy<Product>(); orm.TablePerClass<ActorRole>(); orm.Patterns.PoidStrategies.Add( new GuidOptimizedPoidPattern()); orm.VersionProperty<Entity>(x => x.Version); orm.NaturalId<Product>(p => p.Name); orm.Cascade<Movie, ActorRole>( Cascade.All | Cascade.DeleteOrphans); mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(Decimal) && mi.Name.Contains("Price"), pm => pm.Type(NHibernateUtil.Currency)); mapper.AddPropertyPattern(mi => orm.IsRootEntity(mi.DeclaringType) && !"Description".Equals(mi.Name), pm => pm.NotNullable(true)); mapper.Subclass<Movie>(cm => cm.List(movie => movie.Actors, colm => colm.Index( lim => lim.Column("ActorIndex")), m => { })); var domainClasses = typeof(Entity).Assembly.GetTypes() .Where(t => typeof(Entity).IsAssignableFrom(t)); return mapper.CompileMappingFor(domainClasses); }
private void AppliesGeneralConventions(Mapper mapper) { const string softDeleteWhereClause = "DeletedOn is NULL"; // because VersionModelBase is not an entity I can use the it to customize the mapping of all inherited classes mapper.Class <VersionModelBase>(map => map.Where(softDeleteWhereClause)); // When the collection elements inherits from VersionModelBase then should apply the where-clause for "soft delete" mapper.AddCollectionPattern(mi => mi.GetPropertyOrFieldType().IsGenericCollection() && typeof(VersionModelBase).IsAssignableFrom( mi.GetPropertyOrFieldType().DetermineCollectionElementType()) , map => map.Where(softDeleteWhereClause)); // The default length for string is 100 // Note : because this convetion has no specific restriction other than typeof(string) should be added at first (the order, in this case, is important) mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(string), map => map.Length(100)); // When the property name is Description and is of type string then apply length 500 mapper.AddPropertyPattern(mi => mi.Name == "Description" && mi.GetPropertyOrFieldType() == typeof(string), map => map.Length(500)); // When the property is of type decimal and it refers to a price then applies Currency mapper.AddPropertyPattern(mi => mi.Name.EndsWith("Cost") && mi.GetPropertyOrFieldType() == typeof(decimal), map => map.Type(NHibernateUtil.Currency)); }
/// <summary> /// Initializes the mappings. /// </summary> /// <param name="config">The configuration.</param> public static void InitMappings(Configuration config) { var orm = new ObjectRelationalMapper(); var mapper = new Mapper(orm); mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(string) && !mi.Name.EndsWith("Text"), pm => pm.Length(50)); mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(string) && mi.Name.EndsWith("Text"), pm => pm.Type(NHibernateUtil.StringClob)); orm.Patterns.PoidStrategies.Add(new AssignedPoidPattern()); foreach (var componentDbInit in IoC.ResolveAllInstances<IComponentDbInit>()) { componentDbInit.InitMappings(orm, mapper); } // compile the mapping for the specified entities HbmMapping mappingDocument = mapper.CompileMappingFor(ListOfModelTypes()); // inject the mapping in NHibernate config.AddDeserializedMapping(mappingDocument, "Domain"); // fix up the schema SchemaMetadataUpdater.QuoteTableAndColumns(config); SessionFactory.SessionFactoryInstance = config.BuildSessionFactory(); }
public void ExecuteCustomDelegatedApplier() { var orm = new Mock<IDomainInspector>(); orm.Setup(m => m.IsEntity(It.IsAny<Type>())).Returns(true); orm.Setup(m => m.IsRootEntity(It.IsAny<Type>())).Returns(true); orm.Setup(m => m.IsTablePerClass(It.IsAny<Type>())).Returns(true); orm.Setup(m => m.IsPersistentId(It.Is<MemberInfo>(mi => mi.Name == "Id"))).Returns(true); orm.Setup(m => m.IsPersistentProperty(It.Is<MemberInfo>(mi => mi.Name != "Id"))).Returns(true); var mapper = new Mapper(orm.Object); mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(DateTime) && (mi.Name.StartsWith("Date") || mi.Name.EndsWith("Date")), pm => pm.Type(NHibernateUtil.Date)); var mapping = mapper.CompileMappingFor(new[] {typeof (MyClass)}); var hbmClass = mapping.RootClasses.Single(); var hbmProp = (HbmProperty) hbmClass.Properties.First(p => p.Name == "Date"); hbmProp.Type.name.Should().Be.EqualTo("Date"); hbmProp = (HbmProperty)hbmClass.Properties.First(p => p.Name == "DateOfMeeting"); hbmProp.Type.name.Should().Be.EqualTo("Date"); hbmProp = (HbmProperty)hbmClass.Properties.First(p => p.Name == "MeetingDate"); hbmProp.Type.name.Should().Be.EqualTo("Date"); hbmProp = (HbmProperty)hbmClass.Properties.First(p => p.Name == "StartAt"); hbmProp.Type.Should().Be.Null(); }
private void CustomizeColumns(Mapper mapper) { // in EditionQuotation all references to a class inherited from Cost are not nullables (this is only an example because you can do it one by one) mapper.AddManyToOnePattern( mi => mi.DeclaringType == typeof(EditionQuotation) && typeof(Cost).IsAssignableFrom(mi.GetPropertyOrFieldType()), map => map.NotNullable(true)); // in Quotation all string properties are not nullables (this is only an example because you can do it one by one) mapper.AddPropertyPattern( mi => mi.DeclaringType == typeof(Quotation) && mi.GetPropertyOrFieldType() == typeof(string), map => map.NotNullable(true)); // Quotation columns size customization (if you use a Validator framework you can implements a pattern-applier to customize columns size and "nullable" stuff) // Note: It is needed only for properties without a convention (the property Description has its convetion) mapper.Customize <Quotation>(map => { map.Property(q => q.Code, pm => pm.Length(10)); map.Property(q => q.Reference, pm => pm.Length(50)); map.Property(q => q.PaymentMethod, pm => pm.Length(50)); map.Property(q => q.TransportMethod, pm => pm.Length(50)); }); // Column customization for class ProductQuotation // Note: It is needed only for properties outside conventions mapper.Customize <ProductQuotation>(map => map.Property(pq => pq.Group, pm => { pm.Length(50); pm.Column("GroupName"); })); }
public void ExecuteCustomDelegatedApplier() { var orm = new Mock <IDomainInspector>(); orm.Setup(m => m.IsEntity(It.IsAny <Type>())).Returns(true); orm.Setup(m => m.IsRootEntity(It.IsAny <Type>())).Returns(true); orm.Setup(m => m.IsTablePerClass(It.IsAny <Type>())).Returns(true); orm.Setup(m => m.IsPersistentId(It.Is <MemberInfo>(mi => mi.Name == "Id"))).Returns(true); orm.Setup(m => m.IsPersistentProperty(It.Is <MemberInfo>(mi => mi.Name != "Id"))).Returns(true); var mapper = new Mapper(orm.Object); mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(DateTime) && (mi.Name.StartsWith("Date") || mi.Name.EndsWith("Date")), pm => pm.Type(NHibernateUtil.Date)); var mapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = mapping.RootClasses.Single(); var hbmProp = (HbmProperty)hbmClass.Properties.First(p => p.Name == "Date"); hbmProp.Type.name.Should().Be.EqualTo("Date"); hbmProp = (HbmProperty)hbmClass.Properties.First(p => p.Name == "DateOfMeeting"); hbmProp.Type.name.Should().Be.EqualTo("Date"); hbmProp = (HbmProperty)hbmClass.Properties.First(p => p.Name == "MeetingDate"); hbmProp.Type.name.Should().Be.EqualTo("Date"); hbmProp = (HbmProperty)hbmClass.Properties.First(p => p.Name == "StartAt"); hbmProp.Type.Should().Be.Null(); }
private static Mapper GetMapper() { var orm = GetORM(); var mapper = new Mapper(orm); mapper.AddPropertyPattern( m => orm.IsRootEntity(m.DeclaringType) && !m.Name.Equals("Description"), a => a.NotNullable(true)); return mapper; }
public void AddCustomDelegatedApplier() { var orm = new Mock <IDomainInspector>(); var mapper = new Mapper(orm.Object); var previousPropertyApplierCount = mapper.PatternsAppliers.Property.Count; mapper.AddPropertyPattern(mi => mi.Name.StartsWith("Date") || mi.Name.EndsWith("Date"), pm => pm.Type(NHibernateUtil.Date)); mapper.PatternsAppliers.Property.Count.Should().Be(previousPropertyApplierCount + 1); }
public void AddCustomDelegatedApplier() { var orm = new Mock<IDomainInspector>(); var mapper = new Mapper(orm.Object); var previousPropertyApplierCount = mapper.PatternsAppliers.Property.Count; mapper.AddPropertyPattern(mi => mi.Name.StartsWith("Date") || mi.Name.EndsWith("Date"), pm => pm.Type(NHibernateUtil.Date)); mapper.PatternsAppliers.Property.Count.Should().Be(previousPropertyApplierCount + 1); }
public void CallPatternAppliersOnNestedCompositeElements() { Mock<IDomainInspector> orm = GetMockedDomainInspector(); var mapper = new Mapper(orm.Object); bool reSimplePropertyCalled = false; bool reManyToOneCalled = false; mapper.AddPropertyPattern(mi => mi.DeclaringType == typeof(MyNestedComponent), pm => reSimplePropertyCalled = true); mapper.AddManyToOnePattern(mi => mi.DeclaringType == typeof(MyNestedComponent), pm => reManyToOneCalled = true); mapper.CompileMappingFor(new[] { typeof(MyClass) }); reSimplePropertyCalled.Should().Be.True(); reManyToOneCalled.Should().Be.True(); }
public void CallPatternAppliersOnNestedCompositeElements() { Mock <IDomainInspector> orm = GetMockedDomainInspector(); var mapper = new Mapper(orm.Object); bool reSimplePropertyCalled = false; bool reManyToOneCalled = false; mapper.AddPropertyPattern(mi => mi.DeclaringType == typeof(MyNestedComponent), pm => reSimplePropertyCalled = true); mapper.AddManyToOnePattern(mi => mi.DeclaringType == typeof(MyNestedComponent), pm => reManyToOneCalled = true); mapper.CompileMappingFor(new[] { typeof(MyClass) }); reSimplePropertyCalled.Should().Be.True(); reManyToOneCalled.Should().Be.True(); }
public void WhenRegisterApplierOnPropertyThenInvokeApplier() { Mock<IDomainInspector> orm = GetMockedDomainInspector(); var mapper = new Mapper(orm.Object); mapper.AddPropertyPattern(mi => mi.Name == "Level", pm => pm.Column("myLevelColumn")); HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Person) }); var rc = mapping.RootClasses.Single(); var map = rc.Properties.OfType<HbmMap>().Single(); var hbmCompositeMapKey = (HbmCompositeMapKey)map.Item; var hbmKeyProperty = (HbmKeyProperty)hbmCompositeMapKey.Properties.Single(p => p.Name == "Level"); hbmKeyProperty.Columns.Single().name.Should().Be("myLevelColumn"); }
public void WhenRegisterApplierOnPropertyThenInvokeApplier() { Mock <IDomainInspector> orm = GetMockedDomainInspector(); var mapper = new Mapper(orm.Object); mapper.AddPropertyPattern(mi => mi.Name == "Level", pm => pm.Column("myLevelColumn")); HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Person) }); var rc = mapping.RootClasses.Single(); var map = rc.Properties.OfType <HbmMap>().Single(); var hbmCompositeMapKey = (HbmCompositeMapKey)map.Item; var hbmKeyProperty = (HbmKeyProperty)hbmCompositeMapKey.Properties.Single(p => p.Name == "Level"); hbmKeyProperty.Columns.Single().name.Should().Be("myLevelColumn"); }
private static HbmMapping GetMapping() { var orm = new ObjectRelationalMapper(); var mapper = new Mapper(orm, new CoolPatternsAppliersHolder(orm)); mapper.AddPropertyPattern(IsLongString, p => p.Length(4001)); orm.Patterns.Poids.Add(IsId); orm.Patterns.PoidStrategies.Add(new AssignedPoidPattern()); var domainClasses = typeof (IEntity).Assembly.GetTypes() .Where(t => t.IsClass && !t.IsAbstract) .Where(t => typeof (IEntity).IsAssignableFrom(t)) .ToArray(); orm.TablePerClass(domainClasses); mapper.AddRootClassPattern(t => true, applier => applier.Lazy(false)); var mapping = mapper.CompileMappingFor(domainClasses); return mapping; }
public override void ConfigureMapping(Configuration configuration) { var orm = new ObjectRelationalMapper(); var mapper = new Mapper(orm); mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(string), pm => pm.Length(50)); orm.Patterns.PoidStrategies.Add(new AssignedPoidPattern()); // define the mapping shape // list all the entities we want to map. IEnumerable<Type> baseEntities = GetMappingTypes(); // we map all classes as Table per class orm.TablePerClass(baseEntities); // compile the mapping for the specified entities HbmMapping mappingDocument = mapper.CompileMappingFor(baseEntities); // inject the mapping in NHibernate configuration.AddDeserializedMapping(mappingDocument, "Domain"); SessionFactory.SessionFactoryInstance = configuration.BuildSessionFactory(); }
public override void ConfigureMapping(NHibernate.Cfg.Configuration configuration) { var orm = new ObjectRelationalMapper(); var mapper = new Mapper(orm); mapper.AddPropertyPattern(mi => TypeExtensions.GetPropertyOrFieldType(mi) == typeof(string), pm => pm.Length(50)); orm.Patterns.PoidStrategies.Add(new NativePoidPattern()); orm.Patterns.PoidStrategies.Add(new GuidOptimizedPoidPattern()); // list all the entities we want to map. IEnumerable<Type> baseEntities = GetMappingTypes(); // we map all classes as Table per class orm.TablePerClass(baseEntities); mapper.Customize<Hotel>(ca => ca.Collection(item => item.Reservations, cm => cm.Key(km => km.Column("HotelId")))); mapper.Customize<Room>(ca => ca.Collection(item => item.Reservations, cm => cm.Key(km => { km.Column("RoomId"); km.OnDelete(OnDeleteAction.NoAction); }))); mapper.Customize<Reservation>(ca => { ca.ManyToOne(item => item.Hotel, m => { m.Column("HotelId"); m.Insert(false); m.Update(false); m.Lazy(LazyRelation.Proxy); }); ca.ManyToOne(item => item.Room, m => { m.Column("RoomId"); m.Insert(false); m.Update(false); m.Lazy(LazyRelation.Proxy); }); }); // compile the mapping for the specified entities HbmMapping mappingDocument = mapper.CompileMappingFor(baseEntities); // inject the mapping in NHibernate configuration.AddDeserializedMapping(mappingDocument, "Domain"); // fix up the schema SchemaMetadataUpdater.QuoteTableAndColumns(configuration); SessionFactory.SessionFactoryInstance = configuration.BuildSessionFactory(); }
public void UsageOfCustomMulticolumnsTypeUsingPatternAppliers() { // In this example you can see how create setup ConfORM to map a multi-column usertype with parameters. var orm = new ObjectRelationalMapper(); orm.TablePerClass <LoveEvent>(); // In this case the custom type is used only for a specific property. orm.Complex <LoveEvent>(loveEvent => loveEvent.AllowedWeekDays); var mapper = new Mapper(orm); // In this case I'm using a PatternApplier (directly with delegates instead a specific class) to define the persistent representation of the bool array mapper.AddPropertyPattern(mi => typeof(bool[]).Equals(mi.GetPropertyOrFieldType()) && mi.Name.Contains("Days"), (mi, pm) => { pm.Type(typeof(MulticolumnsBoolArrayType), new { ArraySize = DaysOfTheWeekColumnsNames.Length }); pm.Columns(DaysOfTheWeekColumnsNames.Select(colName => new Action <IColumnMapper>(cm => cm.Name(mi.Name + colName))).ToArray()); }); var mapping = mapper.CompileMappingFor(new[] { typeof(LoveEvent) }); Console.Write(mapping.AsString()); }
private void AppliesGeneralConventions(Mapper mapper) { const string softDeleteWhereClause = "DeletedOn is NULL"; // because VersionModelBase is not an entity I can use the it to customize the mapping of all inherited classes mapper.Class<VersionModelBase>(map => map.Where(softDeleteWhereClause)); // When the collection elements inherits from VersionModelBase then should apply the where-clause for "soft delete" mapper.AddCollectionPattern(mi => mi.GetPropertyOrFieldType().IsGenericCollection() && typeof (VersionModelBase).IsAssignableFrom( mi.GetPropertyOrFieldType().DetermineCollectionElementType()) , map => map.Where(softDeleteWhereClause)); // The default length for string is 100 // Note : because this convetion has no specific restriction other than typeof(string) should be added at first (the order, in this case, is important) mapper.AddPropertyPattern(mi => mi.GetPropertyOrFieldType() == typeof(string), map => map.Length(100)); // When the property name is Description and is of type string then apply length 500 mapper.AddPropertyPattern(mi => mi.Name == "Description" && mi.GetPropertyOrFieldType() == typeof (string), map => map.Length(500)); // When the property is of type decimal and it refers to a price then applies Currency mapper.AddPropertyPattern(mi => mi.Name.EndsWith("Cost") && mi.GetPropertyOrFieldType() == typeof (decimal), map => map.Type(NHibernateUtil.Currency)); }
private void CustomizeColumns(Mapper mapper) { // in EditionQuotation all references to a class inherited from Cost are not nullables (this is only an example because you can do it one by one) mapper.AddManyToOnePattern( mi => mi.DeclaringType == typeof (EditionQuotation) && typeof (Cost).IsAssignableFrom(mi.GetPropertyOrFieldType()), map => map.NotNullable(true)); // in Quotation all string properties are not nullables (this is only an example because you can do it one by one) mapper.AddPropertyPattern( mi => mi.DeclaringType == typeof (Quotation) && mi.GetPropertyOrFieldType() == typeof (string), map => map.NotNullable(true)); // Quotation columns size customization (if you use a Validator framework you can implements a pattern-applier to customize columns size and "nullable" stuff) // Note: It is needed only for properties without a convention (the property Description has its convetion) mapper.Customize<Quotation>(map => { map.Property(q => q.Code, pm => pm.Length(10)); map.Property(q => q.Reference, pm => pm.Length(50)); map.Property(q => q.PaymentMethod, pm => pm.Length(50)); map.Property(q => q.TransportMethod, pm => pm.Length(50)); }); // Column customization for class ProductQuotation // Note: It is needed only for properties outside conventions mapper.Customize<ProductQuotation>(map => map.Property(pq => pq.Group, pm => { pm.Length(50); pm.Column("GroupName"); })); }