ResponsibleChains 1.0.18

dotnet add package ResponsibleChains --version 1.0.18                
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.
<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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 11,053 6/23/2021
1.0.17-beta 243 6/23/2021
1.0.16-beta 244 6/23/2021
1.0.15 345 6/19/2021
1.0.14-beta 227 6/19/2021
0.1.13 339 5/27/2021
0.1.12-beta 227 5/27/2021
0.1.10 320 5/27/2021
0.1.9 325 5/27/2021
0.0.2 352 5/27/2021