예제 #1
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));
 }
예제 #2
0
        static void AddConventionalMapping(Configuration cfg)
        {
            var modelMapper = new ConventionModelMapper();
            modelMapper.IsEntity((x, y) => x.IsClass == true && x.IsSealed == false && x.Namespace == typeof(Program).Namespace);
            modelMapper.BeforeMapClass += (x, y, z) => { z.Id(a => a.Generator(Generators.Identity)); z.Lazy(true); };
            modelMapper.BeforeMapManyToOne += (x, y, z) => { z.Lazy(LazyRelation.Proxy); z.NotNullable(true); };

            var mappings = modelMapper.CompileMappingFor(typeof(Program).Assembly.GetTypes().Where(x => x.IsPublic && x.IsSealed == false));

            cfg.AddMapping(mappings);
        }
예제 #3
0
		protected override HbmMapping GetMappings()
		{
			var mapper = new ConventionModelMapper();
			System.Type baseEntityType = typeof (DomainObject);
			mapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && !baseEntityType.Equals(t));
			mapper.IsRootEntity((t, declared) => baseEntityType.Equals(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 => { }, rel => rel.ManyToMany()); });
			HbmMapping mappings = mapper.CompileMappingFor(new[] {typeof (Class1), typeof (Class2)});
			return mappings;
		}
		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"));
		}
예제 #5
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;
        }
        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 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");
		}
예제 #8
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");
        }
예제 #9
0
        public static HbmMapping Generate()
        {
            //Conventions
              var mapper = new ConventionModelMapper();
              var baseEntity = typeof (EntityBase);

              mapper.BeforeMapProperty += (ispector, member, customizer) => customizer.Length(40);

              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.BeforeMapSet +=
            (insp, prop, map) => map.Key(km => km.Column(prop.GetContainerEntity(insp).Name + "Id"));

              mapper.IsEntity((t, d) => baseEntity.IsAssignableFrom(t) && baseEntity != t);

              mapper.IsRootEntity((t, d) => t.BaseType == baseEntity);

              mapper.IsSet(IsSetFieldType);

              Customize(mapper);

              HbmMapping mappings = mapper.CompileMappingFor(new[]
                                                     {
                                                       typeof (Customization), typeof (Order),
                                                       typeof (Payment),
                                                       typeof (OrderItem), typeof (Product)
                                                     });
              return mappings;
        }
예제 #10
0
        public static void WithConventions(this ConventionModelMapper mapper, Configuration configuration)
        {
            var baseEntityType = typeof(Entity);

            mapper.IsEntity((type, declared) => IsEntity(type));
            mapper.IsComponent((type, b) => IsComponent(type));
            mapper.IsRootEntity((type, declared) => baseEntityType.Equals(type.BaseType));

            mapper.BeforeMapClass += (modelInspector, type, classCustomizer) =>
            {
                classCustomizer.Id(c => c.Column("Id"));
                classCustomizer.Id(c => c.Generator(Generators.HighLow));
                classCustomizer.Table(Inflector.Net.Inflector.Pluralize(type.Name.ToString()));
            };

            mapper.IsPersistentProperty((memberinfo, currentlyPersistent) =>
            {
                return(memberinfo.Name != "Owner");
            });

            mapper.BeforeMapManyToOne += (modelInspector, propertyPath, map) =>
            {
                map.Column(propertyPath.LocalMember.GetPropertyOrFieldType().Name + "Id");
                map.Cascade(Cascade.Persist);
            };

            mapper.BeforeMapBag += (modelInspector, propertyPath, map) =>
            {
                map.Key(keyMapper => keyMapper.Column(propertyPath.GetContainerEntity(modelInspector).Name + "Id"));
                map.Cascade(Cascade.All);
            };

            mapper.BeforeMapProperty += (inspector, member, customizer) => {
                // This is pure guesswork, but seems to be the only way I can think of to alter
                // the column naming of a property mapped as part of a component
                if (typeof(IComponent).IsAssignableFrom(member.LocalMember.DeclaringType))
                {
                    if (member.LocalMember.Name == "Value")
                    {
                        customizer.Column(member.PreviousPath.LocalMember.Name);
                    }
                    else if (member.LocalMember.Name != "Owner")
                    {
                        customizer.Column(member.PreviousPath.LocalMember.Name + member.LocalMember.Name);
                    }
                }
            };


            mapper.Component <RestrictedVisibility <string> >(x => {
                x.Property(c => c.Value);
                x.Property(c => c.Visibility);
                x.Parent(c => c.Owner);
            });

            mapper.Component <RestrictedVisibility <bool> >(x => {
                x.Property(c => c.Value);
                x.Property(c => c.Visibility);
                x.Parent(c => c.Owner);
            });

            // The following probably works, but not if we stop "Owner" from being a persistent property
            // using mapping.IsPersistentProperty, and if we don't do that we get a Too Many Properties exception.
            // There's an old thread on nhusers about how there's no documentation in this area...
            //mapper.BeforeMapComponent += (inspector, member, customizer) => {
            //    if (member.LocalMember.Name == "Owner") {
            //        customizer.Parent(member.LocalMember);
            //    }
            //};

            AddConventionOverrides(mapper);

            HbmMapping mapping = mapper.CompileMappingFor(
                typeof(Entity).Assembly.GetExportedTypes().Where(IsEntity));

            configuration.AddDeserializedMapping(mapping, "MyStoreMappings");
        }
 public static void ExcludeBaseEntity(ConventionModelMapper modelMapper, Type baseEntityType)
 {
     modelMapper.IsEntity((t, declared) => baseEntityType.IsAssignableFrom(t) && baseEntityType != t && !t.IsInterface);
     modelMapper.IsRootEntity((type, declared) => type.BaseType != null && type.BaseType.Equals(baseEntityType));
 }