7

Who has the responsability


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

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@zihotki - in your design, what adds commands to the Unit of Work?
@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.
"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
How about unit tests ? How would you unit test the unit of work this way ?
@Rushino, I'll test the commands and services and I'll create an UoW for each test in "before test" event and close it in "after test" event. But the exact implementation will depend on the command's or service's implementation
5

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.

4 Comments

+1 Thanks Mark! Anyway I'm going to wait for more opinions about : )
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 :)
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
Update: see Zihotki statement.
3

I am a believer in loosely coupled architecture. My controller knows NOTHING about the repository, context or unitofwork. I have created a service layer (not sure that is the right term) that the controller calls. This service then works with the repository (dll) to persist all data.

Comments

2

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.