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 where an object (class instance) is passed through constructors, parameters, or service lookups.
In Blazor, we can configure DI by providing the way to instantiate an object. In Blazor, this is a key architecture pattern that we should use. We have seen a couple of references to it already, for example, in Startup.cs
:
services.AddSingleton<WeatherForecastService>();
Here, we say that if any class wants WeatherForecastService
, the application should instantiate an object of the WeatherForecastService
type. In...