public ConventionMappingCompiler(string tablePrefix, IAttributeMapperFactory attributeMapperFactory) { _mapper = new ConventionModelMapper(tablePrefix) { AttributeMapperFactory = attributeMapperFactory }; }
public void KnowConfigureInjection() { var modelMapper = new ConventionModelMapper(); var configurator = modelMapper.InjectionConfigurator <Foo>() .FillProperty(p => p.PropertyString, () => "lorem") .FillProperty(p => p.PropertyDouble, () => 1); modelMapper.GetInjector <Foo>().Should().Be(configurator); }
public void GetTableInfoWithoutAutoIncrementKey() { var modelMapper = new ConventionModelMapper(); var tableInfo = modelMapper.GetTableInfo <Foo>(); tableInfo.PrimaryKey .Any(p => p.AutoIncrementMethodType != AutoIncrementMethodType.None) .Should().BeFalse(); }
public void GetTableInfoWithoutNoMap() { var modelMapper = new ConventionModelMapper(); ((IModelMapperInternal)modelMapper).SetNoMap <Foo>("LastName"); var tableInfo = modelMapper.GetTableInfo <Foo>(); tableInfo.GetColumnInfo("LastName").Should().BeNull(); }
public void KnowConfigureInjectionExternal() { var modelMapper = new ConventionModelMapper(); var configurator = new InjectionConfiguration <Foo>() .FillProperty(p => p.PropertyDouble, () => 1); ((IModelMapperInternal)modelMapper).SetInjector <Foo>((IInjector)configurator); modelMapper.GetInjector <Foo>().Should().Be(configurator); }
public void GetTableInfoWithoutReadOnlyProperty() { var modelMapper = new ConventionModelMapper(); var tableInfo = modelMapper.GetTableInfo <Foo>(); var columns = tableInfo.Columns.ToList(); tableInfo.GetColumnInfo("ReadOnlyProperty").Should().BeNull(); }
void MapDirectionalRelationship(ConventionModelMapper mapper) { mapper.Subclass <DirectionalRelationship>(map => { map.DiscriminatorValue(RelationshipType.Directional); map.Property(x => x.SecondarySummary, p => { p.NotNullable(false); }); }); }
public void WhenReadOnlyPropertyWithSameBackFieldNoMatch() { var mapper = new ConventionModelMapper(); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; var hbmProperty = hbmClass.Properties.Single(x => x.Name == "ReadOnlyWithSameBackField"); Assert.That(hbmProperty.Access, Does.Not.Contain("field")); }
public void UseSetsTableName() { var modelMapper = new ConventionModelMapper(); (modelMapper as IModelMapperInternal).SetTableName <Foo>("Foo2"); var tableInfo = modelMapper.GetTableInfo <Foo>(); tableInfo.Name.Should().Be("Foo2"); }
public void WhenAutoPropertyNoAccessor() { var mapper = new ConventionModelMapper(); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; var hbmProperty = hbmClass.Properties.Single(x => x.Name == "AProp"); Assert.That(hbmProperty.Access, Is.Null.Or.Empty); }
public void WhenPropertyWithSameBackFieldNoMatch() { var mapper = new ConventionModelMapper(); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; var hbmProperty = hbmClass.Properties.Single(x => x.Name == "SameTypeOfBackField"); Assert.That(hbmProperty.Access, Is.Null.Or.Empty); }
public void WhenSetOnlyPropertyNoMatch() { var mapper = new ConventionModelMapper(); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; var hbmProperty = hbmClass.Properties.Single(x => x.Name == "SetOnlyProperty"); Assert.That(hbmProperty.Access, Is.Null.Or.Not.Contain("field")); }
public void WhenPropertyWithDifferentBackFieldMatch() { var mapper = new ConventionModelMapper(); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; var hbmProperty = hbmClass.Properties.Single(x => x.Name == "WithDifferentBackField"); hbmProperty.Access.Should().Contain("field"); }
protected override void CustomizeMapping(ConventionModelMapper mapper) { mapper.Class <Foo>(cm => cm.IdBag(x => x.Bars, bpm => { bpm.Cascade(Cascade.All); bpm.Type <PersistentQueryableIdBagType <Bar> >(); }, cer => cer.ManyToMany())); }
protected override HbmMapping GetMappings() { var mapper = new ConventionModelMapper(); mapper.IsTablePerClass((type, declared) => false); mapper.IsTablePerClassHierarchy((type, declared) => true); var mappings = mapper.CompileMappingFor(new[] { typeof(Animal), typeof(Reptile), typeof(Mammal), typeof(Lizard), typeof(Dog), typeof(Cat) }); return(mappings); }
public void WhenPoidNoSetterThenApplyNosetter() { var mapper = new ConventionModelMapper(); mapper.Class <MyClass>(x => x.Id(mc => mc.Id)); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; Assert.That(hbmClass.Id.access, Is.EqualTo("nosetter.camelcase-underscore")); }
/// <summary> /// anything that doesn't fit in automapping conventions must be handled with custom mapping. /// do them here. /// </summary> /// <param name="mapper"></param> static void OverrideMapping(ConventionModelMapper mapper) { //mapper.Class<Ddd._CoreDomain.Setting>(x => //{ // x.Id(id => id.Key, idMapper => // { // idMapper.Column("key"); // idMapper.Generator(NHibernate.Mapping.ByCode.Generators.Assigned); // }); //}); }
public void UseAliasAttributeForGettingNames() { var modelMapper = new ConventionModelMapper(); var tableInfo = modelMapper.GetTableInfo <Foo>(); var columns = tableInfo.Columns.ToList(); columns[1].Name.Should().Be("PostCode", "Becouse have alias."); columns[2].Name.Should().Be("FirstName", "Becouse have alias."); }
public void GetTableInfoWithColumnConverterSetByConfiguration() { var modelMapper = new ConventionModelMapper(); ((IModelMapperInternal)modelMapper).SetConverter <Foo>("LastName", new TestConverter()); var tableInfo = modelMapper.GetTableInfo <Foo>(); var columnWithConverter = tableInfo.Columns.Single(c => c.Name == "LastName"); columnWithConverter.Converter.Should().BeOfType <TestConverter>(); }
public void ApplyMapping(ConventionModelMapper mapper) { mapper.BeforeMapClass += (modelInspector, type, classCustomizer) => { classCustomizer.Id(type.GetProperty(IdentityPropertyName, IdentityBindingFlags), m => { m.Generator(new NativeGeneratorDef()); m.Type(new NHibernate.Type.Int64Type()); m.Column(formatter.GetIdentityColumnName(type)); }); }; }
/// <summary> /// Looks through this assembly for any IOverride classes. If found, it creates an instance /// of each and invokes the Override(mapper) method, accordingly. /// </summary> private static void AddConventionOverrides(ConventionModelMapper mapper) { Type overrideType = typeof(IOverride); List <Type> types = typeof(IOverride).Assembly.GetTypes() .Where(t => overrideType.IsAssignableFrom(t) && t != typeof(IOverride)) .ToList(); types.ForEach(t => { IOverride conventionOverride = Activator.CreateInstance(t) as IOverride; conventionOverride.Override(mapper); }); }
public void WhenFieldAccessToField() { var mapper = new ConventionModelMapper(); mapper.Class <MyClass>(mc => mc.Property("aField", x => { })); var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) }); var hbmClass = hbmMapping.RootClasses[0]; var hbmProperty = hbmClass.Properties.Single(x => x.Name == "aField"); Assert.That(hbmProperty.Access, Is.EqualTo("field")); }
public void GetTableInfoWithPrimaryKeyByConvention() { var modelMapper = new ConventionModelMapper(); var tableInfo = modelMapper.GetTableInfo <Foo1>(); var key = tableInfo.PrimaryKey.ToList(); key.Should().HaveCount(1, "Becouse Foo1 dont have key attribute, but have one property which match Id convention"); key[0].Name.Should().Be("Id"); key[0].IsPrimaryKey.Should().BeTrue(); }
public void UseConventionForGettingNamesWhenAliasDoesNotExist() { var modelMapper = new ConventionModelMapper(); var tableInfo = modelMapper.GetTableInfo <Foo>(); var columns = tableInfo.Columns.ToList(); columns[0].Name.Should().Be("Id"); columns[3].Name.Should().Be("LastName"); columns[4].Name.Should().Be("PropertyDouble"); }
public void GetTableInfoWithoutPrimarKeyWhenDontHaveKeyAttributeAndNoConventionMatch() { var modelMapper = new ConventionModelMapper(); var tableInfo = modelMapper.GetTableInfo <Foo2>(); var key = tableInfo.PrimaryKey.ToList(); bool anyKey = key.Any(p => p.IsPrimaryKey); key.Should().HaveCount(0); anyKey.Should().BeFalse(); }
private static void DefineBaseClass(ConventionModelMapper mapper, System.Type[] baseEntityToIgnore) { if (baseEntityToIgnore == null) { return; } mapper.IsEntity((type, declared) => baseEntityToIgnore.Any(x => x.IsAssignableFrom(type)) && !baseEntityToIgnore.Any(x => x == type) && !type.IsInterface); mapper.IsRootEntity((type, declared) => baseEntityToIgnore.Any(x => x == type.BaseType)); }
public void ThrowExceptionWhenUseCurrentTimeGeneratorOnNever() { var modelBuilder = new ModelConfigurationBuilder(); var modelMapper = new ConventionModelMapper(); Action act = () => modelBuilder.Entity <BuilderTestEntity>() .Property(p => p.GeneratedValue) .UseCurrentTimeValueGenerator(ValueGenerated.Never); act.Should().Throw <NotSupportedException>(); }
public void ApplyMapping(ConventionModelMapper mapper) { mapper.BeforeMapManyToOne += (modelInspector, member, propertyCustomizer) => { var parentType = member.LocalMember.GetPropertyOrFieldType(); var childType = member.LocalMember.ReflectedType; propertyCustomizer.Column(formatter.GetIdentityColumnName(parentType)); propertyCustomizer.ForeignKey(formatter.GetForeignKeyConstraintName(parentType, childType)); propertyCustomizer.Index(formatter.GetIndexName(childType, parentType)); propertyCustomizer.Cascade(Cascade.Persist); }; }
private void DefineBaseClass(ConventionModelMapper mapper) { if (BaseEntityToIgnore == null) { return; } mapper.IsEntity((type, declared) => BaseEntityToIgnore.IsAssignableFrom(type) && BaseEntityToIgnore != type && !type.IsInterface); mapper.IsRootEntity((type, declared) => type.BaseType == BaseEntityToIgnore); }
public void ApplyMapping(ConventionModelMapper mapper) { mapper.Class <Project>(map => { map.Set(x => x.Contributors, set => { set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), UserMapping.Contributor, typeof(User))); }); map.Set(x => x.Administrators, set => { set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), UserMapping.Administrator, typeof(User))); }); }); }
void MapRelationship(ConventionModelMapper mapper) { mapper.Class <Relationship>(map => { map.Discriminator(d => { d.Column(nameFormatter.GetColumnName(Reflect.Property <Relationship>(x => x.Type))); }); map.Property(p => p.Behaviour, m => { m.Type <JsonSerializedType <RelationshipBehaviour> >(new { AllowNull = Boolean.FalseString }); }); }); }
private Configuration ConfigureNHibernate() { var config = new Configuration(); config.DataBaseIntegration( db => { db.Dialect<SQLiteDialect>(); db.Driver<SQLite20Driver>(); db.SchemaAction = SchemaAutoAction.Recreate; db.ConnectionString = "Data Source=:memory:;Version=3;New=True;"; }).SetProperty(Environment.CurrentSessionContextClass, "thread_static"); var mapper = new ConventionModelMapper(); // filter entities var baseEntityType = typeof(AbstractEntity); mapper.IsEntity( (t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface); mapper.IsRootEntity((t, declared) => baseEntityType == t.BaseType); // override base properties mapper.Class<AbstractEntity>(map => map.Id(x => x.Id, m => m.Generator(Generators.GuidComb))); mapper.BeforeMapProperty += (modelinspector, member, propertycustomizer) => { if (member.LocalMember.Name == "Name") { propertycustomizer.Unique(true); } }; // compile var mapping = mapper.CompileMappingFor( typeof(Person).Assembly.GetExportedTypes().Where( type => typeof(AbstractEntity).IsAssignableFrom(type))); // use mappings config.AddMapping(mapping); return config; }