Comments (3)
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.
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 IDisposable
s in the parent container. I'll see if I can fix it...
from zenject.
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.diffEDIT: ok, more thorough testing shows issues with cyclic references arise when binding
IDisposable
s 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
, whereFoo
requiresBar
Bar
is bound in subcontainer with kernel- When
Foo
is resolved, it will requireBar
, triggering the subcontainer creation, which will trigger theKernelRegisterer
creation, which in turn requires the parent containersDisposableManager
, which in turn depends onFoo
(as it isDisposable
).
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)
- ZenjectIntegrationTestFixture.Setup destroys objects that prevent UnityTests from running.
- Link Checker Report
- [Doc Bug] Broken link in Readme.md for example of using multiscene setups
- Failed to remove SceneContext from SceneContextRegistry HOT 1
- Circular Dependencies
- Unused sample zenject scripts in build (IL2CPP and Zenject)
- NullReferenceException when second time inject HOT 1
- working with additive scenes? HOT 1
- i need to declare many signals. is there a solution for that? HOT 1
- [Zenject 6 Upgrade] Not possible to bind multiple interfaces to the same implementation when using WithId anymore?
- There is no DisposableManager.RemoveLate method for removing previously registrated objects.
- Prefab installer ignore bind FromInstance
- Passing dynamic data + prefab through method Create (Factory) while that prefab has GameObjectContext HOT 1
- The type or namespace name 'ConstructorChoiceStrategy' could not be found while building NonUnityBuild solution
- "Re-injecting" dependencies into a prefab when it's spawned from a MemoryPool? HOT 1
- Project Context: "Null Installer" assert hit when using Prefab Variant only on built version
- MemoryPool<Param, Value> with CustomFactory HOT 1
- When using Container.Instantiate to create a new object in the Create method of the factory, IMemoryPool is not assigned.
- [question] Is generic installers possible?
- Inquiry Regarding IDisposable in Zenject
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zenject.