Container Hierarchy in config file

Jun 30, 2008 at 11:21 PM
I want to:

a) have a container hierarchy and
b) use a Unity config file to register objects into the containers.

I am stuck on how to create the child containers in the config file. 

If someone can reply with an example of  Unity Config file xml to make a container hierarchy, it will be greatly appreciated.


Jul 1, 2008 at 8:55 AM
have you tried

    <container name="containerone">
    <container name="containertwo">
        <container name="containerthree">
            <container name="containerfour">
Jul 1, 2008 at 2:12 PM

After re-reading the documentation cover-to-cover last night, I see in the Entering Configuration Information section some paragraphs that I missed earlier.  A note says, "You cannot nest containers in the configuration file.  All <container> elements reside at the same "level" with the <containers> element.  You configure nested containers by creating the containers in the required hierarchy in your code and then populating them from the appropriate <container> elements." 

Also, <container> or <containers> are not a documented allowable child element of <container>

So the scenario I was attempted is not supported by the configuration schema or classes in unity at the moment.

It does seem reasonable that in a future release of Unity that a <container> element would support a <containers> child element that has <container> elements. 


Jul 2, 2008 at 6:43 AM
Here's the thing. A <container> element in config is not a Unity container. It's a named set of configurations that can be applied to a container. As such, it's up to you to supply the container, and then you can apply one or many configurations to it. You have to supply the containers yourself, and apply the appropriate configurations to them.

If you had, for example, three <container> elements, named CA, CB, and CC, you could do this:

IUnityContainer container = new UnityContainer();

UnityConfigurationSection configSection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");


and container would end up with the total set of configurations from all three sections.

For child containers, you could do something like this:

IUnityContainer parent = new UnityContainer();
IUnityContainer child1 = parent.CreateChildContainer();
IUnityContainer child2 = parent.CreateChildContainer();


I've found in my applications that the set of containers you end up using are fairly static, and not something I'd want to configure. What are you trying to do that you want to dynamically define containers at runtime? How will you know which one to use at what time?

Jul 3, 2008 at 3:00 PM
ctavares - thanks for the clarifications

In my application I have a tree structure hierarchy of objects and needed to construct the tree from the config file.  Initially, it seemed that since containers can be arranged in a tree, that a simple way to do this would be to use that inherent structure to create my object structure.  As with many initial ideas, it was misguided. I now have a tree node object with a parent property and set that parent property to a named object in Unity using the <property><dependency> element.  Users will persist the tree into a unity configuration file, for later hydration when they reopen the file.  This works very slick with a small amount of code.  The entire tree of objects is in one Unity container.