dotResult 0.0.1
See the version list below for details.
dotnet add package dotResult --version 0.0.1
NuGet\Install-Package dotResult -Version 0.0.1
<PackageReference Include="dotResult" Version="0.0.1" />
paket add dotResult --version 0.0.1
#r "nuget: dotResult, 0.0.1"
// Install dotResult as a Cake Addin #addin nuget:?package=dotResult&version=0.0.1 // Install dotResult as a Cake Tool #tool nuget:?package=dotResult&version=0.0.1
dotResult - The Result Monad for .NET
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")
.FlatMap(ParseConfig)
.FlatMap(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);
}
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 | Versions 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. |
-
.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.