https://github.com/fluxera/fluxera.results
A result object implementation.
https://github.com/fluxera/fluxera.results
ddd dotnet dotnet-core dotnetcore result result-type results
Last synced: about 1 year ago
JSON representation
A result object implementation.
- Host: GitHub
- URL: https://github.com/fluxera/fluxera.results
- Owner: fluxera
- License: mit
- Created: 2024-01-11T20:23:48.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-31T09:50:18.000Z (about 2 years ago)
- Last Synced: 2024-05-31T10:49:12.623Z (about 2 years ago)
- Topics: ddd, dotnet, dotnet-core, dotnetcore, result, result-type, results
- Language: C#
- Homepage:
- Size: 105 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://dev.azure.com/fluxera/Foundation/_build/latest?definitionId=96&branchName=main)
# 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```
- Supports multiple error messages.
- Supports optional multiple success messages.
- Supports custom error and success implementations.
- Provides [FluentAssertions](https://fluentassertions.com/) extensions for simpler unit 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``` class.
```C#
// Create a successful result without a value.
Result result = Result.Ok();
// Create a successful result with a value.
Result result = Result.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 result = Result.Fail("An error occurred.");
Result result = Result.Fail(new Error("An error occurred));
```
The result type ```Result``` is typically used by operations that have no return value.
```C#
public Result PerformOperation()
{
if(this.State == State.Failed)
{
return Result.Fail("The operation failed.");
}
return Result.Ok();
}
```
The result type ```Result``` is typically used by operations that have a return value.
```C#
public Result PerformOperation()
{
if(this.State == State.Failed)
{
return Result.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 accessing the ```IsSuccessful``` or ```IsFailed```
properties.
```C#
// 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 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();
```
## References
[Michael Altmann](https://github.com/altmann)
[FluentResults](https://github.com/altmann/FluentResults)