ResultObject 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ResultObject --version 1.0.0                
NuGet\Install-Package ResultObject -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ResultObject" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResultObject --version 1.0.0                
#r "nuget: ResultObject, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ResultObject as a Cake Addin
#addin nuget:?package=ResultObject&version=1.0.0

// Install ResultObject as a Cake Tool
#tool nuget:?package=ResultObject&version=1.0.0                

ResultObject

Overview

The ResultObject package provides a utility to return errors instead of throwing exceptions, making it a generic and easy-to-use mechanism for handling the outcome of operations. It encapsulates either a success with a value or a failure with error information, promoting cleaner code by eliminating the need for exception-based error handling and offering a clear distinction between success and failure scenarios.

Features

  • Success/Failure Handling: Easily handle success or failure outcomes of operations.
  • Generic Result Type: Support for results containing any type of value.
  • Error Handling: Encapsulate detailed error information on failure.
  • Type-Safe Failures: Transform failure results into a different value type while preserving error information.
  • Implicit Operators: Simplify result handling with implicit conversions between results and values.

Installation

You can install the ResultObject package via NuGet. To do this, use the following command in your project.

DotNet CLI:

dotnet add package ResultObject

Alternatively, you can add it to your project using the Visual Studio package manager UI by searching for " ResultObject."

Usage

Basic Result Example

The Result<T> class represents the outcome of an operation. It can either be a success (containing a value) or a failure (containing error details).

var successResult = Result.Success(42);

var failureResult = Result.Failure<int>(new ResultError("404", "Not Found", "The requested resource was not found."));
// or
var failureResult = Result.Failure<int>("404", "Not Found", "The requested resource was not found.");

Checking Success or Failure

You can check whether an operation was successful or failed using the IsSuccess and IsFailure properties.

if (successResult.IsSuccess)
{
    Console.WriteLine("Operation succeeded with value: " + successResult.Value);
}

if (failureResult.IsFailure)
{
    Console.WriteLine($"Operation failed with error: {failureResult.Error.Message}");
}

Implicit Conversions

The Result<T> class supports implicit conversions between results and values for convenience.

int myValue = Result.Success(100);  // Implicit conversion from result to value.
Result<int> result = 200;  // Implicit conversion from value to result.

Handling Errors

The ResultError class encapsulates details about the failure. This includes an error code, a reason, and a message.

var error = new ResultError("500", "Server Error", "An unexpected error occurred.");
var failedResult = Result.Failure<int>(error);

API Reference

Result<T>

Represents the result of an operation with the following properties:

  • IsSuccess: Indicates if the operation succeeded.
  • IsFailure: Indicates if the operation failed.
  • Value: The result value if the operation succeeded (or null if it failed).
  • Error: Contains error details if the operation failed (or null if it succeeded).
Methods
  • ToFailureResult<TValue>(): Converts a failure result to a result with a different value type while preserving error details.

Result

Helper class to create Result<T> instances:

  • Success(): Creates an empty success result, this can be used to represent a successful operation that doesn't return a value.
  • Success<T>(T value): Creates a success result with value.
  • Failure<T>(ResultError error): Creates a failure result with error details.
  • Failure<T>(string code, string reason, string message): Shorthand to creates a failure result with error details.

ResultError

Represents an error with the following properties:

  • Code: A string representing the error code.
  • Reason: A brief reason for the error.
  • Message: A detailed message explaining the error.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ResultObject:

Package Downloads
MailFusion

A modern, flexible email delivery library for .NET that simplifies sending emails through various providers like SendGrid and Amazon SES. Features include templating support with Scriban, HTML/plain-text email formatting, development environment support, comprehensive error handling, and strongly-typed models. Perfect for applications requiring reliable email delivery with provider flexibility and robust template management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 93 11/16/2024
1.1.2 50 11/15/2024
1.1.1 48 11/15/2024
1.1.0 49 11/15/2024
1.0.4 152 10/14/2024
1.0.3 63 10/14/2024
1.0.2 95 10/9/2024
1.0.1 82 9/8/2024
1.0.0 75 9/7/2024