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.

Links

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

What did we learn this week? Week of 19 januari 2009

Links for this week:

Global namespaces
I wanted to name a namespace “CompanyName.System” which turned out to be a bad idea: it gave me the unfortunate compiler error “The type or namespace name ‘SomeClassName’ could not be found”. The solution is easy according to the MSDN article How to: Use the Namespace Alias Qualifier (C# Programming Guide) but unfortunately the error happened in the designer file of a Windows.Form and there is no point in changing any code there. I suppose Microsoft should use the global:: identifier in generated code but they don’t. Or is there a solution I’m overlooking?

WCF Service hosting and Client access in a Windows Forms application
Actually, I came across this one too late. Last week I made a test application which was I simple Windows Forms application whome hosted a WCF service and connected to that same WCF service. A big NO GO was the result, as this guy also experienced…

WCF Services with constructor parameters
I eventually didn’t use it, but I had a use case this week for having a WCF service in which the constructor would have some parameters. An alternative could be to provide the object instance to use for implementing the service yourself instead of letting WCF instantiate it for you as the standard behaviour is. Again, I’m not the first one willing to do this as these links prove:
Unity & friends: The WCF service host side
WCF Dependency Injection Behavior
WCF Service Dependency Injection

Generics in .Net
I’m still getting used to this. Comming from a C++ background I’m still suprised by compiler errors trying to use a template parameter as a base class, or as an attribute parameter. In C# this is not allowed. Why? Read these posts:
Generic argument as a base class
Generic argument as a base class
Using Generic Parameter as an Attribute constructor parameter

Updated code for NHibernate articles Part 1 to Part 5

A while ago I wrote a series of articles about NHibernate and how mapping objects to classes is done using C#:

  1. NHibernate Part 1: Hello World with NHibernate
  2. NHibernate Part 2: CRUD with NHibernate
  3. NHibernate Part 3: Mapping techniques for aggregation – One To One mapping
  4. NHibernate Part 4: Mapping techniques for aggregation – One-To-Many mapping
  5. NHibernate Part 5: Mapping techniques for aggregation – Many-To-One and Many-To-Many mapping

At that time I used MySql and NHibernate version 1.0.0.0

Now I have updated the code to use a different tooling set and the 2.0.1.GA version of NHibernate.
The tooling set is:
Visual Studio 2008 of which there is a free version available here
SQL Server Express which you can download for free here

The updated code can be downloaded here.

Offline Maps: A new version (bugfix) and update of the base URL’s

I just posted release 1.4.1 of my OfflineMaps application.

There was an error in the 1.4.0 verszion which prohibited the downloading of tiles, which is what the application is all about.

I also noted that Google updated their base-URL’s, so if you download the above version, start by updating the base-URL’s.

You can download it on the project page

Hope you enjoy it !!