Zircon.Results
2.0.0
dotnet add package Zircon.Results --version 2.0.0
NuGet\Install-Package Zircon.Results -Version 2.0.0
<PackageReference Include="Zircon.Results" Version="2.0.0" />
<PackageVersion Include="Zircon.Results" Version="2.0.0" />
<PackageReference Include="Zircon.Results" />
paket add Zircon.Results --version 2.0.0
#r "nuget: Zircon.Results, 2.0.0"
#:package Zircon.Results@2.0.0
#addin nuget:?package=Zircon.Results&version=2.0.0
#tool nuget:?package=Zircon.Results&version=2.0.0
Zircon.Results
A robust result pattern implementation for C# applications providing a clean way to handle success and failure outcomes with comprehensive error aggregation and exception handling.
Features
- Result Pattern Implementation: Clean separation of success and failure states
- Error Aggregation: Collect and manage multiple error messages
- Exception Handling: Automatic error extraction from exceptions
- Generic Support: Strongly typed results with
Result<T>for operations returning values - Extensible Error Extraction: Pluggable system for custom exception error extraction
Installation
dotnet add package Zircon.Results
Basic Usage
Simple Operations
using Zircon.Results;
// Success case
var successResult = Result.Success();
if (successResult.IsSuccess)
{
Console.WriteLine("Operation completed successfully!");
}
// Failure with custom errors
var failureResult = Result.Failure(new[] { "Invalid input", "Missing required field" });
if (!failureResult.IsSuccess)
{
Console.WriteLine($"Errors: {failureResult.GetFormattedErrors("No errors")}");
}
Operations with Return Values
// Success with value
var successWithValue = Result<string>.Success("Hello, World!");
if (successWithValue.IsSuccess)
{
Console.WriteLine($"Result: {successWithValue.Value}");
}
// Failure for generic result
var failureWithValue = Result<int>.Failure(new[] { "Could not parse number" });
Exception Handling
try
{
// Some operation that might throw
var data = ProcessData();
return Result<string>.Success(data);
}
catch (Exception ex)
{
return Result<string>.Failure(ex);
}
Key Classes
Result
Base class for operation results without return values.
Properties:
IsSuccess: Indicates if the operation was successfulErrors: Collection of error messagesException: The original exception that caused failure (if any)
Methods:
Success(): Creates a successful resultFailure(Exception): Creates a failed result from an exceptionFailure(IEnumerable<string>): Creates a failed result with error messagesGetFormattedErrors(fallback, separator): Gets formatted error string
Result<T>
Generic result class for operations that return a value on success.
Additional Properties:
Value: The returned value (only valid whenIsSuccessis true)
Additional Methods:
Success(T value): Creates a successful result with a valueFailure(Exception): Creates a failed result from an exceptionFailure(IEnumerable<string>): Creates a failed result with error messages
Custom Error Extraction
Implement IExceptionErrorExtractor to provide custom error extraction logic for specific exception types:
public class ValidationExceptionExtractor : IExceptionErrorExtractor
{
public bool CanExtract(Exception exception) => exception is ValidationException;
public IEnumerable<string> ExtractErrors(Exception exception)
{
if (exception is ValidationException validationEx)
{
return validationEx.Errors.Select(e => e.ErrorMessage);
}
return new[] { exception.Message };
}
}
Best Practices
- Prefer Result Types: Use Result/Result<T> instead of throwing exceptions for expected failure scenarios
- Check Success: Always check
IsSuccessbefore accessingValuein Result<T> - Handle Errors Gracefully: Use
GetFormattedErrors()to present user-friendly error messages - Avoid Exceptions for Flow Control: Use Result pattern for business logic failures, reserve exceptions for unexpected errors
License
This project is licensed under the MIT License - see the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.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.