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
- using Microsoft.Practices.Unity.Configuration;
Update the Program class as follows:
Code Snippet
- class Program
- {
- static void Main(string[] args)
- {
- IUnityContainer container;
- container = new UnityContainer();
- // Initialize the container with the config file
- container.LoadConfiguration();
- // Resolve IMyComponent
- IMyComponent obj = container.Resolve<IMyComponent>();
- Console.WriteLine(obj.Run(5));
- Console.ReadKey();
- }
- }
Add a configuration file (which is a simple XML file):
Code Snippet
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
- </configSections>
- <unity xmlns="https://schemas.microsoft.com/practices/2010/unity">
- <container>
- <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponent, UnityTest" />
- </container>
- </unity>
- </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
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
- </configSections>
- <unity xmlns="https://schemas.microsoft.com/practices/2010/unity">
- <container>
- <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponent, UnityTest" >
- <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
- </register>
- </container>
- </unity>
- </configuration>
Alias scenario
For the alias scenario, remove the previous configuration file, and change the code as follows :
Code Snippet
- class Program
- {
- static void Main(string[] args)
- {
- IUnityContainer container;
- const string FRENCH = "fr";
- const string DEFAULT = "us";
- // Use the runtime configuration
- container = new UnityContainer();
- // Initialize the container with the config file
- container.LoadConfiguration();
- // Resolve IMyComponent
- IMyComponent obj = container.Resolve<IMyComponent>(DEFAULT);
- IMyComponent objFr = container.Resolve<IMyComponent>(FRENCH);
- Console.WriteLine(obj.Run(5));
- Console.WriteLine(objFr.Run(5));
- Console.ReadKey();
- }
- }
Then use the following configuration file instead, which defines the aliases with the name attribute:
Code Snippet
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
- </configSections>
- <unity xmlns="https://schemas.microsoft.com/practices/2010/unity">
- <container>
- <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponent, UnityTest" name="us" >
- <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
- </register>
- <register type="UnityTest.IMyComponent, UnityTest" mapTo="UnityTest.MyComponentFr, UnityTest" name="fr" >
- <lifetime type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
- </register>
- </container>
- </unity>
- </configuration>
Comments
- Anonymous
September 12, 2011
Thanks for this, I was having problems finding a clear example. - Anonymous
November 14, 2012
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?