Results
A result object implementation. Return result objects from operations
to indicate success or failure instead of throwing exceptions.
Features
- Default result implementations with and without a result value.
- Supports multiple error messages in a result.
- Supports optional multiple success messages.
- Supports custom result implementations with and without a result value.
ResultBase<TResult>
ResultBase<TResult, TValue>
- Supports custom error and success implementations.
- Provides FluentAssertions extensions for simpler uni testing.
- Provides extensions to transform results to
IActionResult instances for ASP.NET controllers.
- Provides extensions to transform results to
IResult instances to be used with Minimal APIs.
Usage
Create Results
To create result instances use the static helper methods found in the
Result and Result<T>classes.
Result result = Result.Ok();
Result<int> result = Result<int>.Ok(42);
Result result = Result.Fail("An error occurred.");
Result result = Result.Fail(new Error("An error occurred));
// Create a failed result for a result that can have a value.
Result<int> result = Result<int>.Fail("An error occurred.");
Result<int> result = Result<int>.Fail(new Error("An error occurred));
The result type Result is typically used by operations that have no return value.
public Result PerformOperation()
{
if(this.State == State.Failed)
{
return Result.Fail("The operation failed.");
}
return Result.Ok();
}
The result type Result<T> is typically used by operations that have a return value.
public Result<int> PerformOperation()
{
if(this.State == State.Failed)
{
return Result<int>.Fail("The operation failed.");
}
return Result.Ok(42);
}
Process Results
To process the result of an operation you can check if the operation was
successful or failed by accewssing the IsSuccessful and/or IsFailed
properties.
Result result = PerformOperation();
if(result.IsFailed)
{
foreach(IError error in result.Errors)
{
Console.WriteLine(error.Message);
}
}
if(result.IsSuccessful)
{
foreach(ISuccess success in result.Successes)
{
Console.WriteLine(success.Message);
}
}
Result<int> result = PerformOperation();
int value = result.Value;
int value = result.GetValueOrDefault();
Future
- Store hierarchical error chain in a root error cause.
- Support custom error and success types.
References
Michael Altmann
FluentResults