Shuttle.Core.Pipelines
14.0.0
Prefix Reserved
dotnet add package Shuttle.Core.Pipelines --version 14.0.0
NuGet\Install-Package Shuttle.Core.Pipelines -Version 14.0.0
<PackageReference Include="Shuttle.Core.Pipelines" Version="14.0.0" />
paket add Shuttle.Core.Pipelines --version 14.0.0
#r "nuget: Shuttle.Core.Pipelines, 14.0.0"
// Install Shuttle.Core.Pipelines as a Cake Addin #addin nuget:?package=Shuttle.Core.Pipelines&version=14.0.0 // Install Shuttle.Core.Pipelines as a Cake Tool #tool nuget:?package=Shuttle.Core.Pipelines&version=14.0.0
Shuttle.Core.Pipelines
PM> Install-Package Shuttle.Core.Pipelines
Observable event-based pipelines based broadly on pipes and filters.
Configuration
In order to more easily make use of pipelines an implementation of the IPipelineFactory
should be used. The following will register the PipelineFactory
implementation:
services.AddPipelineProcessing(builder => {
builder.AddAssembly(assembly);
});
This will register the IPipelineFactory
and, using the builder, add all IPipeline
and IPipelineObserver
implementations as Transient
. The pipeline instances are re-used as they are kept in a pool.
Since pipelines are quite frequently extended by adding observers, the recommended pattern is to make use of an IHostedService
implementation that accepts the IPipelineFactory
dependency:
public class CustomHostedService : IHostedService
{
private readonly Type _pipelineType = typeof(InterestingPipeline);
public CustomHostedService(IPipelineFactory pipelineFactory)
{
Guard.AgainstNull(pipelineFactory, nameof(pipelineFactory));
pipelineFactory.PipelineCreated += PipelineCreated;
}
private void PipelineCreated(object sender, PipelineEventArgs e)
{
if (e.Pipeline.GetType() != _pipelineType
{
return;
}
e.Pipeline.RegisterObserver(new SomeObserver());
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_pipelineFactory.PipelineCreated -= OnPipelineCreated;
await Task.CompletedTask;
}
}
Typically you would also have a way to register the above CustomHostedService
with the IServiceCollection
:
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddCustomPipelineObserver(this IServiceCollection services)
{
services.AddHostedService<CustomHostedService>();
return services;
}
}
The above is a rather naive example but it should give you an idea of how to extend pipelines using the IPipelineFactory
and IHostedService
implementations.
Overview
A Pipeline
is a variation of the pipes and filters pattern and consists of 1 or more stages that each contain one or more events. When the pipeline is executed each event in each stage is raised in the order that they were registered. One or more observers should be registered to handle the relevant event(s).
Each Pipeline
always has its own state that is simply a name/value pair with some convenience methods to get and set/replace values. The State
class will use the full type name of the object as a key should none be specified:
var state = new State();
var list = new List<string> {"item-1"};
state.Add(list); // key = System.Collections.Generic.List`1[[System.String...]]
state.Add("my-key", "my-key-value");
Console.WriteLine(state.Get<List<string>>()[0]);
Console.Write(state.Get<string>("my-key"));
The Pipeline
class has a RegisterStage
method that will return a PipelineStage
instance. The PipelineStage
instance has a WithEvent
method that will return a PipelineStageEvent
instance. This allows for a fluent interface to register events for a pipeline:
IPipelineObserver
The IPipelineObserver
interface is used to define the observer that will handle the events:
public interface IPipelineObserver<in TPipelineEvent> : IPipelineObserver where TPipelineEvent : IPipelineEvent
{
void Execute(TPipelineEvent pipelineEvent);
Task ExecuteAsync(TPipelineEvent pipelineEvent);
}
The interface has two methods that can be implemented. The Execute
method is used for synchronous processing whereas the ExecuteAsync
method is used for asynchronous processing.
Example
Events should derive from PipelineEvent
.
We will use the following events:
public class OnAddCharacterA : PipelineEvent
{
}
public class OnAddCharacter : PipelineEvent
{
public char Character { get; private set; }
public OnAddCharacter(char character)
{
Character = character;
}
}
The OnAddCharacterA
event represents a very explicit event whereas with the OnAddCharacter
event one can specify some data. In this case the character to add.
In order for the pipeline to process the events we will have to define one or more observers to handle the events. We will define only one for this sample but we could very easily add another that will handle one or more of the same, or other, events:
public class CharacterPipelineObserver :
IPipelineObserver<OnAddCharacterA>,
IPipelineObserver<OnAddCharacter>
{
public void Execute(OnAddCharacterA pipelineEvent)
{
var state = pipelineEvent.Pipeline.State;
var value = state.Get<string>("value");
value = string.Format("{0}-A", value);
state.Replace("value", value);
}
public async Task ExecuteAsync(OnAddCharacterA pipelineEvent)
{
Execute(pipelineEvent);
await Task.CompletedTask;
}
public void Execute(OnAddCharacter pipelineEvent)
{
var state = pipelineEvent.Pipeline.State;
var value = state.Get<string>("value");
value = string.Format("{0}-{1}", value, pipelineEvent.Character);
state.Replace("value", value);
}
public async Task ExecuteAsync(OnAddCharacter pipelineEvent)
{
Execute(pipelineEvent);
await Task.CompletedTask;
}
}
Next we will define the pipeline itself:
var pipeline = new Pipeline();
pipeline.RegisterStage("process")
.WithEvent<OnAddCharacterA>()
.WithEvent(new OnAddCharacter('Z'));
pipeline.RegisterObserver(new CharacterPipelineObserver());
pipeline.State.Add("value", "start");
if (sync)
{
pipeline.Execute();
}
else
{
await pipeline.ExecuteAsync();
}
Console.WriteLine(pipeline.State.Get<string>("value")); // outputs start-A-Z
We can now execute this pipeline with predictable results.
Pipelines afford us the ability to better decouple functionality. This means that we could re-use the same observer in multiple pipelines enabling us to compose functionality at a more granular level.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
- Microsoft.Extensions.DependencyInjection (>= 7.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.1)
- Shuttle.Core.Contract (>= 11.1.0)
- Shuttle.Core.Reflection (>= 13.0.0)
- System.Reflection.Emit.Lightweight (>= 4.7.0)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on Shuttle.Core.Pipelines:
Package | Downloads |
---|---|
Shuttle.Esb
Contains the core Shuttle.Esb assembly that should always be referenced when building Shuttle.Esb solutions. |
|
Shuttle.Core.PipelineTransaction
Provides a pipeline observer to handle transaction scopes. |
|
Shuttle.Recall
Event sourcing mechanism. |
|
Shuttle.Core.PipelineTransactionScope
Provides a mechanism to create a transaction scope when a pipeline stage starts. |
|
Shuttle.Core.Mediator.OpenTelemetry
OpenTelemetry instrumentation for Shuttle.Core.Mediator implementations. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
14.0.0 | 1,440 | 8/5/2024 |
13.0.0 | 2,324 | 4/30/2024 |
12.1.1 | 15,449 | 12/1/2022 |
12.0.0 | 27,038 | 9/4/2022 |
11.0.1 | 592 | 4/9/2022 |
11.0.0 | 28,589 | 3/21/2022 |
10.1.0 | 8,833 | 2/9/2021 |
10.0.7 | 18,940 | 11/27/2020 |
10.0.6 | 92,229 | 7/4/2018 |
10.0.5 | 1,035 | 4/12/2018 |
10.0.4 | 1,753 | 4/8/2018 |
10.0.3 | 951 | 4/7/2018 |
10.0.2 | 14,697 | 2/13/2018 |