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§
Required Methods§
Sourcefn command_types(&self) -> Vec<String>
fn command_types(&self) -> Vec<String>
Command type suffixes this handler processes.
Used for subscription derivation and routing.
Sourcefn state_router(&self) -> &StateRouter<Self::State>
fn state_router(&self) -> &StateRouter<Self::State>
Get the state router for rebuilding state from events.
Sourcefn handle(
&self,
cmd: &CommandBook,
payload: &Any,
state: &Self::State,
seq: u32,
) -> CommandResult<EventBook>
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§
Sourcefn rebuild(&self, events: &EventBook) -> Self::State
fn rebuild(&self, events: &EventBook) -> Self::State
Rebuild state from events.
Default implementation uses state_router().with_event_book().
Sourcefn on_rejected(
&self,
_notification: &Notification,
_state: &Self::State,
_target_domain: &str,
_target_command: &str,
) -> CommandResult<RejectionHandlerResponse>
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).