Expand description
CQRS Command Pattern
Provides traits for implementing the Command side of CQRS. Commands represent intentions to change state in the system.
§Example
ⓘ
use backbone_core::{Command, CommandHandler};
// Define a command
pub struct CreateUserCommand {
pub email: String,
pub name: String,
}
impl Command for CreateUserCommand {
type Result = UserId;
}
// Implement the handler
pub struct CreateUserHandler {
user_repository: Arc<dyn UserRepository>,
}
#[async_trait::async_trait]
impl CommandHandler<CreateUserCommand> for CreateUserHandler {
type Error = UserError;
async fn handle(&self, command: CreateUserCommand) -> Result<UserId, Self::Error> {
let user = User::new(command.email, command.name)?;
self.user_repository.create(user).await
}
}Traits§
- Command
- Marker trait for CQRS commands.
- Command
Dispatcher - Command dispatcher for routing commands to their handlers.
- Command
Handler - Handler for executing commands.
- Validatable
Command - Trait for commands that can be validated before execution.
- Validating
Command Handler - Extension trait for handlers that support validated commands.