Skip to main content

IMediator

Trait IMediator 

Source
pub trait IMediator: Send + Sync {
    // Required methods
    fn send<'life0, 'async_trait, T, R>(
        &'life0 self,
        req: T,
    ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             T: IRequest<R> + Send + 'static + 'async_trait,
             R: Send + 'static + 'async_trait,
             Self: Sized + 'async_trait;
    fn send_boxed<'life0, 'async_trait>(
        &'life0 self,
        req: Box<dyn Any + Send>,
    ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Any + Send>, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
    fn publish<'life0, 'async_trait, T>(
        &'life0 self,
        event: T,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: Sized + 'async_trait,
             T: 'async_trait + IEventRequest;
}
Expand description

The mediator dispatches requests to their handlers and publishes events to all registered handlers.

Analogous to MediatR’s IMediator.

§Object safety

The trait is dyn-compatible for callers that only need type-erased dispatch: the generic send/publish methods require Self: Sized, while IMediator::send_boxed takes/returns Box<dyn Any + Send> and is the one available on dyn IMediator:

ⓘ
let mediator: Arc<dyn IMediator> = Arc::new(Mediator::new(provider));
let response = mediator.send_boxed(Box::new(CreateOrderRequest { .. })).await?;
let order = *response.downcast::<OrderView>().expect("typed response");

Required Methods§

Source

fn send<'life0, 'async_trait, T, R>( &'life0 self, req: T, ) -> Pin<Box<dyn Future<Output = Result<R, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, T: IRequest<R> + Send + 'static + 'async_trait, R: Send + 'static + 'async_trait, Self: Sized + 'async_trait,

Send a request and return its structured response.

Source

fn send_boxed<'life0, 'async_trait>( &'life0 self, req: Box<dyn Any + Send>, ) -> Pin<Box<dyn Future<Output = Result<Box<dyn Any + Send>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Send a type-erased request and return a type-erased response.

Works through Arc<dyn IMediator>: the request’s TypeId is looked up in the handler registry and the generated boxed dispatch downcasts it, runs the same pipeline + handler path as IMediator::send, and boxes the response for the caller to downcast.

Source

fn publish<'life0, 'async_trait, T>( &'life0 self, event: T, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: Sized + 'async_trait, T: 'async_trait + IEventRequest,

Publish an event to all registered handlers.

Stays generic (not available on dyn IMediator): event handlers are resolved from DI by their concrete IEventHandler<T> type.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§