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.
Released under the MIT License. See the LICENSE file for further details.
In the Package Manager Console execute
Install-Package Rlx
Or update *.csproj
to include a dependency on
<ItemGroup>
<PackageReference Include="Rlx" Version="0.2.1-*" />
</ItemGroup>
Sample echo program:
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
:
using System.IO;
using static Rlx.Functions;
public class MyClass
{
public static void Unsafe() =>
throw new NotImplementedException();
public static Result<Unit, Exception> Safe() =>
Try(Unsafe);
public static Result<Unit, IOException> IOSafe() =>
Try(Unsafe).Catch<IOException>();
}
And also MVC helpers:
using Rlx;
using Rlx.MvcCore;
using static Rlx.Functions;
interface IDataService {
OptionTask<Data> LoadDataAsync(Guid id);
ResultTask<Data, string> UpdateDataAsync(Data data);
}
class DataController : Controller {
readonly IDataService _dataService;
public DataController(IDataService dataService) =>
_dataService = dataService;
public Task<IActionResult> Get(Guid id) =>
WithErrors().AndThen(_ => _dataService.LoadDataAsync(id))
.ToActionResult();
public Task<IActionResult> Post([FromModel]Data data) =>
WithErrors().AndThen(_ => _dataService.UpdateDataAsync(data))
.ToActionResult(_ => 200, _ => 400, x => Some(x));
Result<Unit, string> WithErrors() =>
ModelState.ToResult().MapError(x => "Bad Request");
}