ChrisMavrommatis.Results 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ChrisMavrommatis.Results --version 2.0.0                
NuGet\Install-Package ChrisMavrommatis.Results -Version 2.0.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="ChrisMavrommatis.Results" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ChrisMavrommatis.Results --version 2.0.0                
#r "nuget: ChrisMavrommatis.Results, 2.0.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 ChrisMavrommatis.Results as a Cake Addin
#addin nuget:?package=ChrisMavrommatis.Results&version=2.0.0

// Install ChrisMavrommatis.Results as a Cake Tool
#tool nuget:?package=ChrisMavrommatis.Results&version=2.0.0                

Results

ChrisMavrommatis.Results is a .NET library designed to provide utility types for handling results and union types in a clean and expressive manner.

Installation

Install the package via NuGet Package Manager

Install-Package ChrisMavrommatis.Results

Or via .NET CLI:

dotnet add package ChrisMavrommatis.Results

Usage

The library includes two main result types:

  • Result<TValue>
  • OneOf<...>

Result

The Result<TValue> class represents the outcome of an operation, encapsulating both success and failure states while containing the successful result's object.

Creating a Successful Result

var successResult = Result<int>.Successful(42);
Console.WriteLine(successResult.Success); // True
Console.WriteLine(successResult.Value);   // 42

Creating a Failed Result

var failedResult = Result<int>.Failed("An error occurred");
Console.WriteLine(failedResult.Success);  // False
Console.WriteLine(failedResult.Message);  // "An error occurred"

Void Result

The Result class represents the outcome of an operation without containing an object, useful for operations that don't return a value.

OneOf

The OneOf<...> struct represents a value that can be one of several possible types.

Supported OneOf types:

  • OneOf<T0, T1>
  • OneOf<T0, T1, T2>
  • OneOf<T0, T1, T2, T3>

Creating a OneOf Instance

public async Task<OneOf<User, Conflict, Error>> CreateAsync(CreateUserRequest request, CancellationToken cancellationToken = default)
{
    if (request is null || string.IsNullOrWhiteSpace(request.Email) || string.IsNullOrWhiteSpace(request.Password))
    {
        return new Error("Invalid Credentials");
    }

    // Other code

    var getResult = await this.userRepository.GetAsync(request.Email, cancellationToken);

    if (getResult.IsSuccess)
    {
        return new Conflict("User already exists");
    }

    // Other code

    var createResult = await this.userRepository.CreateAsync(user, cancellationToken: cancellationToken);
    if (!createResult.IsSuccess)
    {
        return new Error("Could not create user");
    }

    return user;
}

Unwrapping a OneOf Instance

Unwrapping is the preferred way to handle OneOf instances:

var result = await this.userManager.CreateAsync(request, cancellationToken);
var actionResult = result.Unwrap(
    user => this.Ok(user),
    conflict => this.Conflict(),
    error => this.BadRequest(error.Message)
);
return actionResult;

Checking the Result

Sometimes you need to check what type of result it is:

public async Task<OneOf<Ok, NotFound, Conflict, Error>> ChangePasswordAsync(ChangeUserPasswordRequest request, CancellationToken cancellationToken)
{
    // Other code

    var getResult = await this.userRepository.GetAsync(request.Email, cancellationToken);

    if (!getResult.Is<User>())
    {
        // Handle result
    }

    // Other code

    var updateResult = await this.userRepository.UpdateAsync(user, cancellationToken);

    if (!updateResult.Is<Ok>())
    {
        // Handle result
    }

    // Other code
}

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.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
3.0.0 70 11/18/2024
2.0.0 77 11/13/2024
1.0.1 242 7/23/2024
1.0.0 71 7/23/2024