views:

155

answers:

2

I've got this Silverlight Prism application that is using MVVM. The model calls a WCF service and a list of data is returned.

The ViewModel is bound to the View, so the ViewModel should have a List property.

Were should I keep data returned by a WCF service in MVVM?

Should the List property be calling into the Model using its getter? Where the model has a ReturnListOfData() method that returns the data stored in the model.

Or does the ViewModel stores the data after the Model is done with calling the server?


This is a follow up on http://stackoverflow.com/questions/1741271

+4  A: 

Generally if I need to keep the Model objects around (I consider most things coming back from a WCF service a Model object) I will store it in my ViewModel in a "Model" property.

I've seen people go so far as to create a standard Model property on their base ViewModel type, like this (I don't do this, but it's nice):

public class ViewModel<ModelType> : INotifyPropertyChanged ...
{
     //Model Property
     public ModelType Model
     {
          ...
     }
}

It's really up to you. Keeping them as close to their related ViewModels is probably the thing to take away here.

Anderson Imes
+1  A: 

It really depends on other aspects of your application. E.g. how's the data returned by ReturnListOfData() used? Are there other components interested in it? Does user update elements in the list? Can it create new elements that he'll want to save later? etc.

In the simplest case you'd just have a List property exposed by your viewmodel to view, and you'd reset that list to whatever ReturnListOfData() returned. It will probably work for a case when user simply performs a search, doesn't do anything to the data later on, and there's only one view that is interested in that data.

But suppose a user wants to be able to modify elements of that list. Clearly, you'll have to somehow track the changes in that original list, so then when user clicks save (or cancel), you'd send to the server only elements that were changed (or added) or restore the original elements if user clicks cancel. In this case you'd need a Model object, that would keep the original data, so then your viewmodel contains only its copy.

PL