Esempio n. 1
0
            protected override void Populate(
                ConventionModelMapper mapper
                )
            {
                base.Populate(mapper);

                mapper.AddMapping <CommonDomainObjects.Mapping.TemporalObject <char> >();
                mapper.AddMapping <CommonDomainObjects.Mapping.TemporalObjectVersion <char> >();
                mapper.Class <TestObject>(
                    customizeAction => customizeAction.Table(typeof(TestObject).Name));
                mapper.Subclass <TemporalTestObject>(
                    customizeAction => customizeAction.List(
                        temporalObject => temporalObject.Versions,
                        collectionMapper =>
                {
                    collectionMapper.Key(keyMapper => keyMapper.Column("TemporalObjectId"));
                    collectionMapper.Index(
                        listIndexMapper =>
                    {
                        listIndexMapper.Column("Number");
                        listIndexMapper.Base(1);
                    });
                }));
                mapper.Class <TestObjectVersion>(customizeAction => customizeAction.Table(typeof(TestObjectVersion).Name));
            }
Esempio n. 2
0
        protected override HbmMapping GetMappings()
        {
            var mapper = new ConventionModelMapper();

            // Working Example
            //mapper.Class<Toy>(rc => rc.Set(x => x.Animals, cmap => { }, rel => rel.ManyToAny<int>(meta =>
            //                                                                                      {
            //                                                                                        meta.MetaValue(1, typeof (Cat));
            //                                                                                        meta.MetaValue(2, typeof (Dog));
            //                                                                                      })));

            // User needs
            mapper.Class <Toy>(rc => rc.Set(x => x.Animals, cmap =>
            {
                cmap.Table("Animals_Toys");
                cmap.Key(km => km.Column("Cat_Id"));
            }, rel => rel.ManyToAny <int>(meta =>
            {
                meta.MetaValue(1, typeof(Cat));
                meta.MetaValue(2, typeof(Dog));
                meta.Columns(cid =>
                {
                    cid.Name("Animal_Id");
                    cid.NotNullable(true);
                }, ctype =>
                {
                    ctype.Name("Animal_Type");
                    ctype.NotNullable(true);
                });
            })));
            var mappings = mapper.CompileMappingFor(new[] { typeof(Cat), typeof(Dog), typeof(Toy) });

            //Console.WriteLine(mappings.AsString()); // <=== uncomment this line to see the XML mapping
            return(mappings);
        }
Esempio n. 3
0
 protected override void CustomizeMapping(ConventionModelMapper mapper)
 {
     mapper.Class <Foo>(cm => cm.Set(x => x.Bars, spm =>
     {
         spm.Cascade(Cascade.All);
         spm.Type <PersistentQueryableSetType <Bar> >();
     }));
 }
Esempio n. 4
0
 public void ApplyMapping(ConventionModelMapper mapper)
 {
     mapper.Class <Label>(map => {
         map.Property(x => x.Name, mapping => {
             mapping.Unique(true);
             mapping.Index(nameFormatter.GetUniqueIndexName <Label>(l => l.Name));
         });
     });
 }
Esempio n. 5
0
        protected override HbmMapping GetMappings()
        {
            var mapper = new ConventionModelMapper();

            System.Type baseEntityType = typeof(DomainObject);
            mapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t);
            mapper.IsRootEntity((t, declared) => baseEntityType == t.BaseType);
            mapper.Class <DomainObject>(r =>
            {
                r.Version(x => x.EntityVersion, map => { });
                r.Id(x => x.ID, map => map.Generator(Generators.Native));
            });
            mapper.Class <Class1>(r => r.IdBag(x => x.Class2List, map => map.Inverse(true), rel => rel.ManyToMany()));
            mapper.Class <Class2>(r => r.IdBag <Class1>("_class1List", map => map.Table("Class1List"), rel => rel.ManyToMany()));
            HbmMapping mappings = mapper.CompileMappingFor(new[] { typeof(Class1), typeof(Class2) });

            return(mappings);
        }
