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

Version numbering: solutions to questions remaining

A while ago I posted an entry about version numbering and ended it with two questions:

  1. When is backward compatibility broken? This probably looks like a stupid question, but imagine: A DLL named “SomeFunctionality.dll” uses some other DLL’s, named “Other.dll” and “MoreOther.dll”. But then this ”SomeFunctionality.dll” is changed and doesn’t use “Other.dll” anymore, but uses “Another.dll”. However, the interface of “SomeFunctionality.dll” didn’t change. Is backward compatibility broken or not?
  2. What about customer related changes to old software which breaks backward compatibility? Say for example you have version 1.1.0.1 at a customer site. Meanwhile version 2.x.y.z has already been released. Now the customer with version 1.1.0.1 wants some new feature which breaks backward compatibility. However, major version 2 is already taken. What major version do you use?

After some thought I will present you today with possible solutions.
Continue reading

Give Meaning To Your Code – naming of variables and types

The rule
This probably sounds obvious, but apparently it isn’t. What’s more, if you think about it, it even isn’t that easy to make the difference.

Your variables have a Behaviour and a Role
Behaviour maps to the type of your variable, and the role maps to the name of your variable.

The problem
We’ve had a round of code review today and while looking into the code I saw some classes which where really a packing of the same members into several classes. Those classes where then given different names.

An simple example will make clear what I mean (These are my own samples and not the real code):
(code is C++)

class Person
{
public:
   int GetAge();
   void SetAge(int age);

private:
   int m_age;
}

class Husband
{
public:
   int GetAge();
   void SetAge(int age);

private:
   int m_age;
}

class Wife
{
public:
   int GetAge();
   void SetAge(int age);

private:
   int m_age;
}

The Solution
In the above sample code, the classes Husband and Wife shouldn’t really exist. You should use variables with names husband and wife and with type Person. The role is husband and wife and they have the behaviour of Person.

This requires knowledge of the domain your application is written for because the distinction isn’t always that easy, as you will see in the following caveat’s.

Caveat
You probably think that this is obvious and in the above classes it is. But what if you have a class Person and you want to model “Male” and “Female”. Do you create new classes representing those or do you add a member “Sex” to the above class in which you make the difference between males and females?

Or maby you will have started with a class named Male, and then later you want to model females too. Will you make a class Female, or will you rename your class Male to Person and add the Sex member? The last solution will require you to adapt your code everywhere you use Male to set it’s Sex member.

You may also have two classes which are very common but differ slightly. Should you pack them into one class, or should they stay different. Maybe they must inherit from a common baseclass.

Updates
11 November 2006: original version