Desfecho.AspNetCore
2.0.0
See the version list below for details.
dotnet add package Desfecho.AspNetCore --version 2.0.0
NuGet\Install-Package Desfecho.AspNetCore -Version 2.0.0
<PackageReference Include="Desfecho.AspNetCore" Version="2.0.0" />
<PackageVersion Include="Desfecho.AspNetCore" Version="2.0.0" />
<PackageReference Include="Desfecho.AspNetCore" />
paket add Desfecho.AspNetCore --version 2.0.0
#r "nuget: Desfecho.AspNetCore, 2.0.0"
#:package Desfecho.AspNetCore@2.0.0
#addin nuget:?package=Desfecho.AspNetCore&version=2.0.0
#tool nuget:?package=Desfecho.AspNetCore&version=2.0.0
Desfecho
A simple and lightweight Result pattern implementation for .NET.
dotnet add package Desfecho
dotnet add package Desfecho.AspNetCore
Tech stack
.NET 10 · xUnit v3 · Shouldly
Why
Stop throwing exceptions for expected failures. Return a Result<TValue> instead: it's either a value or one or more Errors.
public Result<TodoItem> CompleteTodoItem(Guid id)
{
var todoItem = _todoItems.Find(id);
if (todoItem is null)
{
return Result.NotFound($"Todo item '{id}' was not found.");
}
todoItem.Complete();
return todoItem;
}
public Result DeleteTodoItem(Guid id)
{
return _todoItems.Remove(id)
? Result.Success()
: Result.NotFound($"Todo item '{id}' was not found.");
}
Result<TValue> converts implicitly from TValue, Error and ErrorList, so returning one is all you need, no wrapping, no new. Result is the same without a value.
Consuming
var message = result.Match(
todoItem => $"Completed '{todoItem.Title}'.",
errors => errors[0].Description);
if (result.TryGetValue(out var todoItem)) { ... }
var dto = result
.Then(todoItem => todoItem.Archive()) // returns Result<T> or Result
.Map(todoItem => new TodoItemDto(todoItem));
Value, Error and Errors throw if the result is the other case; prefer Match or TryGetValue / TryGetErrors.
Errors
An Error is a Type, a Description, and an optional Property (set for validation errors):
Result.Invalid("Title", "Title is required."); // ErrorType.Validation
Result.Unauthorized("Authentication is required."); // ErrorType.Unauthorized
Result.Forbidden("You do not have access."); // ErrorType.Forbidden
Result.NotFound("Todo item was not found."); // ErrorType.NotFound
Result.Conflict("Todo item already exists."); // ErrorType.Conflict
Result.Failure("Something went wrong."); // ErrorType.Failure
An ErrorList holds several errors. Result.Invalid also takes an existing property-to-messages map, e.g. from FluentValidation:
var validation = await validator.ValidateAsync(request, ct);
if (!validation.IsValid)
{
return Result.Invalid(validation.ToDictionary());
}
result.Error is the first error, result.Errors all of them.
ASP.NET Core
Desfecho.AspNetCore turns a Result into a minimal API IResult, mapping errors to ProblemDetails (Validation → 400, Unauthorized → 401, Forbidden → 403, NotFound → 404, Conflict → 409, Failure → 500). When all errors are Validation, the response is a validation problem grouped by Property; otherwise it describes the first error..
app.MapPost("/todo-items", (CreateTodoItemRequest request, ISender sender, CancellationToken ct) =>
sender.Send(new CreateTodoItemCommand(request.TodoListId, request.Title), ct)
.ToCreated(value => $"/todo-items/{value.Id}"));
ToOk(), ToCreated(location) and ToNoContent() work on Result/Result<TValue> and on Task of them; ToProblem() on an Error or Errors is available directly for anything else.
Packages
| Package | Contents |
|---|---|
Desfecho |
Result, Result<TValue>, Error, ErrorList, ErrorType |
Desfecho.AspNetCore |
Result<TValue> → minimal API IResult |
Development
dotnet restore
dotnet build --configuration Release
dotnet test --configuration Release
dotnet format --verify-no-changes --severity info
Deployment
Versioning, changelog, and NuGet publish are automated by release-please, tracking Desfecho and Desfecho.AspNetCore as independent packages: merges to main update a release PR from Conventional Commits, and merging that PR tags the release and publishes the affected package(s) to NuGet via GitHub Actions.
License
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
- Desfecho (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.