How to detect changes in the model

Nov 17, 2010 at 1:08 PM


I have a sort of complicated architectural situation. It looks like this:

Shell – Container – Child

The Shell contains a few Containers and the Containers has many Children. If I Click a ChildView, ShellViewModel will register the corresponding VM as ActiveChild. If I make changes to ChildModel I want the ContainerViewModel to know about it so that it can put up a change marker. These are the methods I have come up with so far:

  1. ChildViewModel throws a NotifyOfPropertyChange() whenever something has changed in it. Some of these changes are changes in ChildModel, some are just graphical changes. If I make ContainerViewModel listen to all its ChildViewModels it can notice whenever something has changed.
    Drawbacks are: If I have many ChildViewModels there will be many listeners. Might slow down the system? I will also have to check if the NotifyOfPropertyChange() are changes in the model or just graphical changes.
  2. I can make ChildModel throw NotifyOfPropertyChange() and make ContainerViewModel listen for these.
    Drawbacks: Many listeners. Complex ChildModel. I would like my ChildModel to just be getters and setters. More events, might slow down the system?
  3. ShellViewModel can listen for changes in ActiveChild and then tell Container that it should be marked as changed.
    Drawbacks: It feels very manual to make the ShellViewModel do this.
  4. ContainerViewModel catch the ClickEvent and has an ActiveChild and listen for changes in this. For other reasons my ShellViewModel also need to have a ActiveChild so either the ClickEvent sends two actions, one to ContainerViewModel and one to ShellViewModel, or the method in ContainerViewModel throw an event that ShellViewModel listen for.
    DrawBacks: Many actions.

Which method do you think is the best?


Nov 17, 2010 at 1:57 PM

Have you considered using the EventAggregator? If you model the information as system events, the shell can subscribe (one subscriber) and anyone can publish. Would that work for you?

Dec 29, 2010 at 2:35 PM

It seems like a good solution but I’m not sure it’s the best way.

What I’m actually trying to implement is a dirtyaware app so perhaps I should ask about that instead?

Sorry for the late reply btw.

Dec 29, 2010 at 3:10 PM

You could try hooking/unhooking INPC events on changes in the ActiveItem. That would ensure that there is only one event handler at a time. Is that what you are suggesting in #3? It sounds like the simplest to me with the fewest drawbacks. If you are worried about putting to much responsibility in the ShellViewModel, I would extract a service to manage this specific functionality.

Dec 30, 2010 at 2:05 PM
Edited Dec 30, 2010 at 2:06 PM

Yes, that is #3 and it solves the problem but the code gets quite messy. Child actually has its own children and so on. But if there is no super clean standard solution this works fine.