dotResult 0.4.0

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

// Install dotResult as a Cake Tool
#tool nuget:?package=dotResult&version=0.4.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

Basic usage

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

HandleResult(successResult); // Operation succeeded with result: Hello, World!
HandleResult(failureResult); // Operation failed with error: A not found failure has occurred.

static void HandleResult(Result<int> result)
{
    var message = result.Match(
        failure => $"Operation failed with error: {failure.Message}",
        success => $"Operation succeeded with result: {success}"
    );

    Console.WriteLine(message);
}

Chaining operations

var result = ReadFile("config.txt")
    .Bind(ParseConfig)
    .Bind(CalculateResult);

HandleResult(result);

private static Result<string> ReadFile(string path)
{
    try
    {
        var content = File.ReadAllText(path);
        return Success.From(content);
    }
    catch (Exception ex)
    {
        return Fail.OfType<string>(Failure.Custom(
            code: "File.ReadError",
            message: ex.Message,
            type: "IO"
        ));
    }
}

private static Result<Config> ParseConfig(string content)
{
    try
    {
        var lines = content.Split(Environment.NewLine);
        var config = new Config
        {
            Value = int.Parse(lines[0]) // Assuming the first line is an integer
        };
        return Success.From(config);
    }
    catch (Exception ex)
    {
        return Fail.OfType<Config>(Failure.Custom(
            code: "Config.ParseError",
            message: "Failed to parse configuration: " + ex.Message,
            type: "Parsing"
        ));
    }
}

private static Result<int> CalculateResult(Config config)
{
    if (config.Value < 0)
    {
        return Fail.OfType<int>(Failure.Custom(
            code: "Calculation.Error",
            message: "Configuration value must be non-negative",
            type: "Validation"
        ));
    }

    // Perform some calculation
    return Success.From(config.Value * 2);
}

private static void HandleResult(Result<int> result)
{
    var message = result.Match(
        failure => $"Operation failed with error: {failure.Message}",
        success => $"Operation succeeded with result: {success}"
    );

    Console.WriteLine(message);
}

public class Config
{
    public int Value { get; set; }
}

Replacing Exceptions

Without Result Monad (Using Exceptions)

try
{
    var result = PerformOperation(5);
    Console.WriteLine($"Operation succeeded with result: {result}");
}
catch (Exception ex)
{
    Console.WriteLine($"Operation failed with error: {ex.Message}");
}

static int PerformOperation(int input)
{
    if (input < 0)
    {
        throw new ArgumentException("Input must be non-negative");
    }

    // Simulate some operation
    return input * 2;
}

With Result

var result = PerformOperation(5);
HandleResult(result);

static Result<int> PerformOperation(int input)
{
    if (input < 0)
    {
        return Fail.OfType<int>(Failure.Fatal(message: "Input must be non-negative"));
    }

    // Simulate some operation
    return Success.From(input * 2);
}

static void HandleResult(Result<int> result)
{
    var message = result.Match(
        failure => $"Operation failed with error: {failure.Message}",
        success => $"Operation succeeded with result: {success}"
    );

    Console.WriteLine(message);
}

Query syntax

var result =
    from fileData in ReadFile("config.txt")
    from config in ParseConfig(fileData)
    from calculatedResult in CalculateResult(config)
    select calculatedResult;

HandleResult(result);

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

v0.1.0:
         - support for sync Query Syntax in Result
         - validation failure
         v0.2.0:
         - async matching (MatchAsync)
         - async mapping (MapAsync)
         - async binding (FlatMapAsync)
         v0.3.0
         - support for async Query Syntax in Result
         v0.4.0
         - add IsSuccess and IsFailure properties
         - add Map2 function
         - add Map3 function
         Breaking changes:
         - rename FlatMap to Bind