ResponsibleChains 1.0.18
dotnet add package ResponsibleChains --version 1.0.18
NuGet\Install-Package ResponsibleChains -Version 1.0.18
<PackageReference Include="ResponsibleChains" Version="1.0.18" />
paket add ResponsibleChains --version 1.0.18
#r "nuget: ResponsibleChains, 1.0.18"
// 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
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();
Creating Link Classes
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
- 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
Q & A
Question: Do my links need to implement a specific interface?
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:
- 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
Product | Versions 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. |
-
.NETStandard 2.0
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,186 | 6/23/2021 |
1.0.17-beta | 244 | 6/23/2021 |
1.0.16-beta | 245 | 6/23/2021 |
1.0.15 | 346 | 6/19/2021 |
1.0.14-beta | 228 | 6/19/2021 |
0.1.13 | 340 | 5/27/2021 |
0.1.12-beta | 228 | 5/27/2021 |
0.1.10 | 321 | 5/27/2021 |
0.1.9 | 326 | 5/27/2021 |
0.0.2 | 353 | 5/27/2021 |