MVVM Lists & Collections – Which Approach to Take?

mvvm

I'm currently working on a Silverlight app using Caliburn.Micro.

At present, we have Views (eg: PeopleView) and View Models (eg: PeopleViewModel) that equate to 'pages' of the application.

PeopleView might contain a ListBox ("People") which is bound to an ObservableCollection of Person objects, and has an ItemTemplate assigned to denote how each Person object should be displayed.

However, one of my colleagues has begun to implement a list in another way, where each Person is a View Model (ie: PersonViewModel) and has an associated PersonView to determine how that PersonViewModel should be displayed in the ListBox.

The latter seems more MVVM (or at least has more mention of V and VM!) but I'm not sure whether there's a particularly large advantage to doing one over the other.

Are both of these ways valid? Is either better than the other?

Best Answer

Yes, both ways are valid. There are pros and cons to each approach.

If the Person can be displayed in several different ways depending on the context - whether it's in a ListBox or on a "page" then using your technique with an ItemTemplate to control how it's displayed in the list is probably the better way to go.

I think you need to look at your overall design and see which fits best. Changing the implementation part way through is a bad idea though. Some "pages" will be the old style and others will be the new, leading to inconsistencies in the user experience and potentially increased effort in maintaining and extending the system. If nothing else, you will have to go through the existing code to make it conform to the new format.