The Unity Application Block does not automatically read the configuration information or create and prepare containers. You must programmatically instantiate a Unity container in your application. You can programmatically configure it with registrations, type mappings, and any extensions or you can configure it by reading configuration information from a file.
If the configuration defines an unnamed container, that unnamed container is the default container. You can also declare named containers in addition to the default, unnamed container. By using Unity, you can load the information from each container configuration section into containers that you have already created as part of a container hierarchy.
Note: |
You can load the configuration information for the default container by specifying the default container as follows: section.Containers.Default .Configure(myContainer) |
The following example 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)
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, and you can use more than one container element in a configuration file. |