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.
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> {}
public IActionResult Get(Guid id) =>
_repository.User(id).Resolve<IActionResult>(
user => Ok(user),
error => NotFound()
);
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()
};
}