FluentNHibernate AutoMapping

Official document: http://wiki.fluentnhibernate.org/Auto_mapping

We use an AutoPersistenceModelGenerator to wire up automapping with config.


config.Mappings(m => m.AutoMappings.Add(new AutoPersistenceModelGenerator(typeof(T).Assembly).Generate()));

We also might have some unique conventions. e.g., PropertyName => Column_Name, EntityName=>EntityName_TAB, Id=>EntityName_ID, etc, all can be defined in AutoPersistenceModelGenerator.


    public class AutoPersistenceModelGenerator
    {
        private readonly Assembly _assemblyContainsEntities;

        public AutoPersistenceModelGenerator(Assembly assemblyContainsEntities)
        {
            _assemblyContainsEntities = assemblyContainsEntities;
        }

        public AutoPersistenceModel Generate()
        {
            var mappings = new AutoPersistenceModel();
            mappings.AddEntityAssembly(_assemblyContainsEntities).Where(GetAutoMappingFilter);
            mappings.Conventions.Setup(GetConventions());
            mappings.Setup(GetSetup());
            mappings.UseOverridesFromAssembly(_assemblyContainsEntities);
            return mappings;
        }

        private Action<AutoMappingExpressions> GetSetup() {
            return c => {
                c.FindIdentity = type => type.Name == "Id";

            };
        }

        private Action<IConventionFinder> GetConventions()
        {
            return c =>
            {
                c.Add(ConventionBuilder.Id.Always(x =>
                {
                    x.Column(x.EntityType.Name.ToUpper() + "_ID");
                    x.CustomSqlType("INT"); // otherwise our long type id will be automapped to BIGINT
                }));
                // other way:                c.Add(PrimaryKey.Name.Is(x => x.EntityType.Name + "_ID"));
                // or                        c.Add<PrimaryKeyConvention>();

                c.Add(Table.Is(x => x.EntityType.Name + "_TAB"));
                // The above line can also be replaced by TableNameConvertion
                //                c.Add<TableNameConvention>();</pre>
                c.Add(ForeignKey.EndsWith("_ID"));
                // or                c.Add<Infrastructure.Convertions.ForeignKeyConvention>();
                c.Add<ManyToManyTableNameConvention>();
                c.Add<UnderstoreColumnNameConvention>();
                c.Add<StringPropertyConvention>();
                c.Add<YesNoPropertyConvention>();
                c.Add<AuditPropertyConvention>();
          };
    }

    private bool GetAutoMappingFilter(Type t)
    {
      return t.GetInterfaces().Any(x => x == typeof(IAutoMap) );
    }
 }

 public class UnderstoreColumnNameConvention : IPropertyConvention, IPropertyConventionAcceptance
    {
        public void Apply(IPropertyInstance instance)
        {
            instance.Column(Inflector.Net.Inflector.Titleize(instance.Name).Replace(" ", "_"));
        }

        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
        {
            criteria.Expect(propertyInspector => !AuditPropertyConvention.IsAuditProperty(propertyInspector.Name));
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s