Using Repositories with nHibernate

Intro

The point of this article is to demonstrate 2 ways that I have been using the Repository pattern in conjuction with nHibernate to provide my applications with rich domain objects, I wanted to also get feedback to the options shown and if there are any other ways to implement Repository with nHibernate.

Repository manages session

The first option and the one I used for the Staff Intranet and for older applications is wereby the repository manages the session object and decides where transactions should be used, here is an example of one the methods that we might typically see:

public int Save(User user)
{
    ISession session = NHibernateSessionManager.GetSession();
    ITransaction transaction = session.BeginTransaction();
    try
    {
        session.Save(user);
        session.Flush();
        transaction.Commit();
        return user.Id;
    }
    catch
    {
        transaction.RollBack();
        throw;
    }
    finally
    {
        NHibernateSessionManager.CloseSession();
    }
}

This has a number of the issues:

  • If you need to use a number of repositories in a single transaction this option will not work
  • Integration testing becomes more difficult as the repository handles the session and transactions

Caller manages session

The other way we can implement repositories is to let the calling code manage session and when transactions take place, here is an updated version of the above to accomodate the caller managing session & transactions:

public int Save(User user)
{
    ISession session = NHibernateSessionManager.GetSession();
    session.Save(user);
    session.Flush();
    return user.Id;
}

Our code has been reduced however the caller would now contain the code removed:

public int SaveUser(User user)
{
    ITransaction transaction = NHibernateSessionManager.GetSession().BeginTransaction();
    try
    {
        int userId = userRepository.Save(user);
        transaction.Commit();
        return userId;
    }
    catch (Exception thrownException)
    {
        transaction.RollBack();
        //... log exception, wrap up exception, rethrow, etc...
    }
    finally
    {
        NHibernateSessionManager.CloseSession();
    }
}

We could now quite happily make calls before or after the saving of the user to other repositories knowing that they would all be using the session & transaction.

I’m pretty convinced that this option of having the caller responsible for manageing session & transactions is a better way of implementing repositories, however if you of any other ways then please point me in the right direction 🙂

Helping Integration testing

One thing that I didn’t like having to do was performing integration tests against the database the reason being the extra care that needed to be taken to make sure that data was reverted back to the state it was to begin with, by using the caller in charge of session & transactions approach you can rollback any data changes made.

Advertisements

One thought on “Using Repositories with nHibernate

  1. Pingback: Bookmarks about Session

Comments are closed.