Basic NHibernate session management with Autofac

Using the container scoping capablity of Autofac makes NHibernate session management a breeze. Obviously this only handles the most basic session-per-request scenario but we know that can cover a large percentage of most peoples requirements anyway. I am sure other containers could get to this functionality as well, but I was blown away by how easy it was aith Autofac. Below is my session management configuration:

            builder.Register<NHibernateInstance>().SingletonScoped();
            builder.Register(c => c.Resolve
<NHibernateInstance>().CreateSession()).As<ISession>().ContainerScoped();
            builder.RegisterGeneric(typeof (Repository
<>)).As(typeof (IRepository<>)).ContainerScoped();

So the key points are:

  1. Plug in the Autofac HttpHandler for ASP.Net integration by following the instructions here
  2. Have a singleton instance of the NHibernate session factory (housed in my NHibernateInstance class)
  3. Have a method on that singleton registered instance that returns an ISession (CreateSession in my example)
  4. Configure a container scoped instance of the NHibernate ISession, thus ensuring that the created instance will not cross request boundaries
  5. Have the container dynamically inject the ISession into your IRepository implementation

For more information on container scoping in Autofac for ASP.Net web applications check out the Autofac wiki article.

August 19, 2008 14:11 by steven.burman
E-mail | Permalink | Comments (3) | Comment RSSRSS comment feed

Related posts

Comments

August 27. 2008 12:02

Nick

Still smiling at seeing this here - really happy when anyone digs the possibilities that come with that disposal mechanism Smile

Looking forward to hearing about more of your insights as you get into this project dude, keep it up!

NB

Nick

December 3. 2008 01:02

Michiel

Hi, thanks for posting this, it's been helpful.

I have a question though. Why do you wrap the SessionFactory in a NHibernateInstance class? Wouldn't it be enough to register a SessionFactory as SingletonScoped and a Session as ContainerScoped? That way, the application will have one SessionFactory with which to open a single session for each request (in an ASP.NET web application).

// SingletonScoped is the default in Autofac
builder.Register(c => new Configuration().Configure().BuildSessionFactory());
// ContainerScoped will keep one instance for the ASP.NET request
builder.Register(c => c.Resolve<ISessionFactory>().OpenSession()).ContainerScoped();

Michiel

December 3. 2008 09:15

steven.burman

I see no reason why it couldn't be done exactly how you have demonstrated.

The reason that I have the wrapper class is because my NHibernate configuration is done in code (thanks to Fluent NHibernate) and the wrapper class allows me to cater for that.

Additionally, with my design I have now set NHibernateInstance as an abstract base class. So now I can register SQLiteNHibernateInstance or SqlServerNHibernateInstance depending on my circumstances (eg. unit tests use SQLite).

steven.burman

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 6. 2009 13:26