Asp.net mvc service level and unit of work

Possible duplicate:
How to implement the Unit of work in MVC: Responsibility

Hey. I am developing an ASP.NET MVC web application using the following architecture: UI -> Controller -> Service Layer -> Repository. This raises the question of where the "Unit of Work" template can be exposed. For example, 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 good for the controller to know the amount of work? What if we have several calls for different services, but within the same method of action, and we need transactional behavior?

+3
source share
1 answer

, , UOW . :

  • , , , , -,
  • - ( ) . , , UOW .

public ActionResult List()
{
    var things = ThingService.GetAll();            
    return View(things);
}

public IEnumerable<Thing> GetAll()
{
    using (ObjectContext context = new Container(ConnectionString))
    {
        var work = new UnitOfWork(context);
        return work.Things.GetAll());
    }
}

,

+4

Source: https://habr.com/ru/post/1773743/


All Articles