Reader small image

You're reading from  The MVVM Pattern in .NET MAUI

Product typeBook
Published inNov 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781805125006
Edition1st Edition
Languages
Right arrow
Author (1)
Pieter Nijs
Pieter Nijs
author image
Pieter Nijs

Pieter Nijs is a .NET consultant at Xebia in Belgium, with a keen interest in mobile and cloud development. He's been instrumental in diverse projects, from vast healthcare and telecom systems to compact LOB apps. Now, Pieter's exploring AI's potential to enhance customer projects innovatively. Passionate about technology, he actively experiments and shares knowledge as a conference speaker and trainer. Pieter has been awarded the Microsoft MVP Award since 2017, reflecting his unwavering passion and expertise in serving the community.
Read more about Pieter Nijs

Right arrow

What Is the MVVM Design Pattern?

The MVVM or Model-View-ViewModel pattern is a very commonly used design pattern in the .NET ecosystem, where it has proven to be a good fit for front-end frameworks that utilize XAML to build graphical user interfaces. And it’s not hard to see why.

This book will provide a good understanding of the MVVM design pattern and how to effectively apply it in .NET MAUI projects. It’s important to note that while we will focus on applying MVVM in the context of .NET MAUI, the MVVM pattern itself is not exclusive to the .NET ecosystem. It is a widely used design pattern that has gained popularity across various software development ecosystems, including frameworks like WPF, WinUI, and others. We’ll delve into various aspects of .NET MAUI to understand how it supports and enables the use of MVVM. Throughout the book, we’ll be building the “Recipes!” app as a practical example, showcasing various aspects of the MVVM...

Technical requirements

Although this chapter gives a theoretical overview of MVVM, later there is some code showing MVVM in action so you can start seeing the value this pattern brings. To implement this sample yourself, you’ll need the following: Visual Studio 2022 (17.3 or greater), or any IDE that allows you to create .NET MAUI apps. Near the end of Chapter 2, What Is .NET MAUI?, we’ll be looking at how to get your machine ready for developing .NET MAUI apps. The sample code can also be found on GitHub at https://github.com/PacktPublishing/MVVM-pattern-.NET-MAUI.

Looking at MVVM’s core components

MVVM provides a very clear way to separate UI from business logic, promoting code reusability, maintainability, and testability while allowing flexible UI design and changes.

Having business logic living in the code-behind quickly becomes challenging as applications grow in size and complexity. Code-behind refers to the practice of placing the business logic within the same file as the user interface elements, often resulting in a significant amount of code being invoked through event handlers. This frequently results in a tight coupling between UI elements and the underlying business logic, as UI elements are directly referenced and manipulated within the code. As a result, making adjustments to the UI and performing unit testing can become more difficult. Later on in this chapter, in the MVVM in action section, we will see what it means to have business logic in the code-behind and how that complicates maintainability, testing, et cetera...

Separation of concerns matters

Separation of concerns is an important principle in software development that aims to divide software design and implementation into distinct parts, each with a specific and well-defined responsibility. This principle helps developers create more maintainable and flexible applications by reducing the complexity of each individual component and allowing for more modular and reusable code.

In practical terms, separation of concerns means that different aspects of a system are separated and dealt with independently, without overlapping concerns. This is achieved by creating distinct layers or modules, each with its own well-defined responsibilities and interfaces, and minimizing the dependencies between them.

Let’s consider the example of a restaurant’s management system. In such a system, there could be several concerns like table reservation, order taking, kitchen operations, and billing. According to the principle of separation of...

MVVM in action

Let’s look at a very simple application that shows the user a quote of the day on the screen. When the user clicks a button on the screen, the application will fetch a quote of the day from an API and show it onscreen. Once the button is clicked, it should be hidden. The code-behind approach will show you how this app can be written without using the MVVM pattern, whereas the second example shows the same functionality implemented using MVVM with testability in mind.

The code-behind approach

In the following code snippet, there is no separation of concerns; all the code is handled in the code-behind of the XAML page. While this code seems to be doing what is expected, there is no easy, fast, or robust way to test whether it works. This is because all the logic is handled in the click event handler of the button.

In MainPage.xaml, we define a Button and a Label:

<Grid>
    <Button
       ...

Common misconceptions about MVVM

There are several common misconceptions about MVVM that can lead to misunderstandings of its principles and best practices. Let’s dispel some of these and provide clarity on the pattern.

There should be no code in the code-behind

While it is true that the main purpose of MVVM is to separate the presentation logic from the application logic, it is not necessarily the case that there should be no code in the code-behind. The code-behind can still be used to handle simple UI-related events or for any logic that is tightly coupled with the View.

In fact, there are scenarios where putting some code in the code-behind can be more efficient and maintainable than trying to move everything to the ViewModel. For example, handling UI animations, scrolling, and controlling focus or complex visual behaviors may be easier to implement in the code-behind rather than trying to do it through data binding.

To ensure proper separation of concerns in...

Summary

To summarize, the MVVM pattern separates the concerns of data, UI, and logic, which makes the application easier to test, modify, and extend. By using the Model to represent the data and business logic, the View to present the data to the user, and the ViewModel to mediate between the Model and the View, the MVVM pattern promotes a clear separation of responsibilities that makes it easier to develop and maintain complex applications. Additionally, the use of Commands and data binding provides a powerful way to handle user input and keep the UI in sync with the application’s state. Understanding the components of MVVM is crucial for building successful .NET MAUI applications that are maintainable, scalable, and easy to test.

In Chapter 2, What Is .NET MAUI?, we’ll dive into .NET MAUI so that you have a good understanding of this framework. If you already have a thorough knowledge of .NET MAUI, you can skip this chapter. If you know the basics of it, it should...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The MVVM Pattern in .NET MAUI
Published in: Nov 2023Publisher: PacktISBN-13: 9781805125006
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $19.99/month. Cancel anytime

Author (1)

author image
Pieter Nijs

Pieter Nijs is a .NET consultant at Xebia in Belgium, with a keen interest in mobile and cloud development. He's been instrumental in diverse projects, from vast healthcare and telecom systems to compact LOB apps. Now, Pieter's exploring AI's potential to enhance customer projects innovatively. Passionate about technology, he actively experiments and shares knowledge as a conference speaker and trainer. Pieter has been awarded the Microsoft MVP Award since 2017, reflecting his unwavering passion and expertise in serving the community.
Read more about Pieter Nijs