views:

200

answers:

3

Who has the responsability


Who has the responsibility to start and finish the Unit of work in a MVC architecture?

+3  A: 

The controller. This gets the context, so you can start and finish the unit of work. For example a nHibernate session per request would need you to know when the request had started and finished, so you need the context to give you the request.

Mark Dickinson
+1 Thanks Mark!Anyway I'm going to wait for more opinions about : )
SDReyes
That's cool, it's hard to tell what you want the answer to be, as there is more than one way of looking at unit of work. Some poeple have it as the whole work for a page rendering, others have transaction scopes which could be handled by other classes. Even for more granular cases, I think it is good to have the controller in charge of the resources which it can eventually give back (dispose or whatever). Maybe you could post more and I can try and give a better answer. Thanks :)
Mark Dickinson
Thanks for answer Mark : )Well, in a unit-of-work request oriented. I think you are right.I just believe that there could be something be have not considered. : )-i.e. I hadn't though in transaction oriented UoW : P-Thanks again
SDReyes
Update: see Zihotki statement.
SDReyes
+4  A: 

It's not a responsibility of a controller, it violates SRP. Controller should not even know about UoW at all. In web, one UoW per request to server is usually used. In this case UoW should be disposed at the end of a request and started somewhere after the beginning of a request (ideally start of a UoW should be lazy). The best place to do this is Global.asax (or your HttpApplication class) using Application_EndRequest and Application_BeginRequest handlers.
This can be easily achieved with an IOC framework (my favorite is Windsor), see this question for implementation details.

zihotki
+1 For being right! :P
Raúl Roa
+1 Thanks for your approach Zihotki.
SDReyes
@zihotki - in your design, what adds commands to the Unit of Work?
Jeff Sternal
@Jeff, all commands/services/repositories in the requests share (and belongs to) the same UoW. If the command/service/repository requires a direct reference to UoW (for example, a Session in NHibernate, or DataContext in EF) it should be injected by IOC. How exactly it should be injected (setter or constructor or by direct call to IOC) depends on your architecture.
zihotki
"The best place to do this is Global.asax (or your HttpApplication class) using Application_EndRequest and Application_BeginRequest handlers." or you could get a new httpmodule to hook into these events, and give you a nHibernateSession object per request. I agree explicitly controlling the UoW goes against SRP but ultimately the U is inside the context of the page. Nice answer :) +1
Mark Dickinson
+1  A: 

As zihotki said you would be violating the SRP if you give this responsibility to the controller. This is a data manipulation oriented pattern, and as such should not be a concern for the controller ... that would make it two violations: one for the SRP and anothrt for the SoC principle.

As for who has the responsibility, that's something to be defined by your architecture. The StartRequest/EndRequest suggestion seems solid enough.

JoseMarmolejos
Thanks Jose +1I agree with StartRequest/EndRequest too.
SDReyes