This project has moved and is read-only. For the latest updates, please go here.

Child/Hierarchical Containers

Jan 13, 2010 at 8:57 PM

I think I am confused. I thought that if a parent container was added as an additional export provider, a child container would have access to elements in the parent container and things added explicitly to the child. Here's some code:

            var parentContainer = ((ExportingCompositionContainer)parent)._compositionContainer;
            _compositionContainer = new CompositionContainer(_aggregateCatalog, parentContainer);

The "parent" is a wrapper with a property for the compositoin container it holds. That's why teh first line looks funny.

So, if I look at the parentContainer.Parts, I see what I expect. If I look at the resulting child (_compositionContainer in this context) I see no parts in the catalog.

I was exploring this becuase ImportMany was not working as expected.

My question at the moment is pretty basic. In the above, should the child container include all the parts of the parent?

If so, then I'd love some help figurig out what went wrong.

If not, I'm not sure I understand the usage of hierarchical containers and could use some information on how to make them work.

Thank you,

Jan 14, 2010 at 3:58 AM
Edited Jan 14, 2010 at 4:02 AM

Hi Kathleen

You will not see the parts in the parent container appear in the child catalog as the child container has it's own independent catalog. Those parts will appear in the child container however if the container is queried or if imports are satisfied on a part. The container queries export providers to find stuff, when you pass in another container as an export provider it just adds it to it's internal lists of things to query.

For example:

public interface ILogger {}

public class Logger : ILogger {}

public class GetLoggerFromChild {
  public GetLoggerFromChild() {
    var parentCatalog = new TypeCatalog(typeof(ILogger));
    var parentContainer = new CompositionContainer(parentCatalog);
    var childCatalog = new AggregateCatalog(); //empty
    var childContainer = new CompositionContainer(childCatalog, parentContainer);
    var logger = childContainer.GetExportedValue<ILogger>();

 In the code above logger will be resolved in the parent container.  If you look at the child catalog however you won't see it.



Jan 14, 2010 at 1:55 PM

Thanks for jogging my brain on this.