CQRS:我应该继续追加事件吗?

CQRS: Should I keep appending events?

假设我的系统中有一个设置用户电子邮件的操作,如果我从 CQRS 和事件源的角度多次设置 相同的 电子邮件会发生什么?

只有一次:

CustomerContactInformationSetEvent

或者,继续追加:

CustomerContactInformationSetEvent

CustomerContactInformationSetEvent

CustomerContactInformationSetEvent

CustomerContactInformationSetEvent

Let's say I have an operation in my system that will set a users email, what shall happen if I set the same email multiple times from an CQRS and event sourcing perspective?

在大多数情况下,您希望域模型是 Idempotent Receiver;同一条消息的多个副本应该没有明显的影响。

就您将在事件历史记录中看到的内容而言,您可能会看到以下任何一种方法:

日志中的单个事件,表示消息被设置为新值的时间点,并且当消息的副本首次出现时没有进一步的条目。毕竟模型没有改变,所以我们不需要创建历史变化的表示。

日志中的多个事件:捕获冗余事件在概念上没有错 - 下一版本的模型实际上可能不会将这些事件视为冗余,因此明确捕获它们可能会在以后产生额外的商业价值。

多个日志:一个日志记录所有传入消息(即预写日志),另一个日志包含描述状态更改的单个事件。

第一种方法可能是最常见的。当事件可能无序到达时,记录许多事件可能很重要。

ChangeEmail: bob@example.org
ChangeEmail: alice@example.org
ChangeEmail: bob@example.org

要知道正确的电子邮件地址,您可能需要知道这些更改的顺序,不一定它们到达的顺序。

比较

ChangeEmail: bob@example.org    time:1200
ChangeEmail: alice@example.org  time:1300
ChangeEmail: bob@example.org    time:1200

ChangeEmail: bob@example.org    time:1200
ChangeEmail: alice@example.org  time:1300
ChangeEmail: bob@example.org    time:1400

ChangeEmail: bob@example.org    time:1200
ChangeEmail: bob@example.org    time:1400
ChangeEmail: alice@example.org  time:1300

操作发出 SetCustomerContactInformation 命令。

在大多数情况下,如果新信息已经匹配,则不会记录新的CustomerContactInformationSetEvent事件。

如果上述安排丢失了您域中的重要信息,请根据需要进行调整。我假设一个事件源设置,其中事件顺序是已知的。