I’ve been looking at NHibernate again lately and wanted to start using it in an ASP.NET application. “So what?” you say, well that’s also what I was thinking. But googling for ASP.NET and NHibernate turned out to be very interesting:
What is the problem
The above link on using NHibernate with ASP.NET gives a very good introduction to what the problem is:
The stateless nature of HTTP creates a few challenges with regard to using NHibernate compared to Winforms. One challenge is where to store the SessionFactory, and another one is how to handle each user’s NHibernate Session.
What are the possible solutions?
As the post NHibernate, Part 3 of xxx: NHibernate Session Management proves, your options are infinite. The same above link on using NHibernate with ASP.NET also provides some introductary notes on possible solutions. Another interesting post is on Codeproject: NHibernate Best Practices with ASP.NET, 1.2nd Ed..
The main stream of thought is to make a NHibernate session for every request made to the ASP.Net application. The problem is however how to tap into that request. Most solutions use the IHttpModule interface.
What is this IHttpModule? Wel, it is an interface you can implement which allows you to tap into ASP.Net applications, session and requests. You can read more about it in the article The ASP Column: HTTP Modules
Hope this all sheds some light on what to do. I was originally planning to have an attempt at an implementation but found in the above and below cited articles enough examples and impelmentations that I could hardly provide any more insight. So, check the articles for sample code.
NHibernate Session Management
NHibernate Session Management in Domain Driven App’s
Simple NHibernate Example, Part 4 : Session Management