3
votes

I've just finished setting up a project using MVC 3, Fluent nHibernate, AutoMapper, and Autofac, making use of generic repositories, the unit of work pattern, and a 3-tiered design. I now have come to this problem:

I have a unit of work, but I do not know where/how to commit it. Starting it is easier; use Autofac, inject on a per-HTTP request basis - the constructor for my unit of work begins the transaction. However, what I realized was I ended up with business classes like this:

private readonly IUnitOfWork _unitOfWork;
private readonly IUserRepository _userRepository;

public UserHandler(
    IUserRepository userRepository, 
    IUnitOfWork unitOfWork)
{
    _userRepository = userRepository;
    _unitOfWork = unitOfWork;
}

public void CreateUser(User user)
{
    // Fill the date fields
    user.CreationDt = DateTime.Now;
    user.ModifiedDt = DateTime.Now;

    // Add the user 
    _userRepository.Add(user);

    // Commit the changes
    _unitOfWork.Commit();
}

This is great, as long as I don't need to do any other transactions. But what if, after calling .Add, I decide to call another business class with a similar method? Then I've got 2 commits, which I'm assuming will blow up since I will be attempting to end an already completed transaction.

I thought about putting the commit in my UnitOfWork's Dispose, but that is bad news if an exception occurs. I have seen some cases where people also inject their UnitOfWork into their controllers, but this seems wrong as it breaks separation of concerns, with your controllers bypassing the business layer and calling a database layer directly. Lastly, I'm currently looking into using an attribute like the one used in Sharp Architecture, but I'm also unsure if this is proper approach (again, aren't you giving the controller direct access/knowledge of the data layer?).

Can someone share some wisdom as to where I should be committing?

2

2 Answers

2
votes

As far as you share your database connection within one request you can Begin transaction and commit once response is sent to the client. I head a special action filter to achieve this.

public sealed class CommitOnSuccess : ActionFilterAttribute
{
    public IUnitOfWork UnitOfWork { get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Exception == null)
            UnitOfWork.Commit();
    }
}

That's how I made my action methods plain and lightweight

[CommitOnSuccess]
[HttpPost]
public ActionResult Create(ApplicationManagementViewModel model)
{

}

and changes are to be performed on entities obtained from the repository within Create method

0
votes

in application end request you can commit your unit of work. In your global.asax.cs file add the following function :

protected void Application_EndRequest(object sender, EventArgs e)
    {
     //code to commit unit of work
    }

Also since you are using Autofac you could as well scope IUnitOfWork to InstancePerLifetimeScope and make the class implement IDisposable and in your Dispose function you can commit your session. How is it an issue when an exception occurs, you could have a check in your code something like this:

 if ( HttpContext.Current != null && HttpContext.Current.Error != null) 
      transaction.Rollback();
 else
      transaction.Commit();