示例#1
0
        /// <summary>
        /// Maps all explicit mappings, overrides, and entities found in the assembly of the specified type.
        /// </summary>
        /// <param name="type">Type whose assembly should be inspected.</param>
        /// <param name="entityFilter">Filters entities.</param>
        /// <param name="mappingFilter">Filters mappings.</param>
        /// <param name="overrideFilter">Filters overrides.</param>
        public void MapAssemblyOfType(Type type, Func <Type, bool> entityFilter = null, Func <Type, bool> mappingFilter = null,
                                      Func <Type, bool> overrideFilter          = null)
        {
            var types = type.Assembly.GetExportedTypes().AsEnumerable();

            var entityTypes = new List <Type>();

            foreach (var t in types)
            {
                if (typeof(IConformistHoldersProvider).IsAssignableFrom(t) &&
                    (mappingFilter ?? Accept)(t))
                {
                    ModelMapper.AddMapping(t);
                }
                else if (typeof(IMapperOverride).IsAssignableFrom(t) &&
                         (overrideFilter ?? Accept)(t))
                {
                    ((IMapperOverride)Activator.CreateInstance(t)).Override(ModelMapper);
                }
                else if ((entityFilter ?? Accept)(t))
                {
                    entityTypes.Add(t);
                }
            }

            Mappings.Add(ModelMapper.CompileMappingFor(entityTypes));
        }
示例#2
0
        public void WhenMapDynCompoByDictionaryThenMapItAndItsPropertiesGeneric()
        {
            //NH-3704
            var mapper = new ModelMapper();

            mapper.Class <PersonWithGenericInfo>(
                map =>
            {
                map.Id(x => x.Id, idmap => { });
                map.Component(
                    x => x.Info,
                    new Dictionary <string, System.Type>
                {
                    { "MyInt", typeof(int) }, { "MyDate", typeof(DateTime) }
                },
                    z => { z.Property("MyInt", pm => pm.Column("MY_COLUMN")); });
            });

            var hbmMapping          = mapper.CompileMappingFor(new[] { typeof(PersonWithGenericInfo) });
            var hbmClass            = hbmMapping.RootClasses[0];
            var hbmDynamicComponent = hbmClass.Properties.OfType <HbmDynamicComponent>().SingleOrDefault();

            Assert.That(hbmDynamicComponent, Is.Not.Null);
            Assert.That(
                hbmDynamicComponent.Properties.Select(x => x.Name),
                Is.EquivalentTo(new[] { "MyInt", "MyDate" }));
        }
示例#3
0
        public void MapClassWithConventions()
        {
            var mapper = new ModelMapper();

            mapper.BeforeMapClass +=
                (mi, t, map) => map.Id(x => x.Column((t.Name + "id").ToUpper()));
            mapper.BeforeMapProperty +=
                (mi, propertyPath, map) => map.Column(propertyPath.ToColumnName().ToUpper());

            mapper.Class <MyClass>(ca =>
            {
                ca.Id(x => x.Id, map => { });
                ca.Property(x => x.Something);
            });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

            var hbmClass = hbmMapping.RootClasses[0];

            hbmClass.Should().Not.Be.Null();
            var hbmId = hbmClass.Id;

            hbmId.column1.Should().Be("MYCLASSID");
            var hbmProperty = hbmClass.Properties.OfType <HbmProperty>().Single();

            hbmProperty.column.Should().Be("SOMETHING");
        }
示例#4
0
        public void WhenPropertyUsedAsComposedIdAndPropertiesAndNaturalIdThenMapOnlyAsComposedId()
        {
            var mapper = new ModelMapper();

            mapper.Class <MyClass>(map =>
            {
                map.ComposedId(cm =>
                {
                    cm.Property(x => x.Code);
                    cm.ManyToOne(x => x.Relation);
                });
                map.NaturalId(nm =>
                {
                    nm.Property(x => x.Code);
                    nm.ManyToOne(x => x.Relation);
                });
                map.Property(x => x.Code);
                map.ManyToOne(x => x.Relation);
            }
                                   );
            HbmMapping     hbmMapping    = mapper.CompileMappingFor(new[] { typeof(MyClass) });
            HbmClass       hbmClass      = hbmMapping.RootClasses[0];
            HbmCompositeId hbmCompositId = hbmClass.CompositeId;

            Assert.That(hbmCompositId.Items, Has.Length.EqualTo(2));
            Assert.That(hbmClass.naturalid, Is.Null);
            Assert.That(hbmClass.Properties, Is.Empty);
        }
示例#5
0
        protected HbmMapping GetValidMappings()
        {
            var mapper = new ModelMapper();

            mapper.Class <EntityWithReadOnlyPropertiesDuplicatingColumns>(
                ca =>
            {
                ca.Abstract(true);
                ca.Id(
                    x => x.Id,
                    map =>
                {
                    map.Column("EntityId");
                    map.Generator(Generators.GuidComb);
                });
                ca.ManyToOne(
                    x => x.Self,
                    map =>
                {
                    map.Column("EntityId");
                    map.Update(false);
                    map.Insert(false);
                });
                ca.Property(
                    x => x.IdCopy,
                    map =>
                {
                    map.Column("EntityId");
                    map.Update(false);
                    map.Insert(false);
                });
            });

            return(mapper.CompileMappingFor(new[] { typeof(BadlyMappedEntity) }));
        }
示例#6
0
        public void MapClassWithHardConventions()
        {
            var mapper = new ModelMapper();

            mapper.AfterMapClass +=
                (mi, t, map) => map.Id(x => x.Column((t.Name + "id").ToUpper()));
            mapper.AfterMapProperty +=
                (mi, propertyPath, map) => map.Column(propertyPath.ToColumnName().ToUpper());

            mapper.Class <MyClass>(ca =>
            {
                ca.Id(x => x.Id, map => map.Column("Whatever"));
                ca.Property(x => x.Something, map => map.Column("Whatever"));
            });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

            var hbmClass = hbmMapping.RootClasses[0];

            Assert.That(hbmClass, Is.Not.Null);
            var hbmId = hbmClass.Id;

            Assert.That(hbmId.column1, Is.EqualTo("MYCLASSID"));
            var hbmProperty = hbmClass.Properties.OfType <HbmProperty>().Single();

            Assert.That(hbmProperty.column, Is.EqualTo("SOMETHING"));
        }
        public void WhenCustomizeConditionsThenUseCustomConditionsToRecognizeRootEntities()
        {
            System.Type baseEntityType = typeof(Entity);
            var         inspector      = new SimpleModelInspector();

            inspector.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface);
            inspector.IsRootEntity((t, declared) => baseEntityType == t.BaseType);

            var mapper = new ModelMapper(inspector);

            mapper.Class <Entity>(map => map.Id(x => x.Id,
                                                m =>
            {
                m.Generator(Generators.Guid);
                m.Column("ID");
            }));

            mapper.Class <Activity>(map =>
            {
                map.Discriminator(dm =>
                {
                    dm.Column("DISCRIMINATOR_TYPE");
                    dm.NotNullable(true);
                });
                map.DiscriminatorValue(0);
            });
            mapper.Subclass <FormActivity>(map => map.DiscriminatorValue(1));

            Assert.That(() => mapper.CompileMappingFor(new[] { typeof(Activity), typeof(FormActivity) }), Throws.Nothing);
        }
示例#8
0
        public void WhenMapDynCompoAttributesThenMapAttributes()
        {
            var mapper = new ModelMapper();

            mapper.Class <Person>(map =>
            {
                map.Id(x => x.Id, idmap => { });
                map.Component(x => x.Info, new { MyInt = 5 }, z =>
                {
                    z.Access(Accessor.Field);
                    z.Insert(false);
                    z.Update(false);
                    z.Unique(true);
                    z.OptimisticLock(false);
                });
            });

            var hbmMapping          = mapper.CompileMappingFor(new[] { typeof(Person) });
            var hbmClass            = hbmMapping.RootClasses[0];
            var hbmDynamicComponent = hbmClass.Properties.OfType <HbmDynamicComponent>().SingleOrDefault();

            hbmDynamicComponent.access.Should().Contain("field");
            hbmDynamicComponent.insert.Should().Be.False();
            hbmDynamicComponent.update.Should().Be.False();
            hbmDynamicComponent.optimisticlock.Should().Be.False();
            hbmDynamicComponent.unique.Should().Be.True();
        }
        public void WhenMapSplittedPropertiesThenEachPropertyIsInItsSplitGroup()
        {
            var inspector = new ExplicitlyDeclaredModel();
            var mapper    = new ModelMapper(inspector);

            mapper.Class <MyClass>(map => map.Id(x => x.Id, idmap => { }));
            mapper.Subclass <Inherited>(map =>
            {
                map.Join("MyClassSplit1", mj =>
                {
                    mj.Property(x => x.SomethingA1);
                    mj.Property(x => x.SomethingA2);
                });
                map.Join("MyClassSplit2", mj =>
                {
                    mj.Property(x => x.SomethingB1);
                    mj.Property(x => x.SomethingB2);
                });
                map.Property(x => x.Something0);
            });
            var hbmDoc = mapper.CompileMappingFor(new[] { typeof(Inherited) });

            var hbmClass = hbmDoc.SubClasses[0];

            hbmClass.Joins.Select(j => j.table).Should().Have.SameValuesAs("MyClassSplit1", "MyClassSplit2");
            hbmClass.Properties.Single().Name.Should().Be("Something0");
            var hbmSplit1 = hbmClass.Joins.Single(j => "MyClassSplit1" == j.table);

            hbmSplit1.Properties.Select(p => p.Name).Should().Have.SameValuesAs("SomethingA1", "SomethingA2");
            var hbmSplit2 = hbmClass.Joins.Single(j => "MyClassSplit2" == j.table);

            hbmSplit2.Properties.Select(p => p.Name).Should().Have.SameValuesAs("SomethingB1", "SomethingB2");
        }
示例#10
0
        public static HbmMapping GetMappings()
        {
            ModelMapper mapper = new ModelMapper();

            mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
            HbmMapping domainMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

            //mapper.AddMapping<PessoaMap>();
            //mapper.AddMapping<ClienteMap>();
            mapper.AddMapping <UsuarioMap>();
            mapper.AddMapping <AvaliacaoMap>();
            mapper.AddMapping <SeguidorMap>();
            mapper.AddMapping <PostagemMap>();
            //mapper.AddMapping<CompraMap>();
            //mapper.AddMapping<FornecedorMap>();
            //mapper.AddMapping<ItemCompraMap>();
            //mapper.AddMapping<ItemVendaMap>();
            //mapper.AddMapping<ProdutoMap>();
            //mapper.AddMapping<VendaMap>();
            //mapper.AddMapping<VendedorMap>();

            // HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Pessoa), typeof(Cliente) });

            HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Avaliacao), typeof(Usuario), typeof(Seguidor), typeof(Postagem) });

            return(mapping);
        }
        public static HbmMapping CreateMapping()
        {
            ModelMapper mapper = new ModelMapper();

            mapper.AddMapping <AdminssMap>();
            mapper.AddMapping <AddressMap>();
            mapper.AddMapping <AuthorsMap>();
            mapper.AddMapping <BookcategoryMap>();
            mapper.AddMapping <BookMap>();
            mapper.AddMapping <BooktransactionMap>();
            mapper.AddMapping <ContactdetailsMap>();
            mapper.AddMapping <InventoryMap>();
            mapper.AddMapping <ItemcategoryMap>();
            mapper.AddMapping <ItemtransactionMap>();
            mapper.AddMapping <UserdetailsMap>();
            mapper.AddMapping <UsersMap>();

            // Create an array of types that are not mapped.
            HbmMapping mapping = mapper.CompileMappingFor(new[] {
                typeof(Address), typeof(Authors), typeof(Admins), typeof(Users), typeof(UserDetails),
                typeof(ContactDetails), typeof(Books), typeof(BookCategory), typeof(BookTransaction),
                typeof(Inventory), typeof(ItemCategory), typeof(ItemTransaction)
            });

            return(mapping);
        }
		public void WhenMapDynCompoAttributesThenMapAttributes()
		{
			var mapper = new ModelMapper();
			mapper.Class<Person>(map =>
			{
				map.Id(x => x.Id, idmap => { });
				map.Component(x => x.Info, new { MyInt = 5}, z =>
				                                             {
																											 z.Access(Accessor.Field);
																											 z.Insert(false);
																											 z.Update(false);
																											 z.Unique(true);
																											 z.OptimisticLock(false);
				                                             });
			});

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
			hbmDynamicComponent.access.Should().Contain("field");
			hbmDynamicComponent.insert.Should().Be.False();
			hbmDynamicComponent.update.Should().Be.False();
			hbmDynamicComponent.optimisticlock.Should().Be.False();
			hbmDynamicComponent.unique.Should().Be.True();
		}
示例#13
0
        public void WhenPropertyUsedAsComposedIdAndPropertiesAndNaturalIdThenMapOnlyAsComposedId()
        {
            var mapper = new ModelMapper();

            mapper.Class <MyClass>(map =>
            {
                map.ComposedId(cm =>
                {
                    cm.Property(x => x.Code);
                    cm.ManyToOne(x => x.Relation);
                });
                map.NaturalId(nm =>
                {
                    nm.Property(x => x.Code);
                    nm.ManyToOne(x => x.Relation);
                });
                map.Property(x => x.Code);
                map.ManyToOne(x => x.Relation);
            }
                                   );
            HbmMapping     hbmMapping    = mapper.CompileMappingFor(new[] { typeof(MyClass) });
            HbmClass       hbmClass      = hbmMapping.RootClasses[0];
            HbmCompositeId hbmCompositId = hbmClass.CompositeId;

            hbmCompositId.Items.Should().Have.Count.EqualTo(2);
            hbmClass.naturalid.Should().Be.Null();
            hbmClass.Properties.Should().Be.Empty();
        }
		public void MapClassWithIdAndProperty()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(ca =>
			{
				ca.Id("id", map =>
				{
					map.Column("MyClassId");
					map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
				});
				ca.Version("version", map => { });
				ca.Property("something", map => map.Length(150));
			});
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			hbmClass.Should().Not.Be.Null();
			var hbmId = hbmClass.Id;
			hbmId.Should().Not.Be.Null();
			hbmId.name.Should().Be("id");
			hbmId.access.Should().Be("field");
			var hbmGenerator = hbmId.generator;
			hbmGenerator.Should().Not.Be.Null();
			[email protected]().Be("hilo");
			hbmGenerator.param[0].name.Should().Be("max_low");
			hbmGenerator.param[0].GetText().Should().Be("100");
			var hbmVersion = hbmClass.Version;
			hbmVersion.name.Should().Be("version");
			var hbmProperty = hbmClass.Properties.OfType<HbmProperty>().Single();
			hbmProperty.name.Should().Be("something");
			hbmProperty.access.Should().Be("field");
			hbmProperty.length.Should().Be("150");
		}
        public void WhenMapSplittedPropertiesThenEachPropertyIsInItsSplitGroup()
        {
            var inspector = new ExplicitlyDeclaredModel();
            var mapper    = new ModelMapper(inspector);

            mapper.Class <MyClass>(map =>
            {
                map.Id(x => x.Id, idmap => { });
                map.Join("MyClassSplit1", mj =>
                {
                    mj.Property(x => x.SomethingA1);
                    mj.Property(x => x.SomethingA2);
                });
                map.Join("MyClassSplit2", mj =>
                {
                    mj.Property(x => x.SomethingB1);
                    mj.Property(x => x.SomethingB2);
                });
                map.Property(x => x.Something0);
            });
            var hbmDoc = mapper.CompileMappingFor(new[] { typeof(MyClass) });

            var hbmClass = hbmDoc.RootClasses[0];

            Assert.That(hbmClass.Joins.Select(j => j.table), Is.EquivalentTo(new [] { "MyClassSplit1", "MyClassSplit2" }));
            Assert.That(hbmClass.Properties.Single().Name, Is.EqualTo("Something0"));
            var hbmSplit1 = hbmClass.Joins.Single(j => "MyClassSplit1" == j.table);

            Assert.That(hbmSplit1.Properties.Select(p => p.Name), Is.EquivalentTo(new [] { "SomethingA1", "SomethingA2" }));
            var hbmSplit2 = hbmClass.Joins.Single(j => "MyClassSplit2" == j.table);

            Assert.That(hbmSplit2.Properties.Select(p => p.Name), Is.EquivalentTo(new [] { "SomethingB1", "SomethingB2" }));
        }
        private static HbmMapping CreateMappings()
        {
            var modelMapper = new ModelMapper();

            modelMapper.AddMapping <MovieMapping>();
            modelMapper.AddMapping <SoundtrackMapping>();
            return(modelMapper.CompileMappingFor(new[] { typeof(Movie), typeof(Soundtrack) }));
        }
示例#17
0
        public void WhenRegisterClassMappingThroughCollectionOfTypeThenFilterValidMappings()
        {
            var mapper = new ModelMapper();

            mapper.Executing(x => x.AddMappings(new[] { typeof(object), typeof(MyClassMap), typeof(MyClass), typeof(MyClassBaseMap <>) })).NotThrows();
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

            ModelIsWellFormed(hbmMapping);
        }
        public void WhenRegisterClassMappingThroughCollectionOfTypeThenMapTheClass()
        {
            var mapper = new ModelMapper();

            mapper.AddMappings(new [] { typeof(MyClassMap) });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

            ModelIsWellFormed(hbmMapping);
        }
示例#19
0
        protected static HbmMapping GetMappings()
        {
            //There is a dynamic way to do this, but for simplicity I chose to hard code
            var mapper = new ModelMapper();

            mapper.AddMapping<UsersMap>();
            var mapping = mapper.CompileMappingFor(new[] { typeof(Users) });
            return mapping;
        }
        public void WhenRegisterClassMappingThroughCollectionOfTypeThenFilterValidMappings()
        {
            var mapper = new ModelMapper();

            Assert.That(() => mapper.AddMappings(new[] { typeof(object), typeof(MyClassMap), typeof(MyClass), typeof(MyClassBaseMap <>) }), Throws.Nothing);
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

            ModelIsWellFormed(hbmMapping);
        }
示例#21
0
        public void WhenRegisterClassMappingThenMapTheClass()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping <MyClassMap>();
            mapper.AddMapping <NameMap>();
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

            ModelIsWellFormed(hbmMapping);
        }
        private static HbmMapping GetMappingsLameWay()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping <FundProductMap>();

            var mapping = mapper.CompileMappingFor(new[] { typeof(FundProduct) });

            return(mapping);
        }
        public void WhenRegisterClassMappingThroughTypeThenMapTheClass()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping(typeof(MyClassMap));
            mapper.AddMapping(typeof(InheritedMap));
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Inherited) });

            ModelIsWellFormed(hbmMapping);
        }
		public void WhenIdBagWithOneToManyThenThrow()
		{
			var mapper = new ModelMapper();
			mapper.Class<Animal>(map =>
			{
				map.Id(x => x.Id, idmap => { });
				map.IdBag(x => x.Children, bag => { }, rel => rel.OneToMany());
			});
			Assert.That(() => mapper.CompileMappingFor(new[] { typeof(Animal) }), Throws.TypeOf<NotSupportedException>());
		}
示例#25
0
        //[SetUp]
        public void Setup()
        {
            AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory);

            Configuration configuration = new Configuration();

            configuration.DataBaseIntegration(f => f.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote);

            var mapper = new ModelMapper();

            var allEntities = GetType().Assembly.GetTypes().ToList();

            var mappings = allEntities.Where(t => IsSubclassOfRawGeneric(typeof(ClassMapping <>), t));

            mapper.AddMappings(mappings);
            var mapping = mapper.CompileMappingFor(allEntities);

            configuration.AddDeserializedMapping(mapping, "NHSchema");

            configuration.Configure();

            var se = new SchemaExport(configuration);

            SqlConnection conn = new SqlConnection(configuration.Properties["connection.connection_string"]);

            conn.Open();

            var command = conn.CreateCommand();

            command.CommandText = @"
DECLARE @sql NVARCHAR(max)=''
SELECT @sql=@sql+' Drop table [' + s.NAME + '].[' + t.NAME+'];'
FROM   sys.tables t
       JOIN sys.schemas s
         ON t.[schema_id] = s.[schema_id]
WHERE  t.type = 'U'
Exec sp_executesql @sql;
select @sql = ''
SELECT @sql=@sql+' Drop table [' + s.NAME + '].[' + t.NAME+'];'
FROM   sys.tables t
       JOIN sys.schemas s
         ON t.[schema_id] = s.[schema_id]
WHERE  t.type = 'U';
select @sql";

            conn.Close();


            se.Drop(true, true);

            se.Create(true, true);

            _sessionFactory = configuration.BuildSessionFactory();
        }
示例#26
0
        public void WhenIdBagWithOneToManyThenThrow()
        {
            var mapper = new ModelMapper();

            mapper.Class <Animal>(map =>
            {
                map.Id(x => x.Id, idmap => { });
                map.IdBag(x => x.Children, bag => { }, rel => rel.OneToMany());
            });
            Assert.That(() => mapper.CompileMappingFor(new[] { typeof(Animal) }), Throws.TypeOf <NotSupportedException>());
        }
        private static HbmMapping GetMappings()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping<BlogPostMapping>();
            mapper.AddMapping<CommentMapping>();

            HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(BlogPost), typeof(Comment) });

            return mapping;
        }
示例#28
0
        private static HbmClass CompileClassMapping()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping(typeof(MyClassMap));

            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
            var hbmClass   = hbmMapping.RootClasses[0];

            return(hbmClass);
        }
示例#29
0
        public void OneTimeSetUp()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping <StreetMap>();

            _configuration = new Configuration();
            _configuration.Configure();
            _configuration.AddMapping(mapper.CompileMappingFor(new[] { typeof(Street) }));
            _sessionFactory = _configuration.BuildSessionFactory();
        }
        public void WhenSetKeyThroughEventThenUseEvent()
        {
            var autoinspector = new SimpleModelInspector();
            var mapper        = new ModelMapper(autoinspector);

            mapper.BeforeMapBag += (insp, prop, map) => map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));

            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Parent) });
            var hbmBag     = hbmMapping.RootClasses[0].Properties.OfType <HbmBag>().Single();

            hbmBag.Key.Columns.Single().name.Should().Be("ParentId");
        }
        protected static HbmMapping GetMappings()
        {
            //There is a dynamic way to do this, but for simplicity I chose to hard code
            ModelMapper mapper = new ModelMapper();

            mapper.AddMapping <ChildMap>();
            mapper.AddMapping <ParentMap>();

            HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Parent), typeof(Child) });

            return(mapping);
        }
示例#32
0
        /// <summary>
        /// Gets the NHibernate mapping.
        /// </summary>
        /// <returns>
        /// The NHibernate mapping.
        /// </returns>
        private HbmMapping Map()
        {
            var mapper = new ModelMapper();

            mapper.AddMapping <TestModelMap>();
            mapper.AddMapping <TestChildMap>();
            mapper.AddMapping <AggregateRootTestModelMap>();

            var mapping = mapper.CompileMappingFor(new[] { typeof(TestModel), typeof(TestChildModel), typeof(AggregateRootTestModel) });

            return(mapping);
        }
示例#33
0
		public void WhenDefineEmptyNaturalIdThenNoMapIt()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(map =>
			{
				map.Id(x => x.Id, idmap => { });
				map.NaturalId(nidm =>{});
			});
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			hbmClass.naturalid.Should().Be.Null();
		}
示例#34
0
		public void ExplicitColumnNameIsAlwaysMapped(string columnName)
		{
			var mapper = new ModelMapper();
			mapper.Class<Foo>(cm => cm.Bag(x => x.Bars,
			 							   bpm => { },
										   cer => cer.ManyToMany(mtmm => mtmm.Column(columnName))));
			var mapping = mapper.CompileMappingFor(new[] { typeof(Foo), typeof(Bar) });
			var hbmClass = mapping.RootClasses.Single(x => x.Name == "Foo");
			var hbmBag = hbmClass.Properties.OfType<HbmBag>().Single();
			var hbmManyToMany = (HbmManyToMany)hbmBag.ElementRelationship;
			Assert.AreEqual(columnName, hbmManyToMany.column);
		}
        public HbmMapping GetHbmMapping()
        {
            var         mapper   = new ModelMapper();
            List <Type> mappings =
                typeof(EntityMapper <>).Assembly.GetExportedTypes().Where(x => !x.IsAbstract || !x.IsInterface).Where(
                    x => x.BaseType != null).ToList();

            mapper.AddMappings(mappings);
            IEnumerable <Type> entities =
                typeof(EntityWithTypedId <>).Assembly.GetExportedTypes().Where(x => !x.IsInterface && x.BaseType != null);

            return(mapper.CompileMappingFor(entities));
        }
示例#36
0
        HbmMapping IModelAnnotationsMapper.CompileMapping()
        {
            entityList
            .ForEach(AddMapping);

            var mapping =
                modelMapper
                .CompileMappingFor(entityList.Keys.Select(k => k.Source));

            mapping.defaultaccess = "backfield";

            return(mapping);
        }
		public void WhenIdBagWithManyToManyThenMapIt()
		{
			var mapper = new ModelMapper();
			mapper.Class<Animal>(map =>
													 {
														 map.Id(x => x.Id, idmap => { });
														 map.IdBag(x => x.Children, bag => { }, rel=> rel.ManyToMany());
													 });
			var hbmMapping = mapper.CompileMappingFor(new[]{ typeof(Animal)});
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmIdbag = hbmClass.Properties.OfType<HbmIdbag>().SingleOrDefault();
			Assert.That(hbmIdbag, Is.Not.Null);
			Assert.That(hbmIdbag.ElementRelationship, Is.InstanceOf<HbmManyToMany>());
		}
		public void WhenMapDynCompoPropertiesThenShouldAssignPropertyType()
		{
			var mapper = new ModelMapper();
			mapper.Class<Person>(map =>
			{
				map.Id(x => x.Id, idmap => { });
				map.Component(x => x.Info, new { MyInt = 5, MyDate = DateTime.Now }, z => { });
			});

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().Single();
			hbmDynamicComponent.Properties.OfType<HbmProperty>().Select(x => x.type1).All(x=> x.Satisfy(value=> !string.IsNullOrEmpty(value)));
		}
示例#39
0
        public void WhenDefineEmptyNaturalIdThenNoMapIt()
        {
            var mapper = new ModelMapper();

            mapper.Class <MyClass>(map =>
            {
                map.Id(x => x.Id, idmap => { });
                map.NaturalId(nidm => {});
            });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
            var hbmClass   = hbmMapping.RootClasses[0];

            Assert.That(hbmClass.naturalid, Is.Null);
        }
		public void MapClassWithIdAndProperty()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(ca =>
			{
				ca.Id(x => x.Id, map =>
				{
					map.Column("MyClassId");
					map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
				});
				ca.Property(x => x.Something, map => map.Length(150));
			});
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			ModelIsWellFormed(hbmMapping);
		}
		public void WhenMapDynCompoThenMapItAndItsProperties()
		{
			var mapper = new ModelMapper();
			mapper.Class<Person>(map =>
			{
				map.Id(x => x.Id, idmap => { });
				map.Component(x => x.Info, new { MyInt = 5, MyDate = DateTime.Now }, z => { });
			});

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
			hbmDynamicComponent.Should().Not.Be.Null();
			hbmDynamicComponent.Properties.Select(x=> x.Name).Should().Have.SameValuesAs("MyInt", "MyDate");
		}
		public void WhenMapClassWithoutIdAndWithoutGeneratorThenTypeShouldHaveValue()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(ca => ca.Id(null, map =>
			{
				map.Column("MyClassId");
			}));
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			hbmClass.Should().Not.Be.Null();
			var hbmId = hbmClass.Id;
			hbmId.Should().Not.Be.Null();
			hbmId.column1.Should().Be("MyClassId");
			hbmId.type1.Should().Not.Be.Null();
		}
		public void WhenMapDynCompoThenMapItAndItsProperties()
		{
			var mapper = new ModelMapper();
			mapper.Class<Person>(map =>
			{
				map.Id(x => x.Id, idmap => { });
				map.Component(x => x.Info, new { MyInt = 5, MyDate = DateTime.Now }, z => { });
			});

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmDynamicComponent = hbmClass.Properties.OfType<HbmDynamicComponent>().SingleOrDefault();
			Assert.That(hbmDynamicComponent, Is.Not.Null);
			Assert.That(hbmDynamicComponent.Properties.Select(x=> x.Name), Is.EquivalentTo(new [] {"MyInt", "MyDate"}));
		}
		public void WhenMapComponentAsIdAttributesThenMapAttributes()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(map => map.ComponentAsId(x => x.Id, idmap =>
			{
				idmap.Access(Accessor.Field);
				idmap.Class<MyComponent>();
			}));

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			hbmCompositId.access.Should().Contain("field");
			[email protected]().Contain("MyComponent");
		}
		public void WhenMapClassWithoutIdAndWithoutGeneratorThenTypeShouldHaveValue()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(ca => ca.Id(null, map =>
			{
				map.Column("MyClassId");
			}));
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			Assert.That(hbmClass, Is.Not.Null);
			var hbmId = hbmClass.Id;
			Assert.That(hbmId, Is.Not.Null);
			Assert.That(hbmId.column1, Is.EqualTo("MyClassId"));
			Assert.That(hbmId.type1, Is.Not.Null);
		}
示例#46
0
		public void WhenPropertyUsedAsComposedIdThenNotUsedAsSimpleProperties()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(map =>
															map.ComposedId(cm =>
															{
																cm.Property(x => x.Code);
																cm.ManyToOne(x => x.Relation);
															})
														);
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			Assert.That(hbmCompositId.Items, Has.Length.EqualTo(2));
			Assert.That(hbmClass.Properties, Is.Empty);
		}
		public void WhenMapClassWithoutIdThenApplyTypeOfGeneratorDef()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(ca => ca.Id(null, map =>
													{
														map.Column("MyClassId");
														map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
													}));
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			Assert.That(hbmClass, Is.Not.Null);
			var hbmId = hbmClass.Id;
			Assert.That(hbmId, Is.Not.Null);
			Assert.That(hbmId.column1, Is.EqualTo("MyClassId"));
			Assert.That(hbmId.type1, Is.EqualTo(NHibernateUtil.Int32.Name));
		}
		public void WhenMapComponentAsIdThenMapItAndItsProperties()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(map => map.ComponentAsId(x => x.Id, idmap =>
			                                                          {
			                                                          	idmap.Property(y => y.Code);
			                                                          	idmap.Property(y => y.Name);
			                                                          }));

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			var keyProperties = hbmCompositId.Items.OfType<HbmKeyProperty>();
			keyProperties.Should().Have.Count.EqualTo(2);
			keyProperties.Select(x => x.Name).Should().Have.SameValuesAs("Code", "Name");
		}
示例#49
0
		public void WhenPropertyUsedAsComposedIdThenNotUsedAsSimpleProperties()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(map =>
															map.ComposedId(cm =>
															{
																cm.Property(x => x.Code);
																cm.ManyToOne(x => x.Relation);
															})
														);
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			hbmCompositId.Items.Should().Have.Count.EqualTo(2);
			hbmClass.Properties.Should().Be.Empty();
		}
		public void AbstractClass()
		{
			//NH-3527
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(ca =>
			{
				ca.Abstract(true);
				ca.Id(x => x.Id, map =>
				{
					map.Column("MyClassId");
					map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
				});
				ca.Property(x => x.Something, map => map.Length(150));
			});
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			Assert.AreEqual(hbmMapping.RootClasses[0].@abstract, true);
		}
示例#51
0
		public void TestMapManyToManyGenericCollectionBasedOnItem()
		{
			var mapper = new ModelMapper();

			mapper.Class<ParentWithItemChild>(c =>
			{
				c.Id(x => x.Id, x => x.Generator(Generators.Identity));
				c.IdBag(x => x.Children, bag => { }, rel => rel.ManyToMany());
			});

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(ParentWithItemChild) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmIdbag = hbmClass.Properties.OfType<HbmIdbag>().SingleOrDefault();

			Assert.That(hbmIdbag, Is.Not.Null);
			Assert.That(hbmIdbag.ElementRelationship, Is.InstanceOf<HbmManyToMany>());
		}
		public void WhenMapComponentAsIdThenMapItAndItsProperties()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(map => map.ComponentAsId(x => x.Id, idmap =>
																	  {
																		idmap.Property(y => y.Code);
																		idmap.Property(y => y.Name);
																	  }));

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			var keyProperties = hbmCompositId.Items.OfType<HbmKeyProperty>();
			Assert.That(keyProperties.Count(), Is.EqualTo(2));
			Assert.That(keyProperties.Select(x => x.Name), Is.EquivalentTo(new [] {"Code", "Name"}));
			Assert.That(hbmCompositId.name, Is.EqualTo(For<MyClass>.Property(x => x.Id).Name));
		}
		public void WhenMapClassWithWrongElementsThenAutodiscoverParent()
		{
			// In this case the user will use wrong mapping-elements as ManyToOne and Component (he should realize that it end in an infinite loop)
			var mapper = new ModelMapper();
			mapper.Class<Person>(cm =>
			{
				cm.Id(x => x.Id);
				cm.Bag(x => x.Addresses, cp => { }, cr => cr.Component(ce =>
				{
					ce.ManyToOne(x => x.Owner);
					ce.Property(x => x.Street);
					ce.Component(x => x.Number, y =>
					{
						y.Component(x => x.OwnerAddress, map => { });
						y.Property(x => x.Block);
					});
				}));
			});
			HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Person) });
			VerifyMapping(mapping);
		}
		public void WhenMapClasByClassThenAutodiscoverParent()
		{
			var mapper = new ModelMapper();
			mapper.Component<Address>(cm =>
			{
				cm.ManyToOne(x => x.Owner);
				cm.Property(x => x.Street);
				cm.Component(x => x.Number, y => { });
			});
			mapper.Component<Number>(cm =>
			{
				cm.Component(x => x.OwnerAddress, map => { });
				cm.Property(x => x.Block);
			});
			mapper.Class<Person>(cm =>
			{
				cm.Id(x => x.Id);
				cm.Bag(x => x.Addresses, cp => { }, cr => { });
			});
			HbmMapping mapping = mapper.CompileMappingFor(new[] { typeof(Person) });
			VerifyMapping(mapping);
		}
		public void MapClassWithHardConventions()
		{
			var mapper = new ModelMapper();

			mapper.AfterMapClass +=
				(mi, t, map) => map.Id(x => x.Column((t.Name + "id").ToUpper()));
			mapper.AfterMapProperty +=
				(mi, propertyPath, map) => map.Column(propertyPath.ToColumnName().ToUpper());

			mapper.Class<MyClass>(ca =>
			{
				ca.Id(x => x.Id, map => map.Column("Whatever"));
				ca.Property(x => x.Something, map => map.Column("Whatever"));
			});
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

			var hbmClass = hbmMapping.RootClasses[0];
			hbmClass.Should().Not.Be.Null();
			var hbmId = hbmClass.Id;
			hbmId.column1.Should().Be("MYCLASSID");
			var hbmProperty = hbmClass.Properties.OfType<HbmProperty>().Single();
			hbmProperty.column.Should().Be("SOMETHING");
		}
		public void MapClassWithConventions()
		{
			var mapper = new ModelMapper();

			mapper.BeforeMapClass += 
				(mi, t, map) => map.Id(x => x.Column((t.Name+"id").ToUpper()));
			mapper.BeforeMapProperty += 
				(mi, propertyPath, map) => map.Column(propertyPath.ToColumnName().ToUpper());

			mapper.Class<MyClass>(ca =>
			{
				ca.Id(x => x.Id, map => { });
				ca.Property(x => x.Something);
			});
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });

			var hbmClass = hbmMapping.RootClasses[0];
			Assert.That(hbmClass, Is.Not.Null);
			var hbmId = hbmClass.Id;
			Assert.That(hbmId.column1, Is.EqualTo("MYCLASSID"));
			var hbmProperty = hbmClass.Properties.OfType<HbmProperty>().Single();
			Assert.That(hbmProperty.column, Is.EqualTo("SOMETHING"));
		}
		public void MapClassWithInternalIdAndProperty()
		{
			var mapper = new ModelMapper();
			mapper.Class<MyClass>(ca =>
			{
				ca.Id(x => x._id, map =>
				{
					map.Column("MyClassId");
					map.Generator(Generators.HighLow, gmap => gmap.Params(new { max_low = 100 }));
				});
				ca.Version(x => x._version, map => { });
				ca.Property(x => x._something, map => map.Length(150));
			});
			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			Assert.That(hbmClass, Is.Not.Null);

			var hbmId = hbmClass.Id;
			Assert.That(hbmId, Is.Not.Null);
			Assert.That(hbmId.name, Is.EqualTo("_id"));
			Assert.That(hbmId.access, Is.EqualTo("field"));

			var hbmIdGenerator = hbmId.generator;
			Assert.That(hbmIdGenerator, Is.Not.Null);
			Assert.That(hbmIdGenerator.@class, Is.EqualTo("hilo"));
			Assert.That(hbmIdGenerator.param[0].name, Is.EqualTo("max_low"));
			Assert.That(hbmIdGenerator.param[0].GetText(), Is.EqualTo("100"));

			var hbmVersion = hbmClass.Version;
			Assert.That(hbmVersion, Is.Not.Null);
			Assert.That(hbmVersion.name, Is.EqualTo("_version"));

			var hbmProperty = hbmClass.Properties.OfType<HbmProperty>().Single();
			Assert.That(hbmProperty.name, Is.EqualTo("_something"));
			Assert.That(hbmProperty.access, Is.EqualTo("field"));
			Assert.That(hbmProperty.length, Is.EqualTo("150"));
		}
		public void WhenMapComponentUsedAsComponentAsIdThenMapItAndItsProperties()
		{
			var mapper = new ModelMapper();
			mapper.Component<IMyCompo>(x =>
			                           {
																	 x.Property(y => y.Code, pm => pm.Length(10));
																	 x.Property(y => y.Name);
			                           });
			mapper.Class<MyClass>(map => map.ComponentAsId(x => x.Id));

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			var keyProperties = hbmCompositId.Items.OfType<HbmKeyProperty>();
			keyProperties.Should().Have.Count.EqualTo(2);
			keyProperties.Select(x => x.Name).Should().Have.SameValuesAs("Code", "Name");
			keyProperties.Where(x => x.Name == "Code").Single().length.Should().Be("10");
		}
		public void WhenMapAttributesOfCustomizedComponentUsedAsComponentAsIdWithCustomizationOverrideThenUseComponentAsIdCustomization()
		{
			var mapper = new ModelMapper();
			mapper.Component<IMyCompo>(x =>
			{
				x.Access(Accessor.Field);
				x.Class<MyComponent>();
			});
			mapper.Class<MyClass>(map => map.ComponentAsId(x => x.Id, idmap => idmap.Access(Accessor.NoSetter)));

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			hbmCompositId.access.Should().Contain("nosetter");
			[email protected]().Contain("MyComponent");
		}
		public void WhenMapCustomizedComponentUsedAsComponentAsIdWithCustomizationThenUseComponentAsIdCustomization()
		{
			var mapper = new ModelMapper();
			mapper.Component<IMyCompo>(x =>
			{
				x.Property(y => y.Code, pm=> pm.Length(10));
				x.Property(y => y.Name, pm => pm.Length(20));
			});
			mapper.Class<MyClass>(map => map.ComponentAsId(x => x.Id, idmap =>
			{
				idmap.Property(y => y.Code, pm => pm.Length(15));
				idmap.Property(y => y.Name, pm => pm.Length(25));
			}));

			var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClass) });
			var hbmClass = hbmMapping.RootClasses[0];
			var hbmCompositId = hbmClass.CompositeId;
			var keyProperties = hbmCompositId.Items.OfType<HbmKeyProperty>();
			keyProperties.Select(x => x.length).Should().Have.SameValuesAs("15", "25");
		}