1个对象依赖于另一个,但都依赖于第三个对象

时间:2016-09-14 13:58:43

标签: android dependency-injection dagger-2

我有一个CreateNewGroupActivity,其中创建了2个不同的自定义LinearLayouts 我使用MVP mosby框架和dagger2 我的问题在于依赖注入 每个布局都依赖于 DBManager 类,但 PlaceDetailsPresenter 依赖于 TicketPresenter 类。

如何构建正确的依赖图,以便我只有一个TicketPresenter实例,即相同实例我可以注入 PlaceDetailsPresenter

以下是我现在所处的代码。
为2个演示者和@CreateNewGroupScope提供DBManager的@PerApp范围是CreateNewGroupActivity的范围。

@PerApp
@Component(modules = MyAppModule.class)
public interface MyAppComponent {
    void inject(MyApplication myApp);
    DBManager dbManager();
}

@Module
public class MyAppModule {
    private final MyApplication app;
    public MyAppModule(MyApplication app) {
        this.app = app;
    }
    @Provides @PerApp MyApplication provideMyApp() {
        return app;
    }
    @Provides
    Context provideContext() {
        return app;
    }
    @Provides
    @PerApp
    DBManager provideRealmDBManager() {
        return new DBManager();
    }
}

@Module
public class TicketModule {
    @Provides
    @CreateNewGroupScope
    public TicketPresenter provideTicketPresenter(DBManager dbManager) {
        return new TicketPresenter(dbManager);
    }
}

@CreateNewGroupScope
@Component(modules = TicketModule.class, dependencies = MyAppComponent.class)
public interface TicketComponent {
    void inject(TicketRelativeLayout ticketRelativeLayout);
    TicketPresenter presenter();
}


@Module
public class PlaceDetailsModule {
    @Provides 
    @CreateNewGroupScope
    public PlaceDetailsPresenter providePlaceDetailsPresenter(DBManager dbManager, TicketPresenter ticketPresenter) {
        return new PlaceDetailsPresenter(dbManager, ticketPresenter);
    }
}

@CreateNewGroupScope
@Component(modules = PlaceDetailsModule.class, dependencies = MyAppComponent.class)
public interface PlaceDetailsComponent extends TicketComponent{
    void inject(PlaceDetailsLayout placeDetailsLayout);
}

和JAVA类:

public class PlaceDetailsPresenter ...
   ...
   @Inject
    public PlaceDetailsPresenter(final DBManager dbManager, final TicketPresenter ticketPresenter) {
        this.dbManager = dbManager;
        this.ticketPresenter = ticketPresenter;
    }
...

public class PlaceDetailsLayout extends ...
    ...
    @Inject PlaceDetailsPresenter presenter;

    private void init() {
    PlaceDetailsComponent placeDetailsComponent = DaggerPlaceDetailsComponent.builder()
                .myAppComponent(MyApplication.getComponent(getContext()))
                .placeDetailsModule(new PlaceDetailsModule())
                .build();
        placeDetailsComponent.inject(this);
    }
}
public class TicketPresenter ...{
    @Inject
    public TicketPresenter(final DBManager dbManager) {
        Log.d(TAG, "ticketPresenter constructor");
        this.dbManager = dbManager;
    }
}
public class TicketRelativeLayout extends ...
    @Inject
    TicketPresenter presenter;  

    private void init() {   
        TicketComponent ticketComponent = DaggerTicketComponent.builder()
        .myAppComponent(MyApplication.getComponent(getContext()))
        .ticketModule(new TicketModule()).build();
    ticketComponent.inject(this);
    }
}

更新
基本上,此活动是关于在PlaceDetailsLayout中选定地点的TicketLayout中的特定日期选择正确的故障单类型。 首先,我从图标标签中选择 placetype ,这决定了我可以选择哪些地点,并且还会为此获取所有可用票据的地方。
然后在TicketLayout中选择日期,小时,门票 选择特定日期,填充可用小时数并选择首选小时列出您可以选择的所有可用机票类型
因此,如果您在TicketLayout中选择日期或小时,则其他布局上的无效
但是,如果您在PlaceDetailsLayout中更改/选择其他地方,则它将使用相关数据(日期,小时,故障单类型)完全更改基础 TicketLayout
现在,我有一个可观察的用于地点选择事件,该事件将选定的地名委托给ticketPresenter,以便使用属于此地点的数据填充TicketLayout / strong>即可。
我有一个用于地方的数据库表,另一个用于门票

以及来自未完全实施的活动的屏幕截图: http://imgur.com/a/4gelr

0 个答案:

没有答案