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.96k stars 172 forks source link

[Question] How IDisposable work? #563

Closed enpiech closed 1 year ago

enpiech commented 1 year ago

I have a parent LifetimeScope (AScope), that Register a component AComponent (which implement IDisposable) on scene. then resolve it in RegisterBuildCallback. After that. I create a child scope (BScope) via script AScope.CreateChild. Some class in BScope need AComponent. Then I dispose BScope. Then Dispose method of AComponent is invoked - which is not expected. Is there any way to not invoke AComponent Dispose when dispose BScope? Cause I still need AComponent alive Thanks

hadashiA commented 1 year ago

Then Dispose method of AComponent is invoked - which is not expected.

If AComponent 's Lifetime is a .Singleton, it should not have this behavior. (Or it's a bug.)

If AComponent's Lifetime is .Scoped, then BScope also creates and retains an instance of AComponent. (This is the main difference between Singleton and Scoped.)

It is probably this pattern that your AComponent is Dispose.

enpiech commented 1 year ago

Thanks for your response. Because I use RegisterComponentInHierarchy, AComponent's Lifetime is .Scoped.