Archive for February, 2009

24
Feb
09

Fluent NHibernate with Burrow

I like Burrow for web application development. I’ve used it on two projects now and it basically just disappears into the background.

The new kid on the block is Fluent NHibernate. This is very cool. Bye bye angle bracket tax!

So for my latest project I decided to use them both. Getting them to work together took a little bit of digging around in the source of Burrow, as it likes to be configured by XML from your web.config.

Here how I have it set up.

    <NHibernate.Burrow customConfigurator="VicPol.ERS.Model.Persistence.NHibernateMapping, VicPol.ERS.Model" >
        <persistenceUnits>
            <add name="MDN" nh-config-file="~\nhibernate.config" />
        </persistenceUnits>
    </NHibernate.Burrow>


The interesting part of this is the attribute ‘customConfiguration’ which tells Burrow to instantiate an instance of the type given as the attribute value and use this to customise Burrow and/or NHibernate at run time.

This type must implement NHibernate.Burrow.IConfigurator:

using FluentNHibernate;
using log4net;
using NHibernate.Burrow;
using NHibernate.Cfg;
using VicPol.ERS.Model.Support;
 
 
namespace VicPol.ERS.Model.Persistence
{
    public class NHibernateMapping : IConfigurator
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(NHibernateMapping));
 
 
 
        public virtual void Config(IBurrowConfig val)
        {
        }
 
 
 
        public virtual void Config(IPersistenceUnitCfg puCfg, Configuration nhCfg)
        {
            log.DebugFormat("Config: Setting up NHibernate DependencyInjectionInterceptor and Mappings");
 
            nhCfg.SetInterceptor(new DependencyInjectionInterceptor(Container.EntitySubkernel));
            nhCfg.AddMappingsFromAssembly(typeof(NHibernateMapping).Assembly);
        }
    }
}


The second overload of Config is run for each persistence unit. It takes an instance of NHibernate.Configuration which lets us play around as we see fit with the NH configuration.

I’m using the extension method AddMappingsFromAssembly provided by FluentNHibernate to set up my entity mapping fluently.

As an aside, the other code in here tells NHibernate to use a dependency injection container to construct the entities it loads.

Advertisements