-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolving a service registered on a parent container with a dependency registered in a child container throws #108
Comments
After sleeping on this, and then trying again, I also realized this works:
I tried to understand why by reading the documentation on lifetimes (a couple of times...) but I still don't get it. Why does this make it work? |
The dependency to IQux exists only in the child container, so if the singleton (belonging to the parent container) is requested by the child container it could satisfy the IQux dependency, but the Qux instance would be disposed when the child container ends. This would be very strange since a the singleton (in the parent container) suddenly would be disposed after it's dependency was disposed. In summary, a singleton must obtain it's dependencies only from siblings (in same container) or from parents; never from children. Edit: Yes, none of your classes are disposables, but above rule/principle is still valid. |
But why doesn't this apply to the Let's rename them, to make it more obvious - we have two cases: IIUC, the second case doesn't work because when the child container is disposed, so is the For the first case, I thought that disposing the child container would also dispose (Sorry for asking so many questions, but I'm a curious guy :) Since I know how to make my code work for now, finding out why is mostly for my own learning, and not so urgent as it was...) |
Your first scenario, where IBar is registered in the child container, does not work. Are you sure it passes for you? I copy-pasted your code into a unit test to be certain. An exception is thrown: Unable to resolve type: IBar. A container tracks all objects that should be disposed once the container is disposed. Since the singleton is created on-demand, the singleton must always be created as if the first Resolve call was made directly on the container containing the singleton registration (parent in your case). This is also why when using callback factories (like |
Given the service types defined at the bottom, the following code works, and prints
"A bar."
as expected:If, however, I add another service level (i.e. Foo->Baz->Qux instead of just Foo->Bar), resolving a
IFoo
throws an exception stating thatIQux
cannot be resolved:Note that
IQux
is registered on the same container from which I'm trying to resolve anIFoo
. If I set a breakpoint on that line and trychild.Resolve<IQux>()
orchild.Resolve(typeof(IQux))
in a watch window, it works fine. However, when resolving theBaz
instance needed to fulfillFoo
's dependencies, it seems it can no longer find theQux
registration.Am I doing something unsupported here, or is this a bug in TinyIoC?
The text was updated successfully, but these errors were encountered: