Giter Site home page Giter Site logo

rlx-net's Introduction

Rlx.NET

Travis NuGet

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.

Installation

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>

Usage

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");
}

For additional usage see Tests and MVC Tests.

rlx-net's People

Contributors

brainsnail avatar joncloud avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rlx-net's Issues

Add logo

Reason

Default nuget logo doesn't look very good

Define build script

Reason

It is common practice to define build.sh and build.cmd to produce build artifacts

Use Cases

Local builds
Simpler yaml

Add Unit type

Reason

Facilitate other generics to understand a zero-tuple struct

Use Cases

Returning Option<Unit> to indicate presence of a value

  • Consider predefining static readonly Option<Unit> Some and None

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.