views:

170

answers:

2

I have just started learning MVVM and having a dilemna.

If I have a a main ViewModel and inside this model I have a number of datasets. Now should I be creating a new ViewModel for each row inside the dataset? Or expose the DataSet itself as a DependencyProperty?

For now the dataset has about 20 rows inside it, and the thought of iterating through each row to create a ViewModel binding to each row.... might not be the best option for performance reasons and memory reasons in the future, like when there are 2000+ rows.

Should I still go ahead and create a RowViewModel and iterate through the dataset creating a new RowViewModel? And have an ObservableCollection of RowViewModels or just expose the DataSet?

The binding for this DataSet/ViewModel will be to a combo box, hence why loading only viewable rows would possibly a performance issue as users expect to be able to scroll without any delays.

Any help would be greatly appreciated.

+1  A: 

One important question, imo, is: do you require displaying all those records at once or will you have some kind of paging/navigation mechanism?

If you're going to display all rows in the same view, creating RowViewModels for each row will obviously add overhead. You could alleviate some of that overhead by letting the viewmodels reference the underlying rows instead of copying the row data.

I would look at building some kind of page navigation though, since scrolling through all that data is probably not the best user experience anyway. If you stick to a page size of say, 20, you will only need 20 viewmodels at any one time.

Peter Lillevold
True that, I guess a user won't be able to see the whole 1000 items. And it wouldn't be the best idea.
LnDCobra
+2  A: 

Hi LnDCobra,

If you have a complex combo box where each row has an id, name, picture (maybe some logic involved), etc. and you have a View that represents each row, then you would need to create the rowViewModel with all bindings since you have to test that code (TDD!).

For simple combo boxes where you just want to display a string per row, binding a list of strings or your dataset is more than fine.

As Peter said, be careful with a combo box that can grow so big as this one. Your users won't like it :)

Also, since you are concerned about performance, think about using a DataReader populating a list of strings instead of loading a big fat DataSet.

Cheers,

André

andrecarlucci
Thanks for the DataReader tip, I will look into it :)
LnDCobra
+1 for the datareader tip. Was so hung up in the viewmodel problem that I forgot about questioning the huge dataset :)
Peter Lillevold