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.
ResponsibleChains can be installed using the Nuget package manager or the dotnet CLI
dotnet add package ResponsibleChains
The Examples folder 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>();
...
}
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.
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();
- Chains should end with a "default" link, which does not take a next link dependency
- All links must have exactly 1
public
constructor (or the default constructor) - All links must implement the same interface, or inherit from the same base class
Answer: No, you can make your own interface or base class.
IMyChain chain = new DefaultLink(
new MySecondLink(
new MyFirstLink(new MyOtherDependency())));
Answer:
- This doesn't work well within a dependency injection framework
- You have to instantiate the chain backwards, which is confusing to read
- It looks gross.
ResponsibleChains is maintained by @CarsonTolleshaug