Yaver.Result
2.1.0
dotnet add package Yaver.Result --version 2.1.0
NuGet\Install-Package Yaver.Result -Version 2.1.0
<PackageReference Include="Yaver.Result" Version="2.1.0" />
<PackageVersion Include="Yaver.Result" Version="2.1.0" />
<PackageReference Include="Yaver.Result" />
paket add Yaver.Result --version 2.1.0
#r "nuget: Yaver.Result, 2.1.0"
#:package Yaver.Result@2.1.0
#addin nuget:?package=Yaver.Result&version=2.1.0
#tool nuget:?package=Yaver.Result&version=2.1.0
Yaver.Result
Yaver.Result is a .NET library designed to provide a standardized way to handle operation results, validation, and error management in your applications. It offers a set of result types, extension methods, and utilities to simplify the handling of success, failure, and validation scenarios.
Features
- Result types for success, error, validation, and common HTTP-like outcomes.
- Validation support with rich
ValidationErrordetails. - Paging support with
PagedResult<T>andPagedInfo. - MessagePack serialization support for result envelopes.
- Native AOT-friendly resolver strategy and smoke-test coverage.
Result Types
Yaver.Result provides several result types to cover different scenarios:
- Result: Represents a basic operation result, indicating success or failure, with optional error messages.
- Result<T>: A generic result type that carries a value of type
Ton success. - PagedResult<T>: Represents a paged result set, including pagination metadata (
PagedInfo). - ValidationError: Encapsulates validation error details, such as property name, error message, and severity.
- Result.Void: Used for operations that do not return a value but still indicate success or failure.
- ResultStatus: Enum representing operation state (
Ok,Error,Forbidden,Unauthorized,Invalid,NotFound,Conflict,CriticalError,Unavailable). - PagedInfo: Contains pagination information such as page number, page size, and total count.
HTTP Response Code Mapping
Yaver.Result is designed to help map operation results to appropriate HTTP response codes in web APIs:
- Success (
ResultStatus.Ok): Maps to HTTP 200 OK or 201 Created, depending on the operation. - Error (
ResultStatus.Error): Typically maps to HTTP 400/500 depending on context. - Invalid (
ResultStatus.Invalid): Maps to HTTP 422 Unprocessable Entity for validation failures. - Forbidden (
ResultStatus.Forbidden): Maps to HTTP 403 Forbidden. - Unauthorized (
ResultStatus.Unauthorized): Maps to HTTP 401 Unauthorized. - NotFound (
ResultStatus.NotFound): Maps to HTTP 404 Not Found. - Conflict (
ResultStatus.Conflict): Maps to HTTP 409 Conflict. - Unavailable (
ResultStatus.Unavailable): Maps to HTTP 503 Service Unavailable.
You can use the ResultToResponse utility to convert result objects to HTTP responses in your controllers.
MessagePack and Native AOT
The library includes MessagePack support for result envelopes with an AOT-safe resolver strategy.
- Use
WithYaverResultResolver<T>()to include result envelope formatters for your payload type. - Supported envelope types include
Result,Result<T>,PagedResult<T>, andValidationError. - Native AOT smoke tests are included in the repository to validate publish and runtime behavior.
Project Structure
Result/— Core result types, extensions, and validation utilities.Yaver.Result.csproj— Project file for the library.
Getting Started
- Add a reference to the Yaver.Result project in your .NET solution.
- Use the provided result types (e.g.,
Result,PagedResult,ValidationError) in your application logic. - Leverage extension methods for fluent result handling and validation integration.
Example Usage
Example of mapping a result to an HTTP response in ASP.NET Core:
public IActionResult MyAction()
{
var result = DoSomething();
return result.ToResponse(); // Uses ResultToResponse to map to the correct HTTP status code
}
using Yaver.Result;
public Result DoSomething()
{
if (/* some error condition */)
return Result.Error("An error occurred.");
return Result.Success();
}
using MessagePack;
using MessagePack.Resolvers;
using Yaver.Result;
var options = MessagePackSerializerOptions.Standard
.WithResolver(StandardResolver.Instance)
.WithYaverResultResolver<MyDto>();
var serialized = MessagePackSerializer.Serialize(Result.Success(new MyDto()), options);
var deserialized = MessagePackSerializer.Deserialize<Result<MyDto>>(serialized, options);
License
This project is licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- FastEndpoints (>= 8.1.0)
- FastEndpoints.Messaging.Core (>= 8.1.0)
- FastEndpoints.Messaging.Remote (>= 8.1.0)
- MessagePack (>= 3.1.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.