Dagger2 for Android development and combination with mvp

Introduction

Dagger2 is a dependency injection framework. The dependency injection framework we often use is ButterKnife. Speaking of this, first understand a concept: inversion of control.

Inversion of Control (also known as IOC: Inversion Of Control): It is a design principle in object-oriented programming that can be used to reduce the coupling between computer codes. The most common method is called Dependency Injection, and another method is called Dependency Lookup. Through the inversion of control, when an object is created, the external entities that govern all objects in the system pass the references of the objects it depends on to it. It can also be said that the dependency is injected into the object.

Speaking of the use of Dagger2, the first thing we understand is what are the benefits of using Dagger2?

Dagger2 is used to decouple the code. The new code of a class is very likely to be flooded in multiple classes of the app. If the constructor of the class changes, then these involved classes have to be modified.

If you use Dagger2's Inject annotation constructor, even if the constructor changes significantly, we basically don't need to modify any code.

Basic use

Dagger2 is divided into three parts: relying on the provider, relying on the demander, and dependency injection container (that is, the bridge between the two).

In Dagger2, @Module annotated classes are used as providers, @Component annotated classes are used as bridges, and @Inject annotated variables are used as demanders.

Here to talk about @Inject

The @Inject annotation has two functions:

As a dependent provider, the @Inject annotation is added to the constructor of the class.
As a dependent demander, the @Inject annotation is added to the member variable.
Introduction: If you add this dependency injection to the class, Dagger will construct an instance of this class and satisfy Their dependence. Through this inject annotation, the dependent demand-side object can be sent to the Component class, and the Component class will inject the required objects in the dependent demand-side object according to the dependency declared in the dependent demand-side object.
Note:
The parameter of the inject method cannot use the parent Class to receive,
@Inject annotated fields cannot be private and protected.

Let's briefly talk about the steps to use Dagger2:

Introduce

//Dagger2 依赖注入框架
api 'com.google.dagger:dagger:2.17'
annotationProcessor 'com.google.dagger:dagger-compiler:2.17'

Use without participation

1, Create a class Person, and add @Inject annotation to the constructor of the Person class as a dependent provider

import javax.inject.Inject;

public class Student {
    
    

    @Inject
    public Student() {
    
    
    }

    public String show(){
    
    
        return "我是秀儿";
    }
}

2, We create the ActivityComponent class, automatically load the @Inject object into it, and then provide it to the activity.


@Component
public interface ActivityComponent {
    
    

    //方法可以有多个. 想在哪个页面中使用就注入到哪个页面
    void inject(Main2Activity main2Activity);
    void inject(MainActivity mainActivity);

}

3, in the activity, for injection use


public class Main2Activity extends AppCompatActivity {
    
    

    @Inject
    Student student;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //dagger会在Component前面加入,就是命名了
        DaggerActivityComponent.builder().build().inject(this);
        String show = student.show();
        Toast.makeText(this, show, Toast.LENGTH_SHORT).show();
    }

Use with participation

1, entity class


public class Student {
    
    

    private String name;

//    @Inject
//    public Student() {
    
    
//    }

    @Inject
    public Student(String name) {
    
    
        this.name = name;
    }

    public String show(){
    
    
        return "我是秀儿";
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
}

2, moudel class


@Module
public class StudentModel {
    
    

    String name;

    public StudentModel(String name) {
    
    
        this.name = name;
    }

    @Provides
    public String getName() {
    
    
        return name;
    }

}

3, component class


@Component(modules = StudentModel.class)
public interface ActivityComponent {
    
    
    //方法可以有多个. 想在哪个页面中使用就注入到哪个页面
    void inject(Main2Activity main2Activity);
    void inject(MainActivity mainActivity);

}

4. Used in Activity


public class Main2Activity extends AppCompatActivity {
    
    

    private static final String TAG = "Main2Activity";
    @Inject
    Student student;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //dagger会在Component前面加入,就是命名了
//        DaggerActivityComponent.builder().build().inject(this);
        DaggerActivityComponent.builder().studentModel(new StudentModel("小明")).build().inject(this);
        String show = student.show();
        String name = student.getName();
//        Toast.makeText(this, show, Toast.LENGTH_SHORT).show();
        Log.i(TAG, "onCreate: "+show);
        Log.i(TAG, "onCreate: "+name);
    }
}

Combine dagger and mvp

1. Add @Inject annotation to baseActivity and baseFragment


public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements IActivity,IView {
    
    

    @Inject
    protected P ipresent;
    }
 
 
public abstract class BaseFragment<P extends IPresenter> extends Fragment implements IFragment,IView {
    
    

    @Inject
    protected P ipresenter;
}

2, add on basePresenter

   
public class BasePresenter<M extends IModel,V extends IView> implements IPresenter {
    
    
    protected V view;
    protected M model;
    @Inject
    public BasePresenter(V view, M model) {
    
    
        this.view = view;
        this.model = model;
    }

    @Override
    public void destory() {
    
    
        if(model != null){
    
    
            model.destory();
            model = null;
        }
    }
}

3. Create a module (because there are parameters)

@Module
public class FoodModule {
    
    

   private FoodContract.IFoodView iFoodView;


   public FoodModule(FoodContract.IFoodView iFoodView) {
    
    
       this.iFoodView = iFoodView;
   }

   @Provides
   public FoodContract.IFoodView getiFoodView(){
    
    
       return iFoodView;
   }

   @Provides
   public FoodContract.IFoodModel getFoodModel(){
    
    
       return new FoodModel();
   }
}

4. Create a bridge

@Component(modules = FoodModule.class)
public interface MainActivityComponent {
    
    
    void inject(MainActivity mainActivity);
}

5, use

    @Override
    public void initDate() {
    
    
//         ipresent = new FoodPresenter(this, new FoodModel());
        DaggerMainActivityComponent.builder().foodModule(new FoodModule(this)).build().inject(this);
        ipresent.getFood();
//        ipresent.getFood();
    }```

Guess you like

Origin blog.csdn.net/shuai_ge_feng/article/details/108682307