Skip to main content

Command

Trait Command 

Source
pub trait Command: Send + Sync {
    // Required method
    fn aggregate_id(&self) -> &str;
}
Expand description

Trait for commands that can be dispatched to aggregates.

Commands represent intent to change aggregate state. They are imperative (e.g., CreateUser, UpdateProfile) and can be rejected if business rules aren’t satisfied.

§Design Principles

  • Imperative naming: Use verbs (Create, Update, Delete, Activate)
  • Validation happens in aggregates: Commands may be rejected
  • Identify target: Commands must know which aggregate instance to operate on
  • Serializable: Commands should be serializable for command sourcing

§Example

use arc_core::aggregate::Command;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserCommand {
    CreateUser { id: String, name: String, email: String },
    UpdateProfile { id: String, name: String },
    DeleteUser { id: String },
}

impl Command for UserCommand {
    fn aggregate_id(&self) -> &str {
        match self {
            UserCommand::CreateUser { id, .. } => id,
            UserCommand::UpdateProfile { id, .. } => id,
            UserCommand::DeleteUser { id } => id,
        }
    }
}

Required Methods§

Source

fn aggregate_id(&self) -> &str

Get the aggregate instance ID this command targets.

The command bus uses this to load the correct aggregate instance from the event store.

§Example
let command = UserCommand::CreateUser {
    id: "user-123".to_string(),
    name: "Alice".to_string(),
};

assert_eq!(command.aggregate_id(), "user-123");

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§