ChrisMavrommatis.Results
3.0.0
dotnet add package ChrisMavrommatis.Results --version 3.0.0
NuGet\Install-Package ChrisMavrommatis.Results -Version 3.0.0
<PackageReference Include="ChrisMavrommatis.Results" Version="3.0.0" />
paket add ChrisMavrommatis.Results --version 3.0.0
#r "nuget: ChrisMavrommatis.Results, 3.0.0"
// Install ChrisMavrommatis.Results as a Cake Addin #addin nuget:?package=ChrisMavrommatis.Results&version=3.0.0 // Install ChrisMavrommatis.Results as a Cake Tool #tool nuget:?package=ChrisMavrommatis.Results&version=3.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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.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.