Skip to content

Instantly share code, notes, and snippets.

@monry
Created May 16, 2019 08:04
Show Gist options
  • Save monry/0e154b8abecdfeb80d4a6d210422b5b5 to your computer and use it in GitHub Desktop.
Save monry/0e154b8abecdfeb80d4a6d210422b5b5 to your computer and use it in GitHub Desktop.
Reproduce code that `IInitializable.Initialize()` has not been invoked even if use `WithKernel()` when bind `FromSubContainerResolve()`.
using UnityEngine;
using Zenject;
public class SampleInstaller : MonoInstaller<SampleInstaller>
{
public override void InstallBindings()
{
Container
.Bind(typeof(IMain))
.FromSubContainerResolve()
.ByMethod(
container =>
{
container.BindInterfacesTo<Main>().AsCached();
container.BindInterfacesTo<Sub>().AsCached();
}
)
.WithKernel()
.AsCached()
.NonLazy();
}
private interface IMain
{
}
private interface ISub
{
void Test();
}
private class Main : IMain
{
public Main(ISub sub)
{
Debug.Log("Main: new");
sub.Test();
}
}
private class Sub : IInitializable, ISub
{
public void Initialize()
{
Debug.Log("Sub: Initialize");
}
public void Test()
{
Debug.Log("Sub: Test");
}
}
}
@monry
Copy link
Author

monry commented May 16, 2019

Output log:

Main: new
Sub: Test

@monry
Copy link
Author

monry commented May 16, 2019

Expected:

Main: new
Sub: Test
Sub: Initialize

I think IInitializable.Initialize() be invoked after construct instance and resolve bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment