Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Model and ViewModel

I've never used MVVM before, so I'm probably missing something obvious. When I create a new Panorama application, there's already a ViewModel folder containing ItemViewModel and MainViewModel.

I thought "MainViewModel.cs" is the file that organizes the panorama. However, within MainViewModel, it has this line:

public MainViewModel()
{
    this.Items = new ObservableCollection<ItemViewModel>();
}

The ItemViewModel has no interaction with the panorama. These are the then instantiated like this:

this.Items.Add(new ItemViewModel() 
{ 
    LineOne = "first line", 
    LineTwo = "second line", 
    LineThree = "third line" 
});

Why isn't ItemViewModel just a 'Model'? It implements INotifyPropertyChanged, but for what purpose? I would've thought that the ObservableCollection in MainViewModel would be enough to notify any changes, as demonstrated here

like image 772
Brap Avatar asked Sep 29 '10 14:09

Brap


People also ask

What is ViewModel and model in MVC?

ViewModel = Model that is created to serve the view. ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose. View Model is a model class that can hold only those properties that are required for a view.

What is difference between model and model in MVC?

@model is used to "import" a model in the view page while the @Model represents the imported model and is where you retrieve its properties. Here you see that @model simply imports the Model object to the page while the @Model you get actual value from the retrieved model.

Does a ViewModel need a model?

A ViewModel may and in many cases does use multiple Models. It is itself a "Model" of your view. Consider a profile screen that a user enters their personal information including address.

What is the difference between controller and ViewModel?

View – code that cares about how data is displayed. Controller – code that cares about how data is created/updated/deleted. ViewModel – code that cares both about how data is stored and how it is displayed.


3 Answers

Difference is quite simple.

Model holds business logic.
View model contains presentation logic and is additionally shaped to fit views.

In Your case - view model implements INotifyPropertyChanged. That's pure presentation logic.

Model is not responsible for notifying one particular UI that something has changed, it's responsible for transferring invoices, calculating wages etc.

Sometimes (when model is simple) this abstraction is not necessary though.


Some wiki quotes:

Model: as in the classic MVC pattern, the model refers to either
(a) an object model that represents the real state content (an object-oriented approach), or
(b) the data access layer that represents that content (a data-centric approach).

ViewModel: the ViewModel is a “Model of the View” meaning it is an abstraction of the View that also serves in data binding between the View and the Model. It could be seen as a specialized aspect of what would be a Controller (in the MVC pattern) that acts as a data binder/converter that changes Model information into View information and passes commands from the View into the Model. The ViewModel exposes public properties, commands, and abstractions. The ViewModel has been likened to a conceptual state of the data as opposed to the real state of the data in the Model.

like image 88
Arnis Lapsa Avatar answered Oct 08 '22 18:10

Arnis Lapsa


It's the same general concept behind all MV[x] architectures, albeit MVC, MVP or MVVM:

  • You have the model on the one side, which is basically a software abstraction of your business domain. It does not know and does not care about any UI-related stuff (like e.g. in your case 'notifying the UI about changes'). It implements business logic and that's it.
  • On the other side you have the UI, with specific needs both in technical terms (e.g. 'WPF wants to bind to an ObservableCollection') and also in terms of user presentation (think e.g. about different date orderings or different decimal signs in different languages).
  • To cope with this and to be able to clearly separate these requirements in a clean architecture, you need the [x], in your case the ViewModel. It's the only layer within the software that knows both about the UI and the Model. Otherwise, there should be no connection whatsoever between the two.

In your simple example, this might look like overkill, but a normal business software will have dozens or even hundreds of such MV[x] triplets, and you would have no way to maintain a clean architecture without this.

To answer your question: What you have in your example is just a bit of wiring code to set up the described architecture.

HTH! Thomas

like image 37
Thomas Weller Avatar answered Oct 08 '22 16:10

Thomas Weller


The ObservableCollection will notify when items are added or deleted from the list, but the INotifyPropertyChanged on the ItemViewModel is needed if you want notifications to happen when those properties change.

like image 6
Robaticus Avatar answered Oct 08 '22 16:10

Robaticus