Esempio n. 6
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()));
 }
Esempio n. 7
0
            protected override void Populate(
                ConventionModelMapper mapper
                )
            {
                base.Populate(mapper);

                mapper.Class <Message>(messageMapper => messageMapper.Table("Message"));

                mapper.Class <MessageQueue>(
                    messageQueueMapper => messageQueueMapper.Id(
                        messageQueue => messageQueue.Id,
                        idMapper => idMapper.Generator(Generators.Assigned)));

                mapper.Class <MessageSummary>(
                    messageSummaryMapper =>
                {
                    messageSummaryMapper.Table("Message");
                    messageSummaryMapper.SchemaAction(SchemaAction.None);
                });
            }
Esempio n. 8
0
        public static HbmMapping CreateMappingConfiguration()
        {
            var mapper = new ConventionModelMapper();

            var baseEntityType = typeof(Entity);

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

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

            mapper.Class <Album>(map => map.Id(x => x.AlbumId, m => m.Generator(Generators.Identity)));
            mapper.Class <Artist>(map => map.Id(x => x.ArtistId, m => m.Generator(Generators.Identity)));

            var mapping = mapper.CompileMappingFor(baseEntityType.Assembly.GetExportedTypes());

            return(mapping);
        }
Esempio n. 9
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"));
        }
Esempio n. 10
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 });
                });
            });
        }
Esempio n. 11
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)));
                });
            });
        }
        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 WhenPropertyWithoutFieldNoMatch()
        {
            var mapper = new ConventionModelMapper();

            mapper.Class <MyClass>(mc => mc.Property(x => x.PropertyWithoutField));

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

            var hbmClass    = hbmMapping.RootClasses[0];
            var hbmProperty = hbmClass.Properties.Single(x => x.Name == "PropertyWithoutField");

            Assert.That(hbmProperty.Access, Does.Not.Contain("field"));
        }
Esempio n. 14
0
        public void TestFixtureSetUp()
        {
            configuration = new Configuration();
            configuration.SessionFactory()
            .Integrate.Using <SQLiteDialect>()
            .Connected.Using("Data source=testdb")
            .AutoQuoteKeywords()
            .LogSqlInConsole()
            .EnableLogFormattedSql();
            var mapper = new ConventionModelMapper();

            mapper.Class <Foo>(cm => { });
            mapper.Class <Bar>(cm => { });
            CustomizeMapping(mapper);
            var mappingDocument = mapper.CompileMappingForAllExplicitlyAddedEntities();

            new XmlSerializer(typeof(HbmMapping)).Serialize(Console.Out, mappingDocument);
            configuration.AddDeserializedMapping(mappingDocument, "Mappings");
            new SchemaExport(configuration).Create(true, true);
            sessionFactory = configuration.BuildSessionFactory();
            using (var session = sessionFactory.OpenSession())
                using (var tx = session.BeginTransaction())
                {
                    var foo = new Foo {
                        Bars = CreateCollection()
                    };
                    foo.Bars.Add(new Bar {
                        Data = 1
                    });
                    foo.Bars.Add(new Bar {
                        Data = 2
                    });
                    id = session.Save(foo);
                    tx.Commit();
                }
            sessionFactory.Statistics.IsStatisticsEnabled = true;
        }
Esempio n. 15
0
        public void WhenClassWithoutPoidNorGeeneratorThenApplyGuid()
        {
            var mapper = new ConventionModelMapper();

            mapper.Class <MyClassWithoutPoid>(x => { });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClassWithoutPoid) });

            var hbmClass = hbmMapping.RootClasses[0];
            var hbmId    = hbmClass.Id;

            hbmId.Should().Not.Be.Null();
            hbmId.generator.Should().Not.Be.Null();
            [email protected]().Be("guid");
            hbmId.type1.Should().Be("Guid");
        }
Esempio n. 16
0
        public static void Map(Configuration cfg)
        {
            cfg.AddInputStream(HbmSerializer.Default.Serialize(typeof(ServiceLogEntity).Assembly));
            cfg.AddInputStream(HbmSerializer.Default.Serialize(typeof(Client).Assembly));
            var mapper = new ConventionModelMapper();

            mapper.Class <ArchiveOffer>(m => {
                m.Catalog("Farm");
                m.Table("CoreArchive");
                m.ManyToOne(i => i.PriceList, x => x.Column("PriceCode"));
            });
            var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

            cfg.AddDeserializedMapping(mapping, "AmpService.Models");
        }
Esempio n. 17
0
        public void WhenClassWithoutPoidNorGeeneratorThenApplyGuid()
        {
            var mapper = new ConventionModelMapper();

            mapper.Class <MyClassWithoutPoid>(x => { });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClassWithoutPoid) });

            var hbmClass = hbmMapping.RootClasses[0];
            var hbmId    = hbmClass.Id;

            Assert.That(hbmId, Is.Not.Null);
            Assert.That(hbmId.generator, Is.Not.Null);
            Assert.That(hbmId.generator.@class, Is.EqualTo("guid"));
            Assert.That(hbmId.type1, Is.EqualTo("Guid"));
        }
Esempio n. 18
0
        public void WhenClassWithoutPoidWithGeneratorThenApplyDefinedGenerator()
        {
            var mapper = new ConventionModelMapper();

            mapper.Class <MyClassWithoutPoid>(x => x.Id(null, idm => idm.Generator(Generators.Native)));
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(MyClassWithoutPoid) });

            var hbmClass = hbmMapping.RootClasses[0];
            var hbmId    = hbmClass.Id;

            Assert.That(hbmId, Is.Not.Null);
            Assert.That(hbmId.generator, Is.Not.Null);
            Assert.That(hbmId.generator.@class, Is.EqualTo("native"));
            Assert.That(hbmId.type1, Is.EqualTo(Generators.Native.DefaultReturnType.GetNhTypeName()));
        }
        private HbmMapping GetMappingWithParentInCompo()
        {
            var mapper = new ConventionModelMapper();

            mapper.Class <MyClass>(x =>
            {
                x.Id(c => c.Id);
                x.Component(c => c.Compo);
            });
            mapper.Component <MyCompo>(x =>
            {
                x.Property(c => c.Something);
            });
            return(mapper.CompileMappingForAllExplicitlyAddedEntities());
        }
        /// <summary>
        /// Creates the NHibernare session factory.
        /// </summary>
        private void CreateNHibernateSessionFactory()
        {
            if (DefaultSessionFactory == null)
            {
                // Get the domain entities to map and a mapper to map them.
                var domainType   = typeof(MoneyTestEntity);
                var domainTypes  = domainType.Assembly.GetTypes().Where(t => t.IsClass && t.Namespace == domainType.Namespace);
                var domainMapper = new ConventionModelMapper();

                // Customise the mapping before adding the mappings to the configuration.
                domainMapper.Class <MoneyTestEntity>(c =>
                {
                    c.Id(p => p.Id, m =>
                    {
                        m.Generator(Generators.GuidComb);
                    });
                    c.Property(p => p.Description, m =>
                    {
                        m.Length(100);
                    });
                    c.Property(p => p.Cost, m =>
                    {
                        m.Type <MoneyType>();
                        m.Columns(f => f.Name("CostAmount"), f => f.Name("CostCurrency"));
                    });
                    c.Property(p => p.Retail, m =>
                    {
                        m.Type <MoneyType>();
                        m.Columns(f => f.Name("RetailAmount"), f => f.Name("RetailCurrency"));
                    });
                });

                // Build and the mappings for the test domain entities.
                _configuration.AddMapping(domainMapper.CompileMappingFor(domainTypes));

                _configuration.LinqToHqlGeneratorsRegistry <LinqToHqlRegisterMoney>();

                _configuration.DataBaseIntegration(x =>
                {
                    x.LogSqlInConsole = true;
                });

                DefaultSessionFactory = _configuration.BuildSessionFactory();
            }
        }
