Coder Social home page Coder Social logo

orchesflow's Introduction

Orchesflow

This package provides some extra tools to be used along MediatR + EntityFramework focused to make working with events easier.

Ochestrator flow:

Orchesflow diagram

Using Ochesflow

To enable Orchesflow just add it to your ConfigureServices() method at Startup.cs

services.AddOrchesflow<YourDbContext>();

Sending a Command or Query

using Orchesflow.Orchestration

public class MyController {
  private readonly IOrchestrator _orchestrator;

  public MyController(IOrchestrator orchestrator) {
    _orchestrator = orchestrator
  }

  public async Task < IActionResult > ([FromBody] MyCommand command) {
    // Use SendCommand to send a command
    // A command triggers PreCommitEvents, Database commit and AfterCommitEvents
    // For Queries the method SendQuery() should be used
    // SendQuery() does not trigger events nor database commit
    var response = await _orchestrator.SendCommand(command);
    if (respose.Success) 
       return Ok(response.Data);
    else
       return BadRequest(response.Messages);
  }
}

EventDispatcher

EventDispatcher

The event dispatcher is the responsible to dispatch PreCommitEvents and AfterCommitEvents. This interface (IEventDispatcher) provides methods to add, remove, list or manualy event trigger (not recommended). The event dispatcher is the responsible to dispatch PreCommitEvents and AfterCommitEvents. This interface (IEventDispatcher) provides methods to add, remove, list or manualy event trigger (not recommended).

Available methods for PreCommitEvents
Available methods

GetPreCommitEvent(INotification evt) => Returns a list with all PreCommitEvent

GetPreCommitEvent(INotification evt) => Returns a list with all PreCommitEvent

AddPreCommitEvent(INotification evt) => Adds new PreCommitEvent

AddPreCommitEvent(INotification evt) => Adds new PreCommitEvent

RemovePreCommitEvent(INotification evt) => Remove an existing PreCommitEvent

RemovePreCommitEvent(INotification evt) => Remove an existing PreCommitEvent FirePreCommitEvents(INotification evt) => Manualy fire PreCommitEvent (not recommended)

FirePreCommitEvents(INotification evt) => Manualy fire PreCommitEvent (not recommended)

Available methods for PreCommitEvents

GetAfterCommitEvent(INotification evt) => Returns a list with all AfterCommitEvent

GetAfterCommitEvent(INotification evt) => Returns a list with all AfterCommitEvent

AddAfterCommitEvent(INotification evt) => Adds new AfterCommitEvent

AddAfterCommitEvent(INotification evt) => Adds new AfterCommitEvent

RemoveAfterCommitEvent(INotification evt) => Remove an existing AfterCommitEvent

RemoveAfterCommitEvent(INotification evt) => Remove an existing AfterCommitEvent

FireAfterCommitEvents(INotification evt) => Manualy fire AfterCommitEvent (not recommended)

The EventDispatcher functionality is only available when using SendCommand()

Commit

SaveChanges() method is called whenever MediatR CommandHandler finishes its job, unless any notification is found at the DomainNotifications container. The Commit will only be evoked when using SendCommand()

PreCommitEvents

PreCommitEvents are MediatR events (INotification) that will be triggered before commit occurs. An unlimited number of PreCommitEvents can be added to EventDispatcher. To add a PreCommitEvent use IEventDispatcher.AddPreCommitEvents()

AfterCommitEvents

AfterCommitEvents are MediatR events (INotification) that will be triggered after the commit occurs and if no notification is found at DomainNotifications container.. An unlimited number of PreCommitEvents can be added to EventDispatcher.

DomainNotifications container

This container intention is to store any errors that occurs during the request lifecycle and provide easy access to them from any part of your code. It is important to note that Commit action, AfterCommitEvents and the flow return depends if there is or there is not messages at DomainNotifications container. This funcionality should be used whenever you want to prevent the Commit to happen or prevent AfterCommitEvents to be triggered.

Available methods for IDomainNotifications interface

AddNotification(string notification) => Adds a new notification to container

void CleanNotifications() => Remove all notifications from container

Fallbacks

Sometimes things just go wrong, in this case you can use fallbacks to go back and undo what you did. To enable fallbacks, implement the interface IFallbackable at your RequestHandler or Notification Handler and put your logic inside Fallback() method.

There are three key moments for fallbacks.

  • An error occurred during a PreCommitEvent: All PreCommitEvents already executed will have its fallback method called in the reverse order they were executed

  • An error occurred during Commit phase: The calling handler will have its Fallback method triggered and also all PreCommitEvents executed will have its fallback method called in the reverse order they were executed

  • An error occurred during a AfterCommitEvent: All PreCommitEvents already executed will have its Fallback method triggered, Handler fallback and PreCommitEvents fallbacks will also be triggered in the reverse worder they were executed.

orchesflow's People

Contributors

andresantarosa avatar jeffward01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  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.