Manikandan M
What is Dependency Injection

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

The Dependency Injection pattern involves 3 types of classes.

  1. Client Class: The client class (dependent class) is a class which depends on the service class
  2. Service Class: The service class (dependency) is a class that provides service to the client class.
  3. Injector Class: The injector class injects the service class object into the client class.

As you can see, the injector class creates an object of the service class, and injects that object to a client object. In this way, the DI pattern separates the responsibility of creating an object of the service class out of the client class.
Types of Dependency Injection

As you have seen above, the injector class injects the service (dependency) to the client (dependent). The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method.

  1. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.
  2. Property Injection: In the property injection (aka the Setter Injection), the injector supplies the dependency through a public property of the client class.
  3. Method Injection: In this type of injection, the client class implements an interface which declares the method(s) to supply the dependency and the injector uses this interface to supply the dependency to the client class.

By Manikandan M in Xamarin on Feb 23 2022
  • Uttam Kumar
    Mar, 2022 1

    Dependency Injection is a technique in which an object will receive other object which it depends on called dependency

    • 3
  • Jaydeep Patil
    Jun, 2022 7

    There are also multiple ways to inject DI outside the Constructor

    There are also following different ways to inject the DI without Controller Constructor

    Method 1:

    1. [HttpGet]
    2. [Route("api/Product/GetProducts")]
    3. public IEnumerable<ProductDetail> GetProducts()
    4. {
    5. //method 1
    6. var services = this.HttpContext.RequestServices;
    7. var productService = (IProductService)services.GetService(typeof(IProductService));
    8. return productService.GetProducts();
    9. }

    Method 2:

    1. [HttpPost]
    2. [Route("api/Product/AddProduct")]
    3. public IActionResult AddProduct(ProductDetail product)
    4. {
    5. //Method 2
    6. var productService =
    7. (IProductService)this.HttpContext.RequestServices.GetService(typeof(IProductService));
    8. productService.AddProduct(product);
    9. return Ok();
    10. }

    Method 3:

    1. //Method 3
    2. public IActionResult UpdateProduct([FromServices] IProductService productService,
    3. ProductDetail product)
    4. {
    5. productService.UpdateProduct(product);
    6. return Ok();
    7. }

    HaPPy Coding!

    • 0
  • Muhammad Imran Ansari
    Mar, 2022 10

    Very simple and precise answer is “Dependency Injection (DI) is a programming technique that makes a class independent of its dependencies”. That means you create an object of class whenever needed.

    • 0
  • Md Sarfaraj
    Mar, 2022 10

    Check out the below link I hope it will help you.

    Link: https://stackoverflow.com/questions/130794/what-is-dependency-injection

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS