Result.Match 1.0.3

dotnet add package Result.Match --version 1.0.3
NuGet\Install-Package Result.Match -Version 1.0.3
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="Result.Match" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Result.Match --version 1.0.3
#r "nuget: Result.Match, 1.0.3"
#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 Result.Match as a Cake Addin
#addin nuget:?package=Result.Match&version=1.0.3

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

Build NuGet Badge

Result

Simple library to assist with handling operation results, success, validation and exceptions. Also includes a match method for easier handling of response.

Getting started

Install package Nuget package

Install-Package Result.Match

Example Usage

Match

return result.Match<IActionResult>(
                (success) => OkObjectResult(success),
                (error) => BadRequest(),
                (validation) => ValidationProblemDetails(validation)
                );

public class ExampleUsage
{
    public Result<TestResponseObject> RealUsage(int number)
    {
        var result = RealResponse(1);

        if(result.IsSuccess)
        {
            return OK();
        }
        else if(result.ErrorType == FailureType.Validation)
        {
            //Show validation
            return Problem(result.ValidationErrors);
        }
        else
        {
            //Log Error
            //_logger.log(result.Error)
            return InternalServerError();
        }
    }

    public Result<TestResponseObject> RealResponse(int number)
    {
        try
        {
            if(number > 0)
            {
                return new TestResponseObject("1");
            }
            else
            {
                return Result<TestResponseObject>.Validation("number", "must be greater than 0");
            }
        }
        catch(Exception ex)
        {
            return Result<TestResponseObject>.Failure(ex);
        }
    }

    public Result<TestResponseObject> Success()
    {
        var result = new TestResponseObject("1");
        return result;
         //{"ValidationErrors":null,"Value":{"Id":"1"},"Error":null,"IsSuccess":true,"ErrorType":0}
    }

    public Result<TestResponseObject> Validation()
    {
        var result = Result<TestResponseObject>.Validation("Id", "Must be greater than 1");
        return result;
        //{"ValidationErrors":{"Id":["Must be greater than 1"]},"Value":null,"Error":null,"IsSuccess":false,"ErrorType":2}
    }

    public Result<TestResponseObject> Exception()
    {
        try
        {
            throw new Exception("Test exception message");
        }
        catch(Exception ex)
        {
            return Result<TestResponseObject>.Failure(ex);
        }
        //{"ValidationErrors":null,"Value":null,"Error":{"StackTrace":"   at OperationResult.Tests.ExampleUsage.Exception() in OperationResultExampleTests.cs:line 61","Exception":{ExceptionObject},"Code":"System.Exception","Message":"Test exception message","Type":1},"IsSuccess":false,"ErrorType":1}
    }

    public Result<TestResponseObject> ErrorResult()
    {
        return Result<TestResponseObject>.Failure("MyBusinessError","You cannot do this");
        // {"ValidationErrors":null,"Value":null,"Error":{"StackTrace":"at ErrorResult in OperationResultExampleTests.cs:line 71","Exception":null,"Code":"MyBusinessError","Message":"You cannot do this","Type":0},"IsSuccess":false,"ErrorType":1}
    }
    
}

public class TestResponseObject
{
    public TestResponseObject(string id)
    {
        Id = id;
    }

    public string Id { get; set; }
}

Buy me a coffee

https://www.buymeacoffee.com/timdoestech?new=1

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 was computed.  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 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.3 269 2/15/2023
1.0.2 241 2/15/2023
1.0.0 257 1/15/2023