A powerful execution pattern for composing sequential operations in .NET 8. Combines Chain of Responsibility with workflow and pipeline patterns for flexible, ordered execution.
$ dotnet add package Chain.NetCoreChain is a powerful execution pattern designed for composing sequential operations in .NET. It combines aspects of the Chain of Responsibility pattern with workflow and pipeline concepts to provide flexible, ordered execution of operations.
Chain provides a clean architecture for building processing pipelines. A IChain<T> manager class orchestrates the execution of multiple ILink<T> implementations in sequence. The ExecuteAll() method iterates through each link's Execute() method, allowing the manager class to define the execution sequence.
EmptyChain<T> class provides out-of-the-box functionalityManager class also can have a method called ClosingAction. This action will be run after execution of all links.
Links have two control mechanisms:
There is a ready to use chain called EmptyChain in this assembly. New chains can be implemented either directly from IChain or by
inheriting from EmptyChain.
Create a simple chain by composing links together:
var message = new Message { Name = "M1" };
var c = new EmptyChain<Message>();
c.AddLink<L1>();
c.AddLink<L2>();
c.ExecuteAll(message);
Inherit from EmptyChain for custom chain definitions:
public class L1L2Chain : EmptyChain<Message>
{
public L1L2Chain()
{
AddLink<L1>();
AddLink<L2>();
}
}
var c = new L1L2Chain();
c.ExecuteAll(message);
Define chains in your IoC container for maximum flexibility:
_container = new Container(_ => _.For<IChain<Message>>().Use(CreateChain()).Singleton());
private static IChain<Message> CreateChain()
{
var chain = new EmptyChain<Message>();
chain.AddLink<L1>();
chain.AddLink<L2>();
return chain;
}MIT License - See LICENSE.md for details
For issues, questions, or contributions, please visit the GitHub repository.