Content deleted Content added
m Dating maintenance tags: {{Dubious}} |
|||
(317 intermediate revisions by more than 100 users not shown) | |||
Line 1:
{{Short description|Software architecture design pattern}}
{{Use dmy dates|date=October 2018}}
[[File:MVVMPattern.svg|500px|right|upright=1.5]]
'''Model–view–viewmodel''' ('''MVVM''') is an [[architectural pattern]] in computer software that facilitates the [[Separation of concerns|separation]] of the development of a [[graphical user interface]] (GUI; the ''view'')—be it via a [[markup language]] or GUI code—from the development of the [[business logic]] or [[Front and back ends|back-end]] logic (the ''model'') such that the view is not dependent upon any specific model platform.
The ''viewmodel'' of MVVM is a value converter,<ref name="MVVM-eliminates-valueconverters">{{cite web|url=https://groups.google.com/forum/#!topic/wpf-disciples/P-JwzRB_GE8|title=Thought: MVVM eliminates 99% of the need for ValueConverters|last=Smith|first=Josh |display-authors=etal|website=Google Groups}}</ref> meaning it is responsible for exposing (converting) the [[data object]]s from the model in such a way they can be easily managed and presented. In this respect, the viewmodel is more ''model'' than ''view'', and handles most (if not all) of the view's display logic.<ref name="MVVM-eliminates-valueconverters" /> The viewmodel may implement a [[mediator pattern]], organizing access to the back-end logic around the set of [[use case]]s supported by the view.
MVVM is a variation of [[Martin Fowler (software engineer)|Martin Fowler]]'s Presentation Model design pattern.<ref>{{cite web|url=http://martinfowler.com/eaaDev/PresentationModel.html|title=The Presentation Model Design Pattern|author=Martin Fowler|date=19 July 2004|publisher=Martin Fowler.com}}</ref><ref name="JoshSmith">{{cite journal|last=Smith|first=Josh|date=February 2009|title=WPF Apps with the Model–View–ViewModel Design Pattern|url=http://msdn.microsoft.com/en-us/magazine/dd419663.aspx|journal=MSDN Magazine}}</ref> It was invented by [[Microsoft]] architects Ken Cooper and Ted Peters specifically to simplify [[event-driven programming]] of user interfaces. The pattern was incorporated into the [[Windows Presentation Foundation|Windows Presentation Foundation (WPF)]] (Microsoft's [[.NET Framework|.NET]] graphics system) and [[Silverlight]], WPF's Internet application derivative.<ref name="JoshSmith" /> John Gossman, a Microsoft WPF and Silverlight architect, announced MVVM on his blog in 2005.<ref name="JoshSmith" /><ref name="Gossman2005" />
Model–view–viewmodel is also referred to as '''model–view–binder''', especially in implementations not involving the [[.NET]] platform. [[ZK (framework)|ZK]], a [[web application framework]] written in [[Java (programming language)|Java]], and the [[JavaScript]] [[Library (computing)|library]] [[KnockoutJS]] use model–view–binder.<ref name="JoshSmith" /><ref name="SMassey">{{cite web|last=Massey|first=Simon|title=Presentation Patterns in ZK|date=9 April 2011 |url=http://www.slideshare.net/simbo1905/design-patterns-in-zk-java-mvvm-as-modelviewbinder|access-date=24 March 2012}}</ref><ref>{{cite web |author = Steve Sanderson |title = KnockoutJS |url = http://knockoutjs.com/documentation/observables.html}}</ref>
==Components of MVVM pattern==
;Model
:''Model'' refers either to a [[___domain model]], which represents real state content (an object-oriented approach), or to the [[data access layer]], which represents content (a data-centric approach).{{citation needed|date=February 2015}}
;View
:As in the [[model–view–controller]] (MVC) and [[model–view–presenter]] (MVP) patterns, the ''view'' is the structure, layout, and appearance of what a user sees on the screen.<ref name="MSDN">{{Cite web|url=https://msdn.microsoft.com/en-us/library/hh848246.aspx|title=The MVVM Pattern|website=msdn.microsoft.com|date=4 October 2012 |access-date=2016-08-29}}</ref> It displays a representation of the model and receives the user's interaction with the view (mouse clicks, keyboard input, screen tap gestures, etc.), and it forwards the handling of these to the view model via the [[data binding]] (properties, event callbacks, etc.) that is defined to link the view and view model.
;View model
:The ''view model'' is an abstraction of the view exposing public properties and commands. Instead of the controller of the MVC pattern, or the presenter of the MVP pattern, MVVM has a ''binder'', which automates communication between the view and its bound properties in the view model. The view model has been described as a state of the data in the model.<ref>{{cite web |author = Pete Weissbrod |title = Model–View–ViewModel Pattern for WPF: Yet another approach. |url = http://www.acceptedeclectic.com/2008/01/model-view-viewmodel-pattern-for-wpf.html |archive-url = https://web.archive.org/web/20080201101909/http://www.acceptedeclectic.com/2008/01/model-view-viewmodel-pattern-for-wpf.html |archive-date = 2008-02-01}}</ref>
:The main difference between the view model and the Presenter in the MVP pattern is that the presenter has a reference to a view, whereas the view model does not. Instead, a view directly binds to properties on the view model to send and receive updates. To function efficiently, this requires a binding technology or generating [[boilerplate code]] to do the binding.<ref name="MSDN" />
:Under [[object-oriented programming]], the view model can sometimes be referred to as a [[data transfer object]].<ref>{{cite web|author = Microsoft|title = Tutorial: Create a web API with ASP.NET Core| date=3 April 2024 |url = https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api}}</ref> {{Dubious|date=August 2025}}
;Binder
:Declarative data and command-binding are implicit in the MVVM pattern. In the Microsoft [[solution stack]], the binder is a [[markup language]] called [[XAML]].<ref>{{cite web|last=Wildermuth|first=Shawn|title=Windows Presentation Foundation Data Binding: Part 1|date=11 May 2010 |url=http://msdn.microsoft.com/en-us/library/aa480224.aspx|publisher=Microsoft|access-date=24 March 2012}}</ref> The binder frees the developer from being obliged to write boiler-plate logic to synchronize the view model and view. When implemented outside of the Microsoft stack, the presence of a declarative data binding technology is what makes this pattern possible,<ref name="SMassey"/><ref>{{cite web|title=ZK MVVM|url=http://books.zkoss.org/wiki/Small_Talks/2012/February/New_Features_of_ZK_6#ZK_MVVM|publisher=Potix|access-date=24 March 2012}}</ref> and without a binder, one would typically use MVP or MVC instead and have to write more boilerplate (or generate it with some other tool).
==Rationale==
MVVM was designed to remove virtually all [[Graphical user interface|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.<ref name="JoshSmith" /> Instead of requiring [[User experience design|user experience]] (UX) developers to write GUI code, they can use the framework markup language (e.g. [[Extensible Application Markup Language|XAML]]) and create data bindings to the view model, which is written and maintained by application developers. The separation of roles allows interactive designers to focus on UX needs rather than programming of business logic. The layers of an application can thus be developed in multiple work streams for higher productivity. Even when a single developer works on the entire codebase, a proper separation of the view from the model is more productive, as the user interface typically changes frequently and late in the development cycle based on end-user feedback.{{Citation needed|date=October 2018}}
The MVVM pattern attempts to gain both advantages of separation of functional development provided by MVC, while leveraging the advantages of [[data binding]]s and the framework by binding data as close to the pure application model as possible.<ref name="JoshSmith" /><ref name="Gossman2005">{{cite web |last = Gossman |first = John |title = Tales from the Smart Client: Introduction to Model/View/ViewModel pattern for building WPF apps |date = October 8, 2005 |url = https://docs.microsoft.com/en-us/archive/blogs/johngossman/introduction-to-modelviewviewmodel-pattern-for-building-wpf-apps}}</ref><ref>{{cite web|author = Karl Shifflett|title = Learning WPF M-V-VM.|url = http://karlshifflett.wordpress.com/2008/11/08/learning-wpf-m-v-vm/|access-date = 5 June 2009|archive-url = https://web.archive.org/web/20090413205731/http://karlshifflett.wordpress.com/2008/11/08/learning-wpf-m-v-vm/|archive-date = 13 April 2009|url-status = dead}}</ref>{{clarify|date=February 2015}} It uses the binder, view model, and any business layers' data-checking features to validate incoming data. The result is that the model and framework drive as much of the operations as possible, eliminating or minimizing application logic which directly manipulates the view (e.g., code-behind).
==Criticism==
John Gossman has criticized the MVVM pattern and its application in specific uses, stating that MVVM can be "overkill" when creating simple user interfaces. For larger applications, he believes that generalizing the viewmodel upfront can be difficult, and that large-scale data binding can lead to lower performance.<ref>{{cite journal |last=Gossman |first=John |title=Tales from the Smart Client: Advantages and disadvantages of M-V-VM |url=https://docs.microsoft.com/en-gb/archive/blogs/johngossman/advantages-and-disadvantages-of-m-v-vm |journal=AI Skills Challenge|date=4 March 2006 }}</ref>
==Implementations==
===.NET frameworks===
*.NET Community Toolkit
*[[Avalonia (software framework)|Avalonia]]
*Caliburn, Caliburn.Micro
*Chinook.DynamicMvvm Open source
*DevExpress MVVM
*DotVVM open source project
*FreshMvvm
*Jellyfish
*Mugen MVVM Toolkit
*MVVMLight Toolkit
*MvvmCross
*MvvmZero
*Prism Library
*Rascl
*ReactiveUI
*[[Uno Platform]] - Open source
==== Web Component libraries ====
* Microsoft FAST
* Omi.js
===Java frameworks===
*[[ZK (framework)|ZK Studio]]
===JavaScript frameworks===
*[[Angular (application platform)|Angular]]
*Aurelia
*Durandal
*[[Ember.js]]
*[[Ext JS]]
*[[Knockout (web framework)|Knockout.js]]
*Oracle JET
*[[React (software)|React]]
*[[Svelte]]
*[[Vue.js]]
=== Frameworks for C++ and XAML (Windows) ===
* Xamlcc
==See also==
* [[
* [[Model–view–controller]]
* [[Model–view–presenter]]
==References==
{{Reflist}}
{{Design patterns}}
{{DEFAULTSORT:Model-view-viewmodel}}
[[Category:Software design patterns]]
[[Category:
[[Category:
|