使用 DDD 與 CQRS 模式解決微服務中的商務複雜度Tackle Business Complexity in a Microservice with DDD and CQRS Patterns

設計每個微服務的領域模型,或反映對業務領域了解程度的繫結內容。**Design a domain model for each microservice or Bounded Context that reflects understanding of the business domain.

本節著重於當您需要解決複雜的子系統時所實作的更進階微服務,或是衍生自領域專家對不斷改變之商務規則知識的微服務。This section focuses on more advanced microservices that you implement when you need to tackle complex subsystems, or microservices derived from the knowledge of domain experts with ever-changing business rules. 此節中所使用的架構模式是以領域驅動設計 (Domain-Driven Design,DDD) 以及命令和查詢職責分離 (Command and Query Responsibility Segregation,CQRS) 方法為基礎,如圖 7-1 所示。The architecture patterns used in this section are based on domain-driven design (DDD) and Command and Query Responsibility Segregation (CQRS) approaches, as illustrated in Figure 7-1.

比較外部和內部架構模式的圖表。

外部架構的差異:微服務模式、API 閘道、彈性通訊、pub/sub 等:內部架構:資料 動/CRUD、DDD 模式、相依性插入、多程式庫等。Difference between external architecture: microservice patterns, API gateways, resilient communications, pub/sub, etc., and internal architecture: data driven/CRUD, DDD patterns, dependency injection, multiple libraries, etc.

圖 7-1.Figure 7-1. 外部微服務架構與每個微服務的內部架構模式External microservice architecture versus internal architecture patterns for each microservice

不過,資料驅動微服務的大多數技術,像是如何實作 ASP.NET Core Web API 服務,或如何使用 Swashbuckle 或 NSwag 公開 Swagger 中繼資料,也適用於以 DDD 模式在內部實作之更進階的微服務。However, most of the techniques for data driven microservices, such as how to implement an ASP.NET Core Web API service or how to expose Swagger metadata with Swashbuckle or NSwag, are also applicable to the more advanced microservices implemented internally with DDD patterns. 本節是前面幾節的延伸,因為稍早所述的大部分實務也適用於這裡或任何類型的微服務。This section is an extension of the previous sections, because most of the practices explained earlier also apply here or for any kind of microservice.

本節會先提供 eShopOnContainers 參考應用程式中所使用之簡化 CQRS 模式的詳細資料。This section first provides details on the simplified CQRS patterns used in the eShopOnContainers reference application. 稍後會對您概要說明 DDD 技術,這些技術可讓您尋找常見的模式,以便在應用程式中重複使用。Later, you will get an overview of the DDD techniques that enable you to find common patterns that you can reuse in your applications.

DDD 是一個龐大的主題,內含一組豐富的學習資源。DDD is a large topic with a rich set of resources for learning. 您可以從 Eric Evans 所著 Domain-Driven Design 之類的書籍,以及 Vaughn Vernon、Jimmy Nilsson、Greg Young、Udi Dahan、Jimmy Bogard 和許多其他 DDD/CQRS 專家所提供的其他教材開始著手。You can start with books like Domain-Driven Design by Eric Evans and additional materials from Vaughn Vernon, Jimmy Nilsson, Greg Young, Udi Dahan, Jimmy Bogard, and many other DDD/CQRS experts. 但最重要的是,您需要試著了解如何在專家協助下,將來自交談、白板與領域模型課程的 DDD 技術,應用在您的實體業務領域中。But most of all you need to try to learn how to apply DDD techniques from the conversations, whiteboarding, and domain modeling sessions with the experts in your concrete business domain.

其他資源Additional resources

DDD (領域驅動設計)DDD (Domain-Driven Design)
DDD 書籍DDD books
DDD 訓練課程DDD training
  • Julie Lerman 和 Steve Smith。領域驅動設計基本概念 Julie Lerman and Steve Smith. Domain-Driven Design Fundamentals
    https://bit.ly/PS-DDD