LCSoft.Results
0.1.2
dotnet add package LCSoft.Results --version 0.1.2
NuGet\Install-Package LCSoft.Results -Version 0.1.2
<PackageReference Include="LCSoft.Results" Version="0.1.2" />
<PackageVersion Include="LCSoft.Results" Version="0.1.2" />
<PackageReference Include="LCSoft.Results" />
paket add LCSoft.Results --version 0.1.2
#r "nuget: LCSoft.Results, 0.1.2"
#:package LCSoft.Results@0.1.2
#addin nuget:?package=LCSoft.Results&version=0.1.2
#tool nuget:?package=LCSoft.Results&version=0.1.2
LCSoft: Results Pattern for .NET
A robust, flexible, and type-safe result and error handling library for .NET applications. Provides unified success/failure result types with rich error information and functional pattern matching for clean, expressive code.
✨ Features
- Unified result types for success/failure handling
- Generic Results<TValue> and non-generic Results classes
- Simplified alternative via sResults<TValue> and sResults using the lightweight Error record
- Encapsulation of success value or error with rich error details
- Functional Match methods supporting both Func and Action delegates
- Implicit conversion operators for easy construction from values or errors
- Abstract ErrorsType base class with error code, name, and registry
- Standard predefined error types via StandardErrorType
- Detailed Error record with error code, message, and categorized error type
- ErrorType enum classifying error categories
- Interfaces IResult<TError> and IResult<TValue, TError> with default implementations
- Explicit interface implementations for polymorphic usage
- Centralized error code registry and lookup by code
- Nullable callback support in Match methods
- Designed for testability and code coverage compatibility
- Extensible architecture for custom error and result handling
- Suitable for layered and domain-driven design applications
📦 Installation
Install via NuGet:
dotnet add package LCSoft.Results
Or via Package Manager:
Install-Package LCSoft.Results
🚀 Quick Start
Basic Usage
using LCSoft.Results;
Results result = DoSomething();
if (result.IsSuccess)
{
Console.WriteLine("Success!");
}
else
{
Console.WriteLine($"Failed: {result.Error}");
}
With a Value
Results<int> result = Calculate();
if (result.IsSuccess)
{
int value = result.Value;
Console.WriteLine($"Result: {value}");
}
else
{
Console.WriteLine($"Error: {result.Error}");
}
Non-generic Result
Results result = Results.Success();
result.Match(
success: () => Console.WriteLine("Operation succeeded!"),
failure: error => Console.WriteLine($"Failed with error: {error.Name}")
);
Generic Result with Value
Results<int> result = 42; // implicit success
int value = result.Match(
onSuccess: val => val * 2,
onFailure: error => -1
);
Console.WriteLine(value); // 84
Handling Errors
var error = StandardErrorType.Validation;
Results failureResult = Results.Failure(error);
failureResult.Match(
success: () => Console.WriteLine("This won't be called."),
failure: err => Console.WriteLine($"Validation failed: {err.Name}")
);
Using sResults and sResults<T>
For lighter scenarios, you can use sResults and sResults<T> which use a simplified Error record structure.
sResults<int> result = 10;
result.Match(
success: val => Console.WriteLine($"Success with value {val}"),
failure: err => Console.WriteLine($"Failed with code {err.Code}")
);
Extending Errors
public sealed class CustomErrorType : ErrorsType
{
public static readonly CustomErrorType CustomFailure = new(100, "CustomFailure");
private CustomErrorType(int code, string name) : base(code, name)
{
Register(this);
}
}
Use ErrorsType.FromCode(code) to retrieve error types dynamically.
Interfaces
- IResult<TError> for basic result contracts
- IResult<TValue, TError> for generic result contracts
- Both provide functional Match methods with success and failure delegates
✅ Example
Results<int> ParseNumber(string input)
{
if (int.TryParse(input, out int number))
return Results<int>.Success(number);
return Results<int>.Failure(StandardErrorType.GenericFailure);
}
📄 License
This project is licensed under the CC-BY-NC-ND-4.0 License. See the LICENSE file for details.
🙌 Contributing
Contributions are welcome! Please open issues or submit pull requests on GitHub.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 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 is compatible. 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. |
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LCSoft.Results:
| Package | Downloads |
|---|---|
|
LCSoft.ApiKey
Implementation to use ApiKey Authorization .NET API applications. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 0.1.0: Initial release with basic features.
Version 0.1.1: Minor fixes, cleanup readme
Version 0.1.2: Add compatibility to net6.0, net7.0, net8.0, and net9.0