views:

7549

answers:

5

I am currently playing around with the HybridSessionBuilder class found on Jeffrey Palermo's blog post:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

Using this class, my repository looks like this:

public class UserRepository : IUserRepository
{
    private readonly ISessionBuilder _sessionBuilder;

    public UserRepository(ISessionBuilder sessionBuilder)
    {
        _sessionBuilder = sessionBuilder;
    }

    public User GetByID(string userID)
    {
        using (ISession session = _sessionBuilder.GetSession())
        {
            return session.Get<User>(userID);
        }
    }
}

Is this the best way to go about managing the NHibernate session / factory? I've heard things about Unit of Work and creating a session per web request and flushing it at the end. From what I can tell, my current implementation isn't doing any of this. It is basically relying on the Repository to grab the session from the session factory and use it to run the queries.

Are there any pitfalls to doing database access this way?

+8  A: 

With ASP.Net MVC you want to make sure the life of the session is maintained during the Action method on your controller, as once your controller has exited all your data should be collected. I am not sure if this mechanism will help with that.

You might want to look into S#arp Architechure which is a set of libraries and guidance for building ASP.Net MVC application using nHibernate. http://code.google.com/p/sharp-architecture/

JoshBerke
We're currently managing this session life using a begin request and end request call in the global.asax (ignoring css and js etc)Didn't want to have to decorate every Action with a Unit Of Work instruction.
Perhentian
I was using the End Request at first but then when a critical error would occur the request has already ended the page has been rendered and I didn't see a clean way to handle the error other then sending the user to a generic page. I prefer an explicit close the of the session (Or at a minimum commiting the transaction) as you move from the Controller to the View.
JoshBerke
+1  A: 

I wouldn't open and close sessions on each data request to NHibernate. I would use the Unit of Work libraries that many others suggest or do some more reading. NHForge.org is getting started and I believe that there are some practices on setting up NHibernate for a general web application.

One of the "oh wow that's cool moments" that I've gotten from NHibernate was taking advantage of lazily loading collections during development. It was a neat experience being able to not have to do all those joins in order to display data on some associated object.

By closing the session like this, the above scenario would not be possible.

There might be something that is going on with transactions as well.

Min
+18  A: 

You should not wrap your ISession in a using statement -- the point of passing the ISessionBuilder into the repository constructor (dependency injection) is that the calling code is responsible for controlling the life cycle of the ISession. By wrapping it in a using, Dispose() is called on the ISession and you won't be able to lazy load object members or persist it.

We do something similar by just passing in an ISession to the repository constructor. Mr. Palermo's code, as I understand it, simply adds lazy initialization of the ISession. I don't think that's needed because why would you new up a repository if you're not going to use it?

Jamie Ide
Good answer, Id up-vote it twice if I could :)
UpTheCreek
+1  A: 

This is the setup I used after researching this more. Seems to work great and doesn't have that annoying habit of creating an ISession on static file requests like most guides out there:

http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/

Kevin Pang
What exactly is reason that it opens session for static file requests?
Arnis L.
A: 

Just found a clean solution using Unity to inject a session per request:

http://letsfollowtheyellowbrickroad.blogspot.com/2010/05/nhibernate-sessions-in-aspnet-mvc.html

Giorgio Bozio