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
- Host: GitHub
- URL: https://github.com/joncloud/rlx-net
- Owner: joncloud
- License: mit
- Created: 2017-11-18T21:38:06.000Z (almost 8 years ago)
- Default Branch: publish
- Last Pushed: 2020-06-12T04:13:36.000Z (over 5 years ago)
- Last Synced: 2025-08-04T03:37:15.480Z (2 months ago)
- Topics: error-handling, functional, null, option, result, rustlang
- Language: C#
- Homepage:
- Size: 87.9 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Rlx.NET
[](https://travis-ci.org/joncloud/rlx-net/)
[](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