hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
1.88k stars 163 forks source link

Injected Tickable Instance don't run Tick method #624

Open amenonegames opened 6 months ago

amenonegames commented 6 months ago

Injected Tickable Instance don't run Tick method, when class is registerd as Lifetime.Transient,

Maybe because create new instance when EntryPointDispatcher runs, and register it as TickableLoopItem.

Then, only new instance's Tick medhod called in TickableLoopItem's MoveNext. Other instance injected the classes is not called.

Following is simple testcode. I expected display log "edited from Sample class". But acturally displayed "empty".

    public class SampleContainerBuilder : LifetimeScope
    {
        protected override void Configure(IContainerBuilder builder)
        {

            builder.RegisterEntryPoint<SampleTick>(Lifetime.Transient)
                .AsImplementedInterfaces()
                .AsSelf();

            builder.Register<Sample>(Lifetime.Singleton)
                .AsImplementedInterfaces()
                .AsSelf();
        }
    }

    public class Sample : IStartable
    {

        private SampleTick _sampleTick;

        [Inject]
        public Sample(SampleTick sampleTick)
        {
            _sampleTick = sampleTick;
        }

        public void Start()
        {
            _sampleTick.logText = "edited from Sample class";
        }
    }

    public class SampleTick : ITickable
    {

        public string logText = "empty";

        public void Tick()
        {
            Debug.Log(logText);
        }
    }

== my environment== Unity 2022.3.10f1 VContainer 1.13.2 and 1.14.0 (This occured both version) using SourceGeneratorDLL

LovorDev commented 6 months ago

Your class Sample must be registered by RegisterEntryPoint, cause it have IStartable interface

amenonegames commented 6 months ago

Oh, sorry for my miss registration. But I confirmed by debugger start medhod was called in that code.

And now I tried again with following containerbuilder. But the same thing happened...

    public class SampleContainerBuilder : LifetimeScope
    {
        protected override void Configure(IContainerBuilder builder)
        {

            builder.RegisterEntryPoint<SampleTick>(Lifetime.Transient)
                .AsImplementedInterfaces()
                .AsSelf();

            builder.RegisterEntryPoint<Sample>();
        }
    }