主持人注射Dagger 2

时间:2016-04-25 11:10:35

标签: java android dependency-injection mvp dagger-2

我刚刚开始使用Dagger 2,我在网上找到了数千个指南,每个指南都有不同的实现,现在我有点困惑。 所以基本上这就是我现在所写的内容:

AppModule.java:

@Module
public class AppModule {

 Application mApplication;

 public AppModule(Application application) {
    mApplication = application;
 }

 @Provides
 @Singleton
 Application providesApplication() {
    return mApplication;
 }
}

DataModule.java:

@Module
public class DataModule {

private static final String BASE_URL = "http://beta.fridgewizard.com:9001/api/";

@Provides
@Singleton
NetworkService provideNetworkService() {
    return new NetworkService(BASE_URL);
}

@Provides
@Singleton
SharedPreferences provideSharedPreferences(Application app) {
    return PreferenceManager.getDefaultSharedPreferences(app);
}
}

PrefsModel.java:

@Module(includes = DataModule.class)
public class PrefsModel {

@Provides
@Singleton
QueryPreferences provideQuery(SharedPreferences prefs) {
    return new QueryPreferences(prefs);
}
}

AppComponent.java(因为我需要在演示者中使用它,所以我公开了QueryPreferences对象,希望以这种方式正确):

@Singleton
@Component(modules = {AppModule.class, DataModule.class, PrefsModel.class})
public interface AppComponent {

    void inject(HomeFragment homeFragment);

    QueryPreferences preferences();
    NetworkService networkService();
}

然后我有FwApplication.java:

public class FwApplication extends Application {

private static final String TAG = "FwApplication";

private NetworkService mNetworkService;

private AppComponent mDataComponent;

    @Override
    public void onCreate() {
       super.onCreate();

       buildComponentAndInject();
    }

    public static AppComponent component(Context context) {
      return ((FwApplication)   context.getApplicationContext()).mDataComponent;
    }

    public void buildComponentAndInject() {
       mDataComponent = DaggerComponentInitializer.init(this);
    }

    public static final class DaggerComponentInitializer {
      public static AppComponent init(FwApplication app) {
        return DaggerAppComponent.builder()
                .appModule(new AppModule(app))
                .dataModule(new DataModule())
                .build();
    }
   }
}

最后,我为演示者添加了另一个模块:

@Module
public class PresenterModule {

   @Provides
   Presenter<FwView> provideHomePresenter(NetworkService networkService) {
      return new HomePresenterImpl(networkService);
   }

   @Provides
   Presenter<FwView> provideSearchPresenter(NetworkService networkService) {
      return new SearchPresenterImpl(networkService);
   }

}

以下组件(由于我无法在此处添加作用域依赖项而返回错误):

@Component(dependencies = AppComponent.class, modules = PresenterModule.class)
public interface PresenterComponent {

    void inject(HomePresenterImpl presenter);
}

所以,我在网上阅读文档时几乎没有什么问题不清楚:

  • 如何修复presenter组件中的错误,因为它依赖于NetworkService,它是AppComponent中定义的单例?
  • 我有一个HomeFragment,它应该使用&#34; new HomePresenter(networkService)&#34;来实现HomePresenter。但现在我不知道如何使用DI定义的

编辑 - 修正:

HomeFragment.java:

public class HomeFragment extends Fragment {

private static final String TAG = "FW.HomeFragment";


@Inject
HomePresenterImpl mHomePresenter;

public static HomeFragment newInstance() {
    return new HomeFragment();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FwApplication.component(getActivity()).inject(this);
}

然后我以这种方式修改了presenter构造函数:

@Inject
public HomePresenterImpl(NetworkService networkService) {
    mNetworkService = networkService;
    mInteractor = new InteractorImpl(mNetworkService);
}

然后自动注入NetworkService。

我想知道它是否以这种方式是正确的,因为我必须调用我所拥有的每个片段,它需要一个以与上面代码相​​同的方式构造的演示者:

FwApplication.component(getActivity()).inject(this);

2 个答案:

答案 0 :(得分:13)

你混合了什么。要提供演示者,您应该切换到以下内容:

尽可能使用构造函数注入。它会让事情变得更容易

public class HomePresenterImpl {

    @Inject
    public HomePresenterImpl(NetworkService networkService) {
        // ...
    }

}

要提供接口,请在实现中使用此构造函数注入和 depend

Presenter<FwView> provideHomePresenter(HomePresenterImpl homePresenter) {
    return homePresenter;
}

这样您就不必亲自调用任何构造函数。实际上注入演示者......

public class MyFragment extends Fragment {

    @Inject
    Presenter<FwView> mHomePresenter;

    public void onCreate(Bundle xxx) {
        // simplified. Add your modules / Singleton component
        PresenterComponent component = DaggerPresenterComponent.create().inject(this);
    }
}

这样你就会注入东西。请仔细阅读并尝试理解。这将解决您的主要问题,您仍然无法从同一模块(在相同范围内)提供相同类型的2个演示者

// DON'T
@Provides
Presenter<FwView> provideHomePresenter(NetworkService networkService) { /**/ }

@Provides
Presenter<FwView> provideSearchPresenter(NetworkService networkService) { /**/ }

不会工作。您不能提供2个相同类型的对象。它们难以区分。如果您确定这是您想要的方式,请查看@Qualifiers,如@Named

答案 1 :(得分:5)

如果在构造函数中使用@Inject批注,则不必提供Presenter。在类的构造函数中使用的@Inject批注使该类成为依赖关系图的一部分。因此,它也可以在需要时注入。

另一方面,如果将@Inject注释添加到字段而不是构造函数,则必须提供该类。