This library allows you to use constructor dependency injection (and perform Inversion of Control) with the Chain of Responsibility pattern (see Wikipedia page or read GoF book's Design Patterns). It is entirely written in C# and targets .NET Standard 2.0.
After three year, I have updated the library to .NET Standard 2.0 and I active support it. Feel free to contribute!
Simply use this nuget command:
Install-Package CoRDependencyInjection
Basic usage (and the only one 😉):
public void ConfigureServices(IServiceCollection services)
{
services.AddTransientChain<ITestChain>()
.WithHandler<FirstHandler>()
.WithHandler<SecondHandler>()
.WithHandler<LastHandler>()
.BuildChain();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSingletonChain<ITestChain>()
.WithHandler<FirstHandler>()
.WithHandler<SecondHandler>()
.WithHandler<LastHandler>()
.BuildChain();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddScopedChain<ITestChain>()
.WithHandler<FirstHandler>()
.WithHandler<SecondHandler>()
.WithHandler<LastHandler>()
.BuildChain();
}
The chain will build in the order you provide the handlers. Remember to do not request the next handler in the last one!
Example handler:
public class FirstHandler : ITestChain
{
private readonly ITestHandler _next;
private readonly ShouldContinue _shouldContinue;
public FirstHandler(ITestHandler next, IOption<ShouldContinue> shouldContinueOption)
{
_next = next;
_shouldContinue = shouldContinueOption.Value;
}
public bool Handle()
{
if(_shouldContinue.Continue)
{
return _next.Handle();
}
return false;
}
}
Example chain handle request:
public class HomeController : Controller
{
private readonly ITestHandler _chain;
public FirstHandler(ITestHandler chain)
{
_chain = chain;
}
[HttpPost]
public bool Index()
{
return _chain.Handle();
}
}
All exceptions inherit from CoRDependencyInjectionException
.
EmptyChainException
: Thrown when the chain is empty and build was requested.MissingPublicConstructorException
: Thrown when no public constructor is available.RequestedNextHandlerInTheLastOneException
: Thrown when was next handler was requested in the last one.