如何实现MVC中的工作单元:责任

时间:2021-09-09 13:09:19

Who has the responsability

谁有责任


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

谁有责任在MVC架构中启动和完成工作单元?

4 个解决方案

#1


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.

它不是控制器的责任,它违反了SRP。控制器根本不应该知道UoW。在Web中,通常使用每个服务器请求一个UoW。在这种情况下,UoW应该在请求结束时处理,并在请求开始后的某个地方启动(理想情况下,UoW的启动应该是惰性的)。执行此操作的最佳位置是使用Application_EndRequest和Application_BeginRequest处理程序的Global.asax(或您的HttpApplication类)。这可以通过IOC框架轻松实现(我最喜欢的是Windsor),请参阅此问题以获取实现细节。

#2


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.

控制器。这得到了上下文,因此您可以开始和完成工作单元。例如,每个请求的nHibernate会话需要您知道请求何时开始和结束,因此您需要上下文来为您提供请求。

#3


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.

我是松散耦合架构的信徒。我的控制器知道关于存储库,上下文或单元工作的事情。我已经创建了一个控制器调用的服务层(不确定这是正确的术语)。然后,此服务与存储库(dll)一起使用以保留所有数据。

#4


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.

正如zihotki所说,如果您将此责任交给控制器,您将违反SRP。这是一种面向数据操作的模式,因此不应该成为控制器的一个问题......这将导致两个违规:一个用于SRP,另一个用于SoC原则。

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

至于谁有责任,这是你的架构定义的东西。 StartRequest / EndRequest建议看起来足够扎实。

#1


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.

它不是控制器的责任,它违反了SRP。控制器根本不应该知道UoW。在Web中,通常使用每个服务器请求一个UoW。在这种情况下,UoW应该在请求结束时处理,并在请求开始后的某个地方启动(理想情况下,UoW的启动应该是惰性的)。执行此操作的最佳位置是使用Application_EndRequest和Application_BeginRequest处理程序的Global.asax(或您的HttpApplication类)。这可以通过IOC框架轻松实现(我最喜欢的是Windsor),请参阅此问题以获取实现细节。

#2


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.

控制器。这得到了上下文,因此您可以开始和完成工作单元。例如,每个请求的nHibernate会话需要您知道请求何时开始和结束,因此您需要上下文来为您提供请求。

#3


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.

我是松散耦合架构的信徒。我的控制器知道关于存储库,上下文或单元工作的事情。我已经创建了一个控制器调用的服务层(不确定这是正确的术语)。然后,此服务与存储库(dll)一起使用以保留所有数据。

#4


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.

正如zihotki所说,如果您将此责任交给控制器,您将违反SRP。这是一种面向数据操作的模式,因此不应该成为控制器的一个问题......这将导致两个违规:一个用于SRP,另一个用于SoC原则。

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

至于谁有责任,这是你的架构定义的东西。 StartRequest / EndRequest建议看起来足够扎实。