6

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.

3
  • 1
    Out of interest, why do you think IDataErrorInfo is disgusting? Commented Oct 15, 2009 at 6:32
  • 2
    I think it's a misuse of the indexer to start. ViewModel["Name"] should return a name... not null / "error!". You're also limited to the properties that you've bound in the view. It doesn't lend itself nicely to validators such as ValidateNameInformation where it may check all fields involved. If I am misunderstood in these statements please correct me. Commented Oct 15, 2009 at 14:16
  • 2
    One way I get arround the indexer abuse is to use an explicit interface implementation. That way I don't have a strange "indexer" littering my ViewModel public interface. I'm still working out how to validate multiple fields using MVVM. Commented Dec 21, 2009 at 14:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.