Possible Duplicate:
How to implement Unit of work in MVC: Responsibility
Hi. I am developing an ASP.NET MVC web application using the following architecture: UI -> Controller -> Service Layer -> Repository. The question here is where to expose the Unit Of Work pattern. For instance I have:
public class SomeController
{
public ActionResult AnAction()
{
using(var unitOfWork = UnitOfWorkManager.Create())
{
try
{
this.ServiceA.Foo();
this.ServiceB.Foo();
unitOfWork.Commit();
}
catch(Exception ex)
{
unitOfWork.Rollback();
}
}
}
}
Is it ok for the controller to know about unit of work? What if we have several calls to different services but within the same action method and we need transactional behavior?
We have built an application with the same architecture and our view was that the UOW class is used exclusively in the Services. Reasons:
The Action
public ActionResult List()
{
var things = ThingService.GetAll();
return View(things);
}
The Service
public IEnumerable<Thing> GetAll()
{
using (ObjectContext context = new Container(ConnectionString))
{
var work = new UnitOfWork(context);
return work.Things.GetAll());
}
}
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With