Somum.Results
10.1.6
dotnet add package Somum.Results --version 10.1.6
NuGet\Install-Package Somum.Results -Version 10.1.6
<PackageReference Include="Somum.Results" Version="10.1.6" />
<PackageVersion Include="Somum.Results" Version="10.1.6" />
<PackageReference Include="Somum.Results" />
paket add Somum.Results --version 10.1.6
#r "nuget: Somum.Results, 10.1.6"
#:package Somum.Results@10.1.6
#addin nuget:?package=Somum.Results&version=10.1.6
#tool nuget:?package=Somum.Results&version=10.1.6
Somum.Results
Somum.Results is a .NET Standard 2.0 class library that provides a consistent result model for operations that may succeed or fail.
It separates an operation's success state, returned value, HTTP status code, optional application error code, and structured errors. Use it across handlers, services, and API boundaries to return expected failures without relying on exceptions for normal control flow.
Target Framework
- .NET Standard 2.0
The library can be referenced from .NET Standard-compatible libraries and modern .NET applications.
Core Types
| Type | Purpose |
|---|---|
Result |
Represents the outcome of an operation that does not return a value. |
Result<T> |
Represents the outcome of an operation that returns a value of type T on success. |
ResultErrors |
Stores an HTTP status code, an optional application error code, and a collection of structured errors. |
ResultError |
Describes one validation or processing error associated with a property or operation. |
Success and Failure Semantics
A Result or Result<T> is successful when its ResultErrors collection contains no ResultError entries:
if (result.IsSuccess)
{
// Process result.Value for Result<T>.
}
if (result.IsFailure)
{
// Inspect result.Errors.
}
ResultErrors.IsSuccess is determined by the number of errors, not by the HTTP status code. This allows callers to associate a status code with a result independently of whether the error collection is empty.
Creating Results
Successful operations
Use the static factory methods for explicit success results:
using System.Net;
using Somum.Results;
Result completed = Result.Success();
Result created = Result.Success(HttpStatusCode.Created);
Result<long> identifier = Result.Success(42L, HttpStatusCode.Created);
Result<string> name = Result<string>.Success("Campaign managers");
A value can also be implicitly converted to Result<T>:
Result<string> result = "Campaign managers";
Failed operations
Create structured errors with ResultError or ResultErrors:
using System.Net;
using Somum.Results;
var errors = new ResultErrors(HttpStatusCode.BadRequest, "validation_failed")
.AddError("name", "Name is required.")
.AddError("roles", "At least one role is required.");
Result failure = Result.Failure(errors);
Result<long> typedFailure = errors;
A single ResultError can be combined with an HTTP status code:
ResultErrors errors = new ResultError("name", "Name is required.")
+ HttpStatusCode.BadRequest;
Error Details
A ResultError contains:
Prop— the affected property name or an operation-level identifier.Message— the error message or message-template key.Args— optional values used to format the message.
ResultErrors exposes errors as IReadOnlyList<ResultError>. Add errors through AddError; do not mutate the returned collection.
Status Codes and Composition
Use the + operator to return a copy of a result or error collection with a different HTTP status code:
using System.Net;
using Somum.Results;
Result<string> result = "Created";
result += HttpStatusCode.Created;
ResultErrors errors = new();
errors = errors + HttpStatusCode.UnprocessableEntity;
For Result<T>, adding a ResultError appends that error to the existing result and causes IsFailure to become true:
Result<string> result = "value";
result += new ResultError("value", "The value is invalid.");
Asynchronous Interoperability
Result, Result<T>, and ResultErrors provide implicit conversions to completed Task<Result> or Task<Result<T>> instances. This is useful when a method returns a task but its outcome is already available:
Task<Result> ValidateAsync(bool isValid)
{
if (isValid)
return Result.Success();
return new ResultError("request", "Request is invalid.")
+ HttpStatusCode.BadRequest;
}
Prefer async/await when actual asynchronous work is required.
Usage
Reference the project from another project in the solution:
<ItemGroup>
<ProjectReference Include="..\Somum.Results\Somum.Results.csproj" />
</ItemGroup>
Guidelines
- Use results for expected validation, authorization, and business-rule failures.
- Use exceptions for unexpected or unrecoverable failures.
- Check
IsSuccessbefore readingResult<T>.Value; failed results can contain the default value ofT. - Preserve the HTTP status code and application error code when propagating failures.
- Use stable property identifiers and message keys when errors are consumed by clients or localization layers.
| Product | Versions 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 was computed. 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 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Somum.Results:
| Package | Downloads |
|---|---|
|
Somum.Requests
Base for handling API requests in the Somum API. |
GitHub repositories
This package is not used by any popular GitHub repositories.