ErgodicMage.Result 0.5.0

dotnet add package ErgodicMage.Result --version 0.5.0                
NuGet\Install-Package ErgodicMage.Result -Version 0.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ErgodicMage.Result" Version="0.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ErgodicMage.Result --version 0.5.0                
#r "nuget: ErgodicMage.Result, 0.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ErgodicMage.Result as a Cake Addin
#addin nuget:?package=ErgodicMage.Result&version=0.5.0

// Install ErgodicMage.Result as a Cake Tool
#tool nuget:?package=ErgodicMage.Result&version=0.5.0                

ErgodicMage Result

A simple implementations of the Result Pattern, Guard Clauses and using Result to reduce exceptions.

Result Types

public readonly record struct Result (bool Success, string ErrorMessage, Exception? Exception)
public readonly record struct Result<T> (bool Success, string ErrorMessage, Exception? Exception, T Value)

Example

private readonly record struct Workflow(string Name, int Jobs);

public async Task<Result<string>> DoWorkflow(string? workflow, CancellationToken token = default)
{
    Result result = GuardClause.NullOrWhiteSpace(workflow);
    if (!result.Success) return result;

    Result<Workflow> resultWorkflow = await GetWorkflow(workflow!, token);
    if (!resultWorkflow.Success) return Result.Error($"Workflow {workflow} not found");

    return await Result<string>.TryAsync(async (Workflow workflow, CancellationToken token) =>
    {
        return await DoWork(workflow, token);
    },
    resultWorkflow.Value, token);
}

private static async Task<Result<Workflow>> GetWorkflow(string workflow, CancellationToken? token = default)
    => new Workflow(workflow, 1);

private static async Task<Result<string>> DoWork(Workflow workflow, CancellationToken? token = default)
{
    Result result = GuardClause.Null(workflow);
    if (!result.Success) return result;
    await Task.Delay(1000);
    return workflow.Name;
}

Guard Clauses

Null and empty string guard clauses

public static Result Null(object? input, string? parameterName = null, string? message = null)
public static Result Null(this Result result, object? input, string? parameterName = null, string? message = null)
public static Result NullOrEmpty(string? input, string? parameterName = null, string? message = null)
public static Result NullOrEmpty(this Result result, string? input, string? parameterName = null, string? message = null)
public static Result NullOrWhiteSpace(string? input, string? parameterName = null,  string? message = null)

Minumum and Maximum for any IComparable. Built in type provided: short, int, long, float, double and decimal.

public static Result Minimum<T>(T input, T minimum, string? parameterName = null, string? message = null) where T : struct, IComparable
public static Result Minimum<T>(this Result result, T input, T maximum, string? parameterName = null, string? message = null) where T : struct, IComparable
public static Result Maximum<T>(T input, T maximum, [CallerArgumentExpression(nameof(input))] string? parameterName = null, string? message = null) where T : struct, IComparable
public static Result Maximum<T>(this Result result, T input, T maximum, string? parameterName = null, string? message = null) where T : struct, IComparable

Result exception handlers

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.