Understanding dependency injection
DI is a software pattern and a technique to implement Inversion of Control (IoC).
IoC is a generic term that means we can indicate that the class needs a class instance instead of letting our classes instantiate an object. We can say that our class wants either a specific class or a specific interface.
The creation of the class is somewhere else, and it is up to IoC what class it will create.
When it comes to DI, it is a form of IoC when an object (class instance) is passed through constructors, parameters, or service lookups.
Here is a great resource if you want to dive deeper into DI in .NET: https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection.
In Blazor, we can configure DI by providing a way to instantiate an object; this is a key architecture pattern that we should use. We have already seen a couple of references to it, for example, in Program.cs
:
builder.Services.AddScoped<IBlogApi, BlogApiJsonDirectAccess...