'To which layer in DDD event handlers belongs

We are currently using domain driven desing with commands and events. I can not decide in which layer of DDD should event handlers and command handlers resides.

My feeling is it should reside in application layer but I have not proper arguments. My understanding of application layer in brief is, application layer coordinate business tasks and does not hold any domain state.

Event handlers are the coordinators which reacts to message and call domain objects to do their tasks. Most of the time event handlers only decide which command/event(message) will be called next. Or call some API or other domain logic in domain layer, to do the task before further message execution.

Thats the why I think event and command handlers should resides in application layer.

But, now it is getting more complex. We are using Process Managers too. They handles events same as event handlers do. But they contains temporary state. Moreover how they are constructed, they are holders of business logic too. You can read from them how are events chained and what must be achieved to fulfill this particular process.

How should I threat them? Did they belong to application layer or domain layer?

Summary:

  1. Did event handlers belongs to application layer?
  2. Did command handlers belongs to application layer?
  3. Did Process Managers belongs to application layer?


Solution 1:[1]

Did event handlers belongs to application layer?
Event handlers are located in the Application Layer and sometimes in Domain layer.
Application layer event handlers are more infrastructural in nature, carrying out tasks like sending e-mails and publishing events to other bounded contexts.
Responsibilities:

  • trigger communication with external bounded contexts, call another domain to do something
  • manage communication with external services
  • may notify user about changes
  • forward the Event via a messaging infrastructure

Domain layer event handlers invoke domain logic, such as invoking a domain service.
Responsibilities:

  • handling domain-specific logic in one bounded context
  • delegating to a domain services


Summarise: Event handlers that purely perform domain logic better to place at Domain Layer, but others only at Application Layer

Did command handlers belongs to application layer?
Yes.
Events represent a past, something that already happened and can’t be undone. They are part of the business domain and its ubiquitous language. Commands, on the other hand, represent a wish, an action in the future that can be rejected. Commands are typically the result of a user action. Inside the Domain Layer we don’t have user actions. The command handlers handle the command by orchestrating the workflow of the business operation and effectively replace the Application Service in this job. So the correct place for Command Handlers is Application Layer.

Did Process Managers belongs to application layer?
Yes.
Process Manager coordinates a business process that spans more than a single bounded context. It encapsulates the process-specific logic and maintains a central point of control. In other words, it is an orchestrator, which does not contain any bussiness rules. So it must belong to Application Layer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1