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:
- Create the Objects
- Know which class requires those objects
- 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 change 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 bounded hardly with each other.
Disadvantages of using Dependency Injection
- Dependency Injection is difficult to learn.
- Over usage 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:
- Client Class — The Client Class is a class that depends on the Service Class.
- Service Class — The Service Class is a class that provides service to the Client Class.
- 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
- Constructor Injection — In this way injector supplies dependency through the client class constructor.
- Property Injection — In this way injector supplies dependency through the public property of the Client Class.
- 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 complex topic guys. It will take some time to take it down. So be calm and read it again.
During this series of understanind 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’ll 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.
Jai Hind, Vande Mataram 🇮🇳
Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊
or just mail me at firstname.lastname@example.org