DotJoshJohnson.Pipelines 1.0.0

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

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

DotJoshJohnson.Pipelines

A simple utility library for building and invoking pipelines similar to the ASP.NET Core middleware pipeline.

Getting Started

Quick and Dirty

  1. Install DotJoshJohnson.Pipelines via NuGet.
  2. Build a pipeline.
using DotJoshJohnson.Pipelines;
  
// ... //
  
var pipeline = new PipelineBuilder<PipelineContext>()
    .Use(next => async (context, cancellationToken) =>
    {
        // TODO: do work
          
        await next(context, cancellationToken);
    })
    .Use(next => async (context, cancellationToken) =>
    {
        // TODO: do more work
          
        await next(context, cancellationToken);
    })
    .Build();
  1. Invoke your pipeline!
// continued from step 2 //

var context = new PipelineContext();

await pipeline.Invoke(context);

Using Pipeline Components

Instead of adding delegates directly to your pipeline, you can add components, which are classes that implement IPipelineComponent<TContext>.

using DotJoshJohnson.Pipelines;

// ... //

public class MyCustomComponent : IPipelineComponent<PipelineContext>
{
    public async Task Invoke(PipelineContext context, PipelineInvocationDelegate<PipelineContext> next, CancellationToken cancellationToken)
    {
        // TODO: do work here
        
        await next(context, cancellationToken);
        
        // TODO: maybe do work here as well
    }
}

// ... //

var pipeline = new PipelineBuilder<PipelineContext>()
    .Use<MyCustomComponent>()
    .Build();

When it comes time to invoke your component, it will be "activated", which just means an instance of that class will be requested by the pipeline. The default activator just uses Activator.CreateInstance<TComponent>() to get a new instance of your component. You can implement your own activator (IPipelineComponentActivator) if you'd like or use an extension such as DotJoshJohnson.Pipelines.MicrosoftDependencyInjection that provides one for you. If you're writing your own activator, be sure to tell the builder about it by calling WithActivator() on your pipeline builder. Note that this method must be called before any components are added ot the pipeline.

Custom Pipeline Context

The PipelineContext object you've seen thus far is simply included for convenience. You can use it as-is, extend it via inheritance, or use a totally different class. There is no requirement to use or extend PipelineContext.

Dependency Injection

If you are using Microsoft.Extensions.DependencyInjection, I recommend pulling in DotJoshJohnson.Pipelines.MicrosoftDependencyInjection. This package provides two extensions to IServiceCollection. Note that if you are using pipeline components, you must register each component with the DI container.

Using AddPipeline

using DotJoshJohnson.Pipelines;
using Microsoft.Extensions.DependencyInjection;

// ... //

var services = new ServiceCollection();

// DON'T FORGET THIS STEP
services.AddTransient<MyCustomComponent>();

services.AddPipeline<PipelineContext>(p =>
{
    p.Use(next => async (context, cancellationToken) =>
    {
        // TODO: do work!
        
        await next(context, cancellationToken);
    });
    
    p.Use<MyCustomComponent>();
});

var serviceProvider = services.BuildServiceProvider();

var pipeline = serviceProvider.GetRequiredService<IPipeline<PipelineContext>>();

await pipeline.Invoke(new());

Using AddNamedPipeline

using DotJoshJohnson.Pipelines;
using Microsoft.Extensions.DependencyInjection;

// ... //

var services = new ServiceCollection();

// DON'T FORGET THIS STEP
services.AddTransient<MyCustomComponent>();

services.AddNamedPipeline<PipelineContext>("MyPipeline", p =>
{
    p.Use(next => async (context, cancellationToken) =>
    {
        // TODO: do work!
        
        await next(context, cancellationToken);
    });
    
    p.Use<MyCustomComponent>();
});

var serviceProvider = services.BuildServiceProvider();

var pipeline = serviceProvider.GetRequiredService<INamedPipeline>().Get<PipelineContext>("MyPipeline");

await pipeline.Invoke(new());
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.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DotJoshJohnson.Pipelines:

Package Downloads
DotJoshJohnson.Pipelines.MicrosoftDependencyInjection

Adds support for Microsoft.Extensions.DependencyInjection to DotJoshJohnson.Pipelines.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 810 1/6/2023
1.0.0 796 12/31/2021