liveBook · Manning

Command handler

As explained in Clean Applications with Hexagonal Architecture

A command handler processes a command representing an application state change.

When an adapter injects each use case directly, its dependencies grow and changes to use cases ripple through its wiring. A command handler owns one write use case; an adapter can send its command through a command bus without knowing which handler executes it. section 4.5 section 4.6.1

The handler contract accepts a command and returns Either<E, Void>. A write handler coordinates loading an aggregate, invoking domain behavior, and persisting state; it does not share a handler with a query path. section 4.5.2 section 4.6.1

Listing 4.12 CommandHandler from the Architecture Kernel
public interface CommandHandler<E, C extends Command> #A
  Either<E, Void> handle(final C command); #B
}

The interface captures the handler’s command input and result contract. section 4.5.2

In Spring, the command bus can discover handler beans through the application context. A handler can also remain directly injectable; bus routing adds an option rather than replacing that path. A transaction can group the write use case so failure rolls back state and success commits it. section 4.5.2 section 4.5.3 section 6.3.5

How Clean Applications with Hexagonal Architecture covers it

How deep it goes
Defines a generic handler contract and traces bus routing, Spring discovery, and transactional execution.
Examples
Uses article publication to illustrate loading an aggregate, invoking domain behavior, persisting state, and collecting an event.
What you can do after
Apply it via CommandHandler<E, C> to implement a write path that returns Either<E, Void>.
How it connects
Connects commands and use cases with command buses, adapters, aggregates, repositories, domain events, and transactions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clean Applications with Hexagonal Architecture. The section links point to the parts of the book it draws on.