Dependency Injection is the design pattern that help us to create application which loosely coupled. This means that object should only have those dependency that required during complete task. The main advantages of DI (Dependency Injection) is our application loosely coupled and has provide greater maintainability, test-ability and also re-usability. It is loosely coupled because dependency required by the class are injected from outer world rather than created them self directly win-in code.

Let’s think of a scenario where you are having the fever, you go to a medical shop and ask for medicine but then in return, the medical guy asked which or what type of medicine you need. then what ? You land up visiting the doctor who understands your needs and writes down the medicine which would work for you. Isn’t it. You got your work done with just a call of service. And that’s what Dependency Injection is in software development.

Asp.Net Core has the built-in support for Dependency Injection. Earlier on dot net framework, we used to use third-party dependency injector like Ninject, Autofac, and Unity etc. Being a game changer on Microsoft technology, Asp.Net core has its own simple built-in dependency injection container.

What is Dependency Injection?
Dependency Injection is also known as DI is the design pattern in software engineering. In simple term, DI is the concept of designing the software in such a way that the components of the software communicate with each other without depending on the concrete implementation.  The technique of DI decouple the different components of software and further helps to design the loosely coupling software.
Asp.Net Core Dependency Lifetime
Before we can talk about how injection is done in practice, it is critical to understand what is service lifetime. When a component requests another component through dependency injection, whether the instance it receives is unique to that instance of the component or not depends on the lifetime. Setting the lifetime thus decides how many times a component is instantiated, and if a component is shared.
There are 3 options for this with the built-in DI container in ASP.NET Core:
  • Singleton
  • Scoped
  • Transient
Singleton means only a single instance will ever be created. That instance is shared between all components that require it. The same instance is thus used always.
Scoped means an instance is created once per scope. A scope is created on every request to the application, thus any components registered as Scoped will be created once per request.
Transient The services created using transient lifetime will be created each time they are requested. This lifetime works best for lightweight services.

Dependency Injection can implement 3 ways

(i) Constructor Injection
This is a widely used way to implement DI.
  • Dependency Injection is done by supplying the DEPENDENCY through the class’s constructor when creating the instance of that class.
  • Injected component can be used anywhere within the class.
  • Recommended to use when the injected dependency, you are using across the class methods.
  • It addresses the most common scenario where a class requires one or more dependencies.
(ii) Property/Setter Injection
  • Recommended using when a class has optional dependencies, or where the implementations may need to be swapped.
  • Different logger implementations could be used in this way.
  • Does not require the creation of a new object or modifying the existing one. Without changing the object state, it could work.
(iii) Method Injection
  • Inject the dependency into a single method and generally for the use of that method.
  • It could be useful, where the whole class does not need the dependency, only one method having that dependency.
  • This is the way is rarely used.


Post a Comment

Previous Post Next Post