I'm on ASP.NET Core and the new MediatR which supports pipelines. My pipeline includes validation.
Consider this action:
[HttpPost]
[HandleInvalidCommand]
public IActionResult Foo(Command command)
{
await _mediator.Send(command);
return View();
}
- The command is validated (I'm using FluentValidation)
HandleInvalidCommand
checksModelState.IsValid
, and if invalid then redirects to the view for the user to correct the data- Else the action runs
- The command is sent into the pipeline
- The pipeline validates the command, AGAIN
So if the command is valid, then validation occurs twice (and validators are expensive to run).
How best can I deal with this?
EDIT: The obvious way is to remove validation from the pipeline, but that is no good because the command may come from the UI, but also from the app itself. And you want validation in both cases.