ResultObject 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package ResultObject --version 1.0.4                
NuGet\Install-Package ResultObject -Version 1.0.4                
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.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResultObject --version 1.0.4                
#r "nuget: ResultObject, 1.0.4"                
#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.4

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

ResultObject

<img src="https://raw.githubusercontent.com/ahmedkamalio/DotNet.ResultObject/main/icon.png" alt="ResultObject Icon" width="100" height="100">

Overview

The ResultObject package provides a utility to handle the outcome of operations, either as a success or failure, without relying on exceptions. This promotes cleaner code by clearly separating success from failure scenarios and encapsulating error information.

Features

  • Success/Failure Handling: Easily manage the outcome of operations with type-safe results.
  • Generic Result Type: Results can encapsulate any type of value.
  • Error Handling: Detailed error information available in case of failures.
  • Type-Safe Casting: Use Cast<T>() to transform the result's value while maintaining error information.
  • Nullable Value Access: Retrieve values or errors directly without throwing exceptions.

Installation

You can install the ResultObject package via NuGet:

dotnet add package ResultObject

Alternatively, use the Visual Studio package manager UI to search for "ResultObject."

Usage

Basic Result Example

var successResult = Result.Success(42);
var failureResult = Result.Failure<int>("404", "Not Found", "The requested resource was not found.");

Checking Success or Failure

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

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

Type Casting Results

Use Cast<T>() to cast the value to a different type while preserving error details on failure.

var result = Result.Success<object>("Test Value");
var castResult = result.Cast<string>();

if (castResult.IsSuccess)
{
    Console.WriteLine("Successfully cast value: " + castResult.Value);
}

Error Handling

Create and inspect detailed errors with ResultError.

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

Console.WriteLine($"Error Code: {failedResult.Error.Code}");

Handling Nullable Values

A result with a null value is treated as a failure.

var result = Result.Success<string?>(null);

if (result.IsFailure)
{
    Console.WriteLine("Result is not successful.");
}

API Reference

IResult<TValue>

  • IsSuccess: Indicates if the operation succeeded.
  • IsFailure: Indicates if the operation failed.
  • Value: The value of the operation if successful, otherwise null.
  • Error: The error information if the operation failed, otherwise null.

Result<TValue>

  • Methods:
    • Cast<T>(): Safely cast the result's value to a different type, or propagate the error if the cast fails.

Result

  • Static Methods:
    • Success<TValue>(TValue value): Creates a success result.
    • Failure<TValue>(ResultError error): Creates a failure result with error details.
    • Failure<TValue>(string code, string reason, string message): Creates a failure result with a detailed error.

ResultError

  • Properties:
    • Code: A code representing the type of error.
    • Reason: A brief reason for the error.
    • Message: A detailed message describing 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 96 11/16/2024
1.1.2 51 11/15/2024
1.1.1 49 11/15/2024
1.1.0 50 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