NHibernate and ASP.Net: what’s the problem?

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:

Using NHibernate with ASP.NET

Of course, I could have gone with some of the existing frameworks but then what would I have learned?

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

Enterprise Implementations
There will most certainly be more then the ones I provide here, but these are thye ones I’ve found (but not yet used):
Rhino tools
S#arp Architecture

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.


The ASP.Net Application Model:
Understanding the HttpApplication Class
ASP.NET Applications
Application and Session Objects in ASP.NET
The ASP Column: HTTP Modules

NHibernate Session Management
NHibernate Session Management in Domain Driven App’s
Simple NHibernate Example, Part 4 : Session Management

Existing implementations:
Rhino tools
S#arp Architecture

More questions and trouble:
NHibernate in an ASP.NET MVC application
NHibernate Session Module opens unecessary transactions for CSS and images
NHibernate Sessions with ASP.NET


3 thoughts on “NHibernate and ASP.Net: what’s the problem?

  1. Hi,
    Maybe you should that a look to:
    NHibernate with ASP.NET Problem–Design–Solution [http://www.wrox.com/WileyCDA/WroxTitle/NHibernate-with-ASP-NET-Problem-Design-Solution.productCd-0470488204.html].

    I have read it and I think it has been a very good intro for my as a NH’s beginner.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s