StrongResult 1.0.2
dotnet add package StrongResult --version 1.0.2
NuGet\Install-Package StrongResult -Version 1.0.2
<PackageReference Include="StrongResult" Version="1.0.2" />
<PackageVersion Include="StrongResult" Version="1.0.2" />
<PackageReference Include="StrongResult" />
paket add StrongResult --version 1.0.2
#r "nuget: StrongResult, 1.0.2"
#:package StrongResult@1.0.2
#addin nuget:?package=StrongResult&version=1.0.2
#tool nuget:?package=StrongResult&version=1.0.2
StrongResult
StrongResult is a C# library for representing operation results with explicit success, failure, error, and warning states. It provides both generic (Result<T>) and non-generic (Result) result types for clear, type-safe, and expressive error handling.
Features
- Result Types: Use
Resultfor operations without a return value, andResult<T>for operations that return a value. - Success & Failure: Distinguish between hard success, partial success (with warnings), controlled errors, and hard failures.
- Warnings & Errors: Attach warnings and errors to results for detailed diagnostics.
- Immutability: All result types are immutable and thread-safe.
- Fluent API: Easily create, map, bind, and match results.
- Comprehensive Async Support: Full async/await support with
MapAsync,BindAsync,MatchAsync, and more. Works seamlessly withTaskandValueTask.
Quick Start
Installation
Add StrongResult to your project via NuGet:
dotnet add package StrongResult
Usage
Non-Generic Result
using StrongResult.NonGeneric;
// Hard success
var result = Result.Ok();
// Partial success with warnings
var warning = Warning.Create("W1", "Minor issue");
var partial = Result.PartialSuccess(warning);
// Controlled error
var error = Error.Create("E1", "Validation failed");
var controlled = Result.ControlledError(error, warning);
// Hard failure
var failure = Result.Fail(error);
Generic Result
using StrongResult.Generic;
// Hard success with value
var result = Result<string>.Ok("value");
// Partial success with value and warnings
var partial = Result<string>.PartialSuccess("value", warning);
// Controlled error with value type
var controlled = Result<string>.ControlledError(error, warning);
// Hard failure
var failure = Result<string>.Fail(error);
Handling Results
if (result.IsSuccess)
{
// Access result.Value for generic results
}
else
{
// Handle result.Error and result.Warnings
}
Implicit Conversion
string value = Result<string>.Ok("abc"); // Implicit conversion
Mapping and Binding
var mapped = result.Map(s => s.Length);
var bound = result.Bind(s => Result<int>.Ok(s.Length));
Async Operations
// Async mapping
var mapped = await result.MapAsync(async s => await TransformAsync(s));
// Async binding
var bound = await result.BindAsync(async s => await ValidateAsync(s));
// Async actions
await result
.OnSuccessAsync(async v => await SaveAsync(v))
.OnFailureAsync(async e => await LogAsync(e));
// Working with async results
var output = await GetResultAsync()
.MapAsync(x => x * 2)
.BindAsync(async x => await ProcessAsync(x));
See ASYNC_PATTERNS.md for comprehensive async examples.
Fluent Result Actions
OnSuccess
result.OnSuccess(() => Console.WriteLine("Success!"));
OnFailure
result.OnFailure(error => Console.WriteLine($"Failed: {error.Message}"));
OnWarnings
result.OnWarnings(warnings =>
{
foreach (var w in warnings)
Console.WriteLine($"Warning: {w.Message}");
});
ForEachWarning
result.ForEachWarning(warning => Console.WriteLine($"Warning: {warning.Code} - {warning.Message}"));
Result Kinds
HardSuccess: Operation completed successfully.PartialSuccess: Operation succeeded with warnings.ControlledError: Operation failed with expected/controlled error.HardFailure: Operation failed with unrecoverable error.
API Reference
ResultandResult<T>: Main result types.ErrorandWarning: Attach error and warning information with input validation.- Extension methods:
Map,Bind,Match,OnSuccess,OnFailure,OnWarnings,ForEachWarning, etc. - Async extension methods:
MapAsync,BindAsync,MatchAsync,OnSuccessAsync,OnFailureAsync,OnWarningsAsync,ForEachWarningAsyncwith support forTaskandValueTask.
Documentation
- Async Patterns Guide - Comprehensive async/await examples and best practices
Testing
Unit tests are provided for all core scenarios. See the StrongResult.Test project for examples.
Contributing
Contributions and feedback are welcome! Please open issues or submit pull requests on GitHub.
.NET 8 / C# 12 compatible
| 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 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. |
-
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.