Giter VIP home page Giter VIP logo

operationresult's Introduction

Operation Result

Build Status NuGet License: MIT

This a simple C# class libary to handle error of an operation.

When to Use it?

Operation Result can be used for error handling without throwing costly Exceptions.

Operation Result Pattern

The purpose of the Operation Result design pattern is to give an operation (a method) the possibility to return a complex result (an object), allowing the consumer to:

  • Access the result of an operation; in case there is one.
  • Access the success indicator of an operation.
  • Access the cause of the failure in case the operation was not successful.

OperationResult Namespace

Represents the result of an operation. The result must be immutable and its properties must be read-only.

public struct Result
public struct Result<T>

Properties

Access the result of the operation, in case there is one.

 public T Value { get; set; }

Access the success indicator of the operation.

public bool IsSuccess { get; }

Access the cause of the failure in case the operation was not successful.

public Exception Exception { get; }

Methods

public static Result Success()
    => new Result(true);
public static Result<T> Success<T>(T value)
    => new Result<T>(value);
public static Result Error(Exception exception)
    => new Result(exception);
public static Result<T> Error<T>(Exception exception)
    => new Result<T>(exception);

Implicit operators

public static implicit operator Result<T>(T value)
    => new Result<T>(value);

public static implicit operator Result<T>(Exception exception)
    => new Result<T>(exception);

The following example demonstrates how to use it:

Result<int> IncrementOne(int value)
{
    if (value > 9)
    {
        return new ArgumentOutOfRangeException(nameof(value), "Value cannot be greater than nine.");
    }
    return value++;
}

Deconstructing a Result

public void Deconstruct(out bool success, out T value)
{
    success = IsSuccess;
    value = Value;
}

public void Deconstruct(out bool success, out T value, out Exception exception)
{
    success = IsSuccess;
    value = Value;
    exception = Exception;
}

The following example demonstrates how to use it:

public static void Main()
{
    var (isSuccess, value, exception) = IncrementOne(2);

    // Do something with the data.
}

operationresult's People

Contributors

albertomonteiro avatar victordivino avatar vitorfmuniz avatar

Watchers

 avatar

Forkers

kabaw

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.