dotResult 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package dotResult --version 1.0.0                
NuGet\Install-Package dotResult -Version 1.0.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="dotResult" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add dotResult --version 1.0.0                
#r "nuget: dotResult, 1.0.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.
// Install dotResult as a Cake Addin
#addin nuget:?package=dotResult&version=1.0.0

// Install dotResult as a Cake Tool
#tool nuget:?package=dotResult&version=1.0.0                

dotResult - The Result Monad for .NET

.net workflow

dotResult is a lightweight and intuitive implementation of the Result monad for the .NET platform. It simplifies error handling and value propagation in a functional way, improving code readability and safety.

Give it a star ⭐ !

If you find this project valuable, please consider giving it a star! Your support helps others discover this work and encourages further development.

How to use

Creation

Result<string> successResult = Success.From("Hello, World!");
Result<string> failureResult = Fail.OfType<string>(Failure.NotFound());

Result<string> otherSuccessResult = "Hello, World!";
Result<string> otherFailureResult = Failure.NotFound();

Result<string> yetAnotherSuccessResult = "Hello, World!".ToResult();
Result<string> yetAnotherFailureResult = Failure.NotFound().ToResult<string>();

Retrieving value

int successValue = successResult.Match(failure => failure.Code.Length, success: v => v.Length); // 13
int failureValue = failureResult.Match(failure => failure.Code.Length, success: v => v.Length); // 16 (default not found code is "General.NotFound")

string otherSuccessValue  = otherSuccessResult.OrDefault("default"); // "Hello, World!"
string otherFailureValue = otherFailureResult.OrDefault("default"); // "default"

string yetAnotherSuccessValue = yetAnotherSuccessResult.OrDefault(() => "default"); // "Hello, World!"
string yetAnotherFailureValue = yetAnotherFailureResult.OrDefault(() => "default"); // "default"

Checking result state

if (successResult.IsSuccess)
{
    // some logic
}

if (failureResult.IsFailure)
{
    // some logic
}

Chaining operations


otherSuccessResult
    .Map(v => v.Length) // Result<int> of 13
    .Bind(v => v > 20 ? Success.From(v) : Fail.OfType<int>(Failure.Validation())); // Result<int> of Failure.Validation

otherFailureResult
    .Map(v => v.Length) // Result<int> of Failure.NotFound
    .Bind(v => v > 20 ? Success.From(v) : Fail.OfType<int>(Failure.Validation())); // Result<int> of Failure.NotFound

Query syntax

// Execute function if all results are in success state
Result<int> finalSuccessResult =
    from v1 in successResult
    from v2 in otherSuccessResult
    from v3 in yetAnotherSuccessResult
    let v4 = "Hello, World!"
    select v1.Length + v2.Length + v3.Length + v4.Length; // Result<int> of 52

// Returns failure from first result in failure state
Result<int> finalFailureResult =
    from v1 in failureResult
    from v2 in otherFailureResult
    from v3 in yetAnotherFailureResult
    let v4 = "Hello, World!"
    select v1.Length + v2.Length + v3.Length + v4.Length; // Result<int> of Failure.NotFound

Query syntax alternative


// Execute function if all results are in success state
Result<int> otherFinalSuccessResult =
    Result.Map2(successResult, otherSuccessResult, (v1, v2) => v1.Length + v2.Length); // Result<int> of 26

// Returns failure from first result in failure state
Result<int> otherFinalFailureResult =
    Result.Map2(failureResult, otherFailureResult, (v1, v2) => v1.Length + v2.Length); // Result<int> of Failure.NotFound

// Execute function if all results are in success state
Result<int> yetAnotherFinalSuccessResult =
    Result.Map3(successResult, otherSuccessResult, yetAnotherFailureResult, (v1, v2, v3) => v1.Length + v2.Length + v3.Length); // Result<int> of 39

// Returns failure from first result in failure state
Result<int> yetAnotherFinalFailureResult =
    Result.Map3(failureResult, otherFailureResult, yetAnotherFailureResult, (v1, v2, v3) => v1.Length + v2.Length + v3.Length); // Result<int> of Failure.NotFound

Contribution

If you would like to contribute to this project, check out CONTRIBUTING file.

License

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 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 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 was computed. 
.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.

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.1.0 109 7/25/2024
1.0.0 70 7/11/2024
0.5.0 85 7/9/2024
0.4.0 87 7/8/2024
0.3.0 86 7/7/2024
0.2.0 83 7/6/2024
0.1.0 87 7/6/2024
0.0.1 89 7/5/2024

v1.0.0
         - add Flatten method (Result of Result of T -> Result of T)
         - add ToResult extension method for T
         - add ToResult extension method for Failure
         - add OrDefault method with default value
         - add OrDefault method with default factory