SOLID & Design Patterns Interview Question #43

What is the Chain of Responsibility Pattern?

Structural, Creational & Behavioral Patterns Mid-Level Intermediate

Quick Interview Answer

The Chain of Responsibility Pattern passes a request through a sequence of handlers. Each handler can process the request, enrich it, reject it, or pass it to the next handler.

Detailed Explanation

The Chain of Responsibility Pattern passes a request through a sequence of handlers. Each handler can process the request, enrich it, reject it, or pass it to the next handler.

ASP.NET Core middleware is a familiar chain-style design: each middleware can perform work before and after the next component or terminate the pipeline early.

Code Example

app.Use(async (context, next) =>
{
    Console.WriteLine("Before");
    await next();
    Console.WriteLine("After");
});

app.UseAuthentication();
app.UseAuthorization();