Commands
Commands are messages that express the intent to change application state. They represent actions that should happen, such as "create an order," "update a user profile," or "approve a payment." In Axon Framework, commands are a fundamental building block of your application architecture.
Command-centric approach
Axon Framework takes a messaging-centric approach to application design. Commands are central to how your application processes user actions and external events. The framework provides comprehensive support for:
-
Dispatching commands - Sending commands from controllers, APIs, or other handlers
-
Handling commands - Processing commands to validate requests and apply changes
-
Routing commands - Directing commands to the appropriate handler based on routing keys
-
Coordinating changes - Managing state changes across one or more components
This approach emphasizes what you want to accomplish (commands) rather than how you structure your domain model.
Command handler styles
Command handlers come in three styles, depending on whether they need prior state and where that state lives:
-
Stateless command handlers: no prior state is needed. Each invocation is independent (for example, dispatching a batch of sub-commands, sending a notification, or forwarding a command to an external system). See Command handlers.
-
Event-sourced entities: command handlers live on the entity class itself, alongside its state fields and event sourcing handlers. All logic for the entity lives in one place. See Event-sourced entities.
-
Stateful command handlers: command handlers live in a dedicated class and receive the entity as a parameter via
@InjectEntity. Logic is organized by use case rather than by entity, which aligns with Vertical Slice Architecture. See Stateful command handlers.
Subsections
| Sub-Section | Purpose |
|---|---|
How to write stateless command handlers: parameters, message types, event appending, and configuration |
|
How to dispatch commands from your application code |
|
How to maintain state across commands and enforce business rules using event-sourced entities |
|
Command bus implementations, repositories, and other infrastructure components |
|
How to configure command handling in your application |