public override Configuration Map(Configuration cfg)
        {
            ConventionModelMapper mapper = new ConventionModelMapper();

            mapper.IsEntity((x, y) => this.IsEntity(x, y, this.EntitiesAssemblyName));
            mapper.IsRootEntity((x, y) => this.IsRootEntity(x, y, this.EntitiesAssemblyName));
            mapper.IsOneToMany((x, y) => this.IsOneToMany(x, y));
            mapper.IsManyToOne((x, y) => this.IsManyToOne(x, y));
            mapper.IsManyToMany((x, y) => this.IsManyToMany(x, y));
            mapper.IsBag((x, y) => this.IsBag(x, y));
            mapper.IsSet((x, y) => this.IsSet(x, y));
            mapper.IsProperty((x, y) => this.IsProperty(x, y));
            mapper.IsPersistentProperty((x, y) => this.IsPersistentProperty(x, y));
            mapper.BeforeMapClass      += this.BeforeMapClass;
            mapper.BeforeMapProperty   += this.BeforeMapProperty;
            mapper.BeforeMapSet        += this.BeforeMapSet;
            mapper.BeforeMapOneToMany  += this.BeforeMapOneToMany;
            mapper.BeforeMapManyToOne  += this.BeforeMapManyToOne;
            mapper.BeforeMapManyToMany += BeforeMapManyToMany;

            HbmMapping mappings = mapper.CompileMappingFor(Assembly.Load(this.EntitiesAssemblyName).GetExportedTypes());

            cfg.AddMapping(mappings);

            return(cfg);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void ApplyMapping(ConventionModelMapper mapper)
        {
            mapper.IsOneToMany((member, isDeclared) => {
                if (isDeclared)
                {
                    return(true);
                }

                return(member.GetCustomAttribute <ManyToManyAttribute>() == null);
            });

            mapper.IsSet((member, isDeclared) => {
                if (isDeclared)
                {
                    return(true);
                }

                var prop = member as PropertyInfo;

                if (prop == null || !prop.CanRead)
                {
                    return(false);
                }

                var propertyType = prop.PropertyType;

                if (!propertyType.IsGenericType || propertyType.GetGenericTypeDefinition() != typeof(ISet <>))
                {
                    return(false);
                }

                if (!typeof(IEntity).IsAssignableFrom(propertyType.GetGenericArguments()[0]))
                {
                    return(false);
                }

                return(true);
            });

            mapper.BeforeMapSet += (modelInspector, propertyPath, propertyCustomizer) => {
                if (propertyPath.LocalMember.GetCustomAttribute <ManyToManyAttribute>() != null)
                {
                    return;
                }

                propertyCustomizer.Cascade(Cascade.All | Cascade.DeleteOrphans);
                propertyCustomizer.Access(typeof(SourceCollectionAccessor));

                propertyCustomizer.Key(keyMap => {
                    var parentType = propertyPath.LocalMember.DeclaringType;
                    var childType  = propertyPath.LocalMember.GetPropertyOrFieldType().GetGenericArguments()[0];

                    var columnName = formatter.GetIdentityColumnName(parentType);
                    var fkName     = formatter.GetForeignKeyConstraintName(parentType, childType);

                    keyMap.Column(columnName);
                    keyMap.ForeignKey(fkName);
                });

                propertyCustomizer.Inverse(true);
            };
        }