In DDD, who should be applicable to handle domain events?

Who should be responsible for handling domain events? Application Services, Domain Services, or Organizations?

Let me use a simple example for this question.

Let's say we are working on a store application, and we have an application service dedicated to order operations. In this application, Order is an aggregated root and the following rules, we can work with only one aggregate within a single transaction. After placing an order, it is stored in the database. But much remains to be done. First of all, we need to change the number of items available in the inventory, and secondly, notify another part of the system (perhaps another limited context) so that the delivery procedure for this specific order is started. Since, as already mentioned, we can only change one aggregate in a transaction, I am thinking of publishing OrderPlacedEvent , which will be processed by some components in separate transactions.

The question is: what components should handle this type of event?

+6
source share
2 answers

I would like to:

1) Application level, if the event triggers a modification of another unit in the same limited context.

2) The application level, if the event starts some infrastructure services.

eg. The letter is sent to the client. Therefore, an application service is required to download an order for mail content and mail, and then call the infrastructure service to send mail.

3) I prefer the domain service personally if the event triggers some operations in another limited context.

eg. Delivery or billing, implementation of a domain service infrastructure is responsible for integrating another limited context.

4) The level of infrastructure, if the event needs to be divided into several consumers. The consumer goes to 1), 2) or 3).

For me, the conclusion is the application layer, if the event leads to a separate acceptance test for your limited context.

By the way, what kind of infrastructure does your event last? Include event posting in a transaction?

+6
source

These types of handlers belong to the application layer. You should probably also create a support service method. This way you can start a separate transaction.

+2
source

Source: https://habr.com/ru/post/951149/


All Articles