mvSapphire / PowerPipe

A library for .NET that uses a fluent interface to construct advanced workflows with ease.
MIT License
193 stars 10 forks source link
builder c-sharp chain chain-of-responsibility dotnet-core pipe pipeline powerpipe workflow workflow-engine

drawing

GitHub Workflow Status (with event) badge Nuget Nuget

A .NET Library for Constructing Advanced Workflows with Fluent Interface

PowerPipe is a versatile .NET library designed to streamline the process of building advanced workflows using a fluent interface. The primary objective of this project is to eliminate the need for writing boilerplate code when implementing workflows.

Check out Medium article 👀

If you like this project give it a star 🌟

🔥 Features and Benefits

🧐 Sample use case

Imagine creating an e-commerce platform. The platform must process incoming customer orders, each demanding validation, inventory updates, and potentially more intricate steps.

public  class  ECommercePipelineService : IECommercePipelineService
{
    private  readonly  IPipelineStepFactory _pipelineStepFactory;

    private bool PaymentSucceed(ECommerceContext context) => context.PaymentResult.Status is PaymentStatus.Success;

    public  ECommercePipelineService(IPipelineStepFactory pipelineStepFactory)
    {
        _pipelineStepFactory  =  pipelineStepFactory;
    }

    public IPipeline<OrderResult> BuildPipeline()
    {
        var context = new ECommerceContext();

        return new PipelineBuilder<ECommerceContext, OrderResult>(_pipelineStepFactory, context)
            .Add<OrderValidationStep>()
            .Add<PaymentProcessingStep>()
                .OnError(PipelineStepErrorHandling.Retry,  retryInterval:  TimeSpan.FromSeconds(2), maxRetryCount: 2)
            .If(PaymentSucceed, b => b
                .Add<OrderConfirmationStep>()
                .Add<InventoryReservationStep>())
            .Parallel(b => b
                .Add<CustomerNotificationsStep>()
                .Add<AnalyticsAndReportingStep>(), maxDegreeOfParallelism: 2)
            .Build();
    }
}

🤩 Workflow visualization

Sometimes workflows could be too big to track what is happening.

That's why we created a workflow visualization tool. This tool was designed with simplicity in mind. You have to add just few lines of code to make this work!

Install required packages

Usage

In your Startup/Program file add required services and register middleware:

builder.Services.AddPowerPipeVisualization(o => o.ScanFromType(typeof(ECommercePipelineService)));

// ...

app.UsePowerPipeVisualization();

Then start you application and navigate to /powerpipe endpoint.

And workflow from the sample above parsed to this beautiful diagram. 🌟

drawing

Note! This is the very first version of workflow visualization! Looking forward to your feedback 🤗

Known issues:

  • OnError parsing could lead to missing steps on the diagram
  • Double links between entities

🛠️ Getting started

Installation

Building pipeline

  1. Create pipeline context and result
public class SampleContext : PipelineContext<SampleResult>  
{  
    // Properties and methods specific to the context  
}

public class SampleResult
{
    // Implementation details
}
  1. Create pipeline steps
public class SampleStep1 : IPipelineStep<SampleContext>  
{  
    // Implementation details…
}

public  class  SampleStep2 : IPipelineStep<OrderContext>  
{  
    // Implementation details…
}
  1. Define your pipeline
var pipeline = new PipelineBuilder<OrderProcessingContext, Order>()
    .Add<SampleStep1>()
    .Add<SampleStep2>()
    .Build();
// Define predicate based on context
private bool ExecuteStep2(OrderProcessingContext context) =>
    context.ExecuteStep2Allowed;

var pipeline = new PipelineBuilder<OrderProcessingContext, Order>()
    .Add<SampleStep1>()
    .AddIf<SampleStep2>(ExecuteStep2) 
    .Build();
private bool ExecuteStep2(OrderProcessingContext context) =>
    context.ExecuteStep2Allowed;

var pipeline = new PipelineBuilder<OrderProcessingContext, Order>()
    .AddIfElse<SampleStep1, SampleStep2>(ExecuteStep2) 
    .Build();
private bool ExecuteNestedPipeline(OrderProcessingContext context) =>
    context.ExecuteNestedPipelineAllowed;

var pipeline = new PipelineBuilder<OrderProcessingContext, Order>()
    .If(ExecuteNestedPipeline, b => b
        .Add<SampleStep1>()
        .Add<SampleStep2>())
    .Build();

In order to execute steps in parallel your steps should implement IPipelineParallelStep<TContext> interface

var pipeline = new PipelineBuilder<OrderProcessingContext, Order>()
    .Parallel(b => b  
        .Add<SampleParallelStep1>()
        .Add<SampleParallelStep2>(), maxDegreeOfParallelism: 3)
    .Build();

Currently available only two types of error handling Suppress and Retry

var pipeline = new PipelineBuilder<OrderProcessingContext, Order>()
    .Add<SampleStep1>()
        .OnError(PipelineStepErrorHandling.Retry)
    .Build();

Compensation steps should implement IPipelineCompensationStep<TContext>

public class SampleStep1Compensation : IPipelineCompensationStep<SampleContext> {}

var pipeline = new PipelineBuilder<OrderProcessingContext, Order>()
    .Add<SampleStep1>()
        .CompensateWith<SampleStep1Compensation>()
    .Build();
  1. Extensions: Microsoft Dependency Injection

The PowerPipe.Extensions.MicrosoftDependencyInjection extension provides integration with Microsoft Dependency Injection.

public static IServiceCollection AddPowerPipe(
    this IServiceCollection serviceCollection,
    PowerPipeConfiguration configuration)

By default all found implementations will be registered as Transient.

services.AddPowerPipe(cfg =>
{
    cfg.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly());
});

But you can configure service lifetime per step implementation.

services.AddPowerPipe(cfg =>
{
    cfg.RegisterServicesFromAssemblies(typeof(Program).Assembly)
        .ChangeStepsDefaultLifetime(ServiceLifetime.Scoped)
        .AddSingleton<Step1>()
        .AddTransient<Step2>()
        .AddTransient(typeof(Step2));
});

Check out sample project 👀