tags:

views:

748

answers:

1

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.

+1  A: 

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.

Aran Mulholland
You have a few libraries to help you out, e.g. Validation application block (http://msdn.microsoft.com/en-us/library/cc309509.aspx) and CSLA (http://www.lhotka.net/cslanet/)
armannvg