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?