Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 392
woof-woof Avatar asked Aug 06 '13 17:08

woof-woof


People also ask

What is a domain event DDD?

A domain event is, something that happened in the domain that you want other parts of the same domain (in-process) to be aware of. The notified parts usually react somehow to the events. An important benefit of domain events is that side effects can be expressed explicitly.

Where can I publish a domain event?

Domain events are published using a simple ApplicationEventPublisher interface. By default, when using ApplicationEventPublisher, events are published and consumed in the same thread. Everything happens in the same container.

What is DDD example?

An aggregate is a domain-driven design pattern. It's a cluster of domain objects (e.g. entity, value object), treated as one single unit. A car is a good example. It consists of wheels, lights and an engine.


1 Answers

I think the most common and usual place to put the EventHandlers is in the 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).

This article from Microsoft also gives some examples putting handlers there. Look a the image bellow, taken from the related article:

Aggreagates and Handlers

like image 87
fabriciorissetto Avatar answered Sep 22 '22 09:09

fabriciorissetto