Chronicle is simple process manager/saga pattern implementation for .NET Core that helps you manage long-living and distirbuted transactions.
| master | develop | |
|---|---|---|
| AppVeyor | ||
| CodeCov |
Chornicle is available on NuGet
Install-Package Chronicle_ -Version 3.2.1dotnet add package Chronicle_ --version 3.2.1In order to create and process a saga you need to go through a few steps:
- Create a class that dervies from either
SagaorSaga<TData>. - Inside your saga implemention, inherit from one or several
ISagaStartAction<TMessage>andISagaAction<TMessage>to implementHandleAsync()andCompensateAsync()methods for each message type. An initial step must be implemented as anISagaStartAction<TMessage>, while the rest can beISagaAction<TMessage>. It's worth mentioning that you can implement as manyISagaStartAction<TMessage>as you want. In this case, the first incoming message is going to initialize the saga and any subsequentISagaStartAction<TMessage>orISagaAction<TMessage>will only update the current saga state. - Register all your sagas in
Startup.csby callingservices.AddChronicle(). By default,AddChronicle()will use theInMemorySagaStateRepositoryandInMemorySagaLogfor maintainingSagaStateand for loggingSagaLogDatain theSagaLog. TheSagaLogmaintains a historical record of which message handlers have been executed. Optionally,AddChronicle()accepts anAction<ChronicleBuilder>parameter which provides access toUseSagaStateRepository<ISagaStateRepository>()andUseSagaLog<ISagaLog>()for custom implementations ofISagaStateRepositoryandISagaLog. If either method is called, then both methods need to be called. - Inject
ISagaCoordinatorand invokeProcessAsync()methods passing a message. The coordinator will take care of everything by looking for all implemented sagas that can handle a given message. - To complete a successful saga, call
CompleteSaga()orCompleteSagaAsync(). This will update theSagaStateto Completed. To flag a saga which has failed or been rejected, call theReject()orRejectAsync()methods to update theSagaStateto Rejected. Doing so will utilize theSagaLogto call each message type'sCompensateAsync()in the reverse order of their respectiveHandleAsync()method was called. Additionally, an unhanded exception thrown from aHandleAsync()method will causeReject()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:
If you're looking for documentation, you can find it here.
Icon made by Smashicons from www.flaticon.com is licensed by Creative Commons BY 3.0

