GithubHelp home page GithubHelp logo

gabriel-sk-dev / csharpfunctionalextensions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vkhorikov/csharpfunctionalextensions

0.0 0.0 0.0 912 KB

Functional extensions for C#

License: MIT License

C# 99.59% Dockerfile 0.41%

csharpfunctionalextensions's Introduction

Functional Extensions for C#

Build Status Nuget downloads GitHub license

This library helps write code in more functional way. To get to know more about the principles behind it, check out the Applying Functional Principles in C# Pluralsight course.

Installation

Available on nuget

PM> Install-Package CSharpFunctionalExtensions

.NET 4.0 version Installation

.NET 4.0 version is available as a separate package on nuget

PM> Install-Package CSharpFunctionalExtensionsNet4.0

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

Wrap multiple operations in a TransactionScope

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

Readings and watchings

Contributors

A big thanks to the project contributors!

csharpfunctionalextensions's People

Contributors

vkhorikov avatar space-alien avatar a-z-hub avatar bopazyn avatar hankovich avatar solvingj avatar benprime avatar jvssd avatar altmann avatar sergatgh avatar svroonland avatar golavr avatar azm102 avatar svhankovich avatar mnissl avatar jcsonder avatar yakimovim avatar dpraimeyuu avatar freakingawesome avatar carlha avatar pedromtcosta avatar mukmyash avatar ergwun avatar vladimirmakaev avatar thomasdc avatar linkdotnet avatar robosek avatar mikelthief avatar pilotmike avatar mjharwood avatar

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.