-1

New to MVVM architecture; I've been looking at this article but I'm not sure what pieces of code belong in model or viewmodel. Can someone show me how its done? Also, does every model need a raisepropertychanged() function?

Thank you for your answers in advance!

F Blanchet
  • 1,430
  • 3
  • 21
  • 32
Daniel Li
  • 3
  • 2
  • No model should implement INotifyPropertyChanged at all. Models should be bare POCO classes. Viewmodels must implement INotifyPropertyChanged. – 15ee8f99-57ff-4f92-890c-b56153 Jun 24 '19 at 16:05
  • 2
    If the project is simple enough, the model/viewmodel can be the same. Implement INotifyPropertyChanged on anything you are going to bind against. – Kevin Cook Jun 24 '19 at 16:20
  • So models should just declare POCO classes. I'm reading https://www.tutorialspoint.com/mvvm/mvvm_first_application.htm and it has a bunch of INotifyPropertyChanged stuff in the model; should that be implmented in the viewmodel instead and how so? Thanks! – Daniel Li Jun 24 '19 at 16:51
  • @DanielLi Implementing INPC properly is a big chunk of the definition of a viewmodel. *If* you have a separate model and viewmodel, the model would not implement INPC. That tutorialspoint thing looks nuts to me, at a glance. A Student model would just be bare properties. A StudentViewModel would represent the same concept, one student, but it would have notifying properties, plus commands, a dirty flag property, and other stuff for the UI to play with: If there's a StudentVM.Courses collection, StudentVM might have a SelectedCourse property. As Kevin says, you may dispense with models entirely – 15ee8f99-57ff-4f92-890c-b56153 Jun 24 '19 at 19:39
  • Making StudentViewModel a collection of Student models, that are actually viewmodels, that's bizarre. Then he's setting a DataContext property on a user control manually in code behind, because there's no main viewmodel. That guy is bluffing about knowing MVVM. [The author of this blog has a much more solid understanding](https://rachel53461.wordpress.com/2011/05/08/simplemvvmexample/). – 15ee8f99-57ff-4f92-890c-b56153 Jun 24 '19 at 19:41

2 Answers2

1

Somewhat general answer here but:

Viewmodels should contain the information needed for the associated view and handle view interaction with the model. This includes data to fill things like textboxes and functions that determine what to do when a button is pressed (a command).

The model is everything not directly connected to a view. This might be custom data types or classes for interacting with a database.

The model should not have a raisepropertychange() function - more accurately it should not implement the INotifyPropertyChanged interface. Every viewmodel should implement INotifyPropertyChanged, the easiest way to do this is generally to have a base viewmodel class that all of your viewmodels inherit from which implements the interface.

Here is a good example of the INotifyProperty implementation., I personally use the last option under the C#7 heading in my viewmodel base class and call the SetField() function from each property.

Ben H
  • 113
  • 1
  • 5
0

If you REALLY want to learn MVVM from scratch, try this article (3 parts). From here you will get the big picture. There is explained what is a model, what is a view model, what is a view.

bakunet
  • 197
  • 1
  • 12