RA.Utilities.Core.Results
RA.Utilities.Core.Results is a lightweight .NET library designed to enhance error handling by providing a functional Result type.
It helps you write cleaner, more predictable, and more robust code by avoiding exceptions for expected operational failures.
This approach allows for a clear and explicit representation of success or failure, making your application's control flow easier to follow and maintain.
๐ฏ Purposeโ
In many applications, exceptions are used for both unexpected system faults (e.g., NullReferenceException) and predictable business-level failures (e.g., "user not found").
This can lead to complex try-catch blocks and makes it difficult to distinguish between recoverable errors and true exceptions.
The Result type solves this by providing a wrapper that explicitly represents one of two outcomes:
- Success: The operation completed successfully, and may contain a value.
- Failure: The operation failed, and contains an
Exceptiondetailing the error.
This pattern encourages you to handle failures as part of your normal code flow, leading to more readable and resilient applications.
๐ ๏ธ Installationโ
You can install the package via the .NET CLI:
dotnet add package RA.Utilities.Core.Results
Or through the NuGet Package Manager console:
Install-Package RA.Utilities.Core.Results
Or through the NuGet Package Manager in Visual Studio.
When to use Results Pattern and when to use Exceptionsโ
| Layer | Use Results Pattern | Use Exceptions |
|---|---|---|
| โ Application Layer | โ Yes | โ Avoid |
| โ Domain Layer | โ Yes | โ Avoid |
| ๐ Infrastructure | โ Rarely | โ Yes |
| ๐ External APIs | โ | โ Required |
| ๐งช Tests | โ Preferred | โ Avoid |
| Situation | Use |
|---|---|
| Input validation failed | โ Result pattern |
| Business rule not satisfied | โ Result pattern |
| DB connection failed | โ ๏ธ Exception |
| File not found (unexpected) | โ ๏ธ Exception |
| You want to chain logic steps | โ Result pattern |
| You need a stack trace. | โ ๏ธ Exception |
The Result Typeโ
Result: Represents the outcome of an operation that does not return a value. It can be either a success or a failure.Result<T>: Represents the outcome of an operation that returns a value of typeT. It can be a success holding aTvalue, or a failure holding anException.
Chaining methods (Map, Bind, OnSuccess, OnFailure, Match and their async counterparts) are extension methods declared in the RA.Utilities.Core.Results.Extensions namespace.
The extension methods lived in RA.Utilities.Core.Results up to and including 10.0.0 and moved to RA.Utilities.Core.Results.Extensions in 10.0.1 โ add using RA.Utilities.Core.Results.Extensions; when upgrading. See the migration guide.
Factory Methodsโ
Result.Success(): Creates a success result.Result.Failure(Exception exception): Creates a failure result.
ResultExtensions
The Result and Result types get their extension methods from the RA.Utilities.Core.Results.Extensions namespace.
Results
The Result type is a foundational class designed to handle operational outcomes in a clear, explicit, and robust manner.
Migration Guides
Use this page when upgrading RA.Utilities.Core.Results between versions that introduce breaking changes.