4.6 应用层服务
Application Services 应用服务
An Application Service is a stateless service that implements use cases of the application. An application service typically gets and returns DTOs. It is used by the Presentation Layer. It uses and coordinates the domain objects (entities,repositories, etc.) to implement the use cases.
一个应用服务是一个无状态的服务,它实现了应用程序的用例。一个应用服务通常获取和返回DTO。它被运用于表现层。它使用并协调领域对象(实体、存储库等)来实现用例。
Common principles of an application service are;
应用服务的常见原则:
- Implement the application logic that is specific to the current use-case. Do not implement the core domain logic inside the application services. We will come back to differences between Application Domain logics.
- 实现特定于当前用例的应用逻辑。不要在应用服务中实现核心领域的逻辑。我们将回归到应用域逻辑之间的差异。
- Never get or return entities for an application service method. This breaks the encapsulation of the Domain Layer. Always get and return DTOs.
- 应用服务方法不要获取或返回实体。这将破坏领域层的封装。总是获取和返回DTO。
Example: Assigning an issue to a user 示例:将问题分配给用户
An application service method typically has three steps those are implemented here;
一个应用服务方法通常有三个步骤,具体实现步骤为:
- Get the related domain objects from database to implement the use case.
- 从数据库中获取相关的领域对象用于实现用例。
- Use domain objects (domain services, entities, etc.) to perform the actual operation.
- 使用领域对象(领域服务、实体等)来执行实际操作。
- Update the changed entities in the database.
- 更新数据库中已变更的实体。
The last Update is not necessary if your are using EF Core since it has a Change Tracking system. If you want to take advantage of this EF Core feature, please see the Discussion About the Database Independence Principle section above.
如果你使用的是EF Core,由于它的变更跟踪系统,你不需要做最后的Update。如果你想利用这个EF Core的功能,请看上面关于数据库独立原则的讨论部分。
IssueAssignDto
in this example is a simple DTO class:
本例中的IssueAssignDto
是一个简单的DTO类: