Chain Of Responsibility Pattern In C# – Simplified How-To Guide
Tags: csharp, coding, programming, tutorial
Published: 2024-01-12 17:00:00

Let’s dive into [the Chain of Responsibility pattern in C#](https://www.devleader.ca/2024/01/05/chain-of-responsibility-pattern-in-c-simplified-how-to-guide/)! This is a [design pattern](https://www.devleader.ca/2023/12/31/the-big-list-of-design-patterns-everything-you-need-to-know/) used to handle requests by processing them through a series of potential handlers, each responsible for a specific subset of requests. This pattern’s primary purpose is to decouple the sender of a request from its receiver, increasing flexibility in object-oriented design.

This article’s approach will explore how to implement the Chain of Responsibility pattern in C#, including specifics like its key components and a breakdown of the coding best practices. By the end of this article, you should understand how to use this [design pattern](https://www.devleader.ca/2023/12/31/the-big-list-of-design-patterns-everything-you-need-to-know/) in your next C# project!

---

## Understanding the Chain of Responsibility Pattern

The Chain of Responsibility pattern is a behavioral pattern that allows objects in a software application to pass a request between them in a hierarchical chain. This pattern is used to decouple sender and receiver objects, allowing multiple objects the ability to handle a request. It’s often used in scenarios where there is more than one object that can handle a request, but the exact one that will handle the request isn’t known at runtime.

### Chain of Responsibility Pattern – Hypothetical Scenario

To understand the Chain of Responsibility pattern, consider a hypothetical scenario in which a customer has placed an order on an e-commerce website. The order needs to be fulfilled by the warehouse, but the warehouse is backlogged, and they can’t fulfill the order right away. The order fulfillment process involves several steps, such as retrieving the order information, calculating the shipping costs, gathering the items, and finally, shipping the order.

In this scenario, the Chain of Responsibility pattern can be used to handle the order fulfillment process. Each step in the process can be encapsulated in its own object, allowing for separation of concerns and code reusability. When a customer places an order, the Order object is passed through a chain of handlers, each with the ability to handle the order fulfillment process. The chain of handlers consists of a RetrieveOrderHandler, CalculateShippingHandler, GatherItemsHandler, and ShipOrderHandler.

The benefits of using the Chain of Responsibility pattern in this scenario include increased flexibility and reusability of the code. Because the Order object is passed through a chain of handlers, it’s easy to swap out different handlers as needed. If the shipping provider changes, for example, only the ShipOrderHandler needs to be updated. Additionally, the separation of concerns allows for cleaner, more maintainable code — and we’ll see some code soon!

### Key components of the Chain of Responsibility pattern

The Chain of Responsibility pattern consists of several key components. The first is a set of responsibilities to be performed. In the hypothetical scenario mentioned earlier, the set of responsibilities would consist of retrieving the order information, calculating the shipping costs, gathering the items, and shipping the order. These are more of the abstract “thing that needs to be done”.

The second component is a set of Handler classes that encapsulate those responsibilities. The chain of handlers in the example scenario consisted of a RetrieveOrderHandler, CalculateShippingHandler, GatherItemsHandler, and ShipOrderHandler. Each handler has a HandleRequest method that allows it to perform the specific task its responsible for and to pass the Order object down the chain to the next handler.

The third component is a set of client objects that initiate the request and pass it to the first handler in the chain. In the example scenario, the client object would be the customer’s Order object.

Overall, the Chain of Responsibility pattern offers a useful way to handle complex processes in a software application. By separating concerns and allowing for flexibility in the code, the pattern can help streamline development and make it easier to maintain the application over time.

---

[Remember to subscribe to my FREE weekly software engineering newsletter, Dev Leader Weekly](https://subscribe.devleader.ca)!

---

## Implementing the Chain of Responsibility Pattern in C#

To implement the Chain of Responsibility pattern in C#, you first need to identify the responsibilities that need to be performed by the application. Once these responsibilities are identified, you can create a chain of handlers to process the request. The chain of handlers should include a base abstract class that defines the basic functionality of each handler, as well as the methods required to pass the request down the chain.

### Example of Chain of Responsibility Pattern in C#

Below is an example implementation of the Chain of Responsibility pattern in C#:

```csharp
public abstract class Handler
{
    protected Handler _successor;

    public void SetSuccessor(Handler successor)
    {
        _successor = successor;
    }

    public abstract void HandleRequest(Order order);
}

public class RetrieveOrderHandler : Handler
{
    public override void HandleRequest(Order order)
    {
        if (order.NeedsRetrieval())
        {
            // Perform the retrieval logic
        }
        else if (_successor != null)
        {
            _successor.HandleRequest(order);
        }
    }
}

public class CalculateShippingHandler : Handler
{
    public override void HandleRequest(Order order)
    {
        if (order.NeedsShippingCosts())
        {
            // Perform the shipping cost calculation logic
        }
        else if (_successor != null)
        {
            _successor.HandleRequest(order);
        }
    }
}

public class GatherItemsHandler : Handler
{
    public override void HandleRequest(Order order)
    {
        if (order.NeedsItemGathering())
        {
            // Perform the item gathering logic
        }
        else if (_successor != null)
        {
            _successor.HandleRequest(order);
        }
    }
}

public class ShipOrderHandler : Handler
{
    public override void HandleRequest(Order order)
    {
        if (order.NeedsShipping())
        {
            // Perform the shipping logic
        }
        else if (_successor != null)
        {
            _successor.HandleRequest(order);
        }
    }
}
```

In this example, each handler in the chain inherits from the abstract Handler class, which defines the SetSuccessor method used to set the next handler in the chain and the HandleRequest method used to process the order. Generally, I like passing any type of dependencies in via constructors (avoiding mutating methods like this), but this is just for illustration.

Each concrete handler then overrides the HandleRequest method to perform its specific responsibility, such as retrieving the order, calculating shipping costs, gathering items, or shipping the order. If a handler in the chain is unable to handle the request, it passes the request to the next handler using the SetSuccessor method.

### Tips and Best Practices

To optimize the Chain of Responsibility pattern, it’s important to adhere to some best practices, such as:

* Keep the responsibilities of each handler small and specific.
    
* Avoid circular chains of handlers, as this can create infinite loops.
    
* Use a single method for each handler that processes the request, rather than using multiple handling methods.
    
* Avoid hard-coded loops in handlers and favor a dynamic approach (such as using the SetSuccessor method instead).
    
* Use the Chain of Responsibility pattern for processes where there are multiple ways of handling the request.
    

By following these tips, you can optimize the Chain of Responsibility pattern to create robust, efficient applications that are easy to maintain and extend over time.

---

## Testing and Debugging the Pattern

Of course, testing and debugging are important steps in software development. When working with this pattern, it’s critical to test and debug thoroughly to ensure that the request passes through the chain of handlers as intended, with each handler performing its designated responsibilities and the ultimate result being achieved.

### Unit Testability of Chain of Responsibility

One of the main benefits of using the Chain of Responsibility pattern is the ability to easily swap handlers in and out of the chain. This makes it important to test the chain under various scenarios to ensure that any combination of handlers works successfully.

Unit testing is crucial to verifying the flow of the chain of handlers. The developers should make sure that each handler works as expected, passing the request to the next handler appropriately or fulfilling the request itself when appropriate. This testing method also helps ensure that all handlers perform the exact amount of work required and no more.

Because the chain can be dynamic, we can use mocks (or stubs/fakes if you prefer) to test that successors get passed the responsibility when necessary. Otherwise, the particular handler under test should maintain responsibility as expected.

### Tips to Save Time and Effort

When testing a Chain of Responsibility pattern in C# application architecture, it’s important not to fall into the trap of hard-coding the order of the handlers in the chain. Instead, it’s important to test a range of combinations of handlers to ensure that the order is not having a negative effect on the overall performance of the application.

Debugging the Chain of Responsibility pattern takes a lot of care because each handler performs their responsibility at a different point of time. If the output is not as expected, then ensuring that handlers are handling the request appropriately becomes critical. It’s important to make sure that the pipeline of handlers works as expected by verifying the output of each handler and how it affects the next handler to produce the desired outcome.

Testing and debugging are critical stages in the Chain of Responsibility pattern implementation process — just like any other code you write. Proper testing gives you confidence that the code you are writing meets the requirements, and debugging gives confidence that the code is bug-free.

---

## Wrapping up The Chain of Responsibility Pattern in C#

The Chain of Responsibility pattern is a helpful Gang of Four pattern that allows developers to create robust and flexible application architectures. By using the pattern, developers can refactor application code without making significant structural changes, improving code maintainability and extensibility.

We looked at the Chain of Responsibility pattern, its components, implementation in C#, and best practices. I also discussed the significance of testing and debugging specifically for this pattern. Remember that it’s essential to identify the set of responsibilities, create appropriate classes, and define methods to handle responsibility delegation. Developers should follow the best practices and thoroughly test and debug the code to ensure that it works as expected.

If you’re interested in more learning opportunities, [subscribe to my free weekly newsletter](https://www.devleader.ca/newsletter/) and [check out my YouTube channel](https://youtube.com/@devleader)!

---

### Want More Dev Leader Content?

* Follow along on this platform if you haven’t already!    
* Subscribe to my free weekly software engineering and dotnet-focused newsletter. I include exclusive articles and early access to videos:  
    [**SUBSCRIBE FOR FREE**](https://subscribe.devleader.ca/)    
* Looking for courses? Check out my offerings:  
    [**VIEW COURSES**](https://devleader.ca/courses)    
* Watch hundreds of full-length videos on my YouTube channel:  
    [**VISIT CHANNEL**](https://youtube.com/@devleader)    
* Visit my website for hundreds of articles on various software engineering topics (including code snippets):  
    [**VISIT WEBSITE**](https://devleader.ca/)    
* Check out the repository with many code examples from my articles and videos on GitHub:  
    [**VIEW REPOSITORY**](https://github.com/ncosentino/DevLeader)