Option.Either 0.0.1

A simple library that adds a struct to be used as a discriminated union like return type.

Install-Package Option.Either -Version 0.0.1
dotnet add package Option.Either --version 0.0.1
<PackageReference Include="Option.Either" Version="0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Option.Either --version 0.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Either

Creates a simple struct that can be used as discriminated union like return type, supporting the successful result as well as up to four additonal exceptions that can be returned.

Example using the built in Resolve method

public IActionResult Get(Guid id)
{
   return  _repository.User(id)
                      .Resolve<IActionResult>((
                          user => Ok(user),
                          error => NotFound()
                      ));
}

Example using C# pattern mathcing

public IActionResult Post([FromBody] UserCreateRequest request)
{
    var result = _repository.Create(request.Email, request.Name);
            
    return result.Value switch
    {
        Guid guid =>  Created(guid.ToString(), guid),
        ArgumentException ex => BadRequest(new { ex.Message, request }),
        InvalidDataContractException ex => BadRequest(new { ex.Message, request }),
        _ => ServerError()
    };
}

Either

Creates a simple struct that can be used as discriminated union like return type, supporting the successful result as well as up to four additonal exceptions that can be returned.

Example using the built in Resolve method

public IActionResult Get(Guid id)
{
   return  _repository.User(id)
                      .Resolve<IActionResult>((
                          user => Ok(user),
                          error => NotFound()
                      ));
}

Example using C# pattern mathcing

public IActionResult Post([FromBody] UserCreateRequest request)
{
    var result = _repository.Create(request.Email, request.Name);
            
    return result.Value switch
    {
        Guid guid =>  Created(guid.ToString(), guid),
        ArgumentException ex => BadRequest(new { ex.Message, request }),
        InvalidDataContractException ex => BadRequest(new { ex.Message, request }),
        _ => ServerError()
    };
}

Release Notes

Logo: Pat Hawks / CC BY-SA (https://creativecommons.org/licenses/by-sa/4.0)

  • .NETStandard 2.1

    • 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 History

Version Downloads Last updated
0.0.1 129 8/20/2020