Working With Configuration Files In Unity
Working With Configuration Files In Unity
In the previous posts about Unity I
discussed basic concepts and
showed how to use Unity
in ASP.NET web forms.
In this post I’m going to
show how to use Unity with configuration files.
The Unity Configuration Section
In order to start working with configuration files with Unity the first thing
to understand is the configuration section. Lets start with an example of
a Unity configuration section and then discuss the things that can be configure:
<?xml version="1.0" encoding="utf-8" ?>
<!-- Lifetime manager types should be inserted
if you need lifetime managers -->
<!-- User defined type aliases -->
<!-- An interface for logger implementation -->
<!-- An abstarct class for database classes -->
<!-- Will be configured as singleton by the lifetime
manager above -->
<lifetime type="singleton" />
<param name="connString" parameterType="System.String">
<value value="connection value..."/>
<param name="logger" parameterType="ILogger">
The first thing to notice is the typeAliases element that holds all the aliases
that you want to give to the type you are going to use with the Unity container.
It can reduce the code in the configuration section of the container itself instead
of using the long type definition. In the example above there are four typeAliases:
singleton, external, ILogger and Database. You can see the use of the aliases in
the container section itself.
The containers section is holding all the containers information that you need to
configure. You can have more than one type of container. The way to distinguish
between the containers is the name attribute that can be assigned to every container.
In the container you can define the types that will be injected in the the dependency
injection process. Every type is mapped to another type by the mapTo attribute.
The mapTo attribute will inform the container that the registered type will map to the
type that was written in the attribute. Another thing that I show in the example is the
lifetime element of the type element. The lifetime element will instruct the
Unity container how the registered element will be constructed. In the example above
the FileLogger class will be constructed as a singleton.
In the type element we can register a typeConfig element that can help us
to configure the injection to the constructor, method or properties of that type.
In my example I inject a connection string and a logger object to the
CustomDatabase object. The dependency element in the constructor parameter of
the logger instruct the Unity container that the element itself need to be injected with
the registered type of ILogger.
There are three more elements that can be configured inside the container element
which are instances, extensions and extensionConfig. I’m not going to discuss them
but you can read about them in the Unity documentation.
The Classes I Used in The Example
The abstract Database class:
public abstract class Database
The CustomDatabase class:
public class CustomDatabase : Database
private ILogger _logger;
private string _strConnString;
/// Construct a new CustomDatabase object
public CustomDatabase(string connString, ILogger logger)
_logger = logger;
_strConnString = connString;
The ILogger interface:
public interface ILogger
void Log(string message);
The FileLogger class:
public class FileLogger : ILogger
#region ILogger Members
public void Log(string message)
How To Plug The Configuration File To Unity
After a lot of configuration discussion you probably want to see some
real code of how to use the configuration section. The following code
will show you how to do it.
IUnityContainer container = new UnityContainer();
var section =
var database = container.Resolve<Database>();
var logger = container.Resolve<ILogger>();
First build a new Unity container. Then get the configuration section of
Unity. The last thing to do is to use the Configure method of the relevant
container (in my example the default container) on the container object itself.
After the three steps you can use the container to inject the dependencies
to the types that were written in the configuration file by using the Resolve
In today post I showed how to use configuration files in order to configure a
Unity container. I showed the main elements in the configuration file and also
showed a code of how to use it in an application. One last thing, the Unity
application block was released as stand alone dll and also with enterprise library.
I would expected from the patterns & practices team to provide a designer to Unity
like in all the other application blocks. Maybe it will be in the next enterprise library