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

Dependency Injection, Services, and Messaging

As we continue building our Recipes! app using .NET MAUI, we want to make the most of the MVVM design pattern. MVVM is great for keeping our code organized and also promotes industry-standard practices that make our code base more maintainable and testable. In this chapter, we will focus on two critical concepts that are central to a solid MVVM architecture: Dependency Injection (DI) and messaging. DI promotes separation of concerns and allows our code to be much more testable. Messaging helps us keep different parts of our code from getting tangled up with each other. It allows different areas of our app to talk to each other in a loosely coupled way. Both concepts are extremely important to ensure that our MVVM architecture truly stands out.

Let’s take a look at what this chapter covers:

  • Inversion of Control through Dependency Injection
  • Registering, resolving, and injecting services
  • Messaging

By the end...

Technical requirements

Throughout this chapter, we will be enhancing the functionality of the Recipes! app. All the resources, including additional classes and code required for the topics covered in this chapter, are available on GitHub at https://github.com/PacktPublishing/MVVM-pattern-.NET-MAUI/tree/main/Chapter07. To follow along with this chapter’s content, you can start with the provided Start folder. It contains the initial code and necessary classes specific to this chapter. This code serves as the foundation, building upon what we have learned in the previous chapters. If you want to reference or compare the completed code, including all the code written throughout this chapter, you can find it in the Finish folder.

Inversion of Control through Dependency Injection

Inversion of Control (IoC) is a programming principle where the control over certain aspects of a program’s flow is handed over from the main code to a framework or a container. In simple terms, instead of a component being responsible for managing its dependencies and life cycle, these responsibilities are inverted or delegated to an external controller. This approach is particularly useful for creating modular and flexible systems.

In a typical software design without IoC, a class that requires certain functionalities from other classes would create or manage these dependent objects within itself. With IoC, this creation and management is handled by an external component, hence inverting the control.

IoC can be achieved through various methods such as DI, Factory Pattern, Service Locator, and more. Among these, DI is the most commonly used method in the context of MVVM.

Dependency Injection

DI, a specific form of...

Registering, resolving, and injecting services

.NET MAUI comes with built-in support for DI. It has been designed with DI in mind, which allows for easier configuration and management of services that your application relies on. By providing out-of-the-box support for DI, .NET MAUI enables developers to leverage the concept of DI and IoC to make their code more maintainable and more loosely coupled. As the MVVM pattern benefits tremendously from DI and IoC, it shows again that MVVM and .NET MAUI are a perfect match!

The Microsoft.Extensions.DependencyInjection namespace is where .NET MAUI gets its default implementation for DI. However, it’s important to note that .NET MAUI is DI container agnostic, which means you’re not limited to the default. If you prefer a third-party DI container, you’re free to replace the default with your preferred choice. Let’s see how we can register services using .NET MAUI’s default DI implementation.

Registering...

Messaging

Messaging is a software architectural pattern that facilitates communication between different parts of an application. In the context of .NET MAUI and MVVM architecture, messaging is typically used to send notifications between loosely coupled components, such as between ViewModels, or from a Model to a ViewModel. This decouples the components and promotes a more modular and maintainable code base.

The concept of messaging is especially useful when data needs to be passed or events need to be communicated between parts of your application that do not have a direct relationship. Instead of tightly coupling these parts by having them directly call each other, you can use a messaging system where one part sends a message that any interested part of your application can receive and react to.

This pattern is a form of the Observer pattern, where an object, named the Subject, maintains a list of its dependents, called Observers, and notifies them automatically of any state...

Summary

In this chapter, we delved into two pivotal topics in the architecture of modern applications: DI and messaging. First, we explored DI, a technique for achieving loose coupling between objects and their dependencies. In the context of the MVVM pattern, we utilized this technique to inject services and other dependencies into our ViewModels, enhancing their testability and maintainability.

The latter part of this chapter focused on messaging, another integral component in MVVM applications for promoting decoupled communication between components. We examined WeakReferenceMessenger provided by the MVVM Toolkit, which facilitates loose coupling in the application.

In essence, this chapter aimed to reinforce the importance of loose coupling in software design, showcasing how both DI and messaging contribute significantly to the creation of maintainable and testable applications.

In the upcoming chapter, we’ll delve deep into the intricacies of navigation in .NET...

Further reading

To learn more about the topics that were covered in this chapter, take a look at the following resources:

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 $15.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