Not long ago, I studied Dagger2, but I felt it was rather obscure, and I didn’t use it in the project, so I stopped watching it. But I found MVP + Retrofit + Rxjava + Dagger2 android framework so popular that I thought it was time for another wave.

  • The ultimate goal of this blog series is to build the MVP + Dagger2 framework
  • This blog series contains the following posts:
  1. Dagger series 1 — Prelude: Basic introduction to dependency injection
  2. Dagger 2 series (2) — Basic: @Inject, @Component
  3. Dagger: @Module & @Provides
  4. Dagger 2 series (4) — Foundation: @named & @Qualifier
  5. Dagger: @Scope & @singleton

二, What is Dagger2?

Dagger2 is a Google open source Dependency Injection framework (DI). Dagger2 is a Dependency injection framework from Google. I’m sure some of you are wondering why there’s a 2 there. The framework is developed based on the Dagger developed by Square. So what is dependency injection?

1.1 What is dependency Injection (IOC)

I did some research on the Internet and found the following explanation relatively easy to understand (after customization) :

  1. In specific business logic of a class, need another instance of the class of the corresponding operation, in the traditional design, usually by the caller to create the caller instance (in general, our new ways to create), dependency injection way, however, created by the caller no longer by the caller to create an instance, created by the caller by an instance of the workThe IOC containerTo finish, and theninjectionTo the caller. Therefore also known asDependency injection.
  2. Dependency injectionIs that the caller needs another instance of an object that is not implemented internally, but is passed in from outside in some way,The coupling between classes is resolved. So what does this outside mean? If it’s another class, then the other class is coupled inside, and that’s not what we recommend. Is there a way to put these constructed objects into a container, and when you need an instance, you can just take it from that container. Instances and uses of a class are no longer related, but are connected through a container. Achieve decoupling.

Inversion of control — giving the ability to create your own instance object to a third party tool or container. Target class will create other instances of rights to the third party tools, that is, the original need programmer to independent in the target class with a new way to create instances of related classes of rights, and to the specific business (the so-called IOC container), the container when needed by it’s own way to create an instance of this class, that is: Transfer of rights to create class instances.

1.2 Benefits of dependency injection

Dependency injection is that the caller needs another object instance, which is not implemented inside the caller, but passed in from outside in some way, which solves the coupling between classes.

In plain English, an instantiation of an object is handed to its caller and passed in by some means, a pattern known as dependency injection. Common ways to reduce coupling are:

  • Constructor injection.
  • Setter injection.
  • Interface injection.

Second, according to Dagger2

Dagger2 works by generating dependency injection code at compile time. This is also where it differs from other dependency injection frameworks, which reflect annotation content at run time, affecting runtime efficiency.

chestnuts

public class MainClass {
    private  void main(String [] args){
        John john = new John("john",12);
        john.introduceSelf();
    }
}
Copy the code

The problem with the above example is that if the way John is created (such as construction parameters) is changed, you need to modify the code that created John in MainClass, but also everywhere else that created John. If we were using Dagger2, we wouldn’t need to worry about this, just write where we need John:

@Inject
John john;
Copy the code

PS: Above is the basic concept of Dagger2 before learning introduction, welcome to refer to the correction.

Refer to the article


Dagger,

This is Dagger2

Dagger2 Introductory Practice

The simplest introduction to Dagger2

Android: Dagger2 will make you love it – The end