0
我有幾個相互依賴的類,GamePresenter
和GameManager
。 我試圖注入它們使用構造函數注入,但我仍然無法弄清楚如何使它的工作,因爲我得到了依賴性循環錯誤。匕首2個模塊相互依賴
GameFragment
應注入GamePresenter
成片段,所以第一個就聊到了第二,而GameManager
將被注入到GamePresenter
。
GamePresenter
是:
@Inject
GamePresenter(GameRepository gameRepository, GameManager gameManager, GameContract.View view) {
DaggerGameManagerComponent.builder()
.gameManagerModule(new GameManagerModule(this))
.build()
.inject(this);
this.view = view;
this.gameRepository = gameRepository;
this.gameManager = gameManager;
}
GamePresenterModule
是:
@Module
public class GamePresenterModule {
private final GameContract.View view;
public GamePresenterModule(GameContract.View view) {
this.view = view;
}
@Provides
GameContract.View provideGameContractView() {
return view;
}
}
GamePresenterComponent
是:
@FragmentScoped
@Component(dependencies = RepositoryComponent.class, modules = GamePresenterModule.class)
public interface GamePresenterComponent {
void inject(GameFragment gameFragment);
}
GameManager
是:
@Inject
public GameManager(GamePresenter gamePresenter, Game game) {
this.gamePresenter = gamePresenter;
this.game = game;
}
GameManagerModule
是:
@Module
public class GameManagerModule {
private GamePresenter presenter;
public GameManagerModule(GamePresenter presenter) {
this.presenter = presenter;
}
@Provides
GameManager provideGameManager(Game game) {
return new GameManager(this.presenter, game);
}
@Provides
Game provideGame() {
return new Game();
}
}
GameManagerComponent
是:
@FragmentScoped
@Component(dependencies = GamePresenterComponent.class, modules = GameManagerModule.class)
public interface GameManagerComponent {
void inject(GamePresenter gamePresenter);
}
最後,GameFragment
是:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
DaggerGamePresenterComponent.builder()
.repositoryComponent(((GameApplication) getActivity().getApplication()).getRepositoryComponent())
.gamePresenterModule(new GamePresenterModule(this))
.build()
.inject(this);
}
非常感謝提前!
我該怎麼做?特別是「將其註冊到構造函數或提供者中」 – noloman