ResponsibleChains 1.0.18

.NET Standard 2.0
NuGet\Install-Package ResponsibleChains -Version 1.0.18
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package ResponsibleChains --version 1.0.18
<PackageReference Include="ResponsibleChains" Version="1.0.18" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResponsibleChains --version 1.0.18
#r "nuget: ResponsibleChains, 1.0.18"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ResponsibleChains as a Cake Addin
#addin nuget:?package=ResponsibleChains&version=1.0.18

// Install ResponsibleChains as a Cake Tool
#tool nuget:?package=ResponsibleChains&version=1.0.18

A library for easily implementing a chain of responsibility pattern

GitHub Build and Release NuGet Nuget

This library can help you instantiate a chain of responsibility pattern in a number of convenient ways. See this Wikipedia entry for an explaination of the Chain of Responsibility pattern in Object Oriented design.

Using the package

The Examples folder on GitHub contains two implementations of FizzBuzz using the chain of responsibility pattern with ResponsibleChains.

Your chain should consist of objects which each have reference to the next object in the chain. Such objects are refered to as links in this package.

To construct a chain, use the .AddResponsibleChain<YOUR_INTERFACE_TYPE>() method in ConfigureServices in Startup to add your links in order:

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponsibleChain<IMyChain>()
        .WithLink<MyFirstLink>()
        .WithLink<MySecondLink>()
        .WithLink<DefaultLink>();

    ...
}

No DI? No Problem.

If you don't have access to a service collection or want to manually construct your chain, you can do so using the ResponsibleChainBuilder class:

IMyChain chain = new ResponsibleChainBuilder<IMyChain>()
    .WithLink<MyFirstLink>()
    .WithLink<MySecondLink>()
    .WithLink<DefaultLink>()
    .Build();

An example link might look like this:

public class MyFirstLink : IMyChain
{
    private readonly IMyChain _nextLink;

    public MyFirstLink(IMyChain nextLink)
    {
        _nextLink = nextLink;
    }

    public string DoSomething(string input)
    {
        if (input == "foo")
        {
            return "bar";
        }

        return _nextLink.DoSomething(input);
    }
}

note that the link's constructor takes a reference to the next link in the chain. The ResponsibleChain framework will handle instantiating this class with the correct nextLink reference passed in.

Handling Dependencies

Let's say your link class needs another dependency. Your constructor might look something like this:

 public MyFirstLink(IMyChain nextLink, IMyOtherDependency myOtherDependency)
{
    _nextLink = nextLink;
    _myOtherDependency = myOtherDependency;
}

If you're using the service collection injection method of creating your chain, then you just need to make sure that an instance of IMyOtherDependency is added to the service collection

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponsibleChain<IMyChain>()
        .WithLink<MyFirstLink>()
        .WithLink<MySecondLink>()
        .WithLink<DefaultLink>();

    // Add IMyOtherDependency to services
    services.AddTransient<IMyOtherDendency, MyOtherDependency>();
    ...
}

if you're instantiating the chain manually, you can provide an expression to the WithLink Method

IMyOtherDependency dep = new MyOtherDependency();

IMyChain chain = new ResponsibleChainBuilder<IMyChain>()
    .WithLink<MyFirstLink>(nextLink => new MyFirstLink(nextLink, dep))
    .WithLink<MySecondLink>()
    .WithLink<DefaultLink>()
    .Build();

Constraints

  1. Chains should end with a "default" link, which does not take a next link dependency
  2. All links must have exactly 1 public constructor (or the default constructor)
  3. All links must implement the same interface, or inherit from the same base class

Q & A

Answer: No, you can make your own interface or base class.

Question: Why not just instantiate my chain the old fashioned way?

IMyChain chain = new DefaultLink(
                    new MySecondLink(
                        new MyFirstLink(new MyOtherDependency())));

Answer:

  1. This doesn't work well within a dependency injection framework
  2. You have to instantiate the chain backwards, which is confusing to read
  3. It looks gross.

ResponsibleChains is maintained by @CarsonTolleshaug

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.18 2,243 6/23/2021
1.0.17-beta 139 6/23/2021
1.0.16-beta 144 6/23/2021
1.0.15 149 6/19/2021
1.0.14-beta 124 6/19/2021
0.1.13 160 5/27/2021
0.1.12-beta 127 5/27/2021
0.1.10 157 5/27/2021
0.1.9 157 5/27/2021
0.0.2 175 5/27/2021