MadEyeMatt.Results
1.0.0
See the version list below for details.
dotnet add package MadEyeMatt.Results --version 1.0.0
NuGet\Install-Package MadEyeMatt.Results -Version 1.0.0
<PackageReference Include="MadEyeMatt.Results" Version="1.0.0" />
paket add MadEyeMatt.Results --version 1.0.0
#r "nuget: MadEyeMatt.Results, 1.0.0"
// Install MadEyeMatt.Results as a Cake Addin #addin nuget:?package=MadEyeMatt.Results&version=1.0.0 // Install MadEyeMatt.Results as a Cake Tool #tool nuget:?package=MadEyeMatt.Results&version=1.0.0
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.
Result
Result<int>
- 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.
Error
Success
- 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.
// Create a successful result without a value.
Result result = Result.Ok();
// Create a successful result with a value.
Result<int> result = Result<int>.Ok(42);
// Create a failed result without a value.
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.
// Handle the return value of a result without a value.
Result result = PerformOperation();
// Print all error messages, if the result is failed.
if(result.IsFailed)
{
foreach(IError error in result.Errors)
{
Console.WriteLine(error.Message);
}
}
// Print all success messages, if the result is successful.
if(result.IsSuccessful)
{
foreach(ISuccess success in result.Successes)
{
Console.WriteLine(success.Message);
}
}
// Handle the return value of a result with a value.
Result<int> result = PerformOperation();
// Get the value if the result is successful or will throw if the result is failed.
int value = result.Value;
// Get the value oif the result is successful or will return the default of the value.
int value = result.GetValueOrDefault();
Future
- Store hierarchical error chain in a root error cause.
- Support custom error and success types.
References
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- JetBrains.Annotations (>= 2023.3.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on MadEyeMatt.Results:
Package | Downloads |
---|---|
MadEyeMatt.Results.FluentAssertions
A result object implementation. |
|
MadEyeMatt.Results.AspNetCore
A result object implementation. |
GitHub repositories
This package is not used by any popular GitHub repositories.