Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation in WPF - Custom validation rule or IDataErrorInfo

As a new WPF programer I cant find the difference between two different way to validate user input:

What are the pros and cons of writing custom validation rule against implementing IDataErrorInfo, and vice versa? WhenShould I prefer one over the other?

Update:

Though I got my answer already, I found related article that may help others.

like image 736
ET. Avatar asked Dec 05 '10 13:12

ET.


1 Answers

Basically, if you implement IDataErrorInfo, validation is implemented in the bound object, whereas if you implement validation rules, validation is implemented in objects attached to the binding.

Personally, if you're using MVVM, I think you'd have to be crazy to ever use anything except IDataErrorInfo. You want validation to live in the view model. If it's in your view model, it's centralized and it's testable. If it's in your view, then your validation logic can be wrong, or missing, and the only way to find it is by manually testing your view. That's a huge potential source of avoidable bugs.

There are places where it makes sense to use validation rules - if, for instance, you're building a UI around dumb objects (an XmlDataSource, for example). But for most production applications, I wouldn't go near it.

like image 70
Robert Rossney Avatar answered Oct 08 '22 04:10

Robert Rossney