171

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?

5
  • 70
    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 Commented Dec 26, 2012 at 22:30
  • 2
    I agree that this type of question is useful but the answers below seem more like opinions to me and not facts. Commented Jun 12, 2013 at 8:14
  • 3
    I completely agree as well, however asking "what are the differences" instead of "what are your preferences" probably would have avoided the situation. Commented Aug 28, 2014 at 20:48
  • 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. Commented Jul 15, 2019 at 13:16
  • See softwareengineering.stackexchange.com/questions/159007/… Commented Aug 5, 2021 at 6:24

4 Answers 4

156

I prefer Fluent Validation:

  1. It gives me far better control of my validation rules
  2. Doing conditional validation on different properties is so much easier compared to Data Annotations
  3. It separates the validation from my view models
  4. Unit testing is far easier compared to Data Annotations
  5. It has excellent client side validation support for most standard validation rules
Sign up to request clarification or add additional context in comments.

9 Comments

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?
|
54

NB : In the comments the original author states he now prefers fluent validation

I clearly prefer Data Annotations because ...

  1. 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.
  2. there is excellent support for client side validation (again – without repetition of validation rules!) when using Data Annotation attributes.
  3. Data Annotation attributes can be tested to ensure they're there.
  4. there are nice additional validation attributes created by the community (e.g. Data Annotations Extensions).

6 Comments

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.
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!!!
Hands down the best way. One fewer file to maintain.
|
2

One consideration regarding a validation class vs. attributes, it is possible to have a class that uses different validations, depending on context. For example, an address class can be used in two different instances:

  1. When a valid address is required for shipping
  2. When you are just trying to get demographics information

You could use the same class, but the validation would be different. For demographics for example, you might only require the country or state or city, but not the actual street address. Doing this type of validation would be difficult using attributes.

I know this is a convoluted example, but it does come up in my projects (perhaps 1 or 2 percent of the time) to warrant consideration.

Comments

1

According to my point of view, FluentValidation is better than Data Annotation

Firstly, Unit testing and configuring complex validation rules are easier compared to Data Annotations. Another point is this Although this is a controversial issue, I think dataannotation does not comply with the solid principles

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.