Skip to main content

IPipelineBehavior

Trait IPipelineBehavior 

Source
pub trait IPipelineBehavior<Req, Rsp>: Send + Sync
where Req: Send + 'static, Rsp: Send + 'static,
{ // Required method fn handle<'life0, 'async_trait>( &'life0 self, req: Req, next: Next<Req, Rsp>, ) -> Pin<Box<dyn Future<Output = Result<Rsp, Error>> + Send + 'async_trait>> where 'life0: 'async_trait, Self: 'async_trait; }
Expand description

Typed pipeline behavior that wraps around request handling.

Multiple behaviors form a chain: the first registered behavior is the outermost (runs first on the request, last on the response). A behavior can inspect/modify the request and response, or short-circuit by returning a response without calling Next::run — the value is the handler’s response type, checked by the compiler:

ⓘ
#[derive(Default)]
struct RequireNotes;

#[async_trait::async_trait]
impl IPipelineBehavior<CreateOrderRequest, OrderView> for RequireNotes {
    async fn handle(
        &self,
        req: CreateOrderRequest,
        next: Next<CreateOrderRequest, OrderView>,
    ) -> Result<OrderView> {
        if req.note.is_empty() {
            return Err(Error::Validation("note is required".into()));
        }
        next.run(req).await
    }
}

svc.add_typed_pipeline::<RequireNotes, CreateOrderRequest, OrderView>();

Behaviors are singletons (one instance per host). The DI resolver is only available while a behavior is being constructed: IServiceCollectionExt::add_typed_pipeline_with hands the factory the resolver; inside handle only the typed request/continuation exist.

Required Methods§

Source

fn handle<'life0, 'async_trait>( &'life0 self, req: Req, next: Next<Req, Rsp>, ) -> Pin<Box<dyn Future<Output = Result<Rsp, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§