Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc service layer and unit of work [duplicate]

Tags:

asp.net-mvc

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?

like image 215
Markus Avatar asked Oct 16 '25 21:10

Markus


1 Answers

We have built an application with the same architecture and our view was that the UOW class is used exclusively in the Services. Reasons:

  1. We think the actions should only have view logic so if possible should not know the business rules associated with using multiple repository calls
  2. Business logic rules should (As much as possible) be in a service. We have a our services use none or many repositories using the UOW class and a short lived context object.

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

like image 162
abarr Avatar answered Oct 18 '25 15:10

abarr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!