如何声明依赖 [英] How to declare dependencies

查看:108
本文介绍了如何声明依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Dagger 2,所以我想了解一些基本知识。我有以下代码:

I'm studying Dagger 2 so I would like to understand some basic things. I have the following code:

@Module
public class MainModule {

@Provides
public Presenter provideMainActivityPresenter(Model model){
    return new MainPresenter(model);

}

@Provides
public Model provideMainModel(){
    return new MainModel();
 }
}

和我的 MainPresenter 类如下:

public class MainPresenter implements Presenter {

@Nullable
private ViewImpl view;
private Model model;



public MainPresenter(Model model) {
    this.model = model;
}

@Override
public void setView(ViewImpl view) {
    this.view = view;
  }
 }

代替上面的代码,我可以执行以下操作吗? ?

Instead of the above code, could I do the following?

public class MainPresenter implements Presenter {

@Nullable
private ViewImpl view;

@Inject
Model model;


@Override
public void setView(ViewImpl view) {
    this.view = view;
 }
}

因为 MainPresenter 取决于 Model ,而不是 @Nullable

Because the MainPresenter depends on the Model and it is not @Nullable.
Or this is wrong?

我不知道何时应该将依赖项作为构造函数参数,或者何时应使用 @Inject

I don't understand when I should put a dependency as a constructor argument, or when I should use @Inject

推荐答案

您基本上有3种使用匕首的方法

You have basically 3 ways to use Dagger


  • 构造函数注入

  • 字段注入

  • 自己从模块中提供

(还有创建对象后调用方法的方法注入)

(There is also method injection which calls a method after creating your object)

以下内容使用提供您课程的模块。没错,这是编写和维护的最昂贵的开销。您可以通过传入请求的依赖项来创建对象并返回该对象:

The following is using a module that provides your class. While not wrong, this is the most overhead to write and maintain. You create the object by passing in the requested dependencies and return it:

// in a module

@Provides
public Presenter provideMainActivityPresenter(Model model){
  // you request model and pass it to the constructor yourself
  return new MainPresenter(model);
}

这应该与需要额外设置的东西一起使用,例如 Gson OkHttp Retrofit ,以便您可以在一个对象中创建该对象

This should be used with things that require additional setup, like Gson, OkHttp, or Retrofit so that you can create the object in one place with the required dependencies.

以下内容将用于在您无权访问或无法访问的对象中注入对象不想使用构造。您可以对该字段进行注释并在组件上注册一个方法以注入您的对象:

The following would be used to inject objects where you don't have access or don't want to use the constructo. You annotate the field and register a method at the component to inject your object:

@Component class SomeComponent {
  void injectPresenter(MainPresenter presenter);
}

public class MainPresenter implements Presenter {

  // it's not annotated by @Inject, so it will be ignored
  @Nullable
  private ViewImpl view; 

  // will be field injected by calling Component.injectPresenter(presenter)
  @Inject
  Model model;

  // other methods, etc
}

这将还为您提供了在演示者上注册所有类的开销,并且当您无法使用构造函数(如活动,片段或服务)时应使用该开销。这就是为什么所有这些Dagger示例都具有那些 onCreate(){DaggerComponent.inject(this); } 方法来注入部分Android框架。

This will also provide you with overhead to register all your classes at a presenter and should be used when you can't use the constructor, like Activities, Fragments, or with Services. That's why all those Dagger samples have those onCreate() { DaggerComponent.inject(this); } methods to inject parts of the Android framework.

最重要的是,您可以使用构造函数注入。您用 @Inject 注释构造函数,然后让Dagger了解如何创建它。

Most importantly you can use Constructor Injection. You annotate the constructor with @Inject and let Dagger find out how to create it.

public class MainPresenter implements Presenter {

  // not assigned by constructor
  @Nullable
  private ViewImpl view;

  // assigned in the constructor which gets called by dagger and the dependency is passed in
  private Model model;

  // dagger will call the constructor and pass in the Model
  @Inject 
  public MainPresenter(Model model) {
    this.model = model;
  }
}

这仅要求您注释类构造函数和Dagger

This only requires you to annotated your class constructor and Dagger will know how to handle it, given that all the dependencies (constructor arguments, Model in this example) can be provided.

上面提到的所有方法都会创建一个对象,并且可以/应该在不同的情况下使用。

All of the methods mentioned above will create an object and can / should be used in different circumstances.

所有这些方法要么将依赖项传递给构造函数,要么注入直接使用 @Inject 注释的字段。因此,依赖项应该在构造函数中,或者由 @Inject 注释,以便Dagger知道它们。

All of those methods either pass the dependencies to the constructor, or inject @Inject annotated fields directly. Dependencies should thuse be in the constructor or annotated by @Inject so that Dagger knows about them.

我还写了关于 Dagger 2的基本用法的博客帖子以及更多详细信息。

I also wrote a blog post about the basic usage of Dagger 2 with some further details.

这篇关于如何声明依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