ChrisMavrommatis.Results
2.0.2
dotnet add package ChrisMavrommatis.Results --version 2.0.2
NuGet\Install-Package ChrisMavrommatis.Results -Version 2.0.2
<PackageReference Include="ChrisMavrommatis.Results" Version="2.0.2" />
paket add ChrisMavrommatis.Results --version 2.0.2
#r "nuget: ChrisMavrommatis.Results, 2.0.2"
// Install ChrisMavrommatis.Results as a Cake Addin #addin nuget:?package=ChrisMavrommatis.Results&version=2.0.2 // Install ChrisMavrommatis.Results as a Cake Tool #tool nuget:?package=ChrisMavrommatis.Results&version=2.0.2
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 | Versions 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. |
-
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.