Using DI
DI is one of the tools that we can use in .NET MAUI. It is actually not something new, and it has been used heavily in backend frameworks such as ASP.NET Core or the Java Spring Framework. DI is a technique for achieving dependency inversion (DIP). It can help to decouple the usage of an object from its creation so that we don’t have to depend on the object we use directly. In our app, after we decouple the implementation of the IDataStore
interface, we can start with a mock implementation first and replace it with the actual implementation later.
In .NET MAUI, Microsoft.Extensions.DependencyInjection
—or MS.DI, as we shorten the name in this chapter—is a built-in service that we can use directly.
In the .NET world, there are many DI containers available other than MS.DI. They may be more powerful and flexible than MS.DI, such as the Autofac DI container or the Simple Injector DI container, and so on. Then, why do we choose MS.DI instead of other...