tags:

views:

57

answers:

1

So I was looking into MVVM again after some time where I didn't really follow new developments and I noticed that the amount of tutorials/guides etc. has grown greatly. However most examples/example implementations of MVVM lack to explain something that's not really clear to me. All of these examples are pretty simple and none of them reads something form a database/file/etc.

Now for example I have some paint-like application and I save the paintings in XAML. What belongs into the ViewModel what belongs into the Model?

Does the Model supply functions to load/save the paintings from/to a XAML file?

Does the ViewModel bind to properties that the Model exposes (Color, Width, Position etc.)?

Does the validation happen in the Model or in the ViewModel?

+1  A: 

The ViewModel is the representation of the model which is suitable for the presentation technology you're using.

In your example I believe the Model would not provide the functions to load/save the paintings from/to a XAML file. This would be performed in a Data Access Object/Repository which would be called upon by the ViewModel taking Model instances as input. This bit is often variable though and depends on what your Model classes look like.

The ViewModel itself usually doesn't make use of data binding. It simply exposes the Model to the View in a way which is helpful for the the presentation (View) technology. In the case of WPF/Silverlight that basically means it implements the INotifyPropertyChanged interface.

Validation is usually initiated by the View (like pretty much everything), performed in the ViewModel but often delegated by the ViewModel to the Model. Of course it is best not to repeat your validations throughout your application. The best place for common validations is the Model (refer to IDataErrorInfo). However validations which are specific to your ViewModel can be handled directly in the ViewModel.

b-rad
I think discussion is warranted about exposing data changes happening in the Model to the ViewModel so they can be bound and updated to the View through normal data binding. If you think of a data layer, it would have metrics on the status of the data pulled in the process of maintaining that data (think of an offline data store, specifically). If those metrics exist and change at the Model layer, how would you push change notification from Model to ViewModel for databinding to the View?
ScottCher