关于使用 DomainService

About using DomainService

  1. 我不能完全理解在什么情况下我会使用 DomainService 和 在这种情况下你的文件不详细 在这个问题上足够了。你能给出更详细的解释吗 举个例子?
  2. 如果我只使用 ApplicationService 而从不使用,可能会出现什么问题 使用域服务。
  3. 在DomainService中应该做哪些操作?
  4. 我应该按照什么样的方法来区分这两个 互相服务。

简短回答:只有在需要额外的抽象级别时才使用领域服务。

详细回答如下。

1。我什么时候使用域服务?

来自文档 Domain Services #Introduction:

Creating a Domain Service is especially needed when;

  • You implement a core domain logic that depends on some services (like repositories or other external services).
  • The logic you need to implement is related to more than one aggregate/entity, so it doesn't properly fit in any of the aggregates.

文档中的示例 Domain Services #Introduction:将问题分配给用户时,检查分配的问题数量是否不超过或等于特定数量。

教程中的示例 Part 6: Authors: Domain layer:更改作者姓名时,请检查是否没有使用给定姓名的作者。

2。如果我不使用域服务会有什么问题?

您可能会错过作为核心域逻辑或业务规则的检查。

但一般来说,没问题。来自教程 Part 6: Authors: Domain layer:

DDD tip: Do not introduce domain service methods unless they are really needed and perform some core business rules.

3。我应该在域服务中进行哪些操作?

参见#1 和#4 中的示例。

4。如何区分领域服务和应用服务?

来自文档 Domain Services #Application Services vs Domain Services:

While both of Application Services and Domain Services implement the business rules, there are fundamental logical and formal differences;
  • Application Services implement the use cases of the application (user interactions in a typical web application), while Domain Services implement the core, use case independent domain logic.
  • Application Services get/return Data Transfer Objects, Domain Service methods typically get and return the domain objects (entities, value objects).
  • Domain services are typically used by the Application Services or other Domain Services, while Application Services are used by the Presentation Layer or Client Applications.

来自教程Part 8: Author: Application Layer

  • Used the AuthorManager (domain service) to create a new author.
  • Used the IAuthorRepository.InsertAsync to insert the new author to the database.
  • Used the ObjectMapper to return an AuthorDto representing the newly created author.

DDD tip: Some developers may find useful to insert the new entity inside the _authorManager.CreateAsync. We think it is a better design to leave it to the application layer since it better knows when to insert it to the database (maybe it requires additional works on the entity before insert, which would require to an additional update if we perform the insert in the domain service). However, it is completely up to you.

@aaron'a 的回答很棒。我只想指出另一个讨论“域服务与应用程序服务”的文档:https://docs.abp.io/en/abp/latest/Domain-Driven-Design-Implementation-Guide#domain-logic-application-logic

如果您想使用 ABP 框架实现 DDD,我建议您完整阅读本指南。如果您仍然觉得文档不够详细,可以阅读 Eric Evans 的领域驱动设计一书。 DDD 不是 ABP 框架创建的概念,网络上有大量资源和书籍。