On a yet-to-be-released side project of mine, I decided to use Fluent NHibernate, StructureMap, and ASP.NET MVC. It took me awhile to get everything to play together nicely, so I documented the steps I took in case anyone out there was interested in using in a similar setup.
Step 1: Set up StructureMap
First, I created the StructureMapControllerFactory class (taken from the MvcContrib project):
I then added the following line to the Application_Start() function in Global.asax.cs so that StructureMap would inject dependencies whenever an ASP.NET MVC controller was instantiated:
Step 2: Set up Fluent NHibernate
Next, I created a static function for creating an ISessionFactory (this code may look very different depending on your database and project setup):
Step 3: Hook up Fluent NHibernate with StructureMap
Then, I updated my Application_Start() function in Global.asax.cs so that StructureMap would be aware of how to instantiate an NHibernate ISessionFactory and ISession:
Now, whenever StructureMap needs to create an ISessionFactory, it calls the CreateSessionFactory() method defined in Step 2. Since ISessionFactory is expensive to create, I have configured StructureMap to create it as a Singleton so that it will only be created once per application.
Similarly, whenever StructureMap needs to create an ISession, it will create/retrieve an ISessionFactory instance and call its OpenSession() method. This is scoped at the HttpContext level, which means that at most, one ISession will be created by StructureMap per web request.
Step 4: Clean up
Finally, in order to ensure that we aren’t leaking ISessions on every web request, I added the following line to my Application_EndRequest function to properly dispose of the ISession StructureMap may have created during that web request:
The setup described above works on my machine with the following versions:
- ASP.NET MVC 2
- StructureMap 2.6.1
- Fluent NHibernate 22.214.171.1244