Table of Contents

Interface IPipelineBehavior<TRequest, TResponse>

Namespace
SimpleMediator
Assembly
SimpleMediator.dll

Intercepts handler execution to apply cross-cutting logic.

public interface IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>

Type Parameters

TRequest

Request type traversing the pipeline.

TResponse

Response type returned by the final handler.

Examples

public sealed class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    public async ValueTask<Either<MediatorError, TResponse>> Handle(
        TRequest request,
        IRequestContext context,
        RequestHandlerCallback<TResponse> nextStep,
        CancellationToken cancellationToken)
    {
        logger.LogInformation(
            "Handling {Request} for user {UserId} (correlation: {CorrelationId})",
            typeof(TRequest).Name,
            context.UserId,
            context.CorrelationId);
        var response = await nextStep().ConfigureAwait(false);
        logger.LogInformation("Handled {Request}", typeof(TRequest).Name);
        return response;
    }
}

Remarks

Behaviors are chained in reverse registration order. Each one decides whether to invoke the next step or short-circuit the flow with its own response.

Methods

Handle(TRequest, IRequestContext, RequestHandlerCallback<TResponse>, CancellationToken)

Executes the behavior logic around the next pipeline element.

ValueTask<Either<MediatorError, TResponse>> Handle(TRequest request, IRequestContext context, RequestHandlerCallback<TResponse> nextStep, CancellationToken cancellationToken)

Parameters

request TRequest

Request being processed.

context IRequestContext

Ambient context with correlation ID, user info, tenant info, etc.

nextStep RequestHandlerCallback<TResponse>

Callback to the next behavior or handler.

cancellationToken CancellationToken

Token to cancel the flow.

Returns

ValueTask<Either<MediatorError, TResponse>>

Final result or the modified response from the behavior.