Skip to main content

CommandHandlerDomainHandler

Trait CommandHandlerDomainHandler 

Source
pub trait CommandHandlerDomainHandler: Send + Sync {
    type State: Default + 'static;

    // Required methods
    fn command_types(&self) -> Vec<String>;
    fn state_router(&self) -> &StateRouter<Self::State>;
    fn handle(
        &self,
        cmd: &CommandBook,
        payload: &Any,
        state: &Self::State,
        seq: u32,
    ) -> CommandResult<EventBook>;

    // Provided methods
    fn rebuild(&self, events: &EventBook) -> Self::State { ... }
    fn on_rejected(
        &self,
        _notification: &Notification,
        _state: &Self::State,
        _target_domain: &str,
        _target_command: &str,
    ) -> CommandResult<RejectionHandlerResponse> { ... }
}
Expand description

Handler for a single domain’s command handler logic.

Command handlers receive commands and emit events. They maintain state that is rebuilt from events using a StateRouter.

§Example

struct PlayerHandler {
    state_router: StateRouter<PlayerState>,
}

impl CommandHandlerDomainHandler for PlayerHandler {
    type State = PlayerState;

    fn command_types(&self) -> Vec<String> {
        vec!["RegisterPlayer".into(), "DepositFunds".into()]
    }

    fn state_router(&self) -> &StateRouter<Self::State> {
        &self.state_router
    }

    fn handle(
        &self,
        cmd: &CommandBook,
        payload: &Any,
        state: &Self::State,
        seq: u32,
    ) -> CommandResult<EventBook> {
        dispatch_command!(payload, cmd, state, seq, {
            "RegisterPlayer" => self.handle_register,
            "DepositFunds" => self.handle_deposit,
        })
    }
}

Required Associated Types§

Source

type State: Default + 'static

The state type for this aggregate.

Required Methods§

Source

fn command_types(&self) -> Vec<String>

Command type suffixes this handler processes.

Used for subscription derivation and routing.

Source

fn state_router(&self) -> &StateRouter<Self::State>

Get the state router for rebuilding state from events.

Source

fn handle( &self, cmd: &CommandBook, payload: &Any, state: &Self::State, seq: u32, ) -> CommandResult<EventBook>

Handle a command and return resulting events.

The handler should dispatch internally based on payload.type_url.

Provided Methods§

Source

fn rebuild(&self, events: &EventBook) -> Self::State

Rebuild state from events.

Default implementation uses state_router().with_event_book().

Source

fn on_rejected( &self, _notification: &Notification, _state: &Self::State, _target_domain: &str, _target_command: &str, ) -> CommandResult<RejectionHandlerResponse>

Handle a rejection notification.

Called when a command issued by a saga/PM targeting this aggregate’s domain was rejected. Override to provide custom compensation logic.

Default implementation returns an empty response (framework handles).

Implementors§