English 中文(简体)
Where should the data be stored in MVVM?
原标题:

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 Where to put the calls to WCF or other webservices in MVVM?

最佳回答

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.

问题回答

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.





相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签