PipelineNet
PipelineNet copied to clipboard
Add cancellation tokens
I noticed that the library does not support CancellationTokens, like say, MediatR IPipelineBehaviors does: https://github.com/jbogard/MediatR/blob/v12.4.0/src/MediatR/IPipelineBehavior.cs.
This can be worked around with a custom class:
public class CustomClass
{
public Bitmap Image { get; set; }
public CancellationToken { get; set; }
}
And I cannot think of a way of adding them to core interfaces without breaking every IAsyncMiddleware<TParameter> implementation...
But still, it would be nice to be able to pass the CancellationToken directly:
IAsyncPipeline<TParameter> longRunningPipeline = CreatePipelineSomehow();
Bitmap image = (Bitmap) Image.FromFile("party-photo.png");
await longRunningPipeline.Execute(image, cancellationToken);
~~Oh, but with new interfaces this could work perhaps?~~
+ public interface ICancellebaleAsyncMiddleware<TParameter>
+ {
+ Task Run(TParameter parameter, Func<TParameter, Task> next, CancellationToken cancellationToken);
+ }
public interface IAsyncPipeline<TParameter>
{
+ IAsyncPipeline<TParameter> AddCancellebale<TMiddleware>() where TMiddleware : ICancellebaleAsyncMiddleware<TParameter>; // Does not compile with the same method name.
+ Task Execute(TParameter paramter, CancellationToken cancellationToken);
}
~~And for chains:~~
+ public interface ICancellebaleAsyncMiddleware<TParameter, TRetrun>
+ {
+ Task<TRetrun> Run(TParameter parameter, Func<TParameter, Task<TRetrun>> next, CancellationToken cancellationToken);
+ }
public interface IAsyncResponsibilityChain<TParameter, TReturn>
{
+ IAsyncResponsibilityChain<TParameter, TReturn> ChainCancellebale<TMiddleware>() where TMiddleware : ICancellebaleAsyncMiddleware<TParameter, TReturn>;
+ Task<TReturn> Execute(TParameter paramter, CancellationToken cancellationToken);
}
~~.~~
~~Then it would only break custom pipeline / chain implementations and inheritance. In core implementation, it could just convert one delegate to the other using default(CancellationToken) / CancellationToken.None.~~
What do you think, is it a worthwhile addition?