Code Monkey home page Code Monkey logo

action-workflow's Introduction

Introduction

A library for creating workflows based on/splited into multiple actions. The project itself is currently based on .NET Standard 2.0.

Getting Started

Basic Usage

A workflow consists of multiple actions which are each represented as their own class and executed for/in a specific context (T). To create such an action, the class needs to inherit from the interface IAction<T> as shown underneath:

public class CustomAction : IAction<ContextObject>
{
    public Task ExecuteAsync(ContextObject context)
    {
        // Implement your logic here
    }
}

To associate different actions with each other, we then need to create an ActionSequenceFactory<T> and pass all types of the actions for our workflow:

var seqFactory = ActionSequenceFactory<ContextObject>.CreateFactory(
    new List<Type>()
    {
        typeof(CustomAction),
        // ...
    })

// We can also use the corresponding builder to create the factory
var builder = new ActionSequenceFactoryBuilder<ContextObject>();
builder.AddAction<CustomAction>();
// ...

seqFactory = builder.ToFactory();

This factory allows us to create as many instances of your workflow as we wish, completely independent from each other:

var sequence = seqFactory.Create();

Finally, to execute the created workflow, we need to call the ExecuteAsync method on it:

// Create some context object which gets passed to all actions
var context = new ContextObject();

// Execute the workflow/action-sequence
ActionSequenceExecutionResult result = await sequence.ExecuteAsync(context);

Dependency Injection

Actions can make use of the built-in dependency-injection through their constructor:

public CustomAction(ISomeService someService)
{
    _someService = someService;
}

To provide your own services, an IServiceProvider can be passed when creating a ActionSequence<T> from a ActionSequenceFactory<T>:

var sequence = seqFactory.Create(serviceProvider);

Note: There is currently one built-in service, named IActionContext. The service is scoped to the current action and provides the ability to access functions (e.g. export of objects) and informations (e.g. name or description) about it.

Exports/Imports

As noted above, actions can export objects (using the IActionContext service) and also import these from previous actions. Here's a simple example of an export:

public class CustomAction : IAction<ContextObject>
{
    private readonly IActionContext _context;

    public CustomAction(IActionContext context)
    {
        _context = context;
    }
    
    public Task ExecuteAsync(ContextObject context)
    {
        // Export the object
        _context.Export(new SomeActionExport()
        {
            // ...
        });
        
        return Task.CompletedTask;
    }
}

Now, to import SomeActionExport from another action, we need to specify the import in the constructor and add the FromImport attribute before the parameter. This attribute indicates (to the underlying dependency-injection) that we want to import this object from a previous action:

public AnotherCustomAction([FromImport] SomeActionExport import)
{
    // ...
}

Imports also introduces the dependency of different actions on each other. Actions without imports will be executed first, then the next ones which are possible with the exports of the last action(s), etc.

Note: Circular dependencies between multiple actions are not supported! If the workflow/action-sequence cannot be fully executed, ActionSequenceExecutionResult.Partial is returned.

Issues

When encountering any bugs/problems feel free to create an issue.

action-workflow's People

Contributors

ieeax avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.