简体   繁体   English

Dagger with Android:如何在使用MVP时注入上下文?

[英]Dagger with Android: How to inject context when using MVP?

While developing an Android application I stumbled on a problem. 在开发Android应用程序时,我偶然发现了一个问题。 I just started using Dagger so I know some basic concepts but when using it outside the scopes of tutorials and their use cases things become less clear. 我刚刚开始使用Dagger,所以我知道一些基本概念,但是当在教程范围之外使用它时,它们的用例变得不那么清楚了。

So to get to the point. 所以要明白这一点。 In my application I'm using MVP as described in this blog post: http://antonioleiva.com/mvp-android/ 在我的应用程序中,我正在使用MVP,如本博客文章中所述: http//antonioleiva.com/mvp-android/

So at first I was injecting the Interactor class (the one that handles the data) to the Presenter class and everything was ok. 所以起初我将Interactor类(处理数据的类)注入Presenter类,一切正常。 But then I implemented methods that use SQLite database, so now there was a need to use Context in my Interactor class. 但后来我实现了使用SQLite数据库的方法,所以现在需要在我的Interactor类中使用Context。

I can't figure out how should I do this properly? 我无法弄清楚我该如何正确地做到这一点? My temporary fix was to exclude Dagger from my application and pass Context variable in constructor when creating Presenter class and then Interactor class inside presenter but I'd like to use Dagger. 我的临时修复是将Dagger从我的应用程序中排除,并在创建Presenter类时在构造函数中传递Context变量,然后在presenter中传递Interactor类,但我想使用Dagger。

So my current application looks somewhat like this. 所以我目前的应用看起来有点像这样。

MyActivity implements MyView {     
      MyPresenter p = new MyPresenter(this, getApplicationContext());
}

Constructor inside MyPresenter MyPresenter中的构造函数

MyPresenter(MyView view, Context context) {
      this.view = view;
      MyInteractor i = new MyInteractor(context);
}

And in constructor in MyInteractor I assign Context to a private variable. MyInteractor构造函数中,我将Context分配给私有变量。

I would only need to inject MyInteractor to MyPresenter , because this is the part of application that will need to be tested against different implementations. 我只需要将MyPresenter注入MyInteractor ,因为这是需要针对不同实现进行测试的应用程序的一部分。 But if it would be also possible to inject MyPresenter to MyActivity , that would be great :) 但是,如果它也将是可能的注入MyPresenterMyActivity ,那将是巨大的:)

I hope someone has some experience with what I'm trying to achieve :) 我希望有人对我想要实现的目标有一些经验:)

In your class MyInteractor: 在你的类MyInteractor中:

public class MyInteractor {

    @Inject
    public MyInteractor(Context context) {
       // Do your stuff...
    }
}

MyPresenter-class MyPresenter级

public class MyPresenter {
    @Inject
    MyInteractor interactor;

    public MyPresenter(MyView view) {
        // Perform your injection. Depends on your dagger implementation, e.g.
        OBJECTGRAPH.inject(this)
    }
}

For injecting a Context you will need to write a module with a provides method: 要注入Context,您需要使用provide方法编写一个模块:

@Module (injects = {MyPresenter.class})
public class RootModule {
    private Context context;

    public RootModule(BaseApplication application) {
        this.context = application.getApplicationContext();
    }

    @Provides
    @Singleton
    Context provideContext() {
        return context;
    }
}

Injecting your Presenter-class to your activity is not so easy, because in your constructor you have this MyView-parameter, which could not be set by Dagger. 将Presenter类注入到您的活动中并不容易,因为在您的构造函数中,您有此MyView参数,Dagger无法设置该参数。 You could rethink your design by providing a setMyView-method in your MyPresenter-class instead of using a constructor-parameter. 您可以通过在MyPresenter类中提供setMyView方法而不是使用构造函数参数来重新考虑您的设计。

Edit: Creating RootModule 编辑:创建RootModule

public class BaseApplication extends Application {
    // Store Objectgraph as member attribute or use a Wrapper-class or...

    @Override
    public void onCreate() {
        super.onCreate();
        OBJECTGRAPH = ObjectGraph.create(getInjectionModule());
    } 

    protected Object getInjectionModule() {
        return new RootModule(this);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM