tags:

views:

2383

answers:

6

Most MVVM examples I have worked through have had the Model implement INotifyPropertyChanged, but in Josh Smith's CommandSink example the ViewModel implements INotifyPropertyChanged.

I'm still cognitively putting together the MVVM concepts, so I don't know if:

  • you have to put the INotifyPropertyChanged in the ViewModel to get CommandSink to work
  • this is just an aberration of the norm and it doesn't really matter
  • you should always have the Model implement INotifyPropertyChanged and this is just a mistake which would be corrected if this were developed from a code example to an application

What have been others' experiences on MVVM projects you have worked on?

+10  A: 

I'd say quite the opposite, I always put my INotifyPropertyChanged on my ViewModel - you really don't want to be polluting your model with a fairly WPF specific feature like INotifyPropertyChanged, that stuff should sit in the ViewModel.

I'm sure others would disagree, but that's the way I work.

Steven Robbins
What do you do if a property changes in the model? You need to get it to the view-model somehow. Honest question, I'm dealing with this conundrum right now.
Roger Lipscombe
The EventAggregator in the the Prism code is a good alternative to INotifyPropertyChanged on the model, with a custom property changed event type. The event code in that project supports forwarding events between background and UI threads, which can sometimes be an issue.
Steve Mitcham
@Roger - as a small concession to your usage, you could an an event that the viewmodel can hook into that will get called when your UI updates.
Steve
+1  A: 

I'd say in your ViewModel. It's not part of the Model as the Model is UI agnostic. The Model should be 'everything EXCEPT business agnostic'

Steve Dunn
+4  A: 

It depends on how you've implemented your model. My company uses business objects similar to Lhotka's CSLA objects and make extensive use of INotifyPropertyChanged throughout the business model.

Our validation engine relies heavily on being notified that properties change through this mechanism and it works very well. Obviously, if you are using a different implementation other than business objects where notification of changes isn't as critical to the operation, you may have other methods for detecting change in your business model.

We also have View Models that propagate the changes from the Model where needed, but the View Models themselves are listening to the underlying Model changes.

Steve Mitcham
How exactly do you propagate Model's OnPropertyChanged to ViewModel's OnPropertyChanged? I have a problem when ViewModel has different property names than Model - some kind of name-to-name mapping would be needed then, right?
Martin Konicek
It's not anything real sophisticated, we simply forward the events. I suppose if the names were different then a lookup table could be used. If the change weren't a one-to-one mapping, then you could simply hook the event and then fire the necessary events in the handler.
Steve Mitcham
+7  A: 
Soni Ali
very nice, thanks for the link
Edward Tanguay
+1  A: 

But sometimes (as in this presentation link text) model is service, which supplies application with some data online and then you need to emplement notification that new data arrived or data has changed using events...

Andrey Khataev
A: 

I strongly disagree with the concept that the Model should not implement the INOtifypropertyChange. This interface is not UI specific! It simply informs of a change. Indeed WPF heavily uses this to identify changes but doesn't mean it is a UI interface. I would compare it to the following comment "A tyre is a car accessory". sure it is, but bykes, buses, etc also use it. In summary do not relate the interface to a UI thing.

having said that, it doesn't necessary means I do belive in the Model provinding notifications. Inf act as a rule of thumb, the model should not implement this interface, unless it is necessary. In most cases where no server data is pushed to the client app, the model can be stale. But if listening to financial market data, then i do not see why the model cannot implement the interface. As an example, what if I have a non ui logic such as a service that if receives a Bid or Ask price for a giving value it issues an alert - through an email - or places an order then this could be a possible clean solution.

However, tehre are different ways of achieving things but i would always argue in favour of simplicity and avoid redundancy.

What is better? Defining events on a collection or property changes on the view-model and propagate it to the model or have the view intrinsically update the model (throught the View-Model)?

Bottom line whenever you see someone claiming that "you can't do this or that" it is as sign they do not know what they are talking about.

It really depends on your case, and in fact MVVM is a framework with lots of issues and I am yet to see a commom implementation of MVVM accross the board.

I wish I had more time to explain many flavours of MVVM and some solutions to commom problems - mostly provided by other developers - but I guess I will have to do it another time.

Paulo Sousa