Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Repository files navigation

Chronicle

42150754

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

masterdevelop
AppVeyorBuild statusBuild status
CodeCovcodecovcodecov

Installation

Chornicle is available on NuGet

Package manager

Install-Package Chronicle_ -Version 3.2.1

.NET CLI

dotnet add package Chronicle_ --version 3.2.1

Getting started

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

  1. Create a class that dervies from either Saga or Saga<TData>.
  2. Inside your saga implemention, inherit from one or several ISagaStartAction<TMessage> and ISagaAction<TMessage> to implement HandleAsync() and CompensateAsync() methods for each message type. An initial step must be implemented as an ISagaStartAction<TMessage>, while the rest can be ISagaAction<TMessage>. It's worth mentioning that you can implement as many ISagaStartAction<TMessage> as you want. In this case, the first incoming message is going to initialize the saga and any subsequent ISagaStartAction<TMessage> or ISagaAction<TMessage> will only update the current saga state.
  3. Register all your sagas in Startup.cs by calling services.AddChronicle(). By default, AddChronicle() will use the InMemorySagaStateRepository and InMemorySagaLog for maintaining SagaState and for logging SagaLogData in the SagaLog. The SagaLog maintains a historical record of which message handlers have been executed. Optionally, AddChronicle() accepts an Action<ChronicleBuilder> parameter which provides access to UseSagaStateRepository<ISagaStateRepository>() and UseSagaLog<ISagaLog>() for custom implementations of ISagaStateRepository and ISagaLog. If either method is called, then both methods need to be called.
  4. Inject ISagaCoordinator and invoke ProcessAsync() methods passing a message. The coordinator will take care of everything by looking for all implemented sagas that can handle a given message.
  5. To complete a successful saga, call CompleteSaga() or CompleteSagaAsync(). This will update the SagaState to Completed. To flag a saga which has failed or been rejected, call the Reject() or RejectAsync() methods to update the SagaState to Rejected. Doing so will utilize the SagaLog to call each message type's CompensateAsync() in the reverse order of their respective HandleAsync() method was called. Additionally, an unhanded exception thrown from a HandleAsync() method will cause Reject() to be called and begin the compensation.

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

publicclassMessage1{publicstringText{get;set;}}publicclassMessage2{publicstringText{get;set;}}publicclassSagaData{publicboolIsMessage1Received{get;set;}publicboolIsMessage2Received{get;set;}}publicclassSampleSaga:Saga<SagaData>,ISagaStartAction<Message1>,ISagaAction<Message2>{publicTaskHandleAsync(Message1message,ISagaContextcontext){Data.IsMessage1Received=true;Console.WriteLine($"Received message1 with message: {message.Text}");CompleteSaga();returnTask.CompletedTask;}publicTaskHandleAsync(Message2message,ISagaContextcontext){Data.IsMessage2Received=true;Console.WriteLine($"Received message2 with message: {message.Text}");CompleteSaga();returnTask.CompletedTask;}publicTaskCompensateAsync(Message1message,ISagaContextcontext)=>Task.CompletedTask;publicTaskCompensateAsync(Message2message,ISagaContextcontext)=>Task.CompletedTask;privatevoidCompleteSaga(){if(Data.IsMessage1Received&&Data.IsMessage2Received){Complete();Console.WriteLine("SAGA COMPLETED");}}}

Both messages are processed by mentioned coordinator:

varcoordinator=app.ApplicationServices.GetService<ISagaCoordinator>();varcontext=SagaContext.Create().WithCorrelationId(Guid.NewGuid()).Build();coordinator.ProcessAsync(newMessage1{Text="Hello"},context);coordinator.ProcessAsync(newMessage2{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

About

Implementation of saga pattern for .NET Core

Resources

Contributing

Stars

397 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages