Giter VIP home page Giter VIP logo

Comments (3)

Bit-Rot avatar Bit-Rot commented on July 17, 2024

EDIT: My initial comment was simply wrong - I proposed using both the .WithKernel() method AND having the Facade class extend Kernel and bind to its interfaces. Not only is this redundant, but it looks like it can throw exceptions in more complicated scenarios than the sample provided.

That said, I've also spent a bit of time working with subcontainers and the two approaches of implementing Kernel lifecycle methods proposed in the documentation.

I have also found that using the WithKernel() method, as outlined in the documentation, is not functional.

It should be said that I am able to work around this by using the initial implementation approach the documentation outlines - having my Facade class extend Kernel, and then using BindInterfacesAndSelfTo<MyFacade>().FromSubcontainerResolve() in the main container installer. This is also undesirable for the reasons stated in the documentation (I'd rather not have my Facade class somewhat arbitrarily extend Kernel), but it at least works.

Sample code that does not work, below:

        public class GoodbyeHandler : IDisposable
        {
            public void Dispose()
            {
                Debug.Log("Goodbye World!");
            }
        }

        public class HelloHandler : IInitializable
        {
            public void Initialize()
            {
                Debug.Log("Hello world!");
            }
        }

        public class Greeter
        {
            public Greeter()
            {
                Debug.Log("Created Greeter!");
            }
        }

        public class TestInstaller : MonoInstaller
        {
            public override void InstallBindings()
            {
                Container.Bind<Greeter>()
                    .FromSubContainerResolve().ByMethod(InstallGreeter).WithKernel().AsSingle();
            }

            void InstallGreeter(DiContainer subContainer)
            {
                subContainer.Bind<Greeter>().AsSingle();

                subContainer.BindInterfacesTo<GoodbyeHandler>().AsSingle();
                subContainer.BindInterfacesTo<HelloHandler>().AsSingle();
            }
        }

from zenject.

sbergen avatar sbergen commented on July 17, 2024

I made a quick patch to register (late)disposables, (late)tickables and call Initialize when using WithKernel. Haven't done extensive testing on this, but at least a bunch of tests that depend on this functionality now work: SubContainerKernel.diff

EDIT: ok, more thorough testing shows issues with cyclic references arise when binding IDisposables in the parent container. I'll see if I can fix it...

from zenject.

sbergen avatar sbergen commented on July 17, 2024

I made a quick patch to register (late)disposables, (late)tickables and call Initialize when using WithKernel. Haven't done extensive testing on this, but at least a bunch of tests that depend on this functionality now work: SubContainerKernel.diff

EDIT: ok, more thorough testing shows issues with cyclic references arise when binding IDisposables in the parent container. I'll see if I can fix it...

I gave up on this for now. The issue I was facing is that if you have the following structure, you end up with circular references:

  • Parent container binds Foo : IDisposable, where Foo requires Bar
  • Bar is bound in subcontainer with kernel
  • When Foo is resolved, it will require Bar, triggering the subcontainer creation, which will trigger the KernelRegisterer creation, which in turn requires the parent containers DisposableManager, which in turn depends on Foo (as it is Disposable).

The only way I can think of working around this would be to have some kind of lazy initialization of the subcontainer kernel, which would happen only after resolving the parent container roots, but I couldn't figure out any way to do that.

from zenject.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.