Toolsfactory.Common.Result 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Toolsfactory.Common.Result --version 1.0.0                
NuGet\Install-Package Toolsfactory.Common.Result -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="Toolsfactory.Common.Result" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Toolsfactory.Common.Result --version 1.0.0                
#r "nuget: Toolsfactory.Common.Result, 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 Toolsfactory.Common.Result as a Cake Addin
#addin nuget:?package=Toolsfactory.Common.Result&version=1.0.0

// Install Toolsfactory.Common.Result as a Cake Tool
#tool nuget:?package=Toolsfactory.Common.Result&version=1.0.0                

Toolsfactory.Common Result and Result<T> utility library

The Result and Result<T> classes provide a robust way to represent the outcome of an operation in C#. This approach ensures operations can communicate success, failure, and relevant error details without relying on exceptions.

Key Features

  • Encapsulation of Success/Failure:

    • The Result class encapsulates whether an operation was successful or not (IsSuccess).
    • Access to associated errors through the Errors collection.
  • Generic Support:

    • The Result<T> class extends Result to include a value (T) when the operation succeeds.
  • Immutable Design:

    • Both classes are designed to ensure immutability, enhancing reliability and thread safety.

Class Details

Result

  • Indicates whether an operation succeeded (IsSuccess) or failed (IsFaulted).
  • Provides a collection of errors (IReadOnlyList<Error>) if the operation fails.
  • Constructor overloading allows creating success results or faulted results with error details.

Result<T>

  • Extends Result by adding a value (Value) of type T for successful operations.
  • Throws an exception if you try to access the Value of a failed operation.
  • Ensures Value is cleared upon failure to prevent misuse.

Usage Examples

Creating a Success Result

var successResult = Result.Success();

Creating a Faulted Result

var faultedResult = Result.Failure(new Error("Operation failed"));

Using Result<T> with a Value

var resultWithValue = Result<int>.Success(42);
if (resultWithValue.IsSuccess)
{
    Console.WriteLine(resultWithValue.Value); // Output: 42
}

Using Result<T> with implicit conversion

record Person(string Name, int Age);
Result<Person> GetPersonWithName(string Name)
{
    if (string.IsNullOrWhiteSpace(Name))
    {
        return Result.Failure<Person>(new Error("Name cannot be empty"));
    }
    var person = PersonDB.Where(p => p.Name == Name).FirstOrDefault(); // <= Assume PersonDB is a list of Person records
    return person != null ? person : new Error("Person not found"); // <= Implicit conversion to Result<Person>
}

Handling Errors

if (result.IsFaulted)
{
    foreach (var error in result.Errors)
    {
        Console.WriteLine(error.Message);
    }
}

Extensibility

Custom error handling or additional utilities can be added using the ResultExtensions class.


Let me know if you'd like further details added or the text tailored for a specific use case!

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.
  • 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.0.7 85 12/19/2024
1.0.5 100 12/13/2024
1.0.4 95 12/7/2024
1.0.1 99 12/6/2024
1.0.0 89 12/6/2024