SOLID & Design Patterns Interview Question #67
What is a pipeline behavior in mediator-based architectures?
Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate
Quick Interview Answer
A pipeline behavior wraps request handling with reusable logic before and after the actual handler. Typical concerns include validation, logging, performance measurement, authorization, and transaction management.
Detailed Explanation
A pipeline behavior wraps request handling with reusable logic before and after the actual handler. Typical concerns include validation, logging, performance measurement, authorization, and transaction management.
This resembles Decorator or Chain of Responsibility. Pipeline behavior keeps cross-cutting concerns out of individual handlers, but too many behaviors can make execution flow harder to trace.
Code Example
public class LoggingBehavior<TRequest, TResponse>
{
public async Task<TResponse> Handle(
TRequest request,
Func<Task<TResponse>> next)
{
Console.WriteLine("Before");
var result = await next();
Console.WriteLine("After");
return result;
}
}