In an old blog post I described how you can implement the Open Session in View pattern using Contextual Sessions. Since then I have discovered even easier way to handle the NHibernate sessions in an ASP.NET web application. The approach I have used recently is the one proposed by Ayende in this blog post. His approach is to store the reference to the current session in the current HttpContext and hooking in the session lifecycle management into the BeginRequest and EndRequest events.
Another approach that I haven’t used yet but I certainly will try in some upcoming project is to let your IoC container manage the sessions. Here is an example of how to do that with StructureMap.
1 Comment