MVVM Interview Questions and Answers
What is MVVM?
The Model View ViewModel (MVVM) is an architectural pattern used in software engineering that originated from Microsoft as a specialization of the presentation model design pattern introduced by Martin Fowler.Largely based on the model–view–controller pattern (MVC), MVVM is targeted at UI development platforms which support event-driven programming, such as HTML5,Windows Presentation Foundation (WPF), Silverlight and the ZK framework.
What are the elements of MVVM?
Model: as in the classic MVC pattern, the model refers to either (a) a domain model which represents the real state content (an object-oriented approach), or (b) the data access layer that represents that content (a data-centric approach).
View: as in the classic MVC pattern, the view refers to all elements displayed by the GUI such as buttons, labels, and other controls
View model: the view model is a “model of the view” meaning it is an abstraction of the view that also serves in mediating between the view and the model which is the target of the view data bindings.
Why we need MMVM?
MVVM facilitates a clear separation of the development of the UI from the development of the business logic or back end logic known as the model (also known as the data model to distinguish it from the view model). The view model of MVVM is a value converter. MVVM was designed to make use of data binding functions in WPF to better facilitate the separation of view layer development from the rest of the pattern by removing virtually all GUI code (“code-behind”) from the view layer.
What are advantages of MVVM over MVC?
1. Increases the “Blendability” of your views (ability to use Expression Blend to design views) – This enables a separation of responsibilities on teams that are lucky enough to have a designer and a programmer… each can work independent of the other.
2. “Lookless” view logic – Views are agnostic from the code that runs behind them, enabling the same view logic to be reused across multiple views or have a view easily retooled or replaced. Seperates concerns between “behavior” and “style”.
3. No duplicated code to update views – In code-behind you will see a lot of calls to update view controls. With MVVM you can be assured the view is updated appropriately just by setting the underlying property and all view side-effects thereof.
4. Testability – Since your logic is completely agnostic of your view, unit testing is made easy. You can test the behavior of a ViewModel without involving its view. This also enabled test-driven development of view behavior, which is almost impossible using code-behind.
5. Designer-Developer Workflow – MVVM facilitates a separation of UI and presentation logic concerns from the business layer that makes it easy to streamline the development process by allowing design cycles to happen in parallel with development.
What is INotifyPropertyChanged?
The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed.
For example, consider a Stock object with a property called StockCount. To provide generic property-change notification, the Stock type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when StockCount is changed.
For change notification to occur in a binding between a bound client and a data source, your bound type should either:
- Implement the INotifyPropertyChanged interface (preferred).
- Provide a change event for each property of the bound type.
What is ICommand?
ICommand defines a command
The ICommand type exposes the following members.
Defines the method that determines whether the command can execute in its current state.
Defines the method to be called when the command is invoked.
Occurs when changes occur that affect whether or not the command should execute.