Skip to content

A .NET library to easily create Compensating Transactions

Notifications You must be signed in to change notification settings

steffan267/NCompensate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NCompensate

A .NET library to easily create Compensating Transactions

https://docs.microsoft.com/en-us/azure/architecture/patterns/compensating-transaction

Below are three examples on how to achieve the same behavior with NCompensate:

Example 1 - inline call backs:

await Transaction.New()
    .Add(
        async () => await ExternalService.Post("/courses", model), 
        async (d) => await ExternalService.Remove("/courses", d.Id))
    .AddConditionally(
        model.AllocateRoom,
        async () => await ExternalService.Post("/courses/allocate", model.Id))
    .InvokeWithCompensationAsync();

Example 2 - Injectable ICompensables:

ICompensable createCourse = 
    new Compensable<Course>(async () => await ExternalService.Post("/courses", model))
        .WithCompensation(async (d) => await ExternalService.Remove("/courses", d.Id));

ICompensable allocateRoom =
    new Compensable<int>(async () => await ExternalService.Post("/courses/allocate", model.Id));

await Transaction.New()
    .Add(createCourse)
    .AddConditionally(
        model.AllocateRoom,
        allocateRoom)
    .InvokeWithCompensationAsync();

Example 3 - Hybrid:

var createCourse =
    new Compensable<Course>(
        async () => await ExternalService.Post("/courses", model),
        async (d) => await ExternalService.Remove("/courses", d.Id));

ICompensable allocateRoom =
    new Compensable<int>(async () => await ExternalService.Post("/courses/allocate", model.Id));

await Transaction.New()
    .Add(createCourse)
    .AddConditionally(
        model.AllocateRoom,
        allocateRoom)
    .InvokeWithCompensationAsync();

About

A .NET library to easily create Compensating Transactions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages