Divino.OperationResult
5.0.0
dotnet add package Divino.OperationResult --version 5.0.0
NuGet\Install-Package Divino.OperationResult -Version 5.0.0
<PackageReference Include="Divino.OperationResult" Version="5.0.0" />
<PackageVersion Include="Divino.OperationResult" Version="5.0.0" />
<PackageReference Include="Divino.OperationResult" />
paket add Divino.OperationResult --version 5.0.0
#r "nuget: Divino.OperationResult, 5.0.0"
#:package Divino.OperationResult@5.0.0
#addin nuget:?package=Divino.OperationResult&version=5.0.0
#tool nuget:?package=Divino.OperationResult&version=5.0.0
Operation Result
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; onnet8.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 | Versions 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. |
-
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 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