maximegel / railsharp

A small railway oriented library in C#

Home Page:https://softframe.github.io/railsharp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RailSharp

RailSharp

A small railway oriented library that offers a simple implementation of the Option type and Result type in C#.

Azure DevOps releases Azure DevOps builds Azure DevOps tests Azure DevOps coverage Nuget Nuget downloads License

Quick links: Docs, API

What is RailSharp?

RailSharp uses railway oriented programming to help handle errors and to deal with our old friend NullReferenceException.

To learn more about railway oriented programming, take a look here!

How do I get started?

Here's a quick overview of how to use the Option and Result types:

Option type:

// First, instantiates an option using `Option.None`, `Option.Some(T)` 
// or one of the extension methods.
var option = Option.Some(new User("Alan Turing"));

var userName = option
    // Then, handle the happy path (i.e. when the option contains a value).
    .Map(user => user.Name)
    .Do(name => SayHi(name))
    // Finally, handle the sad path (i.e. when the option contains no value).
    .Reduce(() => "Anonymous")

Result type:

// First, instantiates a result using `Result.Failure(TFailure)`,
// `Result.Success(TSuccess)` or one of the extension methods.
var result = Result.Failure(new UserNotFound());

var httpResponse = result
    // Then, handle the happy path (i.e. when the result is a success).
    .Map(data => Ok(data))
    // Finally, handle the sad paths (i.e. when the result is a failure).
    .Catch<UserNotFound>(err => NotFound())
    .Catch(err => InternalServerError())

You can also take a look at the API documentation or even the unit tests for a deeper understanding.

How do I install it?

First, install NuGet. Then, install the NuGet package avalaible at nuget.org using the dotnet CLI:

dotnet add package RailSharp

I have an issue...

First, you can check if your issue has already been tracked here.

Otherwise, you can check if it's already fixed by pulling the develop branch, building the solution and then using the generated DLL files direcly in your project.

If you still hit a problem, please document it and post it here.

Credits

The original code has been inspired from the Pluralsight course Making Your C# Code More Functional by Zoran Horvat.

License

RailSharp is Copyright © 2018 SoftFrame under the MIT license.