CS.Results 1.1.0

dotnet add package CS.Results --version 1.1.0                
NuGet\Install-Package CS.Results -Version 1.1.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="CS.Results" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CS.Results --version 1.1.0                
#r "nuget: CS.Results, 1.1.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 CS.Results as a Cake Addin
#addin nuget:?package=CS.Results&version=1.1.0

// Install CS.Results as a Cake Tool
#tool nuget:?package=CS.Results&version=1.1.0                

Result Object Pattern Library

This library provides a flexible and easy-to-use implementation of the Result Object Pattern in C#. It helps in handling success and error cases in a standardized way, improving code readability and reducing error-prone conditional logic.

Features

  1. Result Types

The library provides two main result types: Result and Result<TValue>. Result represents an operation that can succeed or fail without returning a value, while Result<TValue> represents an operation that can succeed with a value of type TValue or fail.

  1. Error Handling

Errors are handled using the IError interface, allowing for flexible and customizable error definitions. The library includes a basic Error implementation and allows for custom error types.

  1. Error Factory

An ErrorFactory class is provided to create common error types, such as validation errors, not found errors, and conflict errors. This makes it easy to generate standardized errors throughout your application.

  1. Handling Success and Failure in a fluent manner

The library provides synchronous Handle and asynchronous HandleAsync methods for handling the success and failure of operations. These methods take handlers for success and failure scenarios, making it easy to define what should happen in each case.

  1. Implicit Conversions

Implicit conversions are supported from IError to Result and from a value of type TValue to Result<TValue>. This feature simplifies the creation of result objects from errors and values.

  1. Error Matching

The library includes methods for matching errors by code, making it easy to identify specific errors and respond accordingly.

  1. Custom Errors

Users can define custom errors by implementing the IError interface. This allows for more detailed and application specific error handling.

  1. Immutable Results

Results are immutable, ensuring that once a result is created, its state (success or failure) and any associated errors cannot be changed. This immutability ensures consistency and predictability in error handling.

  1. Composable Error Collections

The library supports creating results with multiple errors, enabling comprehensive error reporting for operations that may encounter multiple issues.

  1. Extensibility

The design of the library allows for extensibility. Developers can create custom error types by implementing the IError interface.

Installation

dotnet add package CS.Results --version 1.1.0

Usage

Creating Results
Successful
var result = Result.Success();
var resultWithValue = Result<TestEntity>.Success(new TestEntity { Id = Guid.NewGuid(), Name = "Test Entity" });
Failure
var error = new CustomError("ERR01", "An error occurred");
var errorResult = Result.FromError(error);

var errors = new List<IError>
{
    new Error("ERR01", "First error"),
    new Error("ERR02", "Second error")
};
var multiErrorResult = Result.FromErrors(errors);
Checking Result Status
if (result.IsSuccess)
{
    Console.WriteLine("Operation was successful");
}
else
{
    Console.WriteLine("Operation failed");
}
Handling Results
Synchronous Handling
result.Handle(
    successHandler: () => Console.WriteLine("Success"),
    failureHandler: errors => Console.WriteLine($"Failure: {string.Join(", ", errors.Select(e => e.Message))}")
);

resultWithValue.Handle(
    successHandler: value => Console.WriteLine($"Success: {value.Name}"),
    failureHandler: errors => Console.WriteLine($"Failure: {string.Join(", ", errors.Select(e => e.Message))}")
);
Asynchronous Handling
await result.HandleAsync(
    successHandler: async () => Console.WriteLine("Success"),
    failureHandler: async errors => Console.WriteLine($"Failure: {string.Join(", ", errors.Select(e => e.Message))}")
);

await resultWithValue.HandleAsync(
    successHandler: async value => Console.WriteLine($"Success: {value.Name}"),
    failureHandler: async errors => Console.WriteLine($"Failure: {string.Join(", ", errors.Select(e => e.Message))}")
);

);

Error Matching
By Error Code as string:
if (result.MatchErrorByCode("ERR01"))
{
    Console.WriteLine("Error code ERR01 matched");
}
By Error as reference:
var customError = new CustomError("ER01", "Custom error");
result.MatchErrorByCode(customError);
Implicit Conversions
Result result = new CustomError("ERR01", "An error occurred");
Result<TestEntity> resultWithValue = new TestEntity { Id = Guid.NewGuid(), Name = "Test Product" };
Error Factory

The ErrorFactory class provides convenient methods to create common errors:

var notFoundError = ErrorFactory.NotFound("Item not found");
var validationError = ErrorFactory.ValidationFailure("Validation failed");
Unit Tests

This library includes extensive unit tests to verify its functionality. The tests are written using the XUnit framework and cover various scenarios for both Result and Result<TValue> classes.

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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 125 5/22/2024
1.0.2 121 5/21/2024
1.0.1 111 5/21/2024
1.0.0 116 5/21/2024