Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 3 The Fundamentals of .NET MAUI

or bootstrapping process. Of course, we could and should follow the same approach for our other dependencies.

One additional concept I have introduced here is the use of constructor injection. Constructor injection is the process of providing the registered dependencies when creating an instance of our Baker. So, when our Baker is created, it is passed an instance of WeighingScale.

If you have a background with Xamarin.Forms, you will have come across the DependencyService. This provided a mechanism for managing dependency injection within an application; however, it received criticism in the past for not supporting constructor injection. This doesn’t mean

it wasn’t possible to achieve constructor injection in Xamarin.Forms applications but it required the use of a third-party package and there are a lot of great packages out there! Now it is all baked into .NET MAUI.

Registering Dependencies

In the previous section, I discussed how to minimize concrete dependencies in your code base. Now let’s look through how to configure those dependencies so that the dependents are given the correct implementations.

Implementations that you register in the generic host builder are referred to as services and the work of providing the implementations out to dependents is referred to as the ServiceProvider. You can register your services using the following.

AddSingleton

A singleton registration means that there will only ever be one instance of the object. So, based on the example of our Baker needing to use an IWeighingScale, we register it as follows:

builder.Services.AddSingleton<IWeighingScale, WeighingScale>();

60

Chapter 3 The Fundamentals of .NET MAUI

Then every time that an IWeighingScale is resolved, we will be provided with the same instance. This suits the weighing scale example because we use the same one throughout our baking process.

It is extremely unlikely that you will ever need to register a view model as a singleton. Doing so can introduce bits of behavior that you are most likely not expecting on top of the fact that you can run the risk of leaking memory.

AddTransient

A transient registration is the opposite of a singleton. Every time an implementation is resolved, a new instance is created and provided. So based on the example of our Baker needing to use an IWeighingScale, we register it as follows:

builder.Services.AddTransient<IWeighingScale, WeighingScale>();

As mentioned, every time an IWeighingScale is resolved, we will be provided with a new instance. A better example here might be the greaseproof paper that lines the cake tins. They are used once and thrown away.

AddScoped

A scoped registration is somewhere in the middle of a singleton and transient. A single instance will be provided for a “scope,” and then when a new scope is created, a new instance will be provided for the life of that scope.

builder.Services.AddScoped<IWeighingScale, WeighingScale>();

61