Skip to main content

CommandHandler

Trait CommandHandler 

Source
pub trait CommandHandler<C: Command, D: MessageBusDriver>:
    Clone
    + Send
    + Sync {
    // Required method
    fn handle(
        &self,
        uow: &mut D::UnitOfWork,
        cmd: C,
    ) -> impl Future<Output = Result<Option<D::Identifier>>> + Send;
}
Expand description

A handler responsible for executing commands.

Implementors of this trait define the logic for processing a specific Command within the context of a UnitOfWork. The handle method is provided a mutable reference to the UnitOfWork, allowing full access to domain state and repositories for the duration of the command lifecycle.

This method must return a future that resolves to either a successful result of type C::Response or a failure, which will cause the unit of work to be rolled back.

Command handlers are only invoked during the command execution phase of the message bus.

Required Methods§

Source

fn handle( &self, uow: &mut D::UnitOfWork, cmd: C, ) -> impl Future<Output = Result<Option<D::Identifier>>> + Send

Handle a command using the given unit of work.

This function should perform any necessary domain logic, mutate aggregates, and register domain events as needed.

If the command is successful, the UnitOfWork will be committed. If an error is returned, the UnitOfWork will be rolled back.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§