14

Let's say I've got a View. It's DataContext is bound to a ViewModel and the ViewModel exposes a Model property.

  • Is it MVVMlike to bind fields in the View to properties in the Model (e.g. Binding Path=Model.FirstName)?
  • Should the Model implement INotifyPropertyChanged?

My team are using Prism and MVVM in WPF. A lot of the Prism examples bind indirectly to the Model. I personally have my doubts that this is the correct approach. I think stuff in the model should expose behaviour (not just at the property level) and should communicate important events by, er, events - that clients can subscribe to or not.

I think that by having domain/model objects implement INotifyPropertyChanged somehow says to the world that it's UI/UX aware and kind of introduces some redundancy in the ViewModels.

What do you think? What works for you? A clear distinction between View/ViewModel/Model or a hybrid as used by the Prism examples?

Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
  • Considering that you cannot bind to fields, no. Properties are a different matter... –  Jan 18 '12 at 16:57

1 Answers1

24

I have seen many people implementing INotifyPropertyChanged directly in their Model and similarly I have seen people doing it in ViewModel as well.

I prefer and do this(implement INotifyPropertyChanged) in ViewModel. I agree with you it sometimes create redundancy in ViewModel but I prefer a clear distinction/separatation between ViewModel and Model and what their purpose should be. To me Model is just literally a Model. It is just representation of my Business Data nothing more nothing less. To me it should not cause any change in View (through notify property changed event). View should talk to ViewModel and ViewModel should use Model. I don't like View directly affecting the Model. I don't like using Model.FirstName because to me it seems like going against MMVM by telling View what is in Model

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • 2
    What I have 10 different instances of the model and each instance has 20 properties, would it not be a pain in the ass to exlpicitly expose all thos properties in ViewModel? – Joe Slater May 31 '13 at 17:41
  • 1
    @AnkurSharma I agree, it would be but as I said that's how some people like me like it even though there is pain. And I explained my reasons in the answer – Haris Hasan May 31 '13 at 18:05