Compiled Chronicles

A software development blog by Angelo Villegas

Exploring MVVM

Understanding the basics

What is MVVM?

Model–View–ViewModel is a software architectural pattern that is structured to separate program logic and graphical user interface. It was invented by Microsoft architects Ken Cooper and Ted Peters specifically to simplify event-driven programming of user interfaces.

The pattern includes three key components:

  1. Model: The model is the data and logic of the application. It does not have any form of interaction with the view. The model represents the actual data and/or information we are dealing with, but not behaviours or actions that modify the information.
  2. View: The view is the structure, layout, and appearance of what a user sees on the screen. The content of View is not interacted with directly to change what is presented. It communicates with the ViewModel through data binding and does not directly interact with the Model.
  3. ViewModel: The view model is an abstraction of the view exposing public properties and commands that the View uses by way of data binding. Instead of the controller of the MVC pattern, or the presenter of the MVP pattern, MVVM has a binder. Binding is used to connect the UI elements in View to the controls in ViewModel.
Model-View-ViewModel Diagram

History

The MVVM pattern can be seen as an evolution of the MVC pattern, which dates back to the 1970s. MVC separates an application into three interconnected components: Model, View, Controller.

The MVVM pattern gained prominence in the context of Microsoft technologies, particularly with the advent of Windows Presentation Foundation (Microsoft’s .NET graphics system) and Silverlight (WPF’s Internet application derivative). These frameworks, introduced in the mid-2000s, facilitated the development of rich and interactive user interfaces for Windows applications.

MVVM was coined and popularised by John Gossman, a Microsoft architect, in 2005 on his blog. It was presented as a design pattern that specifically addresses the challenges of developing XAML-based applications by providing a clear separation of concerns.

One of the critical features that facilitated MVVM’s effectiveness is the concept of data binding. In MVVM, data binding allows automatic synchronisation between the View and ViewModel, reducing the amount of boilerplate code traditionally required for UI updates.

Benefits in Software Development

  • MVVM Promotes a clear separation between different components of an Application. Because of this, the developers and designers can work concurrently on their components. The designers can concentrate on the view, while the developers can work on the view model and model components of the application. This separation makes the codebase more organised and maintainable.
  • Due to its separation of concerns, MVVM enhances testability. The developers can create unit tests for the view model and the model without using the view. The view model, which contains the application’s logic, can be easily tested independently of the view. It can exercise exactly the same functionality as used by the view.
  • It is easier to maintain and redesign the UI of the application without touching the code outside of the view. Changes in one part of the application (e.g. UI enhancements) can be implemented without affecting the other parts and making the new version work with the existing application.
  • It makes for an extensible system whose structure are minimally or not affected by new modifications of the functionality.
  • MVVM leverages data binding mechanisms to automatically synchronise changes between the View and ViewModel. If property values in the ViewModel change, those new values automatically propagate to the view. This reduces the need for manual UI updates and results in a more responsive UI.

Data Binding

Binding is one of the most important feature of MVVM. It manages the link between the view and view model, along with behaviours and event triggers. These capabilities limit the need to place business logic in the view’s code-behind. The ViewModel takes care of exposing the data to show in the View as properties, which will be connected to the controls that will display them using binding.

Binding is also bidirectional, meaning that the view should be able to change the value of one of the ViewModel’s properties if needed.

Binder

In .Net, the binder is a markup language called XAML. It frees the developers from the necessary boiler-plate logic to synchronise the view model and view. The presence of a declarative data binding technology is what makes this pattern possible. Without a binder, one would typically use MVC or some other pattern instead.

Difference between MVC and MVVM

  • MVC is an architectural pattern that separates an application into three main logical components, while MVVM is an architectural pattern that facilitates separation of development of the UI with the help of a markup language.
  • In MVC, the Controller is the entry point of the application while in MVVM, the View is the entry point of the application.
  • With MVC, Controller and View exist with the one-to-many relationship. One Controller can select different View based upon required operation while in MVVM, multiple View can be mapped with single ViewModel and thus, the one-to-many relationship exists between View and ViewModel.
  • MVC separates the View and Controller making sure that the View has no knowledge of the Controller, In MVVM, the View has reference to the ViewModel.

Patterns, Tools, and Practices

The Prism Library is a popular open-source framework for building loosely coupled, maintainable, and testable applications in Xamarin, WPF, and other XAML-based frameworks. It provides various tools and capabilities to implement the MVVM architectural pattern effectively.

It simplifies the implementation of modular applications with it navigation service. Allowing developers to navigate between views while maintaining loose coupling between components. It introduces DelegateCommands, a simple yet powerful way to bind UI elements to ViewModel commands, allowing easy handling of user interactions without compromising MVVM principles.

The Prism Library provides a comprehensive set tools and capabilities that align with the MVVM architectural pattern. It enables developers to create well-structured, maintainable, and testable applications by enforcing best practices and facilitating the implementation of MVVM principles.

Conclusion

Software developers often use different patterns, tools, and practices to help them with the development and testing of website and applications. Learning about these can help you in your software development journey.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *