Author - StudySection Post Views - 159 views
Dependency

Dependency Injection

It is a software design pattern. It allows us to develop loosely coupled code

Dependency Injection helps in reducing the hard-coded dependencies among the classes by injecting those dependencies at run time instead of design time technically.

This is used to implement Inversion of Control (IoC) between classes and their dependencies.

Inversion of Control:

Implementation:
It can apply using an interface or an abstract class
graph

Example Dependency Injection:

Interface Ifood
{
Void Eat();
}

Class bread : IFood
{
Public void Eat(){
console.log(“I eat bread”);
}
}
Class butter: IFood
{
Public void Eat(){
console.log(“I eat butter”);
}
}

Class Food Controller
{
IFood _food ;
Public FoodController (IFood food){ // Constructor Injection
_food = food;
}
Public void StartEat(){
_food.Eat();
}
}
Bread b = new bread();
Foodcontroller food = new Foodcontroller(b); // pass object of bread class into main container class

LifeCycle of Dependency Injection

To implement dependency injection, there are some classes that are participating in DI to configure Dependency injection to decide whether to return a new instance of the service or an existing instance.

AddTransient
These types of services are created every time they are requested. It works best for lightweight, stateless services.

AddScoped
These types of services are created once per request.

AddSingleton
These types of services are created the first time they are requested and every subsequent request will use the same instance.

Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification exam conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri