unity-game-enginezenject

FromNewComponentSibling and then reuse


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?


Solution

  • 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)