Shalaby.Results.ResultHelpers 1.0.2

dotnet add package Shalaby.Results.ResultHelpers --version 1.0.2
                    
NuGet\Install-Package Shalaby.Results.ResultHelpers -Version 1.0.2
                    
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="Shalaby.Results.ResultHelpers" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shalaby.Results.ResultHelpers" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Shalaby.Results.ResultHelpers" />
                    
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 Shalaby.Results.ResultHelpers --version 1.0.2
                    
#r "nuget: Shalaby.Results.ResultHelpers, 1.0.2"
                    
#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=Shalaby.Results.ResultHelpers&version=1.0.2
                    
Install Shalaby.Results.ResultHelpers as a Cake Addin
#tool nuget:?package=Shalaby.Results.ResultHelpers&version=1.0.2
                    
Install Shalaby.Results.ResultHelpers as a Cake Tool

ResultHelpers

A helper library for working with the FluentResult library in C#. It aims to provide a more convenient and expressive way to handle results, especially when dealing with asynchronous operations.

Key Features

  • Full support for asynchronous operations (Async)
  • Implementation of functional programming principles
  • Expressive error handling
  • Support for transformation and chaining operations
  • Support for merging and condition checking

Functional Programming in ResultHelpers

The library implements several functional programming principles:

1. Pure Functions

  • All functions in the library are pure functions
  • No side effects
  • Depend only on their inputs to produce outputs

2. Function Composition

await result
    .MapAsync(value => Transform(value))
    .ThenAsync(transformed => Process(transformed))
    .OnSuccess(processed => Log(processed));

3. Expressive Error Handling

await result
    .EnsureNone(value => value == null, new Error("Value cannot be null"))
    .OnFailure(errors => HandleErrors(errors));

4. Chained Asynchronous Operations

await firstTask
    .ThenAsync(firstResult => SecondOperation(firstResult))
    .ThenAsync(secondResult => ThirdOperation(secondResult));

5. Result Merging

await firstTask
    .MergeThenAsync(secondTask, (first, second) => CombineResults(first, second));

Usage Examples

Transformation and Chaining

public async Task<Result<ProcessedData>> ProcessDataAsync(InputData input)
{
    return await ValidateInput(input)
        .MapAsync(validated => TransformData(validated))
        .ThenAsync(transformed => SaveData(transformed));
}

Error Handling

public async Task<Result> HandleOperationAsync()
{
    return await PerformOperation()
        .OnFailure(errors => LogErrors(errors))
        .OnSuccess(() => LogSuccess());
}

Result Merging

public async Task<Result<CombinedResult>> CombineOperationsAsync()
{
    return await FirstOperation()
        .MergeThenAsync(SecondOperation(), (first, second) => 
            new CombinedResult(first, second));
}

API Documentation

Category Method Description
Transformation MapAsync Transforms a successful result value to a new type. If the original result is a failure, the failure is propagated.
Chaining ThenAsync Chains asynchronous operations, where each operation depends on the successful result of the previous one.
Validation EnsureNone Ensures that a condition is not met on the result value. If the condition is met, returns a failure with the specified error.
Success Handling OnSuccess Executes the specified actions if the result is successful.
Error Handling OnFailure Executes the specified error handlers if the result is a failure.
Result Merging MergeThenAsync Merges the results of two asynchronous operations and then executes a mapping function.
Result Merging SwitchMap Switches to an alternate task if the first task fails, combining errors if both fail.
Logging LogErrors Logs errors from a result using the specified logger.
Error Messages GetErrorsMessage Concatenates all error messages from a result into a single string.
Value Management DiscardValueAsync Discards the value from a result, converting it to a non-generic result.
Fallback FallbackAsync Provides a fallback mechanism for asynchronous operations that produce a result.

Installation

You can install the library via NuGet:

dotnet add package ResultHelpers

Requirements

  • .NET 6.0 or later
  • FluentResults

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the project
  2. Create a feature branch
  3. Submit a Pull Request

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.2 107 4/6/2025
1.0.1 106 4/6/2025
1.0.0 77 4/5/2025