Reputation: 155
I am working in a WPF project using MVVM
.
What's the best practice to validate errors using IDataErrorInfo
? In my Model or ViewModel?
Whats the best pattern to use to implement validation?
P.S. I am Using .NET
3.5.
Upvotes: 4
Views: 1386
Reputation: 2127
If you bind your model to the view, it would be best to use Data Annotations and the Validator class. If you're binding to properties of your view model, then use IDataErrorInfo for validation.
Upvotes: 0
Reputation: 1314
I think there is no right way or wrong way. It all depends on your application and whether or not your using different patterns or architectures or have specific needs within your WPF application.
If your using different tiered architecture, you could put your validation within the business layer of your application. If that's the case, use This link.
Within my applications, I like to put the validation within the viewmodel. Obviously, in some cases this this is a bad idea, for example; If you have a FirstName property within your viewmodel, it means you are only limiting GUI to validate the FirstName property, but what if someone set it from other place.
It all comes down to the needs of your application and requirements. Personally speaking I put them within my ViewModel, as its quick and easy. But for best practices then I recommend that you read through the link.
I also recommend you read through the following links to give you a better understanding;
How to Use Model Validation Rules in WPF ViewModel
OR
Business Layer Validation Sample
Hope this helps!
Upvotes: 5