How to structure data in MVVM
I'm trying to build my first WP7 app and I'm a bit confused about how to do organize data. My data is on an XML file and I have three kinds of data to represent on the device: sales by country, by year, and by city.
I've seen some examples and I think to build 3 classes like CountryViewModel
, YearViewModel
, and CityViewModel
on which I have the single fields exposed as property.
The开发者_如何学JAVAn I think to add a MainViewModel to handle the rest, but in the examples I've seen in MainViewModel
there is a reference to a single itemViewModel
, then should I create MainCountryViewModel
, MainYearViewModel
, and MainCityViewModel
?
Can anyone point me to the right approach?
This looks like an interesting question. Here's what I think is right:
ViewModels should be structured around the Views - they should hold the data in a format that is "friendly" and "convenient" for the views to use.
Behind these ViewModels, you can structure the persistent Models in a format that is convenient for loading and saving - or for sending over the wire.
The reason the samples mostly have a MainViewModel with a list of ItemViewModel's is because most of the samples just show a main window with a ListBox of items
If you look at CodePlex, then you should be able to find quite a few samples with more complicated ViewModel structures - e.g. http://4square.codeplex.com, http://mahtweetswp7.codeplex.com, http://gasmileage.codeplex.com, ...
So... for your app: I think if it fits with your UI, then you should definitely create a MainViewModel and let it have the three properties of observable lists of Country, Year and City-ViewModels.
精彩评论