Design Pattern –  Dependency Injection in C#

Its always a good practice to create a scalable   and robust software or application. For that we not only need good technologies but also need a prefect programming design approach  to design more scalable, reusable, and testable code to improve performance and security.  

Dependency injection is a design pattern in programming, which allows developers to create loosely coupled components in their applications. creation. In ASP.NET Core specifically, DI plays a crucial role in building robust and scalable applications. 

Before understanding what Dependency Injection means in programming, let’s first see what it means in general as it will help  us to understand the concept better. Dependency or dependent means relying on something for support. Like if I say we are relying too much on mobile phones than it means we are dependent on them. 

In general programming when Class A uses some functionality of Class B it is said Class A is dependent on Class B, and as we know to use functionality  of class B, class A create an object of class of B to access functionality of B class. It means Class A directly access B’s functionality. 

It can be also  be  explained as if an application have Email class for email functionality such as sending email and others,  and Database Class to  performs database CRUD operation. you can see the scenario as follow

Suppose that a page need to use these functionality so in C#  Page 1 class need to create object of Email and Database before using It. Now let assume that Each page (class) in web application using object of these class (Email, Database ) directly which means the classes  which are using these functionality are directly dependent on Email and Database classes,   which is not good according to solid principal – inversion of control  This states that a class should not configure its dependencies statically but should be configured by some other class from outside. 

According to the principles, a class should concentrate on fulfilling its responsibilities and not on creating objects that it requires to fulfill those responsibilities. And that’s where dependency injection comes into play: it provides the class with the required objects.


So, transferring the task of creating the object to someone else and directly using the dependency is called dependency injection.

What is Dependency  Injection ?

 Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. This helps you to follow SOLID’s dependency inversion and single responsibility principles. The purpose of Dependency Injection is to promote code modularity, reusability and testability.

 What  are benefits of Dependency Injection ?

  • Helps in Unit testing.
  • Boiler plate code is reduced, as initializing of dependencies is done by the injector component.
  • Extending the application becomes easier.


How to achieve Dependency Injection ?

To use this technique, we need classes that fulfill four basic roles. These are:

  • The service you want to use.
  • The client that uses the service.
  • An interface that’s used by the client and implemented by the service.
  • The injector which creates a service instance and injects it into the client.

Disadwantages Of Dependency Injection

  • It’s a bit complex to learn, and if overused can lead to management issues and other problems.
  • Many compile time errors are pushed to run-time.
  • Dependency injection frameworks are implemented with reflection or dynamic programming. This can hinder use of IDE automation, such as “find references”, “show call hierarchy” and safe refactoring.

Leave a Reply

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