使用MVP / Dagger 2注入演示者

时间:2018-05-17 08:22:59

标签: android mvp dagger-2

我最近一直在学习DI和Dagger 2,我觉得我最终得到的结果比我开始时更多。我的设置包括

  • AppComponent - 提供ApplicationResources和其他与网络相关的内容。 @Singleton Scoped。
  • Feature1ComponentFeature2Component .... - 提供跨屏幕使用的存储库。 @Feature作用域AppComponent的子组件。Screen1Component。有'加'屏幕组件的方法。
  • Screen2ComponentFeatureXComponent ... - 这就是样板的感觉 - 每个屏幕组件只有一个'注入'用于描绘屏幕的片段/活动的方法。子组件@Module public class Screen1Module { private final Screen1Contract.View view; public Screen1Module(Screen1Contract.View view) { this.view = view; } @Provides public Screen1Contract.View provideView() { return view; } } 。每个相应的模块最终看起来像这样:

    ((MyApplication) getActivity().getApplication())
                    .getFeature1Component()
                    .newScreen1Component(new Screen1Module(this))
                    .inject(this);    
    

因此,对于每个屏幕,我最终都会编写一个组件和模块,因为我只能调用以下内容:

(Activity or Fragment)

当我初始化我的观点$( "a" ).on( "click", function() { $( this ).css( "box-shadow", "none" ); });时,为了只注入演示者。

有没有办法做到这一点,不涉及太多的样板?我希望能够在"功能"水平,但我想这并不可能,因为屏幕的生命周期比功能短。

1 个答案:

答案 0 :(得分:0)

这就是为什么dagger-android出生于匕首2.10之后。它简化了Android核心类型的注入。

@ContributesAndroidInjector替换子组件。 DaggerActivity和其他人摆脱getComponent()....inject(this)

its official doc

and a pretty good article I found recently

相关问题