DrifterApps.Seeds.FluentResult 1.1.0

dotnet add package DrifterApps.Seeds.FluentResult --version 1.1.0
                    
NuGet\Install-Package DrifterApps.Seeds.FluentResult -Version 1.1.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="DrifterApps.Seeds.FluentResult" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DrifterApps.Seeds.FluentResult" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DrifterApps.Seeds.FluentResult" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DrifterApps.Seeds.FluentResult --version 1.1.0
                    
#r "nuget: DrifterApps.Seeds.FluentResult, 1.1.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.
#addin nuget:?package=DrifterApps.Seeds.FluentResult&version=1.1.0
                    
Install DrifterApps.Seeds.FluentResult as a Cake Addin
#tool nuget:?package=DrifterApps.Seeds.FluentResult&version=1.1.0
                    
Install DrifterApps.Seeds.FluentResult as a Cake Tool

<img alt='paper plane icons' src='./icon.png' height='10%' width='10%'> 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

Build and Publish NuGet Package CodeQL .Net 8 Tests results .Net 9 Tests results

Table of Contents

Installation

To install FluentResult, you can use the NuGet package manager:

dotnet add package DrifterApps.Seeds.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 by implicit conversion
Result<int> successResult = 42;

// Creating a success result by ToResult extension method
var successResult = 45.ToResult<int>();

// Creating a failure result by implicit conversion
Result<int> failureResult = new ResultError("Error.Code", "Error description");

// Creating a failure result by ToResult extension method
failureResult = new ResultError("Error.Code", "Error description").ToResult<int>();

// Handling success and failure
var finalResult = successResult.OnSuccess(value => (Result<string>)value.ToString())
                               .OnFailure(error => (Result<string>)error);

Aggregating Results

You can aggregate multiple results using the ResultAggregate class.

using DrifterApps.Seeds.FluentResult;

var resultAggregate = ResultAggregate.Create();
Result<Nothing> successResult = Nothing.Value;
Result<Nothing> failureResult = 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>)42);

var finalResult = await resultTask.OnSuccess(async value => await Task.FromResult((Result<string>)value.ToString()))
                                  .OnFailure(async error => await Task.FromResult((Result<string>)error));

Matching Results

You can perform different actions based on success or failure:

using DrifterApps.Seeds.FluentResult;

Result<int> result = 42;

result.Match(
    onSuccess: value => Console.WriteLine($"Success with value: {value}"),
    onFailure: error => Console.WriteLine($"Error: {error.Code} - {error.Message}")
);

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(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 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.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
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 (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.

Version Downloads Last updated
1.1.0 396 3/15/2025
1.0.2 155 3/11/2025
1.0.0 160 3/11/2025
0.1.21 2,007 1/17/2025
0.1.19 181 1/16/2025
0.1.12 533 12/3/2024
0.1.10 491 11/28/2024
0.1.8 1,150 11/13/2024
0.1.7 1,534 10/22/2024
0.1.6 369 10/21/2024
0.1.5 105 10/21/2024
0.1.4 164 10/21/2024
0.1.3 121 10/20/2024
0.1.2 110 10/20/2024
0.1.1 150 10/18/2024
0.1.0 150 10/18/2024