Dagger2注入来自两个不同类的依赖项的最佳方法 [英] Dagger2 Optimal way to inject dependencies from two different classes

查看:197
本文介绍了Dagger2注入来自两个不同类的依赖项的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在搜索并尝试了很多东西之后,我陷入了一些看似简单的问题。
以下是我的模块,负责注入改造。

After searching and trying too many things i'm stuck in some what seems like an easy problem. Below is my module which is reponsible for injecting retrofit.

@Module
public class NetworkingModule
{
    @Provides
    public Retrofit providesRetrofit()
    {
        Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
        Retrofit retrofit = new Retrofit.Builder()
                                        .baseUrl(Constants.BASE_URL)
                                        .addConverterFactory(GsonConverterFactory.create(gson))
                                        .build();

        return retrofit;
    }


}

我的NetworkComponent

My NetworkComponent

@Component( modules = NetworkingModule.class)
public interface NetworkingComponent {
    void inject(DashboardPresenterImpl target);

    void inject(PicksPresenterImpl picksPresenter);

    void inject(LoadsPresenterImpl loadsPresenter);

    void inject(ShippingPresenterImpl shippingPresenter);

    void inject(GeneralFilePresenterImpl generalFilePresenter);
}

带有构造函数注入的实用程序类。请注意,该类还注入了AppPreferences。

A utility class with constructor injection. Please note this class also has injection of AppPreferences.

public class AppUtils {
    private Context context;

    @Inject
    AppPreferences preferences;

    @Inject
    public AppUtils(@ActivityContext Context context)
    {
        this.context = context;

        /*ActivityComponent component = DaggerActivityComponent.builder()
                .activityModule(new ActivityModule((Activity) context))
                .build();
        component.inject(this);*/
    }
}

现在在我的代码中,我想实现这一目标

Now in my Code i want to achieve this

Class MyPresenterImpl{
@Inject
    Retrofit retrofit;

    @Inject
    AppUtils appUtils;
}

请提出一种实现上述目标的优化方法。

Please suggest an optimize and good way to achieve the above.

编辑

添加了AppPreference.java

Added AppPreference.java

public class AppPreferences {
    @Inject
    SharedPreferences sharedPreferences;

    private SharedPreferences.Editor editor;

    @Inject
    public AppPreferences(@ActivityContext Context context)
    {
        ApplicationComponent component = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule((Application) context.getApplicationContext()))
                .build();
        component.inject(this);

        editor = sharedPreferences.edit();
    }

    public void putString(String key, String value)
    {
        editor.putString(key, value).commit();
    }

    public String getString(String key)
    {
        return sharedPreferences.getString(key, null);
    }

}


推荐答案

请确定要使用字段注入还是构造函数注入,并希望选择构造函数注入

Please decide on whether you want to use field injection or constructor injection, and hopefully choose constructor injection.

public class AppUtils {
  private Context context;

  @Inject // field injection?
  AppPreferences preferences;

  @Inject // constructor injection?
  public AppUtils(@ActivityContext Context context)
  {
    // ...
  }
}

如果使用构造函数注入,则Dagger不会注入您的字段,而且以后也不应该自己调用它。

Dagger won't inject your fields if you use constructor injection and you should not call it yourself afterwards either. Your component should really not contain all those methods to inject your presenter etc.

如果您需要某些东西,请将其放在构造函数中。

If you need something, put it in the constructor.

public class AppUtils {
  private Context context;
  private AppPreferences preferences;

  @Inject // constructor injection!
  public AppUtils(@ActivityContext Context context, AppPreferences preferences)
  {
    // ...
  }
}

MyPresenterImpl 同样适用。如果您依赖某项内容,请将其放入构造函数中,并用 @Inject 标记该构造函数,然后Dagger将为您创建具有所有依赖项的对象。

The same applies for MyPresenterImpl. If you depend on something, put it in the constructor, mark the constructor with @Inject, and Dagger will create the object for you with all the dependencies provided.

对于Android框架类型(活动,片段等),您的组件应只包含 .inject(..)方法和

Your components should only contain .inject(..) method for Android framework types (Activities, Fragments, ...) and nothing else.

我最近也用一些有关使用Dagger的一般概念

这篇关于Dagger2注入来自两个不同类的依赖项的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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