事件溯源命令与事件

发布于 2024-11-23 19:47:02 字数 157 浏览 0 评论 0原文

我理解命令和事件之间的区别,但在很多情况下,您最终会在本质上相同的两个类(ThingNameUpdateCommand、ThingNameUpdatedEvent)之间出现冗余和映射。对于这些简单的情况,您是否也可以使用事件作为命令?人们是否将所有命令和所有事件序列化到存储中?对我来说似乎有点多余。

I understand the difference between commands and events but in a lot of cases you end up with redundancy and mapping between 2 classes that are essentially the same (ThingNameUpdateCommand, ThingNameUpdatedEvent). For these simple cases can you / do you use the event also as a command? Do people serialise to a store all commands as well as all events? Just seems to be a little redundant to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

注定孤独终老 2024-11-30 19:47:02

所有这些冗余通常都是有原因的,并且出于多种原因,您希望避免将相同的消息用于两个不同的目的:

  1. 源事件在发生更改时必须进行版本控制,因为它们在以下情况下被存储和重新使用(反序列化):你给聚合根加水。如果类也被用作消息,那么事情就会变得有点尴尬。
  2. 耦合性增加,命令处理程序、域模型和事件处理程序现在使用相同的类。将命令端与事件分离可以简化您以后的生活。
  3. 终于清晰了。命令以要求执行某些操作的语言发出(通常是命令式的)。事件是已经发生的事情的表示(一般是过去时态)。如果您对两者使用相同的类,这种语言就会变得混乱。

最后,这些只是数据类,这并不是“硬”代码。有一些方法可以实际避免一些简单场景(例如代码生成)的输入。例如,我知道 Greg 过去曾使用 XML 和 XSD 转换来创建给定域所需的所有类。

我想说,对于很多简单的情况,您可能想质疑这是否真的是领域(即建模行为)还是只是数据。如果只是数据,请考虑不要在此处使用事件源。下面是 Udi Dahan 的演讲链接,内容涉及打破域模型,以便并非所有模型都需要事件溯源。我自己现在也有点认同这种想法。

http://skillsmatter.com/podcast/design-architecture/talk-来自-udi-dahan

All lot of this redundancy is for a reason in general and you want to avoid using the same message for two different purposes for a number of reasons:

  1. Sourced events must be versioned when they change since they are stored and re-used (deserialized) when you hydrate an aggregate root. It will make things a bit awkward if the class is also being used as a message.
  2. Coupling is increased, the same class is now being used by command handlers, the domain model and event handlers now. De-coupling the command side from the event can simplify life for you down the road.
  3. Finally clarity. Commands are issued in a language that asks something to be done (imperative generally). Events are representations of what has happened (past-tense generally). This language gets muddled if you use the same class for both.

In the end these are just data classes, it isn't like this is "hard" code. There are ways to actually avoid some of the typing for simple scenarios like code-gen. For example, I know Greg has used XML and XSD transforms to create all the classes needed for a given domain in the past.

I'd say for a lot of simple cases you may want to question if this is really domain (i.e. modeling behavior) or just data. If it is just data consider not using event sourcing here. Below is a link to a talk by Udi Dahan about breaking up your domain model so that not all of it requires event-sourcing. I'm kind of in line with this way of thinking now myself.

http://skillsmatter.com/podcast/design-architecture/talk-from-udi-dahan

帅冕 2024-11-30 19:47:02

在完成一些示例,特别是 Greg Young 演示文稿 (http://www.youtube.com/watch ?v=JHGkaShoyNs) 我得出的结论是命令是多余的。它们只是来自您的用户的事件,他们确实按下了该按钮。您应该以与其他事件完全相同的方式存储这些数据,因为您不知道这些数据是否要在将来的视图中使用。您的用户确实添加了该商品,然后又从购物篮中删除了该商品,或者至少尝试这样做。您稍后可能希望使用此信息来提醒用户这一点。

After working through some examples and especially the Greg Young presentation (http://www.youtube.com/watch?v=JHGkaShoyNs) I've come to the conclusion that commands are redundant. They are simply events from your user, they did press that button. You should store these in exactly the same way as other events because it is data you don't know if you will want to use it in a future view. Your user did add and then later remove that item from the basket or at least attempt to. You may later want to use this information to remind the user of this at later date.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文