consider following code
public async Task<Unit> Handle(UpdateVendorCommand request, CancellationToken cancellationToken)
{
var vendor = await _context.Vendor.FindAsync(request.Id);
if (vendor is null)
throw new KeyNotFoundException();
_mapper.Map(request, vendor);
await _context.SaveChangesAsync();
return Unit.Value;
}
I'm getting status code 200 instead of 204 because I can't return null
. How do I return null
if my method return type is Task<Unit>
? Or is there any alternative beside using Task<Unit>
?
My Controller
public class UpdateVendorController : ControllerBase
{
private readonly IMediator _mediator;
public UpdateVendorController(IMediator mediator)
{
_mediator = mediator;
}
[HttpPut]
[Route("vendor")]
[SwaggerOperation(Tags = new[] { "Vendor" })]
public async Task<Unit> UpdateVendor([FromBody] UpdateVendorCommand request)
{
return await _mediator.Send(request);
}
}
CodePudding user response:
For Mediatr, Unit is equivalent to void, not any kind of object that can be nullable.
I'd suggest you change the controller to be more specific in return types:
public async Task<IActionResult> UpdateVendor([FromBody] UpdateVendorCommand request)
{
_ = await _mediator.Send(request); // Don't care about the return type, it's void
return NoContent();
}