views:

206

answers:

1

I can't quite figure out how to get the view model to be notified of changes in the model without adding a bunch of UI specific stuff like INotifyProperyChanged and INotifyCollectionChanged in my model or create tons of different events and do a bunch of things that feel like they're UI specific and should stay out of the model.

Otherwise I'd just have to duplicate all the business logic in the view-model to make sure everything is up to date, and then what's the point of having the model then?

One of the tricky ones that I have in my model is a property of a "Category" class. You can think of it as a tree structure and the property is all leaf-node descendants. Well in the model that property is generated on the fly recursively through all it's children, which is all fine and good. The view-model however needs to bind to that property and needs to know when it changes. Should I just change the model to accommodate the view-model? If I do then the view-model doesn't really do anything at this point, the model raises all the necessary notifications of changes and the view can just bind straight to the model. Also if the model was something I didn't have the source to, how would I get around this?

+3  A: 

I disagree that INotifyPropertyChanged and INotifyCollectionChanged are UI-specific. They are in namespaces and assemblies that are not tied to any particular UI stack. For that reason, I typically put that kind of behavior as low in the system as I can (usually the data layer).

If there's some reason you don't want to put it at that level, that's fine. You can put it in at a higher level such as the service or UI layer. However, you need to make sure all changes to the data structures occur through that layer also.

HTH, Kent

Kent Boogaart
Actually INotifyCollectionChanged is part of System.Collections.Specialized which is in WindowsBase.dll. I've always found this odd.
Cameron MacFarland
Good point Cameron. I find that equally confounding. Updating my answer accordingly...
Kent Boogaart
Hmm, I guess the only time I've ever heard of them are in for WPF tutorials for databinding purposes, but I suppose there's nothing inherently WPF about them other than they're used for WPF.
Davy8
@Davy8: Correct. WPF natively understands these interfaces, but WinForms also understands INotifyPropertyChanged and IBindingList. INotifyCollectionChanged is just the new (simpler) version of IBindingList.
Cameron MacFarland