1
Container.Bind<ICompanion>()
    .To<RouMainMenuPresenterCompanion>()
    .FromNewComponentSibling()
    .WhenInjectedInto<MainMenuPresenter>();

Container.Bind<RouMainMenuPresenterCompanion>()
    .FromResolve();

I want the same instance of RouMainMenuPresenterCompanion to be injected in MainMenuPresenter as ICompanion (FromNewComponentSibling) and reuse this created instance in the future as RouMainMenuPresenterCompanion for any resolver

Example above leads to circular dependency. How can I solve my problem?

Alexandr
  • 3,859
  • 5
  • 32
  • 56

1 Answers1

1

I might not understand correctly but could you just change it to this?

Container.Bind(typeof(ICompanion), typeof(RouMainMenuPresenterCompanion))
    .To<RouMainMenuPresenterCompanion>()
    .FromNewComponentSibling()
    .WhenInjectedInto<MainMenuPresenter>();

Edit: This is probably more what you were looking for:

Container.Bind<RouMainMenuPresenterCompanion>()
    .FromNewComponentSibling()
    .WhenInjectedInto<MainMenuPresenter>();

Container.Bind<ICompanion>()
    .To<RouMainMenuPresenterCompanion>()
    .FromResolveGetter<MainMenuPresenter>(p => p.Companion)
Steve Vermeulen
  • 1,406
  • 1
  • 19
  • 25
  • But now only `MainMenuPresenter` can get this instance. I want everyone else to be able to access it. (there was mistake in my example, fixed now) – Alexandr May 30 '18 at 02:25
  • Ah ok I understand now. I think the best way to handle this is to use FromResolveGetter with MainMenuPresenter. I've editing my answer to show you what I mean – Steve Vermeulen Jun 02 '18 at 14:19
  • But now there is a problem with `MainMenuPresenter` :D `Container.Bind().FromNewComponentSibling();` Can I say like `FromNewComponentSiblingAndResue()`? – Alexandr Jun 02 '18 at 23:23
  • `Container.Bind().FromComponentSibling` means that no matter what class zenject is injecting Foo into, it will always look for Foo on the game object of the component it is injecting. So if you want to have a component added to a specific game object you probably shouldn't be using `FromComponentSibling` – Steve Vermeulen Jun 03 '18 at 07:16