Skip to content

Razenpok/CSharpFunctionalExtensions.UniTask

Repository files navigation

CSharpFunctionalExtensions.UniTask

UniTask extensions for CSharpFunctionalExtensions

Installation

There are several ways to install this library into our project:

  • OpenUPM: After installing openupm-cli, run the following command:
    • openupm add ext.csharpfunctionalextensions.unitask
  • Unity Package Manager (UPM): Add the following line to Packages/manifest.json:
    • "ext.csharpfunctionalextensions.unitask": "https://github.com/Razenpok/CSharpFunctionalExtensions.UniTask.git?path=src/CSharpFunctionalExtensions.UniTask",
  • Clone source code: Clone or download this repository and put the src/CSharpFunctionalExtensions.UniTask folder somewhere in your Unity project

You will also need to obtain a compatible version of CSharpFunctionalExtensions library (major version must match with CSharpFunctionalExtensions.UniTask):

  • UnityNuGet registry: you can install the library directly via OpenUPM using the following comand:
    • openupm add org.nuget.csharpfunctionalextensions
  • NuGetForUnity plugin
  • NuGet: Download a .nupkg file from NuGet registry, change the file extension to .zip, unzip it, find the .dll file in lib/netstandard2.0 folder

Usage

You can find the extensive set of usage examples in the CSharpFunctionalExtensions repository

Here is a short primer if you are not familiar with this library

Get rid of primitive obsession

Result<CustomerName> name = CustomerName.Create(model.Name);
Result<Email> email = Email.Create(model.PrimaryEmail);

Result result = Result.Combine(name, email);
if (result.IsFailure)
    return Error(result.Error);

var customer = new Customer(name.Value, email.Value);

Make nulls explicit with the Maybe type

Maybe<Customer> customerOrNothing = _customerRepository.GetById(id);
if (customerOrNothing.HasNoValue)
    return Error("Customer with such Id is not found: " + id);

Compose multiple operations in a single chain

return _customerRepository.GetById(id)
    .ToResult("Customer with such Id is not found: " + id)
    .Ensure(customer => customer.CanBePromoted(), "The customer has the highest status possible")
    .Tap(customer => customer.Promote())
    .Tap(customer => _emailGateway.SendPromotionNotification(customer.PrimaryEmail, customer.Status))
    .Finally(result => result.IsSuccess ? Ok() : Error(result.Error));

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages