Skip to content

Latest commit

 

History

History
86 lines (63 loc) · 2.51 KB

README.md

File metadata and controls

86 lines (63 loc) · 2.51 KB

OneOf.Deconstruct

Easily deconstruct and use OneOf options without the need for matching. This library offers extension methods for OneOf deconstruction to simplify its usage and make code cleaner.

Installation

Install a OneOf.Deconstruct nuget package

Features

  • Deconstruction extension methods for OneOf objects (like a TypeScript/JavaScript syntax).
  • Easy and clean handling of different OneOf outcomes.
  • No need for lengthy matching syntax.

Extension methods provide deconstruction from 1 to 9 generic parameters, that corresponds to OneOf's count of generic parameters.

Example of usage

Consider a ProductService which can either create or update a product:

OneOf.Deconstruct has extension methods in the OneOf namespace, that way deconstruction can be used straight away without adding extra usings.

Creating a Product

See full example here

using Example;
using Example.Models;
using OneOf;

var service = new ProductService();

var (product, alreadyExists) = service.Create(new Product(Guid.NewGuid(), "Product 1", 1000.00m));

if (product is not null)
{
    Console.WriteLine("Product 1 created");
}
else if (alreadyExists is not null)
{
    Console.WriteLine("Product 1 already exists");
}

Updating a product

product = product! with
{
    Price = -100m
};

var (updatedProduct, notFound, error) = service.Update(product.Id, product);
if (updatedProduct is not null)
{
    Console.WriteLine("Product updated");
}
else if (notFound is not null)
{
    Console.WriteLine("Product not found for update");
}
else if (error is not null)
{
    Console.WriteLine($"Error updating. Code: {error.Code}, description: {error.Description}");
}

Interface & Models

public interface IProductService
{
    OneOf<Product, ProductAlreadyExists> Create(Product product);
    OneOf<Product, ProductNotFound, ProductError> Update(Guid id, Product product);
}

public record Product(Guid Id, string Name, decimal Price);
public record ProductAlreadyExists;
public record ProductNotFound;
public record ProductError(string Code, string Description);

Acknowledgments

A big shoutout to the original OneOf library and its author for creating such a fantastic library. This package is built upon the foundation laid by the original library, and I am very grateful for the inspiration and the work put into it. Thank you!