-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from netcorepal/dev
add domain docs
- Loading branch information
Showing
5 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# 领域事件处理 | ||
|
||
领域事件处理器是一个因`特定目的`而处理`领域事件`的处理逻辑,一个领域事件处理器应该仅针对一个目的来处理领域事件,针对同一`领域事件`的不同目的应该有不同的领域事件处理器。 | ||
|
||
## 定义领域事件处理器 | ||
|
||
|
||
1. 安装nuget包 `NetCorePal.Extensions.Domain.Abstractions` | ||
|
||
```bash | ||
dotnet add package NetCorePal.Extensions.Domain.Abstractions | ||
``` | ||
|
||
2. 领域事件处理器是一个实现了`IDomainEventHandler<TDomainEvent>`接口的类,其中`TDomainEvent`是领域事件的类型。 | ||
|
||
下面是一个领域事件处理器的例子: | ||
|
||
```csharp | ||
public class OrderCreatedDomainEventHandler(IMediator mediator) : IDomainEventHandler<OrderCreatedDomainEvent> | ||
{ | ||
public Task Handle(OrderCreatedDomainEvent notification, CancellationToken cancellationToken) | ||
{ | ||
return mediator.Send(new DeliverGoodsCommand(notification.Order.Id), cancellationToken); | ||
} | ||
} | ||
``` | ||
|
||
## 领域事件处理器必须 | ||
|
||
- 领域事件处理器必须是幂等的,即多次处理同一领域事件,结果应该是一致的。 | ||
- 领域事件处理器必须是无状态的,即不应该有任何状态,所有的状态应该通过领域事件传递。 | ||
|
||
**备注: 在我们的框架中,领域事件处理器是同步执行的,并且其调用的Command与触发领域事件的CommandHandler处于同一事务中** | ||
|
||
## 领域事件处理器可以 | ||
|
||
- 领域事件处理器中可以使用`MediatR`框架来发送命令; | ||
- 领域事件处理器中可以做一些简单的数据转换和信息查询; | ||
- 领域事件处理器中可以调用外部服务来完成一些信息组织和验证; | ||
- 领域事件处理器中可以发出集成事件来将事件传递给其它系统; | ||
|
||
## 领域事件处理器不要 | ||
|
||
- 领域事件处理器中不要包含领域模型的操作,应该由CommandHandler操作领域模型并持久化; | ||
|
||
**备注: 由于我们框架仅对`CommandHandler`做了事务管理,对于领域事件处理器中的操作框架不会做`SaveChangesAsync`,从而导致领域事件处理器中的操作不会被保存到数据库中** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# 领域事件 | ||
|
||
领域事件是领域模型中的一个重要概念,它是领域模型中的一种通信机制,用于在领域模型之间传递消息。 | ||
|
||
领域事件仅包含描述事件发生时领域模型中的数据,不包含任何业务逻辑,业务规则和业务流程。 | ||
|
||
## 定义领域事件 | ||
|
||
1. 安装nuget包 `NetCorePal.Extensions.Domain.Abstractions` | ||
|
||
```bash | ||
dotnet add package NetCorePal.Extensions.Domain.Abstractions | ||
``` | ||
|
||
2. 定义领域事件,需要: | ||
|
||
+ 继承`NetCorePal.Extensions.Domain.IDomainEvent`接口; | ||
+ 为领域事件定义一个空的构造函数,以支持序列化和反序列化; | ||
+ 为领域事件定义一个公共的构造函数,用于初始化领域事件的属性; | ||
+ 为领域事件定义一个公共的属性,用于描述事件发生时领域模型中的数据; | ||
|
||
下面为一个示例: | ||
|
||
```csharp | ||
// 定义领域事件 | ||
using NetCorePal.Extensions.Domain; | ||
namespace YourNamespace; | ||
public record UserCreatedDomainEvent(User user) : IDomainEvent; | ||
``` | ||
|
||
## 领域事件必须 | ||
|
||
- 领域事件必须由领域模型发出; | ||
- 领域事件必须是不可变的; | ||
|
||
## 领域事件可以 | ||
|
||
- 使用`record`关键字定义领域事件; | ||
|
||
## 领域事件不要 | ||
|
||
- 不要包含业务逻辑 | ||
- 不要包含业务规则 | ||
- 不要包含业务流程 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# 值类型 | ||
|
||
## 什么是值类型 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 集成事件 | ||
|
||
|
||
|
||
## 集成事件必须 | ||
|
||
- 集成事件必须使用`DTO`对象,因为集成事件会在需要序列化和反序列化并在分布式系统中传递; | ||
|
||
## 集成事件不要 | ||
|
||
- 集成事件不要使用领域模型 |