ResolveAll and container hierarchies

Jan 22, 2009 at 12:14 AM
Does anyone know if using ResolveAll on a child container results in resolving all types in all the parent and child containers?  It looks like that is the case for me...  I created a parent container that has every child container registered as a instance in the parent container.  So if a container, Parent has 3 child containers named, Child1, Child2, and Child3.  The following is executed:

Parent.RegisterInstance<IUnityContainer>("Child1", childContainer, new ContainerControlledLifetimeManager());
Parent.RegisterInstance<IUnityContainer>("Child2", childContainer, new ContainerControlledLifetimeManager());
Parent.RegisterInstance<IUnityContainer>("Child3", childContainer, new ContainerControlledLifetimeManager());

Then I have a method that returns the container instance based on a container name.  The problem is the call to ResolveAll is adding more container instances to the queue when say Child1 has no IUnityContainer children. 


        private IUnityContainer FindContainer(string containerName)
        {          
                // Search the container hierarchy and add
                // the found container to the dictionary

                Queue<IUnityContainer> containers = new Queue<IUnityContainer>();
                IUnityContainer container = null;
                containers.Enqueue(this.Parent);

                while (containers.Count > 0)
                {
                    IUnityContainer next = containers.Dequeue();
                    
                    try
                    {
                      // Check if this parent container has the specified container name
                      container = next.Resolve<IUnityContainer>(containerName);

                      return container;
                    }
                    catch(ResolutionFailedException ex)
                    {
                        // do nothing
                    }
                    finally
                    {
                        // checked if the container was found
                        if (container == null)
                        {
                            // the container was not found, check its children
                            // Add all the children to the queue and
                            // search the children containers
                            foreach (IUnityContainer child in next.ResolveAll<IUnityContainer>())
                            {
                                containers.Enqueue(child);

                            }  // end foreach
                        }
                    }

                }  // end while
Jan 22, 2009 at 4:49 PM
Nevermind.  I didn't know that Unity resolves types in the Parent if it is not found in the Child.  It looks like that's the reason. 
Jan 22, 2009 at 6:25 PM
Yes, that's the whole point of child containers.