DrifterApps.Seeds.FluentResult
0.1.12
dotnet add package DrifterApps.Seeds.FluentResult --version 0.1.12
NuGet\Install-Package DrifterApps.Seeds.FluentResult -Version 0.1.12
<PackageReference Include="DrifterApps.Seeds.FluentResult" Version="0.1.12" />
paket add DrifterApps.Seeds.FluentResult --version 0.1.12
#r "nuget: DrifterApps.Seeds.FluentResult, 0.1.12"
// Install DrifterApps.Seeds.FluentResult as a Cake Addin #addin nuget:?package=DrifterApps.Seeds.FluentResult&version=0.1.12 // Install DrifterApps.Seeds.FluentResult as a Cake Tool #tool nuget:?package=DrifterApps.Seeds.FluentResult&version=0.1.12
FluentResult
FluentResult is a C# library that provides a fluent API for handling results of operations, including success and failure cases. It includes various utilities and extensions to simplify error handling and result aggregation.
Some of my inspirations comes from Andrew Lock's Series: Working with the result pattern
Table of Contents
Installation
To install FluentResult, you can use the NuGet package manager:
dotnet add package FluentResult
Usage
Basic Result Handling
You can create and handle results using the Result
class and its extension methods.
using DrifterApps.Seeds.FluentResult;
// Creating a success result
var successResult = Result<int>.Success(42);
// Creating a failure result
var failureResult = Result<int>.Failure(new ResultError("Error.Code", "Error description"));
// Handling success and failure
var finalResult = successResult.OnSuccess(value => Result<string>.Success(value.ToString()))
.OnFailure(error => Result<string>.Failure(error));
Aggregating Results
You can aggregate multiple results using the ResultAggregate class.
using DrifterApps.Seeds.FluentResult;
var resultAggregate = ResultAggregate.Create();
var successResult = Result<Nothing>.Success();
var failureResult = Result<Nothing>.Failure(new ResultError("Error.Code", "Error description"));
resultAggregate.AddResult(successResult);
resultAggregate.AddResult(failureResult);
bool isSuccess = resultAggregate.IsSuccess; // false
bool isFailure = resultAggregate.IsFailure; // true
Asynchronous Result Handling
You can handle results asynchronously using the extension methods provided in ResultExtensions.Async.
using DrifterApps.Seeds.FluentResult;
var resultTask = Task.FromResult(Result<int>.Success(42));
var finalResult = await resultTask.OnSuccess(async value => await Task.FromResult(Result<string>.Success(value.ToString())))
.OnFailure(async error => await Task.FromResult(Result<string>.Failure(error)));
Ensuring Validation
You can ensure that a specific validation function returns true using the Ensure
method. If the validation fails, it adds a failure result to the source.
using DrifterApps.Seeds.FluentResult;
var aggregate = ResultAggregate.Create();
aggregate.AddResult(Result<Nothing>.Failure(new ResultError("Error.Code", "Initial error")));
// Ensure validation with IgnoreOnFailure option
var result = aggregate.Ensure(() => true, new ResultError("Validation.Error", "Validation failed"), EnsureOnFailure.IgnoreOnFailure);
// Ensure validation with default ValidateOnFailure option
var resultWithValidation = aggregate.Ensure(() => false, new ResultError("Validation.Error", "Validation failed"));
bool isSuccess = result.IsSuccess; // true
bool isFailure = result.IsFailure; // false
bool isValidationSuccess = resultWithValidation.IsSuccess; // false
bool isValidationFailure = resultWithValidation.IsFailure; // true
Contributing
Contributions are welcome! Please read the contributing guidelines for more information.
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 | net7.0 is compatible. 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 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. |
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on DrifterApps.Seeds.FluentResult:
Package | Downloads |
---|---|
DrifterApps.Seeds.Application
Package Description |
|
DrifterApps.Seeds.Application.Mediatr
Package Description |
|
DrifterApps.Seeds.FluentResult.FluentAssertions
Package Description |
|
FluentResult.FluentAssertions
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.