Home

Awesome

CSharpFunctionalExtensions.UniTask

UniTask extensions for CSharpFunctionalExtensions

Installation

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

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

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