Skip to main content

CommandHandler

Trait CommandHandler 

Source
pub trait CommandHandler<C: Command>:
    Send
    + Sync
    + 'static
    + Send {
    type Error: Into<HexeractError> + Send + Sync + 'static;

    // Required method
    fn handle(
        &self,
        command: C,
        ctx: &HandlerContext,
    ) -> impl Future<Output = Result<C::Output, Self::Error>> + Send;
}
Expand description

Asynchronous handler for a Command.

Each Command type has exactly one registered CommandHandler. The handler receives an immutable reference to itself, the command value and a HandlerContext carrying tracing and cancellation information.

§Example

use hexeract_core::{Command, CommandHandler, HandlerContext, HexeractError};
use uuid::Uuid;

struct CreateUser {
    pub email: String,
}

impl Command for CreateUser {
    type Output = Uuid;
}

struct UserRepository;

impl CommandHandler<CreateUser> for UserRepository {
    type Error = HexeractError;

    async fn handle(
        &self,
        cmd: CreateUser,
        _ctx: &HandlerContext,
    ) -> Result<Uuid, Self::Error> {
        let _ = cmd.email;
        Ok(Uuid::new_v4())
    }
}

Required Associated Types§

Source

type Error: Into<HexeractError> + Send + Sync + 'static

The handler-defined error type, convertible into HexeractError.

Required Methods§

Source

fn handle( &self, command: C, ctx: &HandlerContext, ) -> impl Future<Output = Result<C::Output, Self::Error>> + Send

Handles the command and produces its output.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§