Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of having models in WPF?

Tags:

c#

mvvm

wpf

So far I have yet to see the value of having models in WPF. All my ViewModels, by convention, have an associated Model. Each of these Models is a virtual clone of the their respective ViewModel. Both the ViewModel and Model classes implement INotifyPropertyChanged and the ViewModel just delegates everything to the Model anyway.

Why bother having Models then? Why can't I just move my Model logic up into the ViewModel and call it a day?

It seems rather redundant (that is, not DRY) to have MVVM, and just use VVM by default unless some special edge case demands a Model.

If using explicit model classes better supports unit testing, for example, or some other best practice, I can see the value.

like image 795
Mark Richman Avatar asked May 13 '14 17:05

Mark Richman


People also ask

What is a ViewModel in WPF?

VIEWMODEL: A ViewModel is a model for a view in the application or we can say as abstraction of the view. It exposes data relevant to the view and exposes the behaviors for the views, usually with Commands. The ViewModel is the glue between the View and the outside world. The ViewModel is what the View is bound to.

What is content model in WPF?

This topic summarizes the content model for WPF control and control-like types. The content model describes what content can be used in a control. This topic also lists the content properties for each content model. A content property is a property that is used to store the content of the object.

Why MVVM is used?

Rationale. MVVM was designed to remove virtually all GUI code ("code-behind") from the view layer, by using data binding functions in WPF (Windows Presentation Foundation) to better facilitate the separation of view layer development from the rest of the pattern.

Does WPF use MVVM?

MVVM is the lingua franca of WPF developers because it is well suited to the WPF platform, and WPF was designed to make it easy to build applications using the MVVM pattern (amongst others).


2 Answers

The model is just the low level application data.

The view model is more specific.

  • It's like a window tapping into the data tailored for the view.
  • It also augments the model with details needed for the view. A good example is pagination.
  • A model can have more than one view model. These view models would offer different aspects of the data.
  • You can create a mashup of different data sources. A view model cleanly façades the models involved.

That means there is no 1:1 relationship between models and view models.

So, if you just display the low level data without a lot of additional logic, features, summaries, aggregations, filters, etc. you can do away with view models and just use the model directly. This seems to be the case with your project.

like image 152
nalply Avatar answered Sep 23 '22 09:09

nalply


The model can be auto-generated (Entity Framework), or it might not be a specific class at all (think of a DataTable).

I assume that if you say "I don't use a model", you do in fact use a model, you just don't call it that way.

like image 43
Heinzi Avatar answered Sep 25 '22 09:09

Heinzi