Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVVM, WPF, and Validation

Tags:

c#

.net

mvvm

wpf

Well, I think i have a fairly good understanding of MVVM. But I need some clarifications.

Is the ViewModel responsible for calling the appropriate service to persist model information?

If so, then the ViewModel must have a clean way of determining if the data it holds is valid. If the data is valid, it will update the model accordingly. Finally, a service to persist the Model is invoked given the newly updated model. Then the question is: How do we validate the information of the ViewModel and display this easily in the View?

I've seen a few different approaches to validation. One suggesting using IDataErrorInfo which i think is absolutely disgusting.

Another is adding ValidationRule's to Binding.ValidationRules. However, using this approach one cannot operate in the context of the model as a whole. The ValidationRule object can only perform validation on a single value. An example might be ensuring that a value is an integer or within a certain range.

Another idea which I just started to look into is using BindingGroup's. But I don't know a whole lot about this at this point because I'm still reading on it.

I would like to be able to perform validation logic in a single place to be used by the View and ViewModel. In addition to this requirement, I would like to be able to perform validations against any other value in the ViewModel. Additionally, being able to prevent the ViewModel from persisting data if it is an invalid state. This would need to be easily reflected in the View.

If anyone can point me to some articles or provide some insight to my desired approach I would be very appreciative.

like image 458
Joe Avatar asked Oct 14 '09 22:10

Joe


People also ask

Do you have to use MVVM with WPF?

The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern. Though it is possible to create WPF applications without using the MVVM pattern, a little investment in learning can make building WPF applications much simpler.

What is WPF data validation?

Syncfusion WPF input controls allow you to validate user input and display hints if validation fails. If the user input is invalid, a default red border will be shown around the UIElement.

Is WPF MVVM or MVC?

MVVM (Model-View-ViewModel)It is based on the Model-view-controller pattern (MVC), and is targeted at modern UI development platforms (WPF and Silverlight) in which there is a UX developer who has different requirements than a more "traditional" developer.


1 Answers

We do our data validation in our business model, and only allow saving when the business model allows (because it has valid data), in hindsight we could have done this in the view model however this would mean a different validation approach for every view model. and if you are displaying the same data twice in different ways you might have to re write the validation logic.

We do isdirty and isValid on nearly every field in the business layer, we write our own custom field object and a custom foreign reference object that implements this. then we can bind staraight to these properties to see visually if we are valid/dirty etc. Then we propogate these properties through the view model.

like image 116
Aran Mulholland Avatar answered Oct 30 '22 13:10

Aran Mulholland