Which approach for ASP.NET MVC validatation do you prefer and why? Fluent Validation? Data annotations? Any other?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I prefer Fluent Validation:
- It gives me far better control of my validation rules
- Doing conditional validation on different properties is so much easier compared to Data Annotations
- It separates the validation from my view models
- Unit testing is far easier compared to Data Annotations
- It has excellent client side validation support for most standard validation rules
回答2:
I clearly prefer Data Annotations because ...
- all validation rules can be configured in one place in code (within the model metadata class) and don't need to be repeated anywhere else.
- there is excellent support for client side validation (again – without repetition of validation rules!) when using Data Annotation attributes.
- Data Annotation attributes can be tested to ensure they're there.
- there are nice additional validation attributes created by the community (e.g. Data Annotations Extensions).