Resilient execution flow with H.Necessaire via OperationResult & safe execution extensions

H.Necessaire offers a basic and simple resilience execution model via two paradigms. OperationResult & TryOrFailWithGrace.

Resilient execution flow with H.Necessaire via OperationResult & safe execution extensions
Photo by Jametlene Reskp / Unsplash

H.Necessaire offers a basic and simple resilience execution model via two paradigms:

  • OperationResult
  • TryOrFailWithGrace()

Summary

Resilient execution means simply no crashes, no thrown exceptions. Error handling is part of the normal execution flow and is handled as part of the execution logic. It's not an exception.

With this in mind, below is the simple mechanism that H.Necessaire offers for it.

Usage

dotnet add package H.Necessaire

H.Necessaire

static async Task<OperationResult> DoSomeWorkAlignedWithResilientPattern(ImALogger logger)
{
    if (logger is null)
        return OperationResult.Fail($"{nameof(logger)} is null. A logger must be provided.");

    await logger.LogInfo("Doing some work");

    return OperationResult.Win();
}

Resilient pattern implementation with explicit OperationResult creation

static async Task<OperationResult> WrapSomeWorkWitResilientPattern(ImALogger logger)
{
    OperationResult result = OperationResult.Fail("Work not yet started");

    await new Func<Task>(async () =>
    {
        await logger.LogInfo("Doing some work");
    })
    .TryOrFailWithGrace(
        onFail: ex => result = OperationResult.Fail(ex, $"Error occurred while trying to do work. Reason: {ex.Message}.")
    );

    return result;
}

"Lazy person" resilient pattern implementation with safe execution wrapping via TryOrFailWithGrace

static async Task Main(string[] args)
{
    OperationResult resultA = await DoSomeWorkAlignedWithResilientPattern(logger: null);
    OperationResult resultB = await WrapSomeWorkWitResilientPattern(logger: null);

    OperationResult[] allResults = [resultA, resultB];

    OperationResult globalResult = allResults.Merge();

    //Implement flow based on OperationResult.IsSuccessful
}

Consuming OperationResult

H.Necessaire.Usage.Samples/Src/H.Necessaire.Samples/H.Necessaire.Samples.Resilience/Program.cs at master · hinteadan/H.Necessaire.Usage.Samples
Usage Samples for H.Necessaire Docs WebSite. Contribute to hinteadan/H.Necessaire.Usage.Samples development by creating an account on GitHub.