An open API service indexing awesome lists of open source software.

https://github.com/joncloud/rlx-net

Adds functional programming aspects using functions like Some, None, Ok, Error
https://github.com/joncloud/rlx-net

error-handling functional null option result rustlang

Last synced: about 2 months ago
JSON representation

Adds functional programming aspects using functions like Some, None, Ok, Error

Awesome Lists containing this project

README

          

# Rlx.NET
[![Travis](https://img.shields.io/travis/joncloud/rlx-net.svg)](https://travis-ci.org/joncloud/rlx-net/)
[![NuGet](https://img.shields.io/nuget/v/Rlx.svg)](https://www.nuget.org/packages/Rlx/)

## Description
Rlx.NET provides core helper functions for abstracting null-references and errors. Rlx itself is short for `Rust Language Extensions`.

It was heavily inspired by the [Rust] programming language [Option] and [Result] types.

## Licensing
Released under the MIT License. See the [LICENSE][] file for further details.

[license]: LICENSE.md

## Installation
In the Package Manager Console execute

```powershell
Install-Package Rlx
```

Or update `*.csproj` to include a dependency on

```xml

```

## Usage
Sample echo program:
```csharp
using Rlx;

static void Main(string[] args) {
string message = args.ElementAtOrDefault(0)
.ToOption()
.MapOrElse(() => "Missing Argument", msg => $"Echo: {msg}");

Console.WriteLine(message);
}
```

Need to generically handle exceptions? Wrap up logic with `TryFunctions`:
```csharp
using System.IO;
using static Rlx.Functions;

public class MyClass
{
public static void Unsafe() =>
throw new NotImplementedException();

public static Result Safe() =>
Try(Unsafe);

public static Result IOSafe() =>
Try(Unsafe).Catch();
}
```

And also MVC helpers:
```csharp
using Rlx;
using Rlx.MvcCore;
using static Rlx.Functions;

interface IDataService {
OptionTask LoadDataAsync(Guid id);
ResultTask UpdateDataAsync(Data data);
}

class DataController : Controller {
readonly IDataService _dataService;
public DataController(IDataService dataService) =>
_dataService = dataService;

public Task Get(Guid id) =>
WithErrors().AndThen(_ => _dataService.LoadDataAsync(id))
.ToActionResult();

public Task Post([FromModel]Data data) =>
WithErrors().AndThen(_ => _dataService.UpdateDataAsync(data))
.ToActionResult(_ => 200, _ => 400, x => Some(x));

Result WithErrors() =>
ModelState.ToResult().MapError(x => "Bad Request");
}
```

For additional usage see [Tests][] and [MVC Tests][].

[Tests]: tests/Rlx.Tests
[MVC Tests]: tests/Rlx.MvcCore/Tests
[Rust]: https://www.rust-lang.org/
[Option]: https://doc.rust-lang.org/std/option/
[Result]: https://doc.rust-lang.org/std/result/index.html