ChatGPT解决这个技术问题 Extra ChatGPT

Fluent Validation vs. Data Annotations

What are the operative differences between these two validation packages when used for ASP.NET MVC validatation? They seem to have similar objects, all the way to their object names. Is one related to another? What are their differences? In what way do these differences denote different use cases?

Fluent Validation (3rd party solution)

Data annotations (Microsoft "baked-in")

It is also interesting to note, that questions that are closed as not constructive usually have a lot of upvotes, so they actually ARE helpful to people. There has got to be something wrong with this thing
I agree that this type of question is useful but the answers below seem more like opinions to me and not facts.
I completely agree as well, however asking "what are the differences" instead of "what are your preferences" probably would have avoided the situation.
I think the deal is you have to word it so that answers are less opinion based rather than factual. Don't ask, "What's your favorite?" but "What are the operative differences between?" Then you don't get answers like, "I prefer Fluent Validation." but instead things that foreground differences first and present findings second.

G
George Paoli

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


Some more points from this (webdevbros.net/2010/12/03/…) article: 1. Too many annotations make your model s look ugly (similar to your point 3) 2. Better reusability 3. Better performance (as no Reflection)
@Idsa The performance point sounds dubious. The reflection only needs to happen once per model. This assumes a good implementation, I don't know how this particular implementation works.
@CodeInChaos, looks like you are right. But I will keep it there as I am also not sure (and lazy enough to find out) how it is implemented.
I second the FluentValidation...it rocks. From a code OCD perspective I love that it removes the responsibility of validation from the views and gives it their own classes. I tried xVal for awhile back in MVC1...Data Annotations were alright for simple stuff, but once you got more than a handful of rules you could barely tell what the ViewModel was supposed to represent.
@Darin how do you pass the error messages in the view? can you provide an example how to do it?
R
Robert Harvey

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).


I think most of these properties can be achieved with some form of fluent validation. I don't know if the library in the OP supports this, but in principle it's possible, and not very hard either.
What's the point of testing for the presence of attributes? Isn't that basically repeating validation rules?
@Sam: By testing whether properties are decorated with Data Annotation attributes, you don't test the functionality of the attribute itself; you're just making sure it's there. I should say that now, two years later, I'm on Darin's side and agree with his answer.
@Sam, because you probably want to know if someone removes it from your model.
Great comment Marius. Too bad most of the EF tutorials now days show validation done with Data Annotations. I was also initially hooked up by the simplicity of the annotations, but soon after I tried to implement a custom validation rule, I jumped on team Fluent Validation right away... By the way, too bad that Darin stopped posting :( Most of his comments in StackOverflow are spot on after more than 5 years!!!