Skip to content

Latest commit

 

History

History
40 lines (35 loc) · 1.32 KB

README.md

File metadata and controls

40 lines (35 loc) · 1.32 KB

Either

Nuget GitHub GitHub last commit (branch)

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

Available Variations

public struct Either<T, TError> {}
public struct Either<T, TError1, TError2> {}
public struct Either<T, TError1, TError2, TError3> {}
public struct Either<T, TError1, TError2, TError3, TError4> {}

Example using the built in Resolve method

public IActionResult Get(Guid id) => 
   _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()
    };
}