public static void WithConventions(this ConventionModelMapper mapper, Configuration configuration) {
            Type baseEntityType = typeof(Entity);

            mapper.IsEntity((type, declared) => IsEntity(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.Identity));
                classCustomizer.Table(Inflector.Net.Inflector.Pluralize(type.Name.ToString()));
            };

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

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

            AddConventionOverrides(mapper);

            HbmMapping mapping = mapper.CompileMappingFor(typeof(ActionConfirmation<>).Assembly.GetExportedTypes().Where(t => IsEntity(t)));
            configuration.AddDeserializedMapping(mapping, "TemplateSrcMappings");
        }
Example #2
0
        public static void WithMappings(this ModelMapper mapper, Configuration configuration)
        {
            // Initially the Product table had a FK to Category
            // and Category table had a FK to Product.
            mapper.Class<Product>(map => map.Set(x => x.Categories,
                                                 set =>  {
                                                             set.Key(key =>
                                                             {
                                                                 key.Column("CategoryId");
                                                                 key.ForeignKey("FK_Product_Category_CategoryId");
                                                             });
                                                             set.Table("Product_Category");
                                                         },
                                                         ce => ce.ManyToMany(m => m.Column("ProductId"))));

            mapper.Class<Category>(map => map.Set(x => x.Products,
                                                set =>  {   set.Key(key =>
                                                            {
                                                                key.Column("ProductId");
                                                                key.ForeignKey(
                                                                    "FK_Product_Category_ProductId");
                                                            });
                                                            set.Table("Product_Category");
                                                        },
                                                  ce => ce.ManyToMany(m => m.Column("CategoryId"))));

            // Initially the SubCategories and Parent were NOT mapped.
            mapper.Class<Category>(map => map.Set(x => x.SubCategories,
                                            set =>  {   set.Key(k => k.Column("ParentCategoryId"));
                                                        set.Inverse(true);
                                                    },
                                                    ce => ce.OneToMany()));

            mapper.Class<Category>(map => map.ManyToOne(x => x.Parent,
                                            manyToOne =>    {
                                                                manyToOne.Column("ParentCategoryId");
                                                                manyToOne.Lazy(LazyRelation.NoLazy);
                                                                manyToOne.NotNullable(false);
                                                            }));

            var mapping = mapper.CompileMappingFor(typeof(Entity).Assembly.GetExportedTypes());
            configuration.AddDeserializedMapping(mapping, "l337");
        }
Example #3
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");
        }