-
Notifications
You must be signed in to change notification settings - Fork 125
Closed
Description
Pain
When checking the result status (for example a service called from a minimal API). The code seems more verbose that it could be.
For example:
app
.MapPost("/",
async Task<Results<Created, BadRequest<ValidationProblemDetails>, NotFound>> (
[FromServices] ISender sender,
[FromBody] CreateLeaveCommand command, CancellationToken ct) =>
{
var result = await sender.Send(command, ct);
if (result.Status == ResultStatus.BadRequest) // 👈 here
return TypedResults.BadRequest(result.ToValidationProblem());
if (result.Status == ResultStatus.NotFound) // 👈 here
return TypedResults.NotFound();
return TypedResults.Created();
})
.WithName("CreateLeave");Solution
Add helper fields to IResult so that the code is more concise. The result would look something like:
app
.MapPost("/",
async Task<Results<Created, BadRequest<ValidationProblemDetails>, NotFound>> (
[FromServices] ISender sender,
[FromBody] CreateLeaveCommand command, CancellationToken ct) =>
{
var result = await sender.Send(command, ct);
if (result.IsInvalid) // 👈 here
return TypedResults.BadRequest(result.ToValidationProblem());
if (result.IsNotFound) // 👈 here
return TypedResults.NotFound();
return TypedResults.Created();
})
.WithName("CreateLeave");The above reads much cleaner and removes unneeded code.
- What do you think of the above?
- Would you be happy for me to create a PR for this?
- Do you think
IResultis the best place to put this?
jernejk
Metadata
Metadata
Assignees
Labels
No labels