GithubHelp home page GithubHelp logo

theluckiestsoul / chronicle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snatch-dev/chronicle

0.0 0.0 0.0 5.05 MB

Implementation of saga pattern for .NET Core

License: MIT License

C# 77.09% PowerShell 16.02% Shell 6.89%

chronicle's Introduction

Chronicle is simple process manager/saga pattern implementation for .NET Core that helps you manage long-living and disitrbuted transactions.

master develop
AppVeyor Build status Build status
CodeCov codecov codecov

Installation

Chornicle is available on NuGet

Package manager

Install-Package Chronicle_ -Version 1.8.0

.NET CLI

dotnet add package Chronicle_ --version 1.8.0

Getting started

In order to create and process saga you need to go through few steps:

  1. Create a class that dervies either from Saga or Saga<TData>.
  2. Inside saga implement particular steps that needs to be done or compensated in case of error. The initial step must be implemented as ISagaStartAction<TMessage> while the rest ISagaAction<TMessage>. It's worth mentioning that up can implement as many start actions as you want. In this case first incomming message is going to initialize saga.
  3. Register all your sagas in Startup.cs by calling services.AddChronicle().
  4. Inject ISagaCoordinator and inoke ProcessAsync() methods passing a message. Cooridnator will take care of everything by looking for all implemented sagas that can handle given message.

Bellow is the very simple example of saga that completes once both messages (Message1 and Message2) are received:

public class Message1
{
    public string Text { get; set; }
}

public class Message2
{
    public string Text { get; set; }
}

public class SagaData
{
    public bool IsMessage1Received { get; set; }
    public bool IsMessage2Received { get; set; }
}

public class SampleSaga : Saga<SagaData>, ISagaStartAction<Message1>, ISagaAction<Message2>
{
    public Task HandleAsync(Message1 message, ISagaContext context)
    {
        Data.IsMessage1Received = true;
        Console.WriteLine($"Received message1 with message: {message.Text}");
        CompleteSaga();
        return Task.CompletedTask;
    }
    
    public Task HandleAsync(Message2 message, ISagaContext context)
    {
        Data.IsMessage2Received = true;
        Console.WriteLine($"Received message2 with message: {message.Text}");
        CompleteSaga();
        return Task.CompletedTask;
    }

    public Task CompensateAsync(Message1 message, ISagaContext context)
        => Task.CompletedTask;

    public Task CompensateAsync(Message2 message, ISagaContext context)
        => Task.CompletedTask;

    private void CompleteSaga()
    {
        if(Data.IsMessage1Received && Data.IsMessage2Received)
        {
            Complete();
            Console.WriteLine("SAGA COMPLETED");
        }
    }
}

Both messages are processed by mentioned coordinator:

var coordinator = app.ApplicationServices.GetService<ISagaCoordinator>();

var context = SagaContext
    .Create()
    .WithCorrelationId(Guid.NewGuid())
    .Build();

coordinator.ProcessAsync(new Message1 { Text = "Hello" }, context);
coordinator.ProcessAsync(new Message2 { Text = "World" }, context);

The result looks as follows:

Result

Documentation

If you're looking for documentation, you can find it here.

Icon

Icon made by Smashicons from www.flaticon.com is licensed by Creative Commons BY 3.0

chronicle's People

Contributors

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