What is Dependency Injection?

Jay Tillu
3 min readNov 25, 2022

--

Dependency Injection

While designing an application organization of code is very crucial work. Code Designing concepts help us in organizing the code and Dependency Injection is one of the important concepts of Code Designing.

What is Dependency?

Dependency means relying on something for support.

When class A uses some functionality of class B, then we can say that class A has a dependency on class B.

What is Dependency Injection?

Dependency Injection is a technique in which one object supplies the dependencies of another object.

Dependency Injection means transferring the task of object creation to someone else and directly using ready-made dependency is called Dependency Injection.

Dependency Injection allows you to move dependent object creation outside of the class.

There are three main responsibilities of Dependency Injection:

  1. Create the Objects
  2. Know which class requires those objects
  3. Provide all objects to required classes

If there is any change in Objects, then it’s Dependency Injection’s responsibility to handle that change. So if any object changes in the future then DI will provide the appropriate object to the class. Dependency Injection ensures that the class should not worry about creating objects.

Advantages of using Dependency Injection

  • Dependency Injection helps in unit testing.
  • It helps in reducing the boilerplate code.
  • Dependency Injection helps in loose coupling. It ensures objects are not bound hardly with each other.

Disadvantages of using Dependency Injection

  • Dependency Injection is difficult to learn.
  • Overusage of Dependency Injection can lead to code management issues.
  • Dependency Injection enables a high risk of runtime errors.

How does Dependency Injection work?

The process of Dependency Injection involves three types of classes:

  1. Client Class — The Client Class is a class that depends on the Service Class.
  2. Service Class — The Service Class 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.

Types of Dependency Injection

As we know Injector Class injects the service to the client. Injector Class injects these services in three ways:

  • Through a Constructor
  • Through a Property
  • Through a Method
  1. Constructor Injection — In this way injector supplies dependency through the client class constructor.
  2. Property Injection — In this way, the injector supplies dependency through the public property of the Client Class.
  3. Method Injection — In this way, the client class implements an interface that declares the methods to supply the dependency and the injector uses this interface to supply the dependency to the client class.

That’s all for Dependency Injection. If you didn’t understand the concept Read it again. It is a complex topic guys. It will take some time to take it down. So be calm and read it again.

During this series of understanding Flutter, we will move forward with GetX. Check out my publication and profile to get the latest posts.

So, guys, that’s it for State Management. Feel free to tell me if I miss something, I’d love to learn it from you. Till then Keep Loving, Keep Coding. And Just like always I’ll catch you up in the next article.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

Learn More about Dart and Flutter

Follow me for more such content.

--

--

Jay Tillu

I am Frontend Web Developer and Hobbyist Blogger. As a Web Developer, I hold expertise in HTML, CSS, JavaScript, and React.