RiseOn.ResultRail
1.1.1
dotnet add package RiseOn.ResultRail --version 1.1.1
NuGet\Install-Package RiseOn.ResultRail -Version 1.1.1
<PackageReference Include="RiseOn.ResultRail" Version="1.1.1" />
<PackageVersion Include="RiseOn.ResultRail" Version="1.1.1" />
<PackageReference Include="RiseOn.ResultRail" />
paket add RiseOn.ResultRail --version 1.1.1
#r "nuget: RiseOn.ResultRail, 1.1.1"
#:package RiseOn.ResultRail@1.1.1
#addin nuget:?package=RiseOn.ResultRail&version=1.1.1
#tool nuget:?package=RiseOn.ResultRail&version=1.1.1
RiseOn.ResultRail
RiseOn.ResultRail is a NuGet package that provides functional programming constructs using the Railway-Oriented Programming model. It simplifies error handling and operation branching by encapsulating success and failure states, promoting a functional approach in C#.
Installation
Install the package via NuGet:
dotnet add package RiseOn.ResultRail
or
PM> Install-Package RiseOn.ResultRail
Features
- Railway-Oriented Programming: Seamlessly switch between success and failure rails.
- Result Type Handling: Use
Upshot
andUpshot<T>
types to represent operation outcomes. - Extension Methods: Enjoy helper methods like
OnRail
,OnRailSuccess
,OnRailFail
, andMap
to chain operations.
Examples
Using Upshot
Upshot upshot = Upshot.Success();
Upshot upshot = Upshot.Fail("An error occurred"); or Upshot upshot = Upshot.Fail(new Exception("An error occurred"));
if (upshot.IsSuccess)
{
//Do something
}
else
{
Console.WriteLine($"Operation failed: {upshot.Error.Message}");
\\ or just use ToString() method
Console.WriteLine($"Operation failed: {upshot.Error.ToString()}");
or
Console.WriteLine($"Operation failed: {(string)upshot.Error}");
output: Operation failed: { Message: An error occurred }
}
The Upshot
type represents the result of an operation, indicating success or failure. Here's an example of using Upshot
:
For operations with a value, use Upshot<T>
:
Upshot<int> upshot = Upshot<int>.Success(42);
Upshot<int> upshot = Upshot<int>.Fail("An error occurred"); or Upshot<int> upshot = Upshot<int>.Fail(new Exception("An error occurred"));
var result = upshot.Value;
if (upshot.IsSuccess)
{
Console.WriteLine($"Result: {result}");
output: Result: 42
}
else
{
Console.WriteLine($"Operation failed: {upshot.Error.Message}");
}
Using UpshotExtensions
OnRailSuccess
In OnRailSuccess
extension to execute an action only if the operation is successful.
Whether the operation has a value, you can use the OnRailSuccess
extension to transform the value and return a new Upshot
with the new value, otherwise, it will return the same Upshot
instance.
Upshot upshot = Upshot.Success(); or Upshot upshot = Upshot<int>.Success(10);
upshot.OnRailSuccess(() => Console.WriteLine("Operation succeeded"));
or
var result = upshot.OnRailSuccess(value => value + 10);
output: result = 20;
OnRailFail
In OnRailFail
extension to execute an action only if the operation fails.When the operation fails, the OnRailFail
extension will execute the specified action, otherwise, it will return the same Upshot
instance.
Upshot upshot = Upshot.Fail("An error occurred"); or Upshot upshot = Upshot<int>.Fail(new Exception("An error occurred"));
upshot.OnRailFail(() => Console.WriteLine("Operation failed"));
or
var result = upshot.OnRailFail(error => Console.WriteLine($"Operation failed: {error.Message}"));
OnRail
In OnRail
extension to execute different actions based on the operation's success or failure. The OnRail
it's a wrapper for OnRailSuccess
and OnRailFail
extensions.
Upshot upshot = Upshot.Success();
upshot.OnRail(
success: () => Console.WriteLine("Operation succeeded"),
fail: () => Console.WriteLine("Operation failed")
);
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Just create a Pull Request with your changes. If you have any questions or suggestions, feel free to open an issue.
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 is compatible. 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. 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. |
.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.
-
net8.0
- No dependencies.
-
net9.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.