在读取模型中投影聚合状态,读取端的额外逻辑与事件中的更多数据
Projecting aggregate state in Read Model, extra logic on read side vs more data in the event
比方说,我正在使用事件源并且我的聚合根(匹配项)从 "PlayerWonPoint" 和 "PlayerLostPoint" 事件构建其状态(它也有两个相应的命令)。如果简化,状态从 InProgress 变为 Final。读取端负责表示点历史和一些统计数据。例如,对于网球,分数将表示为
MatchStarted -> 0-0
PlayerWonPoint -> 15-0
PlayerLostPoint -> 15-15
PlayerWonPoint -> 30-15
PlayerWonPoint -> 40-15
PlayerWonPoint -> Player won the game
- 我是否告诉读取模型(使用事件数据)当前分数是多少
表示形式,因此读取模型会将其视为给定并仅附加
table?在这种情况下,读取端非常简单,但我们添加了一些
源事件中不需要的额外数据
聚合自身。
- 或者,我们可以让Read端去搞清楚分数的表示方式?它会
需要额外的逻辑,这与
聚合以跟踪其当前状态。
另一个问题:如果玩家赢得点数并且聚合看到它是 GamePoint、SetPoint 或 MatchPoint ,我是要为此记录不同的事件类型,还是只继续使用 PlayerLostPoint/PlayerLostPoint 事件?因为,聚合体可以从后两种事件类型中重新水合。但是,只有两种事件类型,读取模型变得更加复杂(即需要跟踪游戏、场景等)。我认为添加额外的偶数类型来简化 ReadModel 没有什么大不了的,潜在的额外事件类型也可能对聚合有用,例如,如果最后一个点事件是 "PlayerWonTheMatch"?
,它可以跳过处理所有点事件
Do I tell Read Model (using event data) what is current score
representation
我认为这是你应该走的路。否则,您应该在读取模型上构建另一个聚合。因为只有聚合知道您对其执行特定操作后的分数是多少。还要考虑事件可能会出现乱序,或者出现两次。做好准备。
if player wins the point and aggregate sees that it is a GamePoint,
SetPoint or MatchPoint, do I record different event type for that or I
just keep using only PlayerLostPoint/PlayerLostPoint events?
如果您的域需要,我认为您应该这样做。它提供了有关游戏的更多详细信息,但请确保它是必需的,不要对现实进行编程,否则您最终会遇到类似以下事件:ABallBoySuppliedTennisBallToPlayerEvent,这对您的域可能毫无用处。
比方说,我正在使用事件源并且我的聚合根(匹配项)从 "PlayerWonPoint" 和 "PlayerLostPoint" 事件构建其状态(它也有两个相应的命令)。如果简化,状态从 InProgress 变为 Final。读取端负责表示点历史和一些统计数据。例如,对于网球,分数将表示为
MatchStarted -> 0-0
PlayerWonPoint -> 15-0
PlayerLostPoint -> 15-15
PlayerWonPoint -> 30-15
PlayerWonPoint -> 40-15
PlayerWonPoint -> Player won the game
- 我是否告诉读取模型(使用事件数据)当前分数是多少 表示形式,因此读取模型会将其视为给定并仅附加 table?在这种情况下,读取端非常简单,但我们添加了一些 源事件中不需要的额外数据 聚合自身。
- 或者,我们可以让Read端去搞清楚分数的表示方式?它会 需要额外的逻辑,这与 聚合以跟踪其当前状态。
另一个问题:如果玩家赢得点数并且聚合看到它是 GamePoint、SetPoint 或 MatchPoint ,我是要为此记录不同的事件类型,还是只继续使用 PlayerLostPoint/PlayerLostPoint 事件?因为,聚合体可以从后两种事件类型中重新水合。但是,只有两种事件类型,读取模型变得更加复杂(即需要跟踪游戏、场景等)。我认为添加额外的偶数类型来简化 ReadModel 没有什么大不了的,潜在的额外事件类型也可能对聚合有用,例如,如果最后一个点事件是 "PlayerWonTheMatch"?
,它可以跳过处理所有点事件Do I tell Read Model (using event data) what is current score representation
我认为这是你应该走的路。否则,您应该在读取模型上构建另一个聚合。因为只有聚合知道您对其执行特定操作后的分数是多少。还要考虑事件可能会出现乱序,或者出现两次。做好准备。
if player wins the point and aggregate sees that it is a GamePoint, SetPoint or MatchPoint, do I record different event type for that or I just keep using only PlayerLostPoint/PlayerLostPoint events?
如果您的域需要,我认为您应该这样做。它提供了有关游戏的更多详细信息,但请确保它是必需的,不要对现实进行编程,否则您最终会遇到类似以下事件:ABallBoySuppliedTennisBallToPlayerEvent,这对您的域可能毫无用处。