Just.Railway 1.4.0

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

// Install Just.Railway as a Cake Tool
#tool nuget:?package=Just.Railway&version=1.4.0                

Base for Railway Programming in .NET

This library uses features of C# to achieve railway-oriented programming.

The desire is to make somewhat user-friendly experience while using result-object pattern.

Features

  • Immutable Error class
  • Result object
  • A bunch of extensions to use result-object pattern with
  • Try extensions to wrap function calls with result-object
  • Ensure extensions to utilize result-object in validation scenarios

Getting Started

Install from NuGet.org

# install the package using NuGet
dotnet add package Just.Railway

Examples

Error

using Just.Railway;
Error expectedError = Error.New(type: "Some Error", message: "Some error detail");
Error exceptionalError = Error.New(new Exception("Some Exception"));
Error manyErrors = Error.Many(expectedError, exceptionalError);
// the same result while using .Append(..) or +
manyErrors = expectedError.Append(exceptionalError);
manyErrors = expectedError + exceptionalError;

Note You can easily serialize/deserialize Error to and from JSON

Result

As return value:
Result Foo()
{
    // ...
    if (SomeCondition())
        return Result.Failure(Error.New("Some Error"));
        // or just: return Error.New("Some Error");
    // ...
    return Result.Success();
}

Result<T> Bar()
{
    T value;
    // ...
    if (SomeCondition())
        return Error.New("Some Error");
    // ...
    return value;
}
Consume Result object
Result<int> result = GetResult();

string value = result
    .Append("new") // -> Result<(int, string)>
    .Map((i, s) => $"{s} result {i}") // -> Result<string>
    .Match(
        onSuccess: x => x,
        onFailure: err => err.ToString()
    );
// value: "new result 1"

Result<int> GetResult() => Result.Success(1);
Recover from failure
Result<string> failed = new NotImplementedException();

Result<string> result = failed.TryRecover(err => err.Type == "System.NotImplementedException"
    ? "recovered"
    : err);
// result with value: "recovered"

Try

Result result = Try.Run(SomeAction);
// you can pass up to 5 arguments like this
result = Try.Run(SomeActionWithArguments, 1, 2.0, "3");

// you also can call functions
Result<int> resultWithValue = Try.Run(SomeFunction);

void SomeAction() {}
void SomeActionWithArguments(int a1, double a2, string? a3) {}
int SomeFunction() => 1;

Ensure

int? value = GetValue();
Result<int> result = Ensure.That(value) // -> Ensure<int?>
    .NotNull() // -> Ensure<int>
    .Satisfies(i => i < 100)
    .Result();

int? GetValue() => 1;
Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.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.4.0 113 5/17/2024
1.3.0 143 2/10/2024
1.2.0 190 12/18/2023
1.1.0 135 12/15/2023
1.0.1 141 12/13/2023