Code Monkey home page Code Monkey logo

masstransit.eventstoreintegration's Introduction

MassTransit.EventStore

EventStore audit store for MassTransit

NuGet Build Status

Installation

The library is published on nuget.org.

Use Install-Package MassTransit.EventStoreIntegration to install it.

Usage

This library provides two types of persistence:

  • Audit persistence
  • Event-sourced saga persistence

Audit

To use EventStore as audit store in MassTransit, use the following code:

// initialise ES connection
var eventStoreConnection = EventStoreConnection.Create(connectionString);
await eventStoreConnection.ConnectAsync();

// initialise the store
var store = new EventStoreMessageAudit(eventStoreConnection, "audit-stream-name");

// configure the bus
var bus = ....;
bus.ConnectConsumeAuditObserver(store);
bus.ConnectSendAuditObservers(store);

await bus.StartAsync();

Hence that ConnectSendAuditObservers connects both sending and publishing observers.

Event-Sourced Saga

You can make your saga instances event-sourced and use EventStore to persist them. This will allow you to see the full history of your processed, which is great for tracking and tracing, also gives you a complete audit log for your workflows.

First, you need to make your instances event-sourced. This requires that all state changes are done as event handlers. Here is an example of such instance class:

public class SampleInstance : EventSourcedSagaInstance, SagaStateMachineInstance
{
    public SampleInstance(Guid correlationId) : this()
    {
        CorrelationId = correlationId;
    }

    private SampleInstance()
    {
        Register<ProcessStarted>(x => OrderId = x.OrderId);
        Register<OrderStatusChanged>(x => OrderStatus = x.OrderStatus);
    }

    public string OrderStatus { get; private set; }
    public string OrderId { get; private set; }
}

As you can see, at least two things need to be done:

  • Inherit your instance from the EventSourcedSagaInstance abstract class
  • Register event handlers for all state changes

The base class provides the default InitialState property of type string, so you do not need to add it.

Sample state machine, which uses this instance, looks like this:

public class SampleStateMachine : MassTransitStateMachine<SampleInstance>
{
    public SampleStateMachine()
    {
        InstanceState(x => x.CurrentState);

        Event(() => Started,
            x => x.CorrelateById(e => e.Message.CorrelationId).SelectId(e => e.Message.CorrelationId));
        Event(() => Stopped, x => x.CorrelateById(e => e.Message.CorrelationId));
        Event(() => StatusChanged, x => x.CorrelateById(e => e.Message.CorrelationId));

        Initially(
            When(Started)
                .Then(c => c.Instance.Apply(c.Data))
                .TransitionTo(Running));

        During(Running,
            When(StatusChanged)
                .Then(c => c.Instance.Apply(c.Data)),
            When(Stopped)
                .TransitionTo(Done)
                .Finalize());
    }

    public State Running { get; private set; }
    public State Done { get; private set; }
    public Event<ProcessStarted> Started { get; private set; }
    public Event<ProcessStopped> Stopped { get; private set; }
    public Event<OrderStatusChanged> StatusChanged { get; private set; }
}

You can see that in each When we need to call the ApplyChange method to trigger the instance event handler, which changes the instance state. All changes are then saved as separate events.

The last step is to tell MassTransit to use the EventStore repository:

var connection = EventStoreConnection.Create(connectionString);
var repository = new EventStoreSagaRepository<SampleInstance>(connection);

var bus = Bus.Factory.CreateUsingRabbitMq(c =>
    {
        var host = c.Host(new Uri("rabbitmq://localhost"), h =>
        {
            h.Username("guest");
            h.Password("guest");
        });

        var machine = new SampleStateMachine();
        c.ReceiveEndpoint(host, "essaga_test", 
            ep => ep.StateMachineSaga(machine, repository));
    });

Working sample is available in this repository, in the MassTransit.EventStoreIntegration.Sample project.

The full lifecycle of this saga is then looks like this in the EventStore:

alt text

masstransit.eventstoreintegration's People

Contributors

alexeyzimarev avatar

Watchers

James Cloos 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.