如何在 MVC 中实现工作单元:职责 [英] How to implement Unit of work in MVC: Responsibility

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

问题描述

谁有责任

谁有责任开始和完成 MVC 架构中的工作单元?

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

推荐答案

这不是控制器的责任,它违反了SRP.控制器甚至根本不应该知道 UoW.在 Web 中,通常对服务器的每个请求使用一个 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天全站免登陆