Raxol.Architecture.CQRS.CommandHandler behaviour (Raxol v2.0.1)
View SourceCommand handler behaviour for CQRS pattern implementation in Raxol.
Command handlers are responsible for processing commands and executing the business logic associated with write operations. Each handler should focus on a single command type and maintain consistency.
Handler Design Principles
- Single Responsibility: One handler per command type
- Business Logic Focus: Contain domain logic, not infrastructure concerns
- Idempotent: Handlers should be safe to retry
- Event Generation: Generate domain events for state changes
- Error Handling: Provide meaningful error messages
Usage
defmodule MyApp.Handlers.CreateUserHandler do
use Raxol.Architecture.CQRS.CommandHandler
alias MyApp.Commands.CreateUserCommand
alias MyApp.Events.UserCreatedEvent
alias MyApp.Repositories.UserRepository
@impl true
def handle(%CreateUserCommand{} = command, context) do
with {:ok, user} <- UserRepository.create(command),
{:ok, event} <- create_user_created_event(user, command),
:ok <- publish_event(event, context) do
{:ok, %{user_id: user.id, status: :created}}
else
{:error, :user_already_exists} ->
{:error, :user_already_exists}
{:error, reason} ->
{:error, {:user_creation_failed, reason}}
end
end
defp create_user_created_event(user, command) do
event = %UserCreatedEvent{
user_id: user.id,
name: user.name,
email: user.email,
created_by: command.created_by,
correlation_id: command.correlation_id
}
{:ok, event}
end
end
Summary
Functions
Adds command handling middleware.
Creates a command handler context with metadata.
Creates a command handling pipeline.
Creates a CRUD command handler pattern.
Creates a standardized error response.
Executes compensating actions for command failures.
Creates a saga command handler for distributed transactions.
Creates a standardized success response.
Validates command handler requirements.
Validates command preconditions.
Applies optimistic locking to prevent concurrent modifications.
Wraps a function with transaction support.
Types
Callbacks
Functions
Adds command handling middleware.
Creates a command handler context with metadata.
Creates a command handling pipeline.
Creates a CRUD command handler pattern.
Creates a standardized error response.
Executes compensating actions for command failures.
Creates a saga command handler for distributed transactions.
Creates a standardized success response.
Validates command handler requirements.
Validates command preconditions.
Applies optimistic locking to prevent concurrent modifications.
Wraps a function with transaction support.