Fluent NHibernate

time to read 3 min | 414 words

A while ago I mentioned that as it stood at the time, I didn't see any major benefits for the Fluent NHibernate and that you might as well use the XML directly.

What I wanted is NHibernate auto mapper. Couple of days ago I had the chance to take a look at Fluent NHibernate again. What I saw was almost exactly the dream that I had in mind. Here is my NHibernate mapping configuration for the whole application:

public virtual void ConfigureNHibernate(Configuration configuration)
{
    var model = new AutoPersistenceModel
    {
        Conventions =
            {
                GetForeignKeyNameOfParent = (type => type.Name + "Id"),
                GetForeignKeyName = (info => info.Name + "Id"),
                GetTableName = (type => Inflector.Pluralize(type.Name)),
                GetManyToManyTableName =
                    ((child, parent) =>
                     Inflector.Pluralize(child.Name) + "To" + Inflector.Pluralize(parent.Name))
            }
    };


    model
        .AddEntityAssembly(Assembly)
        .Where(entity =>
            entity.Namespace.EndsWith("Model") &&
            entity.GetProperty("Id") != null &&
            entity.IsAbstract == false
            )
        .Configure(configuration);
}

After that, you are done. Just create an entity in the proper place, hit the /database/create and have a lot of fun.

I am loving it.

I still have some concerns about how I can surgically modify some configuration as needed, but I think that I can manage, overall.