Divino.OperationResult 5.0.0

dotnet add package Divino.OperationResult --version 5.0.0
                    
NuGet\Install-Package Divino.OperationResult -Version 5.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="Divino.OperationResult" Version="5.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Divino.OperationResult" Version="5.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Divino.OperationResult" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Divino.OperationResult --version 5.0.0
                    
#r "nuget: Divino.OperationResult, 5.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.
#:package Divino.OperationResult@5.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Divino.OperationResult&version=5.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Divino.OperationResult&version=5.0.0
                    
Install as a Cake Tool

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.

Requirements

  • Target frameworks: net8.0, net10.0, net11.0.
  • Any C# version can use the properties, factories and implicit conversions.
  • Pattern matching on the union cases needs C# 15. That is the default on net11.0; on net8.0/net10.0, add <LangVersion>15</LangVersion> (requires the .NET 11 SDK).

OperationResult Namespace

Since 5.0, both results are C# 15 union types. A result holds exactly one of its cases:

public union Result(Ok, Exception);
public union Result<T>(T, Exception);

Ok is the success marker of the non-generic Result (Ok.Instance).

Properties

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; }

Access the union contents (T/Ok, an Exception, or null for default).

public object? Value { get; }

Methods

Result.Success();                  // Result
Result.Error(exception);           // Result
Result.Success<T>(value);          // Result<T>
Result.Error<T>(exception);        // Result<T>

result.TryGetValue(out T value);   // Result<T>: typed access to the value
result.GetValueOrDefault();        // Result<T>
result.GetValueOrDefault(fallback);
result.ErrorIs<TException>();

Success values cannot be null, and Error doesn't accept a null exception. Both throw ArgumentNullException.

Implicit conversions

Return the value or the exception directly:

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

Pattern matching (C# 15)

Type patterns look inside the union, and the compiler checks that every case is handled:

string message = IncrementOne(2) switch
{
    int value => $"Got {value}",
    ArgumentOutOfRangeException => "Too big",
    Exception error => error.Message,
    null => "No result",   // default(Result<int>)
};

Prefer a null arm over _, so that the compiler can still warn you about unhandled cases.

Deconstructing a Result

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

    // Do something with the data.
}

Migrating from 4.x

5.0 is a breaking release: Result<T>.Value is now the object? union contents (use TryGetValue/GetValueOrDefault), the public constructors were replaced by factories, and null success values are rejected.

The package ships an agent skill, operationresult-migration, which walks an AI coding agent through the migration. After restore, it is at ~/.nuget/packages/divino.operationresult/5.0.0/skills/operationresult-migration/SKILL.md (see Delivering skills in NuGet packages). You can also read it in this repository: skills/operationresult-migration/SKILL.md.

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.  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.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Divino.OperationResult:

Package Downloads
Divino.OperationResultExtensions

This package contains useful extensions for the OperationResult (Divino.OperationResult).

RabbitMq.Connector

This package is an implementation of the connection whit RabbitMQ that using MediatR package to publish/consumer events.

Jwellington.MessageBroker.Abstractions

Package Description

VoicePickCodeCalculator

The Voice Pick Code is printed on the PTI label, and designed to integrate with the voice picking systems popular in large warehouse management systems (WMS). The Voice Pick Code itself is a 4 digit digest1 of the GTIN and Lot Code (and optional Date) that is calculated using a well proven, standard algorithm.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 338 11/18/2026
4.0.1 393,292 7/5/2024
4.0.0 899 6/24/2024
3.1.0 165,016 11/4/2022
3.0.0 71,128 11/9/2021
2.0.2 3,463 6/4/2021
2.0.1 26,150 8/29/2020
1.0.2 2,021 6/23/2020
1.0.1 1,364 4/14/2020
1.0.0 1,328 4/14/2020

## Version 5.0.0 ##
     - Result and Result<T> are now C# 15 union types: Result(Ok, Exception) and Result<T>(T, Exception)
     - Exhaustive pattern matching: result switch { T value => ..., Exception error => ..., null => ... }
     - Target frameworks: net8.0;net10.0;net11.0
     - Breaking: Value is now object? (the union contents). Use TryGetValue/GetValueOrDefault for the typed value
     - Breaking: public constructors replaced by Success/Error factories and implicit conversions
     - Breaking: successful values cannot be null; default(Result) is neither success nor error
     - Ships an agent skill (skills/operationresult-migration) to migrate code from 4.x
     ## Version 4.0.1 ##
     - Fixed Result Success method to avoid need of Cast
     ## Version 4.0.0 ##
     - Dropped support from netstandard2.0;net5.0 in favor to use new C# features that require new TFM version
     - Enriched interfaces to reduce need to use Reflection in some scenarios.
     ## Version 3.1.0 ##
     - Added interface to make it easy to identify the Result, even when it is generic version
     ## Version 3.0.0 ##
     - Target frameworks: netstandard2.0;net5.0;net6.0
     - Anotated properties with Nullable attributes
     ## Version 2.0.2 ##
     Throw ArgumentNullException