Late4dTrain.Result 1.0.1

dotnet add package Late4dTrain.Result --version 1.0.1
NuGet\Install-Package Late4dTrain.Result -Version 1.0.1
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="Late4dTrain.Result" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Late4dTrain.Result --version 1.0.1
#r "nuget: Late4dTrain.Result, 1.0.1"
#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.
// Install Late4dTrain.Result as a Cake Addin
#addin nuget:?package=Late4dTrain.Result&version=1.0.1

// Install Late4dTrain.Result as a Cake Tool
#tool nuget:?package=Late4dTrain.Result&version=1.0.1

Late4dTrain.Result C# Library

.NET Release to Nuget NuGet version (GSoulavy.Csv.Net)

This repository contains a flexible and type-safe C# library for result-oriented error handling, which promotes clearer and more reliable code. The library is especially useful in environments where exceptions are costly or unwanted, such as in high-performance or concurrent systems.

Table of Contents

Installation

Install the library using the following command:

dotnet add package Late4dTrain.Result

Usage

The Late4dTrain library contains the Result and Result<TError, TValue> classes, and ResultAsyncExtensions for async operations.

A Result object represents the result of an operation that can either succeed or fail, with an associated error of type TError.

Here is a simple example of using the Result class:

var successResult = Result<string>.Ok();
var failResult = Result<string>.Fail("An error occurred");

if (successResult.IsSuccess)
{
    // Perform operation on success
}

if (!failResult.IsSuccess)
{
    Console.WriteLine(failResult.Error);  // Outputs: "An error occurred"
}

Result<TError, TValue> is a subclass of Result, which also carries a value of type TValue when the operation succeeds:

var successResultWithValue = Result<string, int>.Ok(10);
var failResultWithValue = Result<string, int>.Fail("An error occurred");

if (successResultWithValue.IsSuccess)
{
Console.WriteLine(successResultWithValue.Value);  // Outputs: "10"
}

if (!failResultWithValue.IsSuccess)
{
Console.WriteLine(failResultWithValue.Error);  // Outputs: "An error occurred"
}

For async operations, use the extension methods from ResultAsyncExtensions. Here is an example:

Task<Result<string>> taskResult = Task.FromResult(Result<string>.Ok());
await taskResult.OnSuccessAsync(async () => { await DoSomethingAsync(); });

Here are examples of each public method in the Result and Result<TError, TValue> classes and their usage:

  1. OnSuccess(Action action): This method executes the provided action if the result is successful.
var successResult = Result<string>.Ok();
successResult.OnSuccess(() => Console.WriteLine("Operation was successful."));
// Outputs: "Operation was successful."
  1. OnSuccessAsync(Func<Task> action): This method asynchronously executes the provided action if the result is successful.
var successResult = Result<string>.Ok();
await successResult.OnSuccessAsync(async () => { await Task.Delay(1000); Console.WriteLine("Operation was successful."); });
// Outputs: "Operation was successful." after a delay of 1 second.
  1. OnFail(Action action): This method executes the provided action if the result is a failure.
var failResult = Result<string>.Fail("An error occurred");
failResult.OnFail(() => Console.WriteLine("Operation failed."));
// Outputs: "Operation failed."
  1. OnFail(Action<TError> action): This method executes the provided action, giving the error as a parameter, if the result is a failure.
var failResult = Result<string>.Fail("An error occurred");
failResult.OnFail(err => Console.WriteLine($"Operation failed with error: {err}"));
// Outputs: "Operation failed with error: An error occurred"
  1. OnFailAsync(Func<Task> action): This method asynchronously executes the provided action if the result is a failure.
var failResult = Result<string>.Fail("An error occurred");
  1. OnFailAsync(Func<TError, Task> action): This method asynchronously executes the provided action, giving the error as a parameter, if the result is a failure.
var failResult = Result<string>.Fail("An error occurred");
await failResult.OnFailAsync(async err => { await Task.Delay(1000); Console.WriteLine($"Operation failed with error: {err}"); });
// Outputs: "Operation failed with error: An error occurred" after a delay of 1 second.

The Result<TError, TValue> class also has similar methods that take a value as a parameter when the operation is successful:

  1. OnSuccess(Action<TValue> action): This method executes the provided action, giving the value as a parameter, if the result is successful.
var successResultWithValue = Result<string, int>.Ok(10);
successResultWithValue.OnSuccess(value => Console.WriteLine($"Operation was successful with value: {value}."));
// Outputs: "Operation was successful with value: 10."
  1. OnSuccessAsync(Func<TValue, Task> action): This method asynchronously executes the provided action, giving the value as a parameter, if the result is successful.
var successResultWithValue = Result<string, int>.Ok(10);
await successResultWithValue.OnSuccessAsync(async value => { await Task.Delay(1000); Console.WriteLine($"Operation was successful with value: {value}."); });
// Outputs: "Operation was successful with value: 10." after a delay of 1 second.

Remember to use the async versions of these methods in asynchronous contexts to avoid blocking.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

This project is licensed under the terms of the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Late4dTrain.Result:

Package Downloads
Late4dTrain.CosmosDbStorage

Simple cosmosDb storage adapter

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 116 6/21/2023
1.0.0 102 6/21/2023
0.0.4-pre 152 2/17/2022
0.0.3-pre 175 1/24/2022
0.0.2-pre 137 1/21/2022