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 示例:將問題分配給用戶

image

An application service method typically has three steps those are implemented here;
一個應用服務方法通常有三個步驟,具體實現步驟為:

  1. Get the related domain objects from database to implement the use case.
  2. 從資料庫中獲取相關的領域對象用於實現用例。
  3. Use domain objects (domain services, entities, etc.) to perform the actual operation.
  4. 使用領域對象(領域服務、實體等)來執行實際操作。
  5. Update the changed entities in the database.
  6. 更新資料庫中已變更的實體。

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類:

image

Tags: