Dependency Injection
noun
say it: “dih-PEN-den-see in-JEK-shun” /dɪˈpɛndənsi ɪnˈdʒɛkʃən/
ASP.NET Core's built-in system that creates the services a class needs and hands them to it.
What is Dependency Injection?
ASP.NET Core has dependency injection built in: register services in builder.Services, and the framework hands them to constructors and handlers that ask for them.
builder.Services.AddScoped<IRecipeRepository, SqlRecipeRepository>();
app.MapGet("/recipes", (IRecipeRepository recipes) => recipes.ListAsync());
In a sentence
- “Through dependency injection, the controller's constructor receives IProductRepository.”
Level: Apprentice · More ASP.NET Core terms at this level
Updated