11

Who should be responsible for handling domain events? Application services, domain services or entities itself?

Let's use simple example for this question.

Let's say we work on shop application, and we have an application service dedicated to order operations. In this application Order is an aggregate root and following rules, we can work only with one aggregate within single transaction. After Order is placed, it is persisted in a database. But there is more to be done. First of all, we need to change number of items available in the inventory and secondly notify some other part of a system (probably another bounded context) that shipping procedure for that particular order should be started. Because, as already stated, we can modify only one aggregate within transaction, I think about publishing OrderPlacedEvent that will be handled by some components in the separate transactions.

Question arise: which components should handle this type of event?

4

3 回答 3

10

我想:

1) 应用层,如果事件触发在同一有界上下文中修改另一个聚合。

2) 应用层,如果事件触发了一些基础设施服务。

例如,一封电子邮件被发送给客户。所以需要一个应用服务来加载邮件内容和邮件的顺序,然后调用基础设施服务来发送邮件。

3)如果事件在另一个有界上下文中触发某些操作,我个人更喜欢域服务。

例如运输或计费,领域服务的基础设施实现负责整合其他有界上下文。

4) 基础设施层,如果需要将事件拆分给多个消费者。消费者转到 1),2) 或 3)。

对我来说,如果事件导致有界上下文的单独验收测试,则结论是应用层。

顺便问一下,确保活动持久性的基础设施是什么?您是否在事务中包含事件发布?

于 2013-08-07T04:01:47.290 回答
4

这些处理程序属于应用层。您可能也应该创建一个支持应用程序服务的方法。这样您就可以开始单独的交易。

于 2013-08-06T20:05:54.053 回答
3

我认为放置 EventHandlers 的最常见和最常见的位置是在application layer. 与 CQRS 进行类比,EventHandlers 与 CommandHandlers 非常相似,我通常将它们彼此靠近(在应用程序层中)。

Microsoft 的这篇文章还提供了一些将处理程序放在那里的示例。看下面的图片,取自相关文章:

聚合和处理程序

于 2018-07-06T16:41:43.127 回答