Chain of Responsibility Pattern in C# – Real-Time Example (Request Processing Pipeline)
What is Chain of Responsibility Pattern?
The Chain of Responsibility Pattern is a behavioral design pattern where a request is passed through a chain of handlers until one handler processes it.
Each handler:
- Processes the request
- Or passes it to the next handler
👉 Creates a pipeline-like structure.
Why Use Chain of Responsibility Pattern?
- Reduce tight coupling
- Build flexible processing pipelines
- Dynamically add/remove handlers
- Separate responsibilities cleanly
- Avoid large if-else chains
Real-Time Scenario
API Request Pipeline:
- Authentication
- Authorization
- Validation
- Logging
👉 Request passes through multiple handlers sequentially.
Real-Time Example – Request Validation Pipeline
Step 1: Abstract Handler
public abstract class Handler
{
protected Handler _nextHandler;
public void SetNext(Handler nextHandler)
{
_nextHandler = nextHandler;
}
public abstract void Handle(string request);
}
Step 2: Concrete Handlers
Authentication Handler
public class AuthenticationHandler : Handler
{
public override void Handle(string request)
{
Console.WriteLine("Authentication Successful");
_nextHandler?.Handle(request);
}
}
Validation Handler
public class ValidationHandler : Handler
{
public override void Handle(string request)
{
Console.WriteLine("Validation Successful");
_nextHandler?.Handle(request);
}
}
Logging Handler
public class LoggingHandler : Handler
{
public override void Handle(string request)
{
Console.WriteLine("Request Logged");
_nextHandler?.Handle(request);
}
}
Step 3: Build the Chain
var auth = new AuthenticationHandler();
var validation = new ValidationHandler();
var logging = new LoggingHandler();
auth.SetNext(validation);
validation.SetNext(logging);
auth.Handle("Create Invoice Request");
Output
Authentication Successful
Validation Successful
Request Logged
Key Concept
Instead of:
Authenticate();
Validate();
Log();
We do:
auth.Handle(request);
👉 Request flows through handler chain.
Diagram Understanding
Request
↓
AuthenticationHandler
↓
ValidationHandler
↓
LoggingHandler
👉 Each handler processes and forwards request.
Advantages
- ✔ Loose coupling
- ✔ Flexible processing pipeline
- ✔ Easy to add/remove handlers
- ✔ Better separation of concerns
- ✔ Cleaner request processing
Disadvantages
- ✖ Harder debugging
- ✖ Request may go unhandled
- ✖ Too many handlers can increase complexity
When to Use
Use Chain of Responsibility when:
- Multiple handlers process requests
- Request pipeline is needed
- Dynamic processing flow is required
- Responsibilities should be separated
Real Project Mapping (.NET + Angular)
| Feature | Usage |
|---|---|
| Middleware pipeline | Chain of Responsibility |
| Validation pipeline | Chain of Responsibility |
| Logging pipeline | Chain of Responsibility |
| Approval workflow | Chain of Responsibility |
| Request processing | Chain of Responsibility |
ASP.NET Core Real Example
Middleware Pipeline
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints();
👉 ASP.NET Core middleware internally follows Chain of Responsibility.
Advanced Example – Approval Workflow
Employee Request
↓
Manager Approval
↓
HR Approval
↓
Finance Approval
👉 Each stage processes request sequentially.
Chain of Responsibility vs Mediator
| Chain of Responsibility | Mediator |
|---|---|
| Request flows sequentially | Centralized communication |
| Multiple handlers process request | Mediator coordinates objects |
| Pipeline-oriented | Interaction-oriented |
Pro Tip
Chain of Responsibility works well with:
- Middleware
- Validation frameworks
- Logging systems
- Workflow engines
- CQRS pipelines
Summary
Chain of Responsibility helps you:
- Build flexible pipelines
- Separate request processing logic
- Reduce tight coupling
👉 Perfect for:
- Middleware
- Validation
- Approval workflows
- Request processing systems
Comments
Post a Comment