Skip to content
/ cor-di Public

This library allows you to use constructor dependency injection (and perform Inversion of Control) with the Chain of Responsibility pattern.

License

Notifications You must be signed in to change notification settings

esavini/cor-di

Repository files navigation

Chain of Responsibility Dependency Injection

Nuget

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!

Installation

Simply use this nuget command:

Install-Package CoRDependencyInjection

Usage

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();
    }
}

Exceptions

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.

About

This library allows you to use constructor dependency injection (and perform Inversion of Control) with the Chain of Responsibility pattern.

Topics

Resources

License

Stars

Watchers

Forks

Languages