RA.Utilities.Core
RA.Utilities.Core 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
Or through the NuGet Package Manager console:
Install-Package RA.Utilities.Core
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.
Factory Methodsโ
Result.Success(): Creates a success result.Result.Failure(Exception exception): Creates a failure result.
๐๏ธ ResultExtensions
The ResultExtensions class provides a set of powerful extension methods for the
๐๏ธ Results
The Result type is a foundational class designed to handle operational outcomes in a clear, explicit, and robust manner.