RoyalCode.OperationResult.Serialization
2.0.0
dotnet add package RoyalCode.OperationResult.Serialization --version 2.0.0
NuGet\Install-Package RoyalCode.OperationResult.Serialization -Version 2.0.0
<PackageReference Include="RoyalCode.OperationResult.Serialization" Version="2.0.0" />
paket add RoyalCode.OperationResult.Serialization --version 2.0.0
#r "nuget: RoyalCode.OperationResult.Serialization, 2.0.0"
// Install RoyalCode.OperationResult.Serialization as a Cake Addin #addin nuget:?package=RoyalCode.OperationResult.Serialization&version=2.0.0 // Install RoyalCode.OperationResult.Serialization as a Cake Tool #tool nuget:?package=RoyalCode.OperationResult.Serialization&version=2.0.0
OperationResult
Description
The OperationResult library is an essential component for handling the return of operations in systems developed in .Net. Designed to simplify the communication of success or failure between different parts of the system, such as: use cases, APIs, Controllers and HTTP requests, it offers a few main structs/classes, for different situations, called OperationResult, ValidableResult, ResultMessage and ResultErros, also, it supports conversion to ProblemDetails and has components for serialization and deserialization.
Key features
- Standardized returns:
OperationResult
is a struct that contains the result of an operation, or function, of a system. In case of failure the return will contain messages to inform the problem occurred. This is lighter than firing exceptions. - Generic Return: The struct
OperationResult
also has its own generic version,OperationResult<TValue>
, allowing you to return results from different types of operations in your system consistently and efficiently. - Volatile results: The struct
ValidableResult
allows you to add error messages, allowing you to use them in validation scenarios. - Standardized messages: The library facilitates the creation of standardized messages through the
ResultMessage
class. - Error Codes: The
ResultMessage
class allows you to associate meaningful error codes with the results of unsuccessful operations, making it easier to identify and deal with problems. - Error Description and Additional Data: In addition to error codes, you can include detailed descriptions of the errors that occurred, including additional information, which helps debugging and troubleshooting faster and more efficiently.
- Results for Minimal API and Controllers: The library offers features for generating results suitable for systems based on Minimal API and Controllers, speeding up development and making interaction with the presentation layer easier.
- Conversion to
ProblemDetails
: The conversion of results into objects of typeProblemDetails
is facilitated by the library, allowing the standardization of error presentation according to the RFC 7807 specification. - Serialization and Deserialization: The
ResultMessage
class provides support for serialization and deserialization, making the exchange of information between different system components more practical and consistent. - Conversion from HTTP responses: The library has methods to generate
OperationResult
from HTTP responses, deserilizing the messages and convertingProblemDetails
, also supporting plain text. - Implicit operation e convertions: There are implicit operations to, for example, use
+=
to add messages to the error collection and conversions between errors, message values and return types. - Monad aspect: The
OperationResult
encapsulates the value or collection of errors depending on whether the result is success or failure. To work with these values, there are methods to extract or convert them.
How to use
It is simple to start using the OperationResult library in your project:
Install the NuGet package:
dotnet add package RoyalCode.OperationResult
.Import the namespace into your code:
using OperationResults;
- Create a method that returns an
OperationResult<T>
and returns an error message or the value.:
public OperationResult<MyModel> DoSomething(string input)
{
if (string.IsNullOrEmpty(input))
return ResultMessage.InvalidParameter("Some error message", nameof(input));
return new MyModel(input);
}
4 - You can also return an OperationResult
in minimal APIs, using methods for converting to IResult
:
// MapPost("/products")
public static async Task<CreatedMatch<Product>> Create(
ProductDto productDto /* FromBody */,
IProductService productService /* FromServices */)
{
OperationResult<Product> result = await productService.CreateProductAsync(productDto);
return result.CreatedMatch(p => $"products/{p.Id}");
}
5 - There are implicit conversions for the 'Match' types in the library, such as the example for OkMatch<T>
:
// MapGet("/products/{id}")
public static OkMatch<Product> Get(int id, IProductService productService)
{
Product? product = productService.Find(id);
if (product is null)
return ResultMessage.NotFound("Product not found", nameof(id));
return product;
}
6 - You can also convert an HttpResponseMessage
to an OperationResult
:
var response = await client.GetAsync("api/products/1");
OperationResult<ProductDto> result = await response.ToOperationResultAsync<ProductDto>();
See more details in the documentation.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 is compatible. 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
- RoyalCode.OperationResult (>= 2.0.0)
- System.Collections.Immutable (>= 6.0.0)
- System.Text.Json (>= 6.0.0)
-
net6.0
- RoyalCode.OperationResult (>= 2.0.0)
-
net7.0
- RoyalCode.OperationResult (>= 2.0.0)
-
net8.0
- RoyalCode.OperationResult (>= 2.0.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on RoyalCode.OperationResult.Serialization:
Package | Downloads |
---|---|
RoyalCode.OperationResult.ApiResults
Extensions methods for adapt operations results objects to minimal api http results. |
|
RoyalCode.OperationResult.Http
Extension methods for deserialize operations results objects from http responses. |
|
RoyalCode.OperationResult.ProblemDetails
Extensions methods for adapt operations results objects to problem details. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 230 | 3/13/2024 |
2.0.0-rc9.15 | 172 | 11/15/2023 |
2.0.0-rc9.14 | 73 | 11/6/2023 |
2.0.0-rc9.13 | 71 | 11/3/2023 |
2.0.0-rc9.12 | 59 | 11/3/2023 |
2.0.0-rc9.11 | 98 | 8/25/2023 |
2.0.0-rc9 | 236 | 8/2/2023 |
2.0.0-rc8 | 253 | 7/24/2023 |
2.0.0-rc7 | 235 | 7/22/2023 |
2.0.0-rc6 | 204 | 7/20/2023 |
2.0.0-rc5 | 175 | 7/19/2023 |
2.0.0-rc4 | 177 | 7/16/2023 |
2.0.0-rc3 | 188 | 7/16/2023 |
2.0.0-rc2 | 198 | 7/7/2023 |
2.0.0-rc1 | 163 | 7/7/2023 |
2.0.0-rc-9.20 | 54 | 3/11/2024 |
2.0.0-preview-2 | 202 | 7/6/2023 |
2.0.0-preview-1 | 182 | 3/2/2023 |