Package Description
$ dotnet add package FastProjects.ResultPatternResult pattern implementation with IActionResult mapping for ASP.NET Core.
Basic usage example:
public async Task<Result<User>> GetByIdAsync(Guid id)
{
var user = await _userRepository.GetAsync(id);
if (user is null)
{
return Result.NotFound($"User with {id} not found.");
}
return Result.Success(user);
}
Translation of the result to IActionResult (example with FastEndpoints usage):
public override async Task HandleAsync(
GetUserByIdRequest request,
CancellationToken ct)
{
var command = new GetUserByIdQuery(request.UserId);
var result = await mediator.Send(command, ct);
if (result.IsSuccess)
{
Response = new GetUserByIdResponse(
Id: result.Value.Id,
Name: result.Value.Name);
}
else
{
await SendResultAsync(result.ToWebResult());
}
}