sonbua / responsibilitychain Goto Github PK
View Code? Open in Web Editor NEWJust another micro-framework for implementing Chain of Responsibility pattern in .NET Core and .NET Framework
License: MIT License
Just another micro-framework for implementing Chain of Responsibility pattern in .NET Core and .NET Framework
License: MIT License
CancellationToken
s are essential to asynchronous operations. They should be part of any async method's parameters by default, even though as an optional one.
Framework currently supports in resolving dependencies via an injected IServiceProvider instance in the composite handler's constructor like this
public class WorkLogParser : Handler<string, int>
public WorkLogParser(IServiceProvider serviceProvider) : base(serviceProvider)
{
AddHandler<NestedHandler1>();
AddHandler<NestedHandler2>();
}
}
This makes SUT (WorkLogParser) hard to test in case one wants to mock out a nested handler (such as logging, database access,...) or a dependency of a nested handler.
So a handler (and composite handler) should expose all dependencies via constructor parameters instead of the service locator anti-pattern.
public class WorkLogParser : Handler<string, int>
public WorkLogParser(NestedHandler1 nestedHandler1, NestedHandler2 nestedHandler2)
{
AddHandler(nestedHandler1);
AddHandler(nestedHandler2);
}
}
Currently child handler, which is added to an asynchronous composite, is not intercepted.
This is the current interface definition of IHandler<TIn, TOut>
and IAsyncHandler<TIn, TOut>
public interface IHandler<TIn, TOut> : IHandler
{
TOut Handle(TIn input, Func<TIn, TOut> next);
}
public interface IAsyncHandler<TIn, TOut> : IHandler
{
Task<TOut> HandleAsync(TIn input, Func<TIn, Task<TOut>> next);
}
When client consumes this API via a composite handler, she often has to pass null
as the next handler. This is inconvenient and adds noise to the code base. Having an additional Handle
method on the abstract composite, which requires the input
object only and no next
delegate, improves readability.
Current IAsyncHandler<TIn, TOut>
definition:
public interface IAsyncHandler<TIn, TOut> : IHandler
{
Task<TOut> HandleAsync(TIn input, Func<TIn, Task<TOut>> next);
}
HandleAsync()
, once started, couldn't be cancelled.
It should look like this:
public interface IAsyncHandler<TIn, TOut> : IHandler
{
Task<TOut> HandleAsync(TIn input, Func<TIn, CancellationToken, Task<TOut>> next, CancellationToken cancellationToken);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.