DotNetHelp.Pipeline 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DotNetHelp.Pipeline --version 1.0.0                
NuGet\Install-Package DotNetHelp.Pipeline -Version 1.0.0                
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="DotNetHelp.Pipeline" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNetHelp.Pipeline --version 1.0.0                
#r "nuget: DotNetHelp.Pipeline, 1.0.0"                
#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.
// Install DotNetHelp.Pipeline as a Cake Addin
#addin nuget:?package=DotNetHelp.Pipeline&version=1.0.0

// Install DotNetHelp.Pipeline as a Cake Tool
#tool nuget:?package=DotNetHelp.Pipeline&version=1.0.0                

dotnethelp Pipeline

Workflow Branch Coverage Line Coverage Method Coverage NuGet Download

A simple pipeline builder for C#

Install

dotnet add package DotNetHelp.Pipeline

Examples

Simple

A simple pipeline with two steps executed with a single input item. Each step is executed by passing in a PipelineRequest. A pipeline request has access to both the item being processed and a shared global context which is shared across all request to the pipeline. A Context allows you to store and retieve objects stored within each step.

using DotNetHelp.Pipelines;

var  _cancellationToken = new CancellationToken();
var _builder = new PipelineBuilder();

_builder
        .AddInlineStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data))
                {
                        data.Increment(data.Id);
                }
                r.Context.Add(new Data(1));
                return Task.CompletedTask;
        })
        .AddInlineStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data))
                {
                        data.Increment(data.Id);
                }
                r.Context.Add(new Data(2));
                return Task.CompletedTask;
        });

var _pipeline = _builder.Build(_cancellationToken);
var input = new SourceData();
Context result = await _pipeline.InvokeSync(input);

Multiple Input

using DotNetHelp.Pipelines;

var  _cancellationToken = new CancellationToken();
var _builder = new PipelineBuilder();

_builder
        .AddInlineStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data))
                {
                        data.Increment(data.Id);
                }
                r.Context.Add(new Data(1));
                return Task.CompletedTask;
        })
        .AddInlineStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data))
                {
                        data.Increment(data.Id);
                }
                r.Context.Add(new Data(2));
                return Task.CompletedTask;
        });

var _pipeline = _builder.Build(_cancellationToken);
var input = new List<SourceData> { new SourceData(1), new SourceData(2) };
Context result = await _pipeline.InvokeMany(input);

Async Input

using DotNetHelp.Pipelines;

var  _cancellationToken = new CancellationToken();
var _builder = new PipelineBuilder();

_builder
        .AddInlineStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data))
                {
                        data.Increment(data.Id);
                }
                r.Context.Add(new Data(1));
                return Task.CompletedTask;
        })
        .AddInlineStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data))
                {
                        data.Increment(data.Id);
                }
                r.Context.Add(new Data(2));
                return Task.CompletedTask;
        });

var _pipeline = _builder.Build(_cancellationToken);
await _builder.Invoke();

foreach (var input in inputs)
{
        await _pipeline.AddInput<T>(input);
}

await Finalise();

await foreach (var item in Result.WithCancellation(_cancellationToken.Token))
{
        yield return item;
}

Filtering

A filter will allow the request to continue to following steps if the filter returns true. If the filter returns false the context will be discarded and no futher steps will be executed for this input.

using DotNetHelp.Pipelines;

var  _cancellationToken = new CancellationToken();
var _builder = new PipelineBuilder();

_builder
        .AddInlineFilterStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data) && data.Id % 2 == 0)
                {
                        return Task.FromResult(false);
                }

                return Task.FromResult(false);
        })
        .AddInlineStep((r) =>
        {
                if (r.Item.TryGetValue(out SourceData data))
                {
                        data.Increment(data.Id);
                }
                r.Context.Add(new Data(2));
                return Task.CompletedTask;
        });

var _pipeline = _builder.Build(_cancellationToken);
var input = new SourceData();
Context result = await _pipeline.InvokeSync(input);

Branching

A branch is a sub pipeline that once completed will continue back to parent pipeline. The branch will only be executed if the filter returns true. If the filter returns false the pipeline will continue to the next steps without executing the branch sub pipeline.

using DotNetHelp.Pipelines;

var  _cancellationToken = new CancellationToken();
var _builder = new PipelineBuilder();

_builder
       .AddInlineFilterStep((r) =>
       {
               if (r.Item.TryGetValue(out SourceData data) && data.Id % 2 == 0)
               {
                       return Task.FromResult(false);
               }

               return Task.FromResult(false);
       })
       .AddInlineBranchStep((r) =>
       {
               if (r.Item.TryGetValue(out SourceData data) && data.Id % 2 == 0)
               {
                       return Task.FromResult(true);
               }
               return Task.FromResult(false);
       },
       (b) =>
       {
               b
               .AddInlineStep((r) =>
               {
                       if (r.Item.TryGetValue(out SourceData data))
                       {
                               data.Increment(data.Id);
                       }
                       return Task.CompletedTask;
               });
       })
       .AddInlineStep((r) =>
       {
               if (r.Item.TryGetValue(out SourceData data))
               {
                       data.Increment(data.Id);
               }
               r.Context.Add(new Data(2));
               return Task.CompletedTask;
       });

var _pipeline = _builder.Build(_cancellationToken);
var input = new SourceData();
Context result = await _pipeline.InvokeSync(input);

Forking

A fork is a sub pipeline that once completed will return its output. If a fork is triggered no further pipeline steps will be executed for this request. The fork will only be executed if the filter returns true. If the filter returns false the pipeline will continue to the next steps without executing the fork sub pipeline.

using DotNetHelp.Pipelines;

var  _cancellationToken = new CancellationToken();
var _builder = new PipelineBuilder();

_builder
       .AddInlineFilterStep((r) =>
       {
               if (r.Item.TryGetValue(out SourceData data) && data.Id % 2 == 0)
               {
                       return Task.FromResult(false);
               }

               return Task.FromResult(false);
       })
       .AddInlineForkStep((r) =>
       {
               if (r.Item.TryGetValue(out SourceData data) && data.Id % 2 == 0)
               {
                       return Task.FromResult(true);
               }
               return Task.FromResult(false);
       },
       (b) =>
       {
               b
               .AddInlineStep((r) =>
               {
                       if (r.Item.TryGetValue(out SourceData data))
                       {
                               data.Increment(data.Id);
                       }
                       return Task.CompletedTask;
               });
       })
       .AddInlineStep((r) =>
       {
               if (r.Item.TryGetValue(out SourceData data))
               {
                       data.Increment(data.Id);
               }
               r.Context.Add(new Data(2));
               return Task.CompletedTask;
       });

var _pipeline = _builder.Build(_cancellationToken);
var input = new SourceData();
Context result = await _pipeline.InvokeSync(input);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
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.2 254 3/21/2023
1.0.1 225 3/20/2023
1.0.0 229 3/19/2023
0.0.6 224 3/18/2023
0.0.5 213 3/17/2023