What is dependency injection in asp.net core.
Loading
What is dependency injection in asp.net core.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaydeep PatilPosted Jun 24, 2024, 12:02 PM
Hi,
Please have a look into my below article to understand DI in detail with some practical examples.
https://www.c-sharpcorner.com/blogs/dependency-injection-using-net-core-api
Dharmeshwaran SPosted Jun 23, 2024, 8:58 AM
Dependency Injection is the Technique not only specific to .net. It is used across multiple languages
Dependency Injection (DI) is a design pattern used to implement Inversion of Control (IoC). It allows a class to receive its dependencies from an external source rather than creating them itself. This leads to more modular, testable, and maintainable code.
Simple Explanation
Imagine you have a car, and the car needs an engine to function. Instead of the car creating its own engine, it receives an engine from outside. This way, you can easily swap the engine with another one if needed, and you can test the car by providing a mock engine.
Basic Components of DI
Example in C#
Let's see a simple example in C# without using any DI framework.
Without Dependency Injection
In this example, the
Carclass creates its ownEngine.With Dependency Injection
Here, the
Engineis injected into theCarfrom the outside.Types of Dependency Injection
Benefits of Dependency Injection
In summary, Dependency Injection is a powerful pattern that helps to create flexible, testable, and maintainable code by externalizing the creation and management of dependencies.