使用 DDD 和 CQRS 模式处理微服务中的业务复杂性

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. 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.

本节重点介绍当你需要处理复杂的子系统,或者业务规则多变、需要依赖领域专家知识的微服务时,所要实现的更高级的微服务。本节使用的架构模式基于领域驱动设计(DDD)和命令查询职责分离(CQRS)方法,如图 7-1 所示。

Diagram comparing external and internal architecture patterns.

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.

架构模式对比图:外部架构(微服务模式、API 网关、弹性通信、发布/订阅等)与 内部架构(数据驱动/CRUD、DDD 模式、依赖注入、多类库等)的区别。

Figure 7-1. External microservice architecture versus internal architecture patterns for each microservice

图 7-1 微服务外部架构与每个微服务的内部架构模式对比

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.

然而,大多数关于数据驱动型微服务的技术(例如如何实现 ASP.NET Core Web API 服务,或如何使用 Swashbuckle 或 NSwag 暴露 Swagger 元数据)也同样适用于内部采用 DDD 模式的高级微服务。本节是前几节内容的延伸,因为之前解释的大多数实践方法在这里也同样适用,或适用于任何类型的微服务。

This section first provides details on the simplified CQRS patterns used in the eShopOnContainers reference application. Later, you will get an overview of the DDD techniques that enable you to find common patterns that you can reuse in your applications.

本节首先详细介绍了在 eShopOnContainers 参考应用程序中使用的简化版 CQRS 模式。随后,你将概览一系列 DDD 技术,帮助你发现可以在应用程序中复用的通用模式。

DDD is a large topic with a rich set of resources for learning. 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. 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.

DDD 是一个非常宏大的主题,拥有丰富的学习资源。你可以从 Eric Evans 所著的《领域驱动设计》(Domain-Driven Design)一书开始,以及 Vaughn Vernon、Jimmy Nilsson、Greg Young、Udi Dahan、Jimmy Bogard 等众多 DDD/CQRS 专家的补充材料入手。但最重要的是,你需要尝试通过与具体业务领域的专家进行对话、头脑风暴(Whiteboarding)以及领域建模会议,来学习如何应用 DDD 技术。

posted @ 2026-05-17 17:02  菜鸟吊思  阅读(11)  评论(0)    收藏  举报