EasyChain 1.0.10-gd800b6c417

This is a prerelease version of EasyChain.
dotnet add package EasyChain --version 1.0.10-gd800b6c417
                    
NuGet\Install-Package EasyChain -Version 1.0.10-gd800b6c417
                    
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="EasyChain" Version="1.0.10-gd800b6c417" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasyChain" Version="1.0.10-gd800b6c417" />
                    
Directory.Packages.props
<PackageReference Include="EasyChain" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EasyChain --version 1.0.10-gd800b6c417
                    
#r "nuget: EasyChain, 1.0.10-gd800b6c417"
                    
#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.
#addin nuget:?package=EasyChain&version=1.0.10-gd800b6c417&prerelease
                    
Install EasyChain as a Cake Addin
#tool nuget:?package=EasyChain&version=1.0.10-gd800b6c417&prerelease
                    
Install EasyChain as a Cake Tool

EasyChain

<p align="left"> <a href="https://github.com/cleberMargarida/easy-chain/actions/workflows/workflow.yml"> <img src="https://github.com/cleberMargarida/easy-chain/actions/workflows/workflow.yml/badge.svg" alt="Build-deploy pipeline"> </a> <a href="https://www.nuget.org/packages/EasyChain"> <img src="https://img.shields.io/nuget/vpre/EasyChain.svg" alt="EasyChain Nuget Version"> </a>
<a href="https://github.com/cleberMargarida/easy-chain/actions/runs/10553862586#summary-29234823720"> <img src="https://camo.githubusercontent.com/64e5de57df4409175a42e38d3fe23291f6fe8bc1ccf2bf2a2007c7af2df7832c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f436f6465253230436f7665726167652d38342532352d737563636573733f7374796c653d666c6174" alt="EasyChain Coverage"> </a> </p>

EasyChain is a lightweight .NET library designed for implementing the Chain of Responsibility pattern. It enables you to define a sequence of handlers to process messages in a flexible and decoupled manner.

With EasyChain, you not only get linear chains, but you also have the advantage of branching into tree structures, giving you more power and flexibility when dealing with complex workflows.

To learn more about chain of responsibilities pattern, read the following article Refactoring Guru - Chain of Responsibility.

Features

  • .NET Standard Support: Compatible with all .NET applications, including .NET Core, .NET 5, .NET 6, .NET 7, .NET 8, and .NET 9.
  • Integration with Dependency Injection: Fully supports Microsoft.Extensions.DependencyInjection, including various lifetime options.
  • Fluent API: Define and configure chains of handlers using a simple and expressive fluent API.
  • Asynchronous Processing: Handles messages asynchronously.
  • Runtime Compilation: Uses System.Linq.Expressions to compile methods dynamically at runtime, ensuring no code is embedded between your handlers.
  • Ease of Use: Extremely straightforward to set up and use.
  • Tree-like Branching Support: Fork and create tree-like structures, allowing flexible processing paths and parallel workflows.

Installation

To install EasyChain, add the following package to your project via NuGet:

dotnet add package EasyChain

Usage

1. Define Your Chain

Here's a quick example to get you started:

class CarChain : IChainConfig<Car>
{
    public void Configure(IChainBuilder<Car> callChain)
    {
        callChain.SetNext<CarYearHandler>()
                 .SetNext<EngineSizeHandler>()
                 .SetNext<CarModelHandler>();
    }
}

This example represents a simple linear chain, where each handler processes the Car object in sequence. You can visualize this process in the following diagram:

Handlers are lined-up

2. Register the Chain

builder.Services.AddChain<CarChain>();

3. Run the Chain

IChain<Car> chain = app.Services.GetService<IChain<Car>>();

var message = new Car
{
    Model = "FooModel",
    Year = 2024,
};

await chain.Run(message);

4. Forking Example

EasyChain allows you to fork the chain into multiple branches and merge them back later, providing the flexibility to split the handling process into parallel paths. Here's how you can fork your chain:

class CarChain : IChainConfig<Car>
{
    public void Configure(IChainBuilder<Car> callChain)
    {
        callChain.SetNext<CarYearHandler>()
                 .Fork((left, right) =>
                 {
                     left.SetNext<EngineSizeHandler>();
                     right.SetNext<CarModelHandler>();
                 })
                 .Merge()
                 .SetNext<CarPriceHandler>();
    }
}

This example demonstrates a chain that forks into two branches:

  • The first branch processes EngineSizeHandler.
  • The second branch processes CarModelHandler.

After both branches are processed, the chain merges and continues with CarPriceHandler. You can visualize the branching behavior like this:

Object Tree Branching

This shows the power of EasyChain, which allows you to manage not just linear processing, but complex tree-like workflows, all within a fluent API.


5. Building In-Line

If you prefer to build your chain in-line, EasyChain offers an API for that:

var chain = Chain<Car>.CreateBuilder()
                      .SetNext<CarYearHandler>()
                      .SetNext<CarModelHandler>()
                      .Build();

This approach allows you to create and configure chains dynamically in a single statement. However, note that it does not support dependency injection, and all handler classes must be parameterless.


6. Implementing a Handler

To implement a handler in EasyChain, your class must implement the IHandler<T> interface, where T is the type of message the handler will process. Each handler must define the Handle method, which takes a message and a reference to the next handler in the chain:

class CarYearHandler : IHandler<Car>
{
    public async Task Handle(Car message, ChainHandling<Car> next)
    {
        // Process the message (e.g., check car's year)
        if (message.Year > 2000)
        {
            // Pass the message to the next handler in the chain
            await next(message);
        }
    }
}

In this example:

  • The handler processes a Car message.
  • If the car's year is greater than 2000, the message is passed to the next handler.

Each handler can implement its own logic and decide whether to continue the chain based on conditions.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact

For any questions or support, please reach out to cleber.margarida@outlook.com

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.10-gd800b6c417 70 9/19/2024
1.0.9-g84549e6040 71 9/18/2024
1.0.8-ga458241a04 64 9/18/2024
1.0.6-g02eb2d1167 62 9/18/2024
1.0.5-g5a6d0b3b32 67 8/26/2024
1.0.4-g5d078facc3 79 8/26/2024
0.0.0-gefbb5722af 67 8/26/2024
0.0.0-gc451631ecf 63 8/26/2024
0.0.0-g9078683bbb 65 8/26/2024
0.0.0-g88c59b3e2e 56 8/26/2024
0.0.0-g854dbc783b 61 8/26/2024
0.0.0-g76f2ca2a54 61 8/26/2024
0.0.0-g768ab18893 63 8/26/2024
0.0.0-g70e38b4ba6 61 8/26/2024
0.0.0-g6d3bd44630 64 8/26/2024
0.0.0-g3a1c312e1b 59 8/26/2024
0.0.0-g35dd161270 64 8/26/2024
0.0.0-g027d335fae 60 8/26/2024