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
TRequestRequest type traversing the pipeline.
TResponseResponse 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
requestTRequestRequest being processed.
contextIRequestContextAmbient context with correlation ID, user info, tenant info, etc.
nextStepRequestHandlerCallback<TResponse>Callback to the next behavior or handler.
cancellationTokenCancellationTokenToken to cancel the flow.
Returns
- ValueTask<Either<MediatorError, TResponse>>
Final result or the modified response from the behavior.