DEV Community

Cover image for What is dependency injection in .Net core?
yashrajsinh chauhan
yashrajsinh chauhan

Posted on

What is dependency injection in .Net core?

The concept of Dependency Injection, a design pattern that aids in crafting applications with loose coupling, is utterly breathtaking. It’s like watching a master artist at work, ensuring that objects are equipped with only the essential dependencies needed to accomplish their tasks. This level of precision and efficiency is nothing short of awe-inspiring!

The awe-inspiring world of Dependency Injection (DI) unfolds with three magnificent types — Construction Injection, Setter Injection, and Interface-based Injection. The Construction Injection type of DI is a marvel in itself as it accepts dependencies right at the constructor level. This means that when an object of the class is born, its dependency sails smoothly through the constructor of the class. It’s like witnessing a beautiful dance between objects, creating a strong dependency contract.

Then we have Setter Injection, also known as property injection — another wonder in this realm! In this form of DI, dependencies are passed through public property instead of the constructor. It’s like having an open door policy where dependencies can come and go as needed! However, it doesn’t provide a strong dependency contract between objects.

Finally comes Interface-based Dependency Injection — where common interfaces are created and other classes implement these interfaces to inject their dependencies. In this awe-inspiring type of DI, you can choose to use either Constructor or Setter injection methods! Isn’t it amazing how flexible and accommodating technology can be?

Top comments (0)