Esempio n. 21
0
        public void ApplyMapping(ConventionModelMapper mapper)
        {
            mapper.Class <TicketRelationship>(map => {
                map.ManyToOne(x => x.PrimaryTicket, propMap => {
                    propMap.Column(nameFormatter.GetForeignKeyColumnName(Reflect.Property <TicketRelationship>(x => x.PrimaryTicket)));
                    propMap.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <TicketRelationship>(x => x.PrimaryTicket), typeof(TicketRelationship)));
                    propMap.Index(nameFormatter.GetIndexName(typeof(TicketRelationship), Reflect.Property <TicketRelationship>(x => x.PrimaryTicket)));
                    propMap.Cascade(Cascade.Persist);
                });

                map.ManyToOne(x => x.SecondaryTicket, propMap => {
                    propMap.Column(nameFormatter.GetForeignKeyColumnName(Reflect.Property <TicketRelationship>(x => x.SecondaryTicket)));
                    propMap.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <TicketRelationship>(x => x.SecondaryTicket), typeof(TicketRelationship)));
                    propMap.Index(nameFormatter.GetIndexName(typeof(TicketRelationship), Reflect.Property <TicketRelationship>(x => x.SecondaryTicket)));
                    propMap.Cascade(Cascade.Persist);
                });
            });
        }
        public ISessionFactory Create()
        {
            var cfg = new Configuration();

            cfg.DataBaseIntegration(
                db =>
            {
                db.ConnectionString =
                    "Server=tcp:localhost;Database=DataSample;Trusted_Connection=true;Encrypt=False;";
                db.Dialect <MsSql2008Dialect>();
                db.BatchSize          = 250;
                db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                db.SchemaAction       = SchemaAutoAction.Update;
            }).SessionFactory().GenerateStatistics();

            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.Equals(t.BaseType));

            // override base properties
            mapper.Class <AbstractEntity>(
                map =>
            {
                map.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
            });

            mapper.BeforeMapProperty += OnBeforeMapProperty;

            // compile
            var mapping =
                mapper.CompileMappingFor(
                    typeof(AbstractEntity).Assembly.GetExportedTypes().Where(type => typeof(AbstractEntity).IsAssignableFrom(type)));

            // use mappings
            cfg.AddMapping(mapping);

            // build
            return(cfg.BuildSessionFactory());
        }
        public void ApplyMapping(ConventionModelMapper mapper)
        {
            mapper.Class <HierarchicalTicketRelationship>(map => {
                map.Mutable(false);

                map.ManyToOne(x => x.Ticket, m => {
                    m.Column(nameFormatter.GetForeignKeyColumnName(Reflect.Property <HierarchicalTicketRelationship>(x => x.Ticket)));
                    m.Cascade(Cascade.None);
                });
                map.ManyToOne(x => x.RelatedTicket, m => {
                    m.Column(nameFormatter.GetForeignKeyColumnName(Reflect.Property <HierarchicalTicketRelationship>(x => x.RelatedTicket)));
                    m.Cascade(Cascade.None);
                });
                map.ManyToOne(x => x.TicketRelationship, m => {
                    m.Column(nameFormatter.GetForeignKeyColumnName(Reflect.Property <HierarchicalTicketRelationship>(x => x.TicketRelationship)));
                    m.Cascade(Cascade.None);
                });
            });
        }
        public WeatherDbConfig(IConfiguration appConfig, IOptions <NHibernateExportSettings> exportSettings)
        {
            this.DataBaseIntegration(db =>
            {
                db.Dialect <SQLiteDialect>();
                db.ConnectionString = appConfig.GetConnectionString("DefaultConnection");
            });
            ConfigurationExportSettings = exportSettings.Value;

            /*
             * // This chunk is for explicit coded mappings, uncomment WeatherForcastMapping to use this
             * var mapper = new ModelMapper();
             * //Here we're adding explicitly coded mappings but
             * //convention based mappings are supported
             * mapper.AddMappings(typeof(WeatherDbConfig).Assembly.GetExportedTypes());
             * var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
             *
             * AddMapping(mapping);
             */
            //http://fabiomaulo.blogspot.com/2011/04/nhibernate-32-mapping-by-code_13.html
            var mapper         = new ConventionModelMapper();
            var baseEntityType = typeof(EntityBase);

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

            mapper.Class <EntityBase>(map =>
            {
                map.Schema("weatherExample");
                map.Id(x => x.Id, idCfg => idCfg.Generator(Generators.Identity));
            });

            mapper.BeforeMapManyToOne += (insp, prop, map) => map.Column(prop.LocalMember.GetPropertyOrFieldType().Name + "Id");
            mapper.BeforeMapManyToOne += (insp, prop, map) => map.Cascade(Cascade.Persist);
            mapper.BeforeMapBag       += (insp, prop, map) => map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));
            mapper.BeforeMapBag       += (insp, prop, map) => map.Cascade(Cascade.All);


            ConfiguredMapping = mapper.CompileMappingFor(baseEntityType.Assembly.GetExportedTypes().Where(t => t.Namespace.EndsWith("Models")));
            AddMapping(ConfiguredMapping);
        }
        public void WhenPropertyVersionFromBaseEntityThenFindItAsVersion()
        {
            var mapper         = new ConventionModelMapper();
            var baseEntityType = typeof(BaseEntity);

            mapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface);
            mapper.IsRootEntity((t, declared) => baseEntityType.Equals(t.BaseType));
            mapper.Class <BaseEntity>(
                map =>
            {
                map.Id(x => x.Id, idmap => { });
                map.Version(x => x.Version, vm => { });
            });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });

            var hbmClass   = hbmMapping.RootClasses[0];
            var hbmVersion = hbmClass.Version;

            Assert.That(hbmVersion, Is.Not.Null);
            Assert.That(hbmVersion.name, Is.EqualTo("Version"));
        }
        public Configuration GetConfiguration()
        {
            if (_configuration != null)
            {
                return(_configuration);
            }

            Configuration cfg = new Configuration();

            cfg.DataBaseIntegration(db =>
            {
                db.ConnectionString = "Server=localhost;Integrated Security=SSPI;Database=nhibernatetestdb";
                db.Dialect <MsSql2012Dialect>();
                db.Driver <Sql2008ClientDriver>();
            });

            var mapper = new ConventionModelMapper();

            Type baseEntityType = typeof(BaseEntity);

            mapper.IsEntity((t, _) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface);
            mapper.IsRootEntity((t, _) => baseEntityType == t.BaseType);
            mapper.Class <BaseEntity>(t => t.Id(e => e.Id, m => m.Generator(new IdentityGeneratorDef())));
            mapper.BeforeMapManyToOne += (i, p, m) => m.Cascade(Cascade.Persist);
            mapper.BeforeMapList      += (i, p, m) => m.Cascade(Cascade.Persist);
            mapper.BeforeMapSet       += (i, p, m) => m.Cascade(Cascade.Persist);
            mapper.BeforeMapBag       += (i, p, m) => m.Cascade(Cascade.Persist);

            var mapping = mapper.CompileMappingFor(typeof(BaseEntity).Assembly.ExportedTypes.Where(t => typeof(BaseEntity).IsAssignableFrom(t)));

            cfg.AddMapping(mapping);

            _configuration = cfg;

            var updateSchema = new SchemaUpdate(_configuration);

            updateSchema.Execute(false, true);

            return(_configuration);
        }
Esempio n. 27
0
        public void ApplyMapping(ConventionModelMapper mapper)
        {
            mapper.Class <User>(map => {
                map.Set(x => x.ContributorTo, set => {
                    set.Key(key => {
                        key.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <Project>(x => x.Contributors), typeof(Project)));
                    });
                    set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), Contributor, typeof(User)));
                }, r => r.ManyToMany(mtm => {
                    mtm.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <User>(x => x.ContributorTo), typeof(User)));
                }));

                map.Set(x => x.AdministratorOf, set => {
                    set.Key(key => {
                        key.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <Project>(x => x.Administrators), typeof(Project)));
                    });
                    set.Table(nameFormatter.GetManyToManyTableName(typeof(Project), Administrator, typeof(User)));
                }, r => r.ManyToMany(mtm => {
                    mtm.ForeignKey(nameFormatter.GetForeignKeyConstraintName(Reflect.Property <User>(x => x.AdministratorOf), typeof(User)));
                }));
            });
        }
        public void WhenVersionFromBaseEntityThenShouldntMapVersionAsProperty()
        {
            var mapper         = new ConventionModelMapper();
            var baseEntityType = typeof(BaseEntity);

            mapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface);
            mapper.IsRootEntity((t, declared) => baseEntityType == t.BaseType);
            mapper.Class <BaseEntity>(
                map =>
            {
                map.Id(x => x.Id, idmap => { });
                map.Version(x => x.Version, vm => { });
            });
            var hbmMapping = mapper.CompileMappingFor(new[] { typeof(Person) });

            var hbmClass   = hbmMapping.RootClasses[0];
            var hbmVersion = hbmClass.Version;

            Assert.That(hbmVersion, Is.Not.Null);
            Assert.That(hbmVersion.name, Is.EqualTo("Version"));
            Assert.That(hbmClass.Properties, Is.Empty, "because one is the POID and the other is the version");
        }
Esempio n. 29
0
        public void Configure(Configuration configuration)
        {
            var mapper = new ConventionModelMapper();

            var baseEntityType = typeof(Entity);

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

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

            mapper.Class <Entity>(map =>
            {
                map.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
                map.Version(x => x.Version, m => m.Generated(VersionGeneration.Never));
            });

            var mapping = mapper.CompileMappingFor(baseEntityType.Assembly.GetExportedTypes());

            configuration.AddDeserializedMapping(mapping, "Cronos");
        }
Esempio n. 30
0
        public static void WithConventions(this ConventionModelMapper mapper)
        {
            var baseEntityType = typeof(Entity);

            mapper.IsEntity((type, declared) => baseEntityType.IsAssignableFrom(type) && baseEntityType != type && !type.IsInterface);
            mapper.IsRootEntity((type, declared) => baseEntityType.Equals(type.BaseType));

            mapper.BeforeMapManyToOne += (modelInspector, propertyPath, map) => map.Column(propertyPath.LocalMember.GetPropertyOrFieldType().Name + "Id");
            //mapper.BeforeMapManyToOne += (modelInspector, propertyPath, map) => map.Cascade(Cascade.Persist);
            mapper.BeforeMapBag += (modelInspector, propertyPath, map) => map.Key(keyMapper => keyMapper.Column(propertyPath.GetContainerEntity(modelInspector).Name + "Id"));
            //mapper.BeforeMapBag += (modelInspector, propertyPath, map) => map.Cascade(Cascade.All);

            mapper.BeforeMapClass += (modelInspector, type, classCustomizer) =>
            {
                classCustomizer.Id(c => c.Column(type.Name + "Id"));
                classCustomizer.Id(c => c.Generator(Generators.GuidComb));
                classCustomizer.Table(ReservedTableNameHandler(type));
            };

            mapper.Class <Entity>(map => {
                map.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
                map.Version(x => x.Version, m => m.Generated(VersionGeneration.Always));
            });
        }
        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;
        }