Using DI with ViewModel classes
Most of the popular MVVM frameworks today include a DI container to manage dependencies. Because .NET now includes its own DI container, we will use that one. The .NET team has incorporated the DI container that used to be bundled with ASP.NET Core. It’s both lightweight and easy to use. Luckily, this container is now available to all types of .NET projects via a NuGet package:
- Open the project from the previous chapter or use the project in the
Start
folder in the GitHub repository for this chapter. In theMyMediaCollection
project, open NuGet Package Manager and search forMicrosoft.Extensions.Hosting
:
Figure 4.1 – Microsoft’s DI NuGet package
- Select the package and install the latest stable version. After the installation completes, close the NuGet Package Manager tab and open
App.xaml.cs
. We will make a few changes here to start using the DI container.The DI container implements...