Skip to content

.Net library to orchestrate operations and commands. Helps to follow Single Responsibility Pattern

License

Notifications You must be signed in to change notification settings

joymon/Orchestration

Repository files navigation

Area Badges
Build Build status
Code GitHub code size in bytes GitHub repo size
Code Quality Total alerts
Security Known Vulnerabilities
Test Testcodecov
Issues Average time to resolve an issue Percentage of issues still open
Deployment Build statusBuild status

Orchestration

.Net library to orchestrate operations and commands. Helps to follow Single Responsibility Pattern

When to use this

  • You have some data/context and needs to perform series of operations on it.
  • Where operations are independent.
  • If you are ok on dealing with a state. Here in this case the context.
  • If you are struck with .Net 4.0. On 4.5 there is Banzai nuget library which can do things in async way.

How to use

Goto nuget package manager console and run the below command to get the package added to the project.

install-package Orchestration

Simple usage

IOperationOrchestrator<int> orchestrator = new OperationOrchestrator<int>(new List<IOperation<int>>() { new FindSquare() });
orchestrator.Start(10);       
public class FindSquare : IOperation<int>
{
        void IOperation<int>.Execute(int context)
        {
                Console.WriteLine("Square of {0} is {1}", context, context * context);
        }
}

Output: Square of 10 is 100

Working with 2 operations

IOperationOrchestrator<int> orchestrator = new OperationOrchestrator<int>(new List<IOperation<int>>()
        {
                new FindSquare(),
                new FindSquareRoot()
        });
orchestrator.Start(16);
internal class FindSquareRoot : IOperation<int>
{
    void IOperation<int>.Execute(int context)
    {
        Console.WriteLine("SquareRoot of {0} is {1}", context, Math.Sqrt(context));
    }
}
public class FindSquare : IOperation<int>
{
    void IOperation<int>.Execute(int context)
    {
        Console.WriteLine("Square of {0} is {1}", context, context * context);
    }
}

Output : Square of 9 is 81

SquareRoot of 9 is 3

Using context to communicate between operation steps

Instead of feeding the output from one step to another step, its kept in the context object / state.

internal class CalculationContext
{
    public int[] Numbers { get; set; }
    public int Sum { get; set; }
    public int Average { get; set; }
}
IOperationOrchestrator<CalculationContext> orchestrator = new OperationOrchestrator<CalculationContext>(
    new List<IOperation<CalculationContext>>() {
        new FindSumOperation(),
        new FindAverageOperation()
    });
CalculationContext context = new CalculationContext() { Numbers = new int[] { 1, 2, 3, 6 } };
orchestrator.Start(context);
Console.WriteLine("Sum={0},Average={1}", context.Sum, context.Average);
internal class FindSumOperation : IOperation<CalculationContext>
{
    void IOperation<CalculationContext>.Execute(CalculationContext context)
    {
        context.Sum = context.Numbers.Sum();
    }
}
internal class FindAverageOperation : IOperation<CalculationContext>
{
    void IOperation<CalculationContext>.Execute(CalculationContext context)
    {
        context.Average = context.Sum / context.Numbers.Length;
    }
}

Output : Sum=12,Average=3

About

.Net library to orchestrate operations and commands. Helps to follow Single Responsibility Pattern

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages