Dagger2 Android (Part 1) – Dependency, DIP in S.O.L.I.D and Dependency Injection

Tram Ho

1.What is Dependency?

Below is an example for a more understandable explanation of dependencies:

There are 2 Class A and Class B and A using some methods of B => When B changes, methods A will be more or less changed.

  • From that it can be easily seen that A depends on B and B is dependent of A. A may not function or function incorrectly, … when B is changed.
  • The situation will get worse and worse when A depends on too many things other than B like C, D, E, … or B is used from many things other than A like A1, A2, A3 ,. ..
  • Or when A, A1, A2, A3, …. all depend on B. Then B needs to change to meet A’s needs, resulting in A1, A2, A3 all affected.
  • This will be very serious and we probably will not control when the relationship between them here is nn

Why dependencies are bad:

  • Reduce code reuse
  • Make it difficult to test, write unit test
  • Reduced maintainability when scaling projects

So how to solve the above problem, how to solve it for A to reduce the influence when B changes. We learn about DI (Dependency Injection) below

2. DIP (Dependency inversion principle) principle is the ultimate in SOLID

Principle content:

  • High level modules should not depend on low level modules. Both should depend on the abstraction. (High-level modules should not depend on low-level modules. Both should depend on abstractions.)
  • Interface (abstraction) should not depend on details, but details should depend on abstraction. (Abstractions should not depend on details. Details should depend on abstractions.)

Based on an example explaining the principle:

  • I have a high-level module called the Power Socket
  • I have a low-end module called an electric light bulb
  • And an interface (abstraction) is a lamp holder

In order for a light bulb to light, the bulb holder at one end will be connected to the power outlet , the other end requires the electric bulb to just be a round tail regardless of whether it is incandescent or fluorescent. halogen lamp, LED (implementation).

3. DI (Dependency Injection)

The first thing Dependency Injection is built on the Inversion of Control concept. This said, a class gets its dependencies from the outside instead of hard dependencies. More understandably, a class configures ways to get its dependencies from other classes dynamically instead of instantiating the dependencies itself.

Next, Dependency Injection is a processing technique that passes parameters (Dependency) where it should be used at runtime instead of passing in at compile time . Back in item 1, using DI we can minimize or keep the source code of class A every time class B changes to ensure the application still works as expected.

So, what are ways of transmitting dependency (DI)? There are several ways as follows:

  • Constructor injection: Pass the dependency through the constructor of the class. This is the most common way
  • Setter injection: Pass the dependencies through the class preconfigured setter function
  • Interface injection: This is less commonly used because it is cumbersome. The class to be injected will implement an interface. This interface contains a method setInject. The container will inject the dependency into the class to be injected through calling the interface’s setInject function

What are the downsides of DI?

  • Difficult to debug
  • It is quite difficult to learn and understand in the beginning
  • Code complexity is higher …..

The article has references from:

https://medium.com/@harivigneshjayapalan/dagger-2-for-android-beginners-introduction-be6580cb3edb

https://toidicodedao.com/2015/03/24/solid-la-gi-ap-dung-cac-nguyen-ly-solid-de-tro-thanh-lap-trinh-vien-code-cung/

https://martinfowler.com/articles/injection.html

https://www.tutorialsteacher.com/ioc/dependency-injection

Share the news now

Source : Viblo