Is this a good way to wire up NHibernate using StructureMap?

I am blogging this code snippet in order to ask some people at altnetseattle if this is a good implementation or not.

I want to know whether or not this is considered a good way of wiring up NHibernate using StructureMap. I am making use of the registry DSL feature, this code would get called somewhere in my bootstrapper.

Elsewhere in my code, my Repositories simply have a constructor dependency on ISession.

NHibernateConfiguration is my class that does all configuration of NHibernate using FluentNHibernate.

   1: public class DataAccessRegistry : Registry
   2: {
   3:     public DataAccessRegistry()
   4:     {
   5:         // NHibernate ISessionFactory
   6:         ForRequestedType<ISessionFactory>()
   7:             .CacheBy(InstanceScope.Singleton)
   8:             .TheDefault.Is.ConstructedBy(() => new NHibernateConfiguration().BuildSessionFactory());
   9:  
  10:         // NHibernate ISession
  11:         ForRequestedType<ISession>()
  12:             .CacheBy(InstanceScope.Hybrid)
  13:             .TheDefault.Is.ConstructedBy(() => ObjectFactory.GetInstance<ISessionFactory>().OpenSession());
  14:     }
  15: }

Comments

#1 Jeremy D. Miller on Saturday, February 28 2009 at 11:14 AM

I'd strongly advise you to change the ISession configuration to this lambda:

.TheDefault.Is.ConstructedBy( context => context.GetInstance<ISessionFactory>().OpenSession()

That is how my team configures NH with StructureMap. Referencing ObjectFactory in side a lambda there would be bad juju.