If you want to create a pipeline looks like MiddleWares in the Dotnet core you should use the chain of responsibility design pattern. To ease the implementation I have created a Nuget library in order to easily create a pipeline for your application by using the chain of responsibility and builder patterns together.
Find the source code here
Let's say we are going to handle the logic of a coffee shop, the steps are
- Taking the customer's order
- receiving the payment
- making the coffee
Therefore you can write something simple like this
var order = new OrderData()
{
Name = "Coffee",
State = "None"
};
await new Pipeline()
.Next(new OrderHandler())
.Next(new CheckoutHandler())
.Next(new ProducingHandler())
.Run(order);
In above scenario the order object will travel through all the handlers starting from the OrderHandler and then CheckoutHandler and so on.
The order object must implement the IPipelineData interface
public class OrderData : IPipelineData
{
public string Name { get; set; }
public string State { get; set; }
}
and every WorkStation must inherit from WorkStation abstract class
public class OrderWorkStation : WorkStation
{
[AutoRetry(attempts: 3)]
protected override async Task InvokeAsync(IPipelineData data)
{
if(!(data is OrderData order)) throw new ArgumentNullException();
order.State = nameof(OrderHandler);
Console.WriteLine($"State:{nameof(OrderHandler)} objectState: " +
$"{JsonConvert.SerializeObject(order)}");
return Task.CompletedTask;
}
}
As it can be seen by defining the AutoRetry attribute you can ask the pipeline to retry this workstation if it fails
Find the nuget here
Find the source code here