tags:

views:

234

answers:

3

As a newbie to nHibernate, I am finding the creation of sessions to be a tad confusing.

Why doesn't nHibernate ship with something like what (i believe) Unit Of Work does?

or is there something built and UofW is more of an addon/preference? (context: ASP.NET MVC web application with SQL server).

Note: I am new to nHibernate, and spoiled with how MS does things. I would love it if things that are generally required to be shipped with the product download etc.

+4  A: 

The session is kind of unit of work and also an identity map. You can create your own unit of work abstraction over it. Different applications need different kinds of unit of works and different programmers like different implementations too.

You won't need more than 50 lines of code to create your own unit of work implementation to abstract session and transaction management.

Paco
A: 

I agree with Paco, although in a web application this can be a little more work.

I think the real issue is that most of us want to isolate our upstream code from a direct dependency on NHibernate and therefore we don't want to work directly against the session.

Once you wrap you head around NHibernate this abstraction becomes pretty simple but but it's a little harder to get your head around this abstraction at the same time you are trying to learn about NHibernate for the first time.

Andrew Hanson
+2  A: 

In case you haven't found it yet... there is a Unit of Work component for web applications that is part of the NHibernate Contrib project, called NHibernate Burrow. It is reasonably flexible and supports short (session-per-request) and long (sessions spanning more than one request) conversations.

I have used Burrow with ASP.NET MVC in the past and it works fine, though I haven't tried the long conversation in MVC. In it's current state, in order to use it, you will need to update the library references and compile the Burrow project. It's a little extra work but it's worth it.

Steven Lyons