如何实现MVC中的工作单位:责任 [英] How to implement Unit of work in MVC: Responsibility

查看:120
本文介绍了如何实现MVC中的工作单位:责任的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁负责

谁有责任开始和完成单位在一个MVC架构中的工作?

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

推荐答案

控制器不负责,它违反了 SRP 。控制器根本不应该知道UoW。在网络中,通常使用每个服务器请求的一个UoW。在这种情况下,UoW应该在请求结束时处理,并在请求开始之后开始(理想情况下,UoW的开始应该是懒惰的)。最好的方法是使用Application_EndRequest和Application_BeginRequest处理程序来完成这个工作的是Global.asax(或者你的HttpApplication类)。

这可以很容易地用IOC框架来实现(我最喜欢的是Windsor),参见这个问题,了解实施细节。

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.

这篇关于如何实现MVC中的工作单位:责任的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