Unity 2 – Configuration file and Unity (Part 2)


I’m real fan of Unity, I already worked with other Ioc, but Unity remains my favorite.

In my previous post, I explained how to configure Unity at runtime. This one will describe how to use the configuration file.

First of all add the following references to your project:

  • System.Configuration.dll
  • Microsoft.Practices.Unity.Configuration.dll

Simple scenario

Add a using directive to get easy access to the container initializer:

Code Snippet
  1. using Microsoft.Practices.Unity.Configuration;

Update the Program class as follows:

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         IUnityContainer container;
  6.         container = new UnityContainer();
  7.  
  8.         // Initialize the container with the config file
  9.         container.LoadConfiguration();
  10.         // Resolve IMyComponent
  11.         IMyComponent obj = container.Resolve<IMyComponent>();
  12.  
  13.         Console.WriteLine(obj.Run(5));
  14.  
  15.         Console.ReadKey();
  16.     }
  17. }

Add a configuration file (which is a simple XML file):

Code Snippet
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.     <configSections>
  4.         <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  5.     </configSections>
  6.     <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  7.         <container>
  8.             <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponent, UnityTest"  />
  9.         </container>
  10.     </unity>
  11.  
  12. </configuration>

Run the example and the result is the same as in my previous post.

Singleton scenario

To use the injected object as a singleton, just define a ControledContainerLifetimeManager, which manages the lifetime of your object as a unique instance:

Code Snippet
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.     <configSections>
  4.         <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  5.     </configSections>
  6.     <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  7.         <container>
  8.             <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponent, UnityTest"  >
  9.                 <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
  10.             </register>
  11.         </container>
  12.     </unity>
  13.  
  14. </configuration>

Alias scenario

For the alias scenario, remove the previous configuration file, and change the code as follows :

Code Snippet
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         IUnityContainer container;
  6.         const string FRENCH = "fr";
  7.         const string DEFAULT = "us";
  8.         // Use the runtime configuration
  9.         container = new UnityContainer();
  10.  
  11.         // Initialize the container with the config file
  12.         container.LoadConfiguration();
  13.  
  14.         // Resolve IMyComponent
  15.         IMyComponent obj = container.Resolve<IMyComponent>(DEFAULT);
  16.         IMyComponent objFr = container.Resolve<IMyComponent>(FRENCH);
  17.  
  18.         Console.WriteLine(obj.Run(5));
  19.         Console.WriteLine(objFr.Run(5));
  20.  
  21.         Console.ReadKey();
  22.     }
  23. }

Then use the following configuration file instead, which defines the aliases with the name attribute:

Code Snippet
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.     <configSections>
  4.         <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  5.     </configSections>
  6.     <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  7.         <container>
  8.             <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponent, UnityTest" name="us" >
  9.                 <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
  10.             </register>
  11.             <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponentFr, UnityTest" name="fr" >
  12.                 <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
  13.             </register>
  14.         </container>
  15.     </unity>
  16.  
  17. </configuration>

Comments (2)
  1. Anonymous says:

    Thanks for this, I was having problems finding a clear example.

  2. Anonymous says:

    I am getting this error somehow. Binding file and references look ok.

    System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize —> System.Configuration.ConfigurationErrorsException: Unrecognized configuration section unity.

    Any idea on this?

Comments are closed.

Skip to main content