コード例 #1
0
 public ConventionMappingCompiler(string tablePrefix, IAttributeMapperFactory attributeMapperFactory)
 {
     _mapper = new ConventionModelMapper(tablePrefix)
     {
         AttributeMapperFactory = attributeMapperFactory
     };
 }
コード例 #2
0
        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);
        }
コード例 #3
0
        public void GetTableInfoWithoutAutoIncrementKey()
        {
            var modelMapper = new ConventionModelMapper();

            var tableInfo = modelMapper.GetTableInfo <Foo>();

            tableInfo.PrimaryKey
            .Any(p => p.AutoIncrementMethodType != AutoIncrementMethodType.None)
            .Should().BeFalse();
        }
コード例 #4
0
        public void GetTableInfoWithoutNoMap()
        {
            var modelMapper = new ConventionModelMapper();

            ((IModelMapperInternal)modelMapper).SetNoMap <Foo>("LastName");

            var tableInfo = modelMapper.GetTableInfo <Foo>();

            tableInfo.GetColumnInfo("LastName").Should().BeNull();
        }
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: ModelMapperShould.cs プロジェクト: ML13/Kros.Libs
        public void GetTableInfoWithoutReadOnlyProperty()
        {
            var modelMapper = new ConventionModelMapper();

            var tableInfo = modelMapper.GetTableInfo <Foo>();

            var columns = tableInfo.Columns.ToList();

            tableInfo.GetColumnInfo("ReadOnlyProperty").Should().BeNull();
        }
コード例 #7
0
        void MapDirectionalRelationship(ConventionModelMapper mapper)
        {
            mapper.Subclass <DirectionalRelationship>(map => {
                map.DiscriminatorValue(RelationshipType.Directional);

                map.Property(x => x.SecondarySummary, p => {
                    p.NotNullable(false);
                });
            });
        }
コード例 #8
0
        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"));
        }
コード例 #9
0
        public void UseSetsTableName()
        {
            var modelMapper = new ConventionModelMapper();

            (modelMapper as IModelMapperInternal).SetTableName <Foo>("Foo2");

            var tableInfo = modelMapper.GetTableInfo <Foo>();

            tableInfo.Name.Should().Be("Foo2");
        }
コード例 #10
0
        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);
        }
コード例 #11
0
        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);
        }
コード例 #12
0
        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"));
        }
コード例 #13
0
        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");
        }
コード例 #14
0
 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()));
 }
コード例 #15
0
        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);
        }
コード例 #16
0
        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"));
        }
コード例 #17
0
 /// <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);
     //    });
     //});
 }
コード例 #18
0
        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.");
        }
コード例 #19
0
        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>();
        }
コード例 #20
0
 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));
         });
     };
 }
コード例 #21
0
ファイル: Conventions.cs プロジェクト: vahid82h/Sharp-Lite
        /// <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);
            });
        }
コード例 #22
0
        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"));
        }
コード例 #23
0
        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();
        }
コード例 #24
0
        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");
        }
コード例 #25
0
        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();
        }
コード例 #26
0
 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));
 }
コード例 #27
0
        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>();
        }
コード例 #28
0
ファイル: ManyToOneMapping.cs プロジェクト: csf-dev/agiil
        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);
            };
        }
コード例 #29
0
ファイル: NhConfig.cs プロジェクト: shosca/NhAutoMap
 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);
 }
コード例 #30
0
        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)));
                });
            });
        }
コード例 #31
0
        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 });
                });
            });
        }
コード例 #32
0
        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;
        }