Bookmarks

MVVM — model — view — viewmodel

Model–View–ViewModel

An interface architecture pattern that divides an application into a model with the data, a view and a view model — an intermediary between them; data binding connects the view and the view model.

Also: model-view-viewmodel, model view viewmodel

checked 8 Nov

All terms

MVVM separates the interface from the logic: the visual part knows nothing about data storage or network requests, and the details of controls never touch the logic. Data binding holds the relationships between the layers: when a value changes in one layer, it is updated in the layers bound to it as well.

Three layers

LayerWhat it is responsible for
Modelthe data and the rules for processing it: access to the database, network APIs and the cache; knows nothing about the view
Viewwhat the user sees: windows, pages, controls; describes appearance only
ViewModelthe view state and commands: takes data from the model and presents it in a form convenient for display

The view and the view model are connected by data binding, so changing a field in the view model changes the text on screen without any update code, while user actions invoke view model commands. The view model also holds view state that markup cannot express: the selected item, view or edit mode. A screen usually has its own view model, and the dependency runs from the view to the view model: the view model knows nothing about the specific screen.

A binding example

xmlXAML: controls bound to view model properties and commands
<Label Text="{Binding UserName}" /><Button Text="Load" Command="{Binding LoadCommand}" />

Where the term comes from

John Gossman, a Microsoft architect working on WPF, described the MVVM pattern in a blog post in October 2005. He called the pattern a variety of MVC for modern interface platforms, where a designer draws the view in a declarative language such as XAML while a developer writes the logic. The pattern’s second pillar is the shared data binding mechanism — without it the view model has nothing to bind to the view.

Where it is used

  • Microsoft applications: WPF, .NET MAUI — the documentation describes MVVM as a way to separate the interface from the logic.
  • Mobile development: on Android, the Jetpack ViewModel stores the screen’s state and survives its rotation; a similar approach is used in SwiftUI.
  • The web: Knockout is built around MVVM, and an early version of Vue described itself as the pattern’s view model layer.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • MVCmodel–view–controller

    In MVC the controller takes user input and decides what to update; in MVVM data binding handles the exchange of values between the screen and the logic.

In depth in the references

where the topic is covered in detail