vkhorikov / ValidationInDDD

The source code for the Pluralsight course about Validation, DDD, and FluentValidation library
https://enterprisecraftsmanship.com/ps-ddd-validation
112 stars 35 forks source link

FluentValidator asynchronous rules #2

Closed kimsagro1 closed 3 years ago

kimsagro1 commented 3 years ago

FluentValidator's integration with ASP.NET Core has a major caveat

You should not use asynchronous rules when using ASP.NET automatic validation as ASP.NET’s validation pipeline is not asynchronous.

Within the RegisterRequestValidator you call the StateRepository to get all the states. This is currently a synchronous call but I imagine in most real world scenarios this would be an asynchronous call.

I'm curious how you would you handle this scenario?

vkhorikov commented 3 years ago

You'll need to use the synchronous version of the repository method in FluentValidation validators. I'd just keep the single (sync) version of GetAllStates(), but if the performance/throughput is crucial for the project, you can also introduce an async version (GetAllStatesAsync()) for the use in controllers.

kimsagro1 commented 3 years ago

you can also introduce an async version (GetAllStatesAsync()) for the use in controllers

So at this point you have the move all the validation out of the fluent validator into the controller, right?

vkhorikov commented 3 years ago

No, I would just keep using the sync version in the validators, but if I need an async version (for the use in controllers), then I'd introduce an async version. But that's only if it really makes a difference in terms of performance or throughput. Otherwise, just use the sync version everywhere.