Skip to content

Latest commit

 

History

History
53 lines (44 loc) · 1.63 KB

README.md

File metadata and controls

53 lines (44 loc) · 1.63 KB

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();