Command Handlers
When a command arrives, something needs to orchestrate the response: load the right aggregate, invoke the domain method, persist the result, and handle errors. If the API controller did this orchestration, the controller would mix domain logic into infrastructure code. If the aggregate did this orchestration, the aggregate would need to know about persistence.
Command handlers solve this by acting as thin orchestrators. They receive a command, coordinate the necessary steps, and delegate all business logic to the aggregate. The handler itself contains no business rules.
Facts
Command handlers are connected to an aggregate.
Command handlers are always connected to a single aggregate. One command handler per aggregate is the norm, with all aggregate commands handled within it.
A Command handler contains multiple handlers.
Each method in a command handler is connected to one specific command and processes that command.
Handlers are single-purpose.
Each handler method is responsible for handling a single type of command. With one command type per method, the command handling logic stays focused and manageable.
Handlers should deal only with the associated aggregate.
Methods in a command handler should only deal with managing the lifecycle of the aggregate associated with it. Any state change beyond an aggregate's boundary should be performed by eventual consistency mechanisms, like raising an event and consuming it in the event handler of the other aggregate.
Command handlers invoke domain logic.
Command handlers do not contain business logic themselves. Instead, they invoke methods on aggregates or domain services to perform the necessary actions.
Command handlers coordinate actions.
Command handlers coordinate multiple actions related to each command. Primarily, a command handler hydrates (fetches) the aggregate, invokes methods that change its state and persists the changes through a repository.
Command handlers can return values.
When processed synchronously, command handlers can return values to the caller. This is useful for scenarios like authentication where you need to return a token, or when you need to return the ID of a newly created resource.
A single event can have multiple event handlers, and they don't return values. A command, by contrast, has only one handler, so that handler can pass its return value back to the caller. This is consistent with the command pattern, where a command represents an intent to perform an action and may need to provide immediate feedback.
By default, command handlers return the position of the command in the command stream back to the caller.
Handler methods are enclosed in Unit of Work context.
Each handler method is automatically enclosed within a Unit of Work context. This means that all interactions with the infrastructure is packaged into a single transaction. This makes it all the more important to not mix multiple responsibilities or aggregates when handling a command.
Commands can be handled asynchronously.
Synchronous command handling is the norm because it preserves data integrity. You can still configure the domain to handle commands asynchronously for performance reasons.
Best Practices
Ensure idempotency.
Command handling should be idempotent, meaning that handling the same command multiple times should not produce unintended side effects. This can be achieved by checking the current state before applying changes.
Handle exceptions gracefully.
When an exception occurs, a command handler should perform any necessary rollback actions. The command handler should also return meaningful error messages to the caller.
Validate commands.
Make sure that commands are validated before processing. This can be done in a separate validation layer or within the command handler itself.
Return values only when necessary.
Only return values from command handlers when they are genuinely needed by the caller. For example, return authentication tokens or newly created resource IDs, but not entire entity representations. Return values should be small and relevant to the immediate needs of the caller.
Next steps
For practical details on defining and using command handlers in Protean, see the guide:
- Command Handlers: Defining handlers, workflow, return values, idempotency, and error handling.
For the read-side counterpart:
- Query Handlers: Processing structured read intents from projections (the read side of CQRS).
For design guidance:
- Application Service vs Command Handler: When to use which, with decision tree and comparison table.
- Thin Handlers, Rich Domain: Keeping handlers thin by pushing logic into the domain model.
- Command Idempotency: Handling duplicate commands safely.