162

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?

ruffin
  • 16,507
  • 9
  • 88
  • 138
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • 66
    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 – Dmitry Efimenko Dec 26 '12 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. – Ian Warburton Jun 12 '13 at 08: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. – Jeremy A. West Aug 28 '14 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. – ruffin Jul 15 '19 at 13:16
  • See https://softwareengineering.stackexchange.com/questions/159007/are-fluent-interfaces-more-flexible-than-attributes-and-why/159039#159039 – Akira Yamamoto Aug 05 '21 at 06:24

2 Answers2

149

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
George Paoli
  • 2,257
  • 1
  • 24
  • 29
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 6
    Some more points from this (http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/) 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) – SiberianGuy Jul 24 '11 at 15:41
  • 2
    @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. – CodesInChaos Jul 24 '11 at 15:49
  • @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. – SiberianGuy Jul 24 '11 at 15:55
  • 2
    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. – Brandon Linton Jul 25 '11 at 01:21
  • @Darin how do you pass the error messages in the view? can you provide an example how to do it? – Jaime Sangcap Feb 08 '14 at 06:15
  • @Daskul, Fluent Validation adds error messages to the ModelState, the same way DataAnnotations do. So you would display them in the view the same way you would display those messages if you were using DataAnnotations -> using the `Html.ValidationMessageFor` or `Html.ValidationSummary` helpers. – Darin Dimitrov Feb 08 '14 at 10:25
  • thank you for reply. but I think you are referring to the Fluent API, I was thinking about FluentValidation library. sorry. But I found now the solution using IoC and changing the ValidationFactory to custom factory – Jaime Sangcap Feb 08 '14 at 11:37
  • Glad there is a link to my blog post there, thanks @siberianGuy, I'll keep it up to date – Dai Bok Mar 22 '17 at 14:43
  • My vote is with fluent validation for the reasons above AND that it makes my model's cleaner/easier to interpret in a glance. – pim Jan 24 '18 at 23:38
49

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).
Patrick
  • 8,175
  • 7
  • 56
  • 72
Marius Schulz
  • 15,976
  • 12
  • 63
  • 97
  • 2
    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. – CodesInChaos Jul 24 '11 at 15:26
  • What's the point of testing for the presence of attributes? Isn't that basically repeating validation rules? – Sam Jun 18 '13 at 05:59
  • 40
    @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. – Marius Schulz Jun 21 '13 at 09:48
  • @Sam, because you probably want to know if someone removes it from your model. – Steve Feb 17 '14 at 18:38
  • 6
    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!!! – Koshera May 05 '16 at 15:31
  • Hands down the best way. One fewer file to maintain. – Jakub Keller Apr 19 '19 at 14:40