Dekiru.Conduit
1.0.11
Prefix Reserved
See the version list below for details.
dotnet add package Dekiru.Conduit --version 1.0.11
NuGet\Install-Package Dekiru.Conduit -Version 1.0.11
<PackageReference Include="Dekiru.Conduit" Version="1.0.11" />
paket add Dekiru.Conduit --version 1.0.11
#r "nuget: Dekiru.Conduit, 1.0.11"
// Install Dekiru.Conduit as a Cake Addin #addin nuget:?package=Dekiru.Conduit&version=1.0.11 // Install Dekiru.Conduit as a Cake Tool #tool nuget:?package=Dekiru.Conduit&version=1.0.11
Conduits
Conduits is a minimal library for creating pipelines with middlewares. The intent is to use it encapsulate buisness logic in a way that is easy to overview and test.
A pipeline consists of an implementation of the Pipeline<TInput, TOutput>
, with zero or more implementations of Interceptor<TInput, TOutput>
.
TInput
and TOutput
may be the same type, or different types. The TInput
of a pipeline is the type that is passed to the Process
method of the pipeline. The TOutput
of a pipeline is the type that is returned by the Process
method of the pipeline. TInput
must be a reference type, while TOutput
may be a value type.
Note:
TInput
is considered the endpoint of a pipeline, and must be unique. Creating multiple pipelines with the sameTInput
is not supported, regardless of theTOutput
.
Creating a pipeline
The following example demonstrates how to create a pipeline with a single interceptor.
public class MyPipeline : Pipeline<MyClass, MyClassResult>
{
public MyPipeline()
{
AddInterceptor<MyInterceptor>();
AddInterceptor(new MyInterceptorSingleton());
AddInterceptor(provider => new MyInterceptor(provider.GetRequiredService<MyService>()));
AddInterceptor(() => new MyInterceptor());
}
public override Task<MyClassResult> Process(MyClass input, CancellationToken cancellationToken)
{
return Task.FromResult(input);
}
}
Things to note:
- The
AddInterceptor
methods can used to add interceptors to the pipeline. This is normally done in the constructor of the pipeline. TheMyInterceptor
class is an implementation ofInterceptor<TInput, TOutput>
. - There are four overloads of the
AddInterceptor
method:AddInterceptor<TInterceptor>()
- Adds an interceptor of typeTInterceptor
to the pipeline.AddInterceptor(IInterceptor<TInput, TOutput> interceptor)
- Adds an instance ofTInterceptor
to the pipeline.AddInterceptor(Func<IServiceProvider, IInterceptor<TInput, TOutput>> factory)
- Adds an interceptor to the pipeline using a factory method that receives anIServiceProvider
.AddInterceptor(Func<IInterceptor<TInput, TOutput>> factory)
- Adds an interceptor to the pipeline using a factory method that does not receive any parameters.
- The
Process
method is the entry point of the pipeline. This is where the input is processed by the pipeline. - The
Process
method receives the input, and aCancellationToken
. - Any external services required by the pipeline should be declared as constructor parameters.
- Both the pipeline and the interceptors are registered as scoped services in the
IServiceProvider
.
Creating an interceptor
The following example demonstrates how to create an interceptor.
public class MyInterceptor : Interceptor<MyClass, MyClassResult>
{
public override Task BeforeProcessing(MyClass input, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public override Task AfterProcessing(MyClassResult output, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public override Task OnError(MyClass input, Exception exception, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
Things to note:
- Regardless of success or failure, the pipeline returns by calling the intercepters in the reverse order they were added to the pipeline.
- If an exception occurs in an interceptor, the remaining interceptors are not called, but the OnError method is called for each interceptor in the reverse order.
- The
BeforeProcessing
andAfterProcessing
methods have default implementations that return a completed task. These methods may be overridden to perform operations before and after the pipeline processes the input. - The methods may modify, but not replace, the input and output of the pipeline.
- If an exception is thrown during the processing of the pipeline, the
OnError
method is called. This method may be overridden to perform operations when an exception occurs.- When an exception occurs, the
AfterProcessing
method is not called.
- When an exception occurs, the
- If the interceptor requires services from the DI container, they can be declared as constructor parameters.
Registering the pipelines
PipelineExtensions
provides the extension method AddPipelines
for IServiceCollection
that registers all pipelines and interceptors in the assembly, as well as any referenced assemblies.
Pipelines are registered as Pipeline<TInput, TOutput>
rather than the concrete implementation. This allows the pipelines to be resolved from the DI container based on the TInput
type. The interceptors, instead, are registered via their implementation type, as the order of interceptors is important.
AddPipelines
will also register the Dispatcher
class as a singleton service. The Dispatcher
class is used to dispatch the input to the correct pipeline based on the TInput
type.
Dispatching the input
Dispatching a payload is done by resolving the Dispatcher
class from the DI container and calling the DispatchAsync
method.
var dispatcher = serviceProvider.GetRequiredService<Dispatcher>();
var result = await dispatcher.DispatchAsync<MyClass, MyClassResult>(new MyClass());
The pipeline is resolved based on the TInput
type of the payload, rather then relying on the concrete implementation of the pipeline.
Attempting to dispatch a payload with an unregistered TInput
type will result in an exception.
Why?
The main goal of this library is to provide a way to encapsulate business logic in a way that is easy to overview and test, without introducing a lot of unnecessary code. By using pipelines and interceptors, the business logic can be split into smaller, more manageable parts. This also makes it easier to test the individual parts of the business logic.
Why not use a library like MediatR? Honestly, mainly because MediatR contains a lot of features that we have no use for, as well as enouraging boiler-plate heavy code.
Its implementation of middleware handling relies on a bit of magic, which makes it harder to follow the flow of the code. By using a more explicit approach, we hope to make it easier to understand the code.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
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 | |
---|---|---|---|
8.0.3 | 116 | 11/15/2024 | |
8.0.2 | 138 | 10/21/2024 | |
8.0.1 | 140 | 10/4/2024 | |
8.0.0 | 332 | 8/22/2024 | |
6.0.2 | 75 | 10/21/2024 | |
6.0.1 | 84 | 10/4/2024 | |
6.0.0 | 110 | 8/22/2024 | |
2.0.6 | 157 | 7/8/2024 | |
2.0.5 | 196 | 6/12/2024 | |
2.0.4 | 136 | 6/10/2024 | |
2.0.3 | 92 | 6/10/2024 | |
2.0.2 | 108 | 5/31/2024 | |
2.0.1 | 112 | 5/31/2024 | |
2.0.0 | 114 | 5/31/2024 | |
1.3.1 | 128 | 5/10/2024 | |
1.3.0 | 110 | 5/8/2024 | |
1.2.8 | 137 | 4/25/2024 | |
1.2.7 | 109 | 4/25/2024 | |
1.2.6 | 118 | 4/25/2024 | |
1.2.5 | 117 | 4/21/2024 | |
1.2.4 | 104 | 4/18/2024 | |
1.2.2 | 140 | 4/13/2024 | |
1.2.1 | 94 | 4/12/2024 | |
1.2.0 | 94 | 4/12/2024 | |
1.1.2 | 97 | 4/10/2024 | |
1.1.1 | 126 | 4/9/2024 | |
1.1.0 | 105 | 4/9/2024 | |
1.0.11 | 121 | 4/3/2024 | |
1.0.10 | 125 | 4/2/2024 | |
1.0.9 | 130 | 3/28/2024 | |
1.0.8 | 116 | 3/27/2024 | |
1.0.7 | 109 | 3/27/2024 | |
1.0.6 | 94 | 3/26/2024 | |
1.0.5 | 103 | 3/26/2024 | |
1.0.4 | 111 | 3/26/2024 | |
1.0.3 | 118 | 3/26/2024 | |
1.0.2 | 122 | 3/26/2024 | |
1.0.1 | 122 | 3/26/2024 | |
1.0.0 | 112 | 3/26/2024 |