简体   繁体   English

在DDD中,谁应该负责处理域事件?

[英]In DDD, who should be resposible for handling domain events?

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. 在此应用程序中, Order是聚合根和遵循规则,我们只能在单个事务中使用一个聚合。 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. 因为, OrderPlacedEvent ,我们只能修改事务中的一个聚合,我想发布OrderPlacedEvent单独事务中的某些组件处理的OrderPlacedEvent

Question arise: which components should handle this type of event? 问题出现了:哪些组件应该处理这类事件?

I'd like to: 我想:

1) Application layer if the event triggers modification of another Aggregate in the same bounded context. 1)应用层,如果事件触发在同一有界上下文中修改另一个聚合。

2) Application layer if the event trigger some infrastructure service. 2)如果事件触发某些基础结构服务,则为应用层。

eg An email is sent to the customer. 例如,向客户发送电子邮件。 So an application service is needed to load order for mail content and mail to and then invoke infrastructure service to send the mail. 因此,需要一个应用程序服务来加载邮件内容和邮件的订单,然后调用基础结构服务来发送邮件。

3) I prefer a Domain Service personally if the event triggers some operations in another bounded context. 3)如果事件在另一个有界上下文中触发某些操作,我个人更喜欢域服务。

eg Shipping or Billing, an infrastructure implementation of the Domain Service is responsible to integrate other bounded context. 例如,运输或计费,域服务的基础结构实现负责集成其他有界上下文。

4) Infrastructure layer if the event need to be split to multiple consumers. 4)如果事件需要拆分为多个消费者,则为基础设施层。 The consumer goes to 1),2) or 3). 消费者转到1),2)或3)。

For me, the conclusion is Application layer if the event leads to an seperate acceptance test for your bounded context. 对我来说,如果事件导致对您的有界上下文进行单独的验收测试,则结论是应用层。

By the way, what's your infrastructure to ensure durability of your event? 那么,您的基础设施是什么来确保您的活动的持久性? Do you include the event publishing in the transaction? 您是否在交易中包含事件发布?

These kind of handlers belong to application layer. 这些处理程序属于应用程序层。 You should probably create a supporting application service's method too. 您也应该创建一个支持应用程序服务的方法。 This way you can start separate transaction. 这样您就可以开始单独的交易。

I think the most common and usual place to put the EventHandlers is in the application layer . 我认为放置EventHandler最常见和最常见的地方是application layer Doing the analogy with CQRS, EventHandlers are very similar to CommandHandlers and I usually put them both close to each other (in the application layer). 使用CQRS进行类比,EventHandler与CommandHandler非常相似,我通常将它们彼此靠近(在应用程序层中)。

This article from Microsoft also gives some examples putting handlers there. 微软的这篇文章也提供了一些将处理程序放在那里的例子。 Look a the image bellow, taken from the related article: 看一下下面的图片,取自相关文章:

积极和处理程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM