MVVM Interview Questions and Answers
by Bharathkumar, on Sep 10, 2022 10:18:43 PM
Q1. 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.
Q2. What are the benefits of MVVM?
The different layers View,ViewModel and Model are loosely coupled.This gives application developed using MVVM pattern following advantages:
- Application is easer to maintain and extend
- Application is easier to unit test
Q3. 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
Q4. What are popular MVVM frameworks?
- Prism
- MVVM Light
- Caliburn Micro
Q5. What is a data binding?
Data binding is used to connect data source with data target.Data source provides the data to the data target.Data source can be any CLR object.Data target is usually WPF element such as textbox or combobox.With WPF databinding data is automatically synchronized ,this means that the data changes in the source are automatically reflected in the data target.Similarly the data changes in the data target are reflected in the data source.
Q6. What is PRISM?
PRISM is a set of guidelines for developing easy to maintain and flexible desktop applications.Applications built using PRISM are easier to change.
Q7. What is Delegate Command?
Delegate Command implements ICommand and IActiveAware interface.So instead of implementing the ICommand interface we can use the Delegate Command to simplify the
command creation in our application.
Q8. 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.
Q9. What is dependency property?
Dependency property adds certain features in the normal CLR property system.Class defining dependency property inherits from the DependencyObject class.WPF UI controls are usually inherited from DependencyObject.So UI controls supports dependency properties.
Q10. What is ICommand?
ICommand defines a command
The ICommand type exposes the following members.
CanExecute :Defines the method that determines whether the command can execute in its current state.
Execute :Defines the method to be called when the command is invoked.
CanExecutehanged :Occurs when changes occur that affect whether or not the command should execute.
Q11. What is the key feature that differentiates MVVM from other UI separation patterns like MVC and MVP?
Data binding is the key feature that differentiates MVVM from other UI separation patterns like MVC and MVP.
Q12. What is validation in MVVM?
When your application starts accepting data input from end users you need to consider validating that input. To make sure it conforms to your overall requirements.
Q13. 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.
Q14. What are the responsibilities of View?
The main purpose and responsibilities of views is to define the structure of what the user sees on the screen. The structure contains static and dynamic parts.
- Static parts are the XAML hierarchy that defines the controls and layout of controls that a view is composed of.
- Dynamic part is like animations or state changes that are defined as part of the View.
- The primary goal of MVVM is that there should be no code behind in the view.
- In view you at least need the constructor and a call to initialize component.
- The event handling, action and data manipulation logic code shouldn’t be in the code behind in View.
- There are also other kinds of code that have to go in the code behind any code that’s required to have a reference to UI element. It is inherently view code
Q15. What is the View First Construction in Code-behind?
Another way is that you can do view first construction by simply constructing the view model yourself in the code behind of your View by setting the DataContext property there with the instance.
Typically, the DataContext property is set in the constructor method of view, but you could also defer the construction until the Load event of the view fires.
using SystemWindows.Controls;
namespace MVVMDemo.Views{
/// <summary>
/// Interaction logic for StudentView.xaml ///
</summary>
public partial StudentView : UserControl {
public StudentView() {
InitializeComponent();
this.DataContext = new MVVMDemo.ViewModel.StudentViewModel();
}
}
}
Q16. What is Dependency Injection / IoC Containers?
Inversion of Control (IoC) and dependency injection are two design patterns that are closely related and the container is basically a chunk of infrastructure code that does both of these patterns for you. IoC pattern is about delegating responsibility for construction and the dependency injection pattern is about providing dependencies to an object that’s already been constructed.
Q17. What are the disadvantages of MVVM?
- Some people think that for simple UI, MVVM can be an overkill.
- Similarly in bigger cases, it can be hard to design the ViewModel.
- Debugging would be a bit difficult when we have complex data bindings.