在Asp.net mvc中应该有服务层吗?

时间:2020-12-13 16:34:26

Should there be a Service Layer in Asp.net MVC between Controller and Repository? As Repository is there for only Data Access. Some business logic is leaked into Controller. This might create a problem if the same operation is used by classic Asp.Net client as we have to duplicate the logic in Controller.

控制器和存储库之间是否应该在Asp.net MVC中有服务层?由于存储库仅用于数据访问。一些业务逻辑泄漏到Controller中。如果经典Asp.Net客户端使用相同的操作,这可能会产生问题,因为我们必须在Controller中复制逻辑。

6 个解决方案

#1


2  

I highly recommend using a service layer so you can share common functionality between different web applications. It maybe a case that the architecture is already in existence and you may want to add a new mvc website on the front end.

我强烈建议您使用服务层,以便在不同的Web应用程序之间共享通用功能。这可能是架构已经存在的情况,您可能希望在前端添加新的mvc网站。

For simple architectures built from scratch it maybe overkill.

对于从头开始构建的简单架构,它可能有点过分。

#2


10  

If you follow Domain Driven Design to the letter, you'll find that there are 3 types of service (don't you love overloaded terms?).

如果您按照字母驱动设计进行操作,您会发现有3种类型的服务(您不喜欢重载条款吗?)。

  • Domain Services : Encapsulates business logic that doesn't naturally fit within a domain object, and are NOT typical CRUD operations - those would belong to a Repository.
  • 域服务:封装不自然地适合域对象的业务逻辑,并且不是典型的CRUD操作 - 那些属于存储库。
  • Application Services : Used by external consumers to talk to your system (think Web Services). If consumers need access to CRUD operations, they would be exposed here (but handled by the appropriate Repository)
  • 应用程序服务:由外部使用者用于与您的系统通信(想想Web服务)。如果消费者需要访问CRUD操作,它们将在此处公开(但由适当的存储库处理)
  • Infrastructure Services : Used to abstract technical concerns (e.g. MSMQ, email provider, etc)
  • 基础设施服务:用于抽象技术问题(例如MSMQ,电子邮件提供商等)

Sounds like you need Domain Services to encapsulate/share your business logic.

听起来您需要域服务来封装/共享您的业务逻辑。

Hope that helps!

希望有所帮助!

#3


4  

TBH, I've gone both ways on this. My current perspective is that a Repository is a service, it's just a service which happens to have the job of handling CRUD ops for a domain aggregate root. now, if you're "repositories" are directly mapped 1:1 with your entities (and therefore, not repositories so much as DAOs') then I could see the argument. But generally, I add layers of abstraction as needed, but not until proven that they're needed for a given app. Otherwise, you over-engineer.

TBH,我已经双管齐下了。我目前的观点是,Repository是一个服务,它只是一个服务,碰巧有一个域聚合根处理CRUD操作的工作。现在,如果您的“存储库”与您的实体直接映射1:1(因此,不是DAO的存储库),那么我可以看到参数。但一般来说,我会根据需要添加抽象层,但直到证明它们是给定应用程序所需的。否则,你过度工程。

#4


3  

I like the extra level of abstraction a service layer provides between my controllers and repositories. I feel it helps tremendously in fulfilling the Single Responsibility Principle.

我喜欢服务层在我的控制器和存储库之间提供的额外抽象级别。我觉得在履行单一责任原则方面有很大帮助。

#5


3  

I experimented with this a little bit. The app I was building was pretty CRUD-y, and my service layer ended up exposing nearly the same interfaces as my repositories. In addition, most of the service calls didn't add any extra logic on top of the repo, they were just pass-through methods. The only place where the service layer did anything was during an update or insert of a new record. I decided that in a CRUD-based system, the service layer caused more friction than it added value. I ended up extending my business model classes so that the service logic was exposed as operations against the model, thus keeping my controller methods lean and clean.

我对此进行了一些实验。我正在构建的应用程序非常CRUD-y,我的服务层最终暴露出与我的存储库几乎相同的接口。此外,大多数服务调用都没有在repo之上添加任何额外的逻辑,它们只是传递方法。服务层执行任何操作的唯一位置是在更新或插入新记录期间。我决定在基于CRUD的系统中,服务层引起的摩擦大于增值。我最终扩展了我的业务模型类,以便将服务逻辑暴露为对模型的操作,从而使我的控制器方法保持精简和清洁。

In a more behavior-driven system, however, I think a service layer might add more value.

然而,在一个更加行为驱动的系统中,我认为服务层可能会增加更多价值。

#6


1  

I like to have repositories directly in the controller in most cases, and make a service where I feel it leaks. I try to have rich domain objects, but in those cases where logic doesn't fit in the domain classes, I create a service.

我喜欢在大多数情况下直接在控制器中拥有存储库,并在我觉得泄漏的地方进行服务。我尝试拥有丰富的域对象,但在那些逻辑不适合域类的情况下,我创建了一个服务。

#1


2  

I highly recommend using a service layer so you can share common functionality between different web applications. It maybe a case that the architecture is already in existence and you may want to add a new mvc website on the front end.

我强烈建议您使用服务层,以便在不同的Web应用程序之间共享通用功能。这可能是架构已经存在的情况,您可能希望在前端添加新的mvc网站。

For simple architectures built from scratch it maybe overkill.

对于从头开始构建的简单架构,它可能有点过分。

#2


10  

If you follow Domain Driven Design to the letter, you'll find that there are 3 types of service (don't you love overloaded terms?).

如果您按照字母驱动设计进行操作,您会发现有3种类型的服务(您不喜欢重载条款吗?)。

  • Domain Services : Encapsulates business logic that doesn't naturally fit within a domain object, and are NOT typical CRUD operations - those would belong to a Repository.
  • 域服务:封装不自然地适合域对象的业务逻辑,并且不是典型的CRUD操作 - 那些属于存储库。
  • Application Services : Used by external consumers to talk to your system (think Web Services). If consumers need access to CRUD operations, they would be exposed here (but handled by the appropriate Repository)
  • 应用程序服务:由外部使用者用于与您的系统通信(想想Web服务)。如果消费者需要访问CRUD操作,它们将在此处公开(但由适当的存储库处理)
  • Infrastructure Services : Used to abstract technical concerns (e.g. MSMQ, email provider, etc)
  • 基础设施服务:用于抽象技术问题(例如MSMQ,电子邮件提供商等)

Sounds like you need Domain Services to encapsulate/share your business logic.

听起来您需要域服务来封装/共享您的业务逻辑。

Hope that helps!

希望有所帮助!

#3


4  

TBH, I've gone both ways on this. My current perspective is that a Repository is a service, it's just a service which happens to have the job of handling CRUD ops for a domain aggregate root. now, if you're "repositories" are directly mapped 1:1 with your entities (and therefore, not repositories so much as DAOs') then I could see the argument. But generally, I add layers of abstraction as needed, but not until proven that they're needed for a given app. Otherwise, you over-engineer.

TBH,我已经双管齐下了。我目前的观点是,Repository是一个服务,它只是一个服务,碰巧有一个域聚合根处理CRUD操作的工作。现在,如果您的“存储库”与您的实体直接映射1:1(因此,不是DAO的存储库),那么我可以看到参数。但一般来说,我会根据需要添加抽象层,但直到证明它们是给定应用程序所需的。否则,你过度工程。

#4


3  

I like the extra level of abstraction a service layer provides between my controllers and repositories. I feel it helps tremendously in fulfilling the Single Responsibility Principle.

我喜欢服务层在我的控制器和存储库之间提供的额外抽象级别。我觉得在履行单一责任原则方面有很大帮助。

#5


3  

I experimented with this a little bit. The app I was building was pretty CRUD-y, and my service layer ended up exposing nearly the same interfaces as my repositories. In addition, most of the service calls didn't add any extra logic on top of the repo, they were just pass-through methods. The only place where the service layer did anything was during an update or insert of a new record. I decided that in a CRUD-based system, the service layer caused more friction than it added value. I ended up extending my business model classes so that the service logic was exposed as operations against the model, thus keeping my controller methods lean and clean.

我对此进行了一些实验。我正在构建的应用程序非常CRUD-y,我的服务层最终暴露出与我的存储库几乎相同的接口。此外,大多数服务调用都没有在repo之上添加任何额外的逻辑,它们只是传递方法。服务层执行任何操作的唯一位置是在更新或插入新记录期间。我决定在基于CRUD的系统中,服务层引起的摩擦大于增值。我最终扩展了我的业务模型类,以便将服务逻辑暴露为对模型的操作,从而使我的控制器方法保持精简和清洁。

In a more behavior-driven system, however, I think a service layer might add more value.

然而,在一个更加行为驱动的系统中,我认为服务层可能会增加更多价值。

#6


1  

I like to have repositories directly in the controller in most cases, and make a service where I feel it leaks. I try to have rich domain objects, but in those cases where logic doesn't fit in the domain classes, I create a service.

我喜欢在大多数情况下直接在控制器中拥有存储库,并在我觉得泄漏的地方进行服务。我尝试拥有丰富的域对象,但在那些逻辑不适合域类的情况下,我创建了一个服务。