android – Dagger2:组件不能依赖于多个范围的组件

是的,我知道以前曾经问过,是的,我知道这是“按设计”.

但是我想做这样的事情:

@Component(modules = {RealmModule.class})
public interface RealmComponent {
    Realm realm();
}


@Component(modules = {RepositoryModule.class})
public interface RepositoryComponent {
    PersonRepository personRepository();

    ScheduleRepository schedulesRepository();
}

@Component(dependencies = {RealmComponent.class,RepositoryComponent.class})
public interface AppDataComponent
        extends RealmComponent,RepositoryComponent {
}

@ApplicationScope
@Component(dependencies = {AppContextComponent.class,AppDataComponent.class,AppDomainComponent.class,AppPresentationComponent.class,AppUtilsComponent.class})
public interface ApplicationComponent
        extends AppContextComponent,AppDataComponent,AppDomainComponent,AppUtilsComponent,AppPresentationComponent {
    void inject(CustomApplication customApplication);

    void inject(DashboardActivity dashboardActivity);
}

但是,我得到的是无范围,每次我注入JobManager或ScheduleRepository或其他任何东西,我得到一个新的实例.唯一可以“修复”的方法就是这个.

@Module
public class JobManagerModule {
    private JobManager jobManager;

    @Provides
    public JobManager jobManager(Context context) {
        if(jobManager == null) {
            jobManager = new JobManager(context,new Configuration.Builder(context).networkUtil(
                    new WifiOrMobileNetworkUtil(context)).build());
        }
        return jobManager;
    }
}

不是粉丝.

那么,如何构建和拆分依赖树,而不是制作一个巨大的巨型blob组件,其中列出了每个单独的模块和每个单独的提供方法(而不是这些“子组件”组件依赖性)?

我尝试使用子组件,但是你必须为最终的ApplicationComponent提供每个模块.

我不知道该怎么做.我尝试为每个第一级组件指定@Singleton,为每个AppDataLevelComponent指定@SubcomponentScope,我也尝试为每个子组件创建一个新范围,但它们都失败了“不能依赖于多个范围的组件”.

编辑:显然,为了获得作用域提供程序,使用作用域标记组件是不够的 – 您还必须指定@Provides注释方法的作用域.

@Module
public class RepositoryModule {
    @Provides
    @Singleton
    public PersonRepository personRepository() {
        return new PersonRepositoryImpl();
    }

    @Provides
    @Singleton
    public ScheduleRepository schedulesRepository() {
        return new SchedulesRepositoryImpl();
    }
}

与此同时,我最终得到了这个超级组件.

@Singleton
@Component(modules = {
        AppContextModule.class,DbMapperModule.class,DbTaskModule.class,RealmModule.class,RepositoryModule.class,InteractorModule.class,ServiceModule.class,PresenterModule.class,XmlPersisterModule.class
})
public interface ApplicationComponent
        extends AppContextComponent,AppPresentationComponent {

其中xyzComponent类只是存储提供方法的接口…

(请注意this structure is an anti-pattern as described by Martin Fowler,and you should organize modules based on features / activities,并使用组件依赖关系将它们转换为子范围组件.组件依赖关系用于替代您的超级作用域组件,并“继承”依赖关系提供程序.)

解决方法

我有同样的问题,就像你之前没有,并使用相同的超级组件方法结束,除了我使用@Subcomponents组织事情,而不是在ubercomponent中列出所有模块(我称之为“顶部”或“应用程序”组件).

你可能会在这里看到一个例子:
How to migrate missing inject from module with complete = false from Dagger 1 to Dagger 2

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...