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:
Add a using directive to get easy access to the container initializer:
Update the Program class as follows:
Add a configuration file (which is a simple XML file):
Run the example and the result is the same as in my previous post.
To use the injected object as a singleton, just define a ControledContainerLifetimeManager, which manages the lifetime of your object as a unique instance:
For the alias scenario, remove the previous configuration file, and change the code as follows :
Then use the following configuration file instead, which defines the aliases with the name attribute: