A common senario: A model with a collection of item models.
E.g a House with a collection of People.
How to structure this correctly for MVVM - particulary with regard to updating the Model and ViewModel collections with additions and deletes?
Model House
contains a collection of model People
(normally a List<People>
).
View model HouseVM
contains the House object that it wraps and an ObservableCollection of view model PeopleVM
(ObservableCollection<PeopleVM>
). Note that we end up here with the HouseVM holding two collections (that require syncing):
1. HouseVM.House.List<People>
2. HouseVM.ObservableCollection<PeopleVM>
When the House is updated with new People (add) or People leave (remove) that event must now be handled in both collections the Model House People collection AND the the VM HouseVM PeopleVM ObservableCollection.
Is this structure correct MVVM?
Is there anyway to avoid having to do the double update for Adds and Removes?
Your general approach is perfectly fine MVVM, having a ViewModel exposing a collection of other ViewModels is a very common scenario, which I use all over the place. I would not recommend exposing items directly in a ViewModel, like nicodemus13 said, as you end up with your view binding to models without ViewModels in between for your collection's items. So, the answer to your first question is: Yes, this is valid MVVM.
The problem you are addressing in your second question is the synchronization between the list of people models in your house model and the list of people ViewModels in your house ViewModel. You have to do this manually. So, no there is no way to avoid this.
What you can do: Implement a custom
ObservableCollection<T>
,ViewModelCollection<T>
, which pushes it's changes to an underlying collection. To get two way synching, make the model's collection an ObservableCollection<> too and register to theCollectionChanged
event in your ViewModelCollection.This is my implementation. It uses a ViewModelFactory service and so on, but just have a look at the general principal. I hope it helps...
In this situation I simply make the model expose
ObservableCollection
s rather thanList
s. There's no particular reason why it shouldn't. TheObservableCollection
is in theSystem.Collections.ObjectModel
namespace of theSystem
assembly, so there's no unreasonable extra dependencies, you almost certainly haveSystem
anyway.List
is inmscorlib
, but that's as much a historical artefact as anything.This simplifies the model-viewmodel interactions massively, I can't see a reason not to do it, using
List
s on the model just creates lots of unpleasant boiler-plate code. You are interested in the events, after all.Also, why is your
HouseVM
wrapping anObservableCollection<PeopleVM>
, rather thanObservableCollection<People>
? VMs are for binding to views, so I would think that whatever is binding to yourObservableCollection<PeopleVM>
is actually interested inPeople
, otherwise you're binding-within-a-binding, or is there a specific reason why this is useful? I wouldn't generally have a VM expose other VMs, but maybe that's just me.Edit about libraries/WCF
I don't see why having a model in a library, or even exposed by a WCF-server should affect whether they raise events or not, it seems perfectly valid to me (obviously the WCF-service won't expose the events directly). If you don't like this, I think you're stuck with having to chain multiple updates, though I wonder if you're actually just manually doing the same work as the event would do in an
ObservableCollection
, unless I've misunderstood some of it.Personally, like I said, I'd keep the VMs simple, and have them expose the minimum and not expose other VMs. It can take some redesign and make certain parts a bit of a pain (e.g.
Converter
s, however, you end up with a simple, easy-to-manage design with some simple-to-handle irritations on the edges.It seems to me that your current route is going to end up very complex rather quickly and, most importantly, awkward to follow... However, YMMV, it's just my experience :)
Perhaps moving some of the logic to explicit services might help?