Unity Application Block Basics
This post will help you to start working with the Unity
application block. I’m going to show some basic examples of how
to use Unity API. You can read the previous post in this
subject in order to get an introduction to Unity.
Setting Up a Unity Container
In order to start working with Unity the first thing we have to do
is to set up a Unity container. The Unity container is the basic
object of Unity. The container is populated with the registered mappings
and help us to resolve registered mappings.
There are three ways to set up a container:
- By registering types in the container using the container methods.
- By XML format configuration file which use the unity schema.
- By using the container configuration API to configure the container.
The Unity container has two main methods to register types:
- RegisterType – The type is registered in the container and the
container is responsible to initialize it when appropriate.
- RegisterInstance – An instance of a type is registered in the container
with a lifetime which you can include. During the registered lifetime
the container will return the instance when needed.
After the registration of the types you will be able to use the Unity
container’s Resolve method in order to get the relevant type.
The following example shows how to register a mapping
between an interface of ILogger to a FileLogger concrete class:
In order to register a mapping from an ILogger interface to more then
one concrete class you can use a name for the concrete like in this
In some cases you already have an existing object and you want to
use it. The Unity container enables us to register an instance with the
RegisterInstance method of the container.
When registering an instance, the default container behavior will
be to return the same instance every time it is needed when the Resolve
method is called. The object will act as a singleton.
In the example I pass the FileLogger instance to the RegisterInstance
method. You can override the singleton behavior by passing the
ExternallyControlledLifetimeManager class as a second parameter
in the RegisterInstance.
Resolve an Object
After registering the types in the Unity container you’ll want to use
them. How do you do it? The answer is the Resolve method of
the container. The following example is the first example that I
showed including the use of the Resolve method:
As you can see the Resolve method will return an ILogger
which as expected will be of type FileLogger under the hood.
The Unity container will instantiate the FileLogger and will
return a new instance of that class as an ILogger interface.
If you registered more then one concrete class you can use the
Resolve method overload that gets a name of the mapping to return
the relevant mapped class or use the ResolveAll method to get
an IEnumerable collection of the registered types:
To sum up the post, I showed the basics of using the Unity
application block. I showed how to set up a Unity container
using the Unity API and how to resolve the Unity mappings from
the container. These are very basic operations. In the next posts in this
subject I’ll explain other aspects of Unity like using Unity with