Skip to main content

Module command

Module command 

Source
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.
CommandDispatcher
Command dispatcher for routing commands to their handlers.
CommandHandler
Handler for executing commands.
ValidatableCommand
Trait for commands that can be validated before execution.
ValidatingCommandHandler
Extension trait for handlers that support validated commands.