【问题标题】:Managing dependencies in big multi library module project with Dagger 2使用 Dagger 2 管理大型多库模块项目中的依赖项
【发布时间】:2019-08-28 03:35:13
【问题描述】:

我正在尝试将 Dagger 2 DI 实现到我们的多库模块项目(30 个模块并且它会增长),每个库模块都依赖于 build.gradle 文件中的几个其他模块,例如:gradle实施项目(“:核心”)

我们有主模块:app,它包含应用程序类并包含每个库模块。

所以我决定在 :app 模块中将有一个单例组件,它将包括下面的所有模块。

@Component(modules = [
    AppModule::class,
    AndroidSupportInjectionModule::class,
    ViewModelFactoryModule::class,
    BusinessModule::class,
    BusinessRepositoryModule::class,
    BusinessDetailFragmentModule::class,
    BusinessCollectionVerticalFragmentModule::class
])
@Singleton
interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }

    fun inject(mainApplication: MainApplication)
}

我之所以有4个业务模块是因为

  • BusinessModule 是一个抽象类,它包含该库模块中的所有活动和视图模型。

  • BusinessRepositoryModule 提供存储库实例(稍后将替代模拟存储库)。

  • 剩下的两个模块是片段,它们包含在BusinessModule中的活动中。像这样: @ContributesAndroidInjector(modules = [BusinessCollectionVerticalFragmentModule::class])

问题是,我只介绍了两个库模块,而且已经很乱了。 管理这种规模项目的模块的正确方法是什么?

【问题讨论】:

    标签: android dependency-injection dagger-2 dagger


    【解决方案1】:

    您在应用程序级别调用所有组件。在应用程序级别使用所有组件效率低下。制作单独的组件并使用@scope 表示法,它将组件的生命周期绑定到活动。

    【讨论】:

      猜你喜欢
      • 2011-10-14
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      相关资源
      最近更新 更多