Reader small image

You're reading from  .NET MAUI Cross-Platform Application Development - Second Edition

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781835080597
Edition2nd Edition
Languages
Right arrow
Author (1)
Roger Ye
Roger Ye
author image
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye

Right arrow

Software Design with Dependency Injection

Having introduced navigation and the Shell in .NET MAUI, we’ve laid the groundwork for building a comprehensive application. However, we are currently utilizing a mock data service, which we intend to modify in this chapter. Before diving into this, let’s first review the best practices in software design, starting with an overview of design principles. Later, we will explore how to leverage dependency injection in our application.

Software design principles and patterns typically form the backbone of best practices in software design. These principles offer rules and guidelines that software designers adhere to in crafting an efficient and clean design structure. They play a key role in shaping the software design process as they dictate the most effective practices. Design patterns are effectively best practices that experienced developers in object-oriented software employ. They function as templates designed to address...

Technical requirements

To test and debug the source code in this chapter, you need to have Visual Studio 2022 installed on your PC or Mac. Please refer to the Development environment setup section in Chapter 1, Getting Started with .NET MAUI, for the details.

The source code for this chapter is available in the following branch on GitHub: https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform-Application-Development-Second-edition/tree/main/2nd/chapter06.

To check out the source code of this chapter, we can use the below command:

$ git clone -b 2nd/chapter06 https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform-Application-Development-Second-edition.git PassXYZ.Vault2

To find out more about the source code in this book, please refer to the Managing the source code in this book section in Chapter 2, Building Our First .NET MAUI App.

A brief overview of design principles

Design principles are high-level guidelines that offer valuable advice on design considerations. These principles can provide essential guidance to help you make better design decisions. Some general design principles are applicable not only to software design but also to other design disciplines.

Let’s review some general design principles before we explore the commonly used design principles (SOLID) in software development.

Exploring types of design principles

Design principles encompass a vast subject area. Thus, rather than delving into intricate details, I will provide insights from my experiences in implementing design principles during development, offering a concise overview of the principles discussed in this book. We will begin with high-level principles such as DRY, KISS, and YAGNI, and then progress to those more commonly used in software development. In the realm of object-oriented programming (OOP), the most widely...

Implementing DI

DI is a technique that can be utilized in .NET MAUI. Although not a novel concept, it has been extensively employed in backend frameworks like ASP.NET Core and the Java Spring Framework. DI facilitates dependency inversion (DIP) by decoupling an object’s usage from its creation, eliminating the need for direct reliance on the object. In our app, once we have separated the IDataStore interface implementation, we can commence with a mock implementation and subsequently replace it with the actual implementation.

In .NET MAUI, the Microsoft.Extensions.DependencyInjection service, which we will refer to as MS.DI throughout this chapter, is readily available for us to utilize as a built-in feature.

In the realm of .NET, numerous DI containers are available besides MS.DI. Some of these alternatives, such as the Autofac DI container and the Simple Injector DI container, offer enhanced power and flexibility compared to MS.DI. At this point, one may wonder why...

Replacing the mock data store

As explored in previous sections, we can register the implementation of data store services in MauiProgram.cs as follows:

builder.Services.AddSingleton<IDataStore<Item>, MockDataStore>();
builder.Services.AddSingleton<IUserService<User>, UserService>();

In the above code snippet, we receive an instance of MockDataStore for the IDataStore interface. This is a mock implementation to simplify the initial development. Now, it’s time to substitute this with the actual implementation. We will replace the above code with the following:

builder.Services.AddSingleton<IDataStore<Item>, DataStore>();
builder.Services.AddSingleton<IUserService<User>, UserService>();

Here, DataStore is the actual implementation of the IDataStore service, which we will fully implement in the remainder of this chapter.

The password database is a local database in the KeePass 2.x format. Within this database...

Summary

In this chapter, we began by introducing design principles. Following this, we delved into the SOLID design principles, and I shared insights gleaned from the development of our app. Among the most crucial SOLID principles is the DIP. DI is a technique that applies the DIP in practical implementation. In our app, we utilize the built-in DI service of .NET MAUI to decouple dependencies, enabling us to separate the implementation of the service from the interface.

We accumulated extensive knowledge about .NET MAUI and successfully completed our app implementation by replacing MockDataStore with the actual implementation. We established CRUD operations on top of this new IDataStore service, resulting in a fully functional password manager app.

Although we have incorporated essential features in our app, users often expect additional desirable features in a password manager application, such as fingerprint scanning and one-time passwords. Some of these features are platform...

Further reading

  • Beginning SOLID Principles and Design Patterns for ASP.NET Developers, by Bipin Joshi
  • Autofac is an inversion of control (IoC) container for .NET Core, ASP.NET Core, .NET 4.5.1+, and more: https://autofac.org/
  • Simple Injector is a DI container that can support .NET 4.5 and .NET Standard: https://simpleinjector.org/

Learn more on Discord

Join our community’s Discord space for discussions with the author and other readers:

https://packt.link/cross-platform-app

lock icon
The rest of the chapter is locked
You have been reading a chapter from
.NET MAUI Cross-Platform Application Development - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781835080597
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
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye