Furlong provides several implementations of the Chain of Responsibility pattern, supporting sync, async-await, request, request-response, and local delegates (also using async-await, et al).
This netstandard2.0
library contains several implementations of the Chain of Responsibility pattern, one of the Behavioural Patterns from the Gang of Four.
Each implementation has the following characteristics.
- A chain represents two or more links.
- A chain can only be accessed by the first link.
- Each link determines if the request should be passed to the next link in the chain.
Install the library from NuGet using:
dotnet add package Furlong
Simple chain with no response.
var chain = ChainFactory<MyRequest>
.Initialize()
.StartWith(new MyLink1())
.FollowedBy(new MyLink2())
.FollowedBy(new MyLink3())
.Build();
var request = new MyRequest();
chain.Handle(request);
Asynchronous chain returning a response.
var chain = AsyncChainFactory<MyRequest>
.Initialize()
.StartWith(new MyLink1())
.FollowedBy(new MyLink2())
.FollowedBy(new MyLink3())
.Build();
var request = new MyRequest();
var source = new CancellationTokenSource();
var response = await chain.HandleAsync(request, source.Token);
Synchronous chain using local delegates, with no response.
var chain = LocalChainFactory<MyRequest>
.Initialize()
.StartWith(Handle1)
.FollowedBy(Handle2)
.FollowedBy(Handle3)
.Build();
var request = new MyRequest();
chain.Handle(request);
private void Handle1(MyRequest request, out bool cancel)
{
cancel = false;
// Do something..
}
Further examples can be found in the Unit Tests project, these include:
- Simple Dependency Injection
- Dependency Injection using a Context Resolver
- Using a custom interface as the request definition
Please add any feedback or issue to the Issues section in this project.
This project is licensed under the MIT license.
This library is copyright (©) Mark Dungey 2000-2020.