实现订单微服务

This commit is contained in:
hello 2024-08-30 15:05:22 +08:00
parent 3101ff4e97
commit d85b5b6539
2 changed files with 10 additions and 5 deletions

View File

View File

@ -5,19 +5,17 @@ CQRS 是命令和查询责任分离的英文缩写,它是一种将读取操作
## 为什么要使用 CQRS 模式 ## 为什么要使用 CQRS 模式
在传统的体系结构中,使用同一数据模型查询和更新数据库。 这十分简单,非常适用于基本的 CRUD 操作。
![crud](https://oss.xcode.me/notes/helloshop/crud.png) 在传统的体系结构中,使用同一数据模型查询和更新数据库。 这十分简单,非常适用于基本的 CRUD 操作。
CQRS 将读取和写入分离到不同的模型,使用命令来更新数据,使用查询来读取数据。读取存储可以是写入存储的只读副本,或者读取和写入存储可以具有完全不同的结构。 使用多个只读副本可以提高查询性能,尤其是在只读副本靠近应用程序实例的分布式方案中。 CQRS 将读取和写入分离到不同的模型,使用命令来更新数据,使用查询来读取数据。读取存储可以是写入存储的只读副本,或者读取和写入存储可以具有完全不同的结构。 使用多个只读副本可以提高查询性能,尤其是在只读副本靠近应用程序实例的分布式方案中。
![cqrs](https://oss.xcode.me/notes/helloshop/cqrs.png)
## 使用 MediatR 实现 CQRS 中的 Command 模式 ## 使用 MediatR 实现 CQRS 中的 Command 模式
![ordering-service-flow](https://oss.xcode.me/notes/helloshop/ordering-service-flow.svg)
```shell ```shell
dotnet add package MediatR dotnet add package MediatR
``` ```
@ -72,6 +70,8 @@ builder.Services.AddMediatR(options => options.RegisterServicesFromAssembly(Asse
## 使用 Mediator 请求管道处理 CQRS 中的命令 ## 使用 Mediator 请求管道处理 CQRS 中的命令
![ordering-service-identified-command](https://oss.xcode.me/notes/helloshop/ordering-service-identified-command.svg)
`LoggingBehavior`、`ValidatorBehavior` 和 `TransactionBehavior` `LoggingBehavior`、`ValidatorBehavior` 和 `TransactionBehavior`
```csharp ```csharp
@ -129,6 +129,11 @@ services.AddDaprClient();
builder.Services.AddTransient<IDistributedEventLogService, DistributedEventLogService<OrderingServiceDbContext>>(); builder.Services.AddTransient<IDistributedEventLogService, DistributedEventLogService<OrderingServiceDbContext>>();
``` ```
## 通用类型映射和验证
![ordering-service-validator-mapper](https://oss.xcode.me/notes/helloshop/ordering-service-validator-mapper.svg)
## 参考资料 ## 参考资料