The Unity Application Block does not automatically read the configuration information or create and prepare containers. To provide maximum flexibility, you use code in your application to instantiate a Unity container and then populate it with the registrations, type mappings, and extensions defined in the configuration file. This allows you to create a nested container hierarchy from the configuration information and to manage the lifetime of each container.
If the configuration defines only a single unnamed container or specifies the default container, the following code will instantiate a new Unity container and then load it with the registrations, type mappings, and extensions defined in the configuration file for that container.
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section
= (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);
Dim container As IUnityContainer = New UnityContainer()
Dim section As UnityConfigurationSection _
= CType(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)
section.Containers.Default.Configure(container)
To load the configuration information for a specific named container, you use the container name defined in the configuration instead of referencing the default container. For example, if you have a container defined in the configuration with the name containerOne, you can instantiate and load it using the following code.
IUnityContainer container = new UnityContainer();
UnityConfigurationSection section
= (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers["containerOne"].Configure(container);
Dim container As IUnityContainer = New UnityContainer()
Dim section As UnityConfigurationSection _
= CType(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)
section.Containers("containerOne").Configure(container)
This means that you can dispose containers when required by managing the variables that hold references to your containers.
To create a hierarchy of nested containers from configuration information, you simply create the containers in the required hierarchy using the CreateChildContainer method and then load each one with the appropriate container information. The following code shows an example of instantiating and loading two containers from a configuration file that contains registrations, type mappings, and extensions for two containers named containerOne and nestedChildContainer.
IUnityContainer parentContainer = new UnityContainer();
IUnityContainer childContainer = parentContainer.CreateChildContainer();
UnityConfigurationSection section
= (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers["containerOne"].GetConfigCommand().Configure(parentContainer);
section.Containers["nestedChildContainer"].Configure(childContainer);
Dim parentContainer As IUnityContainer = New UnityContainer()
Dim childContainer As IUnityContainer = parentContainer.CreateChildContainer()
Dim section As UnityConfigurationSection _
= CType(ConfigurationManager.GetSection("unity"), UnityConfigurationSection)
section.Containers("containerOne").GetConfigCommand().Configure(parentContainer)
section.Containers("nestedChildContainer").Configure(childContainer)
Note: |
| You cannot nest containers in the configuration file. All <container> elements reside at the same "level" within 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. If required, you can load more than one container from the same <container> element. |