Updated: ContainerControlled instead of ExternallyControlled in the bullet list of lifetime managers. Thanks, Ross Smith!
I have been using it for years. Yes, I am aware that there are lots of other tools for the same purpose and with more or less functionality, but I actually like Unity. It has a good combination of out of the box features, performance and extensibility options. Some of its aspects, I believe, are not well know, so I set out to write a couple of posts on it, this being the first. This will not be an introduction to IoC and DI, I am assuming you already know that.
First things first: the best way to install Unity is by using Nuget:
It will install both Unity and the Common Service Locator, and all .NET versions from 2.0 are supported.
The Common Service Locator is an attempt to define a common interface for a IoC functionality; different IoC libraries implement adapters for the Common Service Locator, so that they can be used through the common interface, without tying the implementation to a particular one. Most IoC containers already implement one such adapter.
We create a Unity container like this:
And we set up the Common Service Locator from it:
An IoC container is no good without registrations. You can register entries on the configuration file or through code. An IoC container is particularly good for minimizing coupling, by having dependencies on interfaces and abstract base classes instead of actual implementations, so these registrations basically consist of an interface of abstract base class as the key and a concrete type or instance as the value. Let’s consider a simple interface, ILogger, and two concrete implementations, ConsoleLogger and FileLogger:
The implementation is not very important, but lets consider the following ILogger interface:
Registration By Configuration File
If you want to register by configuration, place the following content on the Web.config or App.config file:
Notice the two registrations, both for the interface ILogger, one mapping to ConsoleLogger, without a name, and another mapping to FileLogger, with a name of File. This is perfectly valid, there can be multiple declarations for the same interface (or abstract base class), provided they have different names. If no name is specified, it will default to the empty string (“”).
We have to tell Unity to load the configuration from the .config file, and we do it like this (do reference the Microsoft.Practices.Unity.Configuration namespace for the LoadConfiguration extension method):
Registration By Code
Alternatively, if you want to register entries by code, do so as:
With registration by code, you can do something that is not available with the configuration file approach: registering a concrete instance as the value. Here’s how:
This has the advantage that you can create an instance anyway you like, with any specific configuration, and make it available to Unity.
Pay attention to this: the Common Service Locator has no methods for adding entries to the registration, just for resolving them.
After you have some entries registered, you can ask for them, either by calling Unity directly (not recommended) or through the Common Service Locator (better):
Or, if you want to obtain a particular named registration:
For dynamic invocation, when you don’t have at compile time the desired Type, and thus no strong typing:
It is possible to get all registered values for a given type:
And, if there is the need, you can also resolve instances for a Type, that is, not using generics:
For the single instance methods, if no such registration exists (for the type or the name), an exception will be thrown. For the enumeration ones, an empty list will be returned.
By now, you might ask yourself: OK, but what if we ask Unity for some registered interface two or more times, will it give me the same instance or different instances? If we have registered an instance, by calling RegisterInstance instead of RegisterType, Unity will indeed return the same instance all the time. Otherwise, Unity will do what the Lifetime Manager tells it to do. For a good discussion, see Understanding Lifetime Managers.
A Lifetime Manager is an instance of a class that inherits from LifetimeManager and is responsible for creating or retrieving stored instances of mapped classes. Unity comes with the following out of the box implementations, but you are free to implement others:
ContainerControlledLifetimeManager (aka, singleton): Unity will only create an instance of the concrete type, and will return it every time, effectively treating it as a singleton;
PerThreadLifetimeManager (perthread): a different instance will be created once for each thread that the method it is called on, and the same instance will be returned on all subsequent calls for the same thread;
TransientLifetimeManager (transient): a new instance will be created and returned every time.
HierarchicalLifetimeManager (hierarchical): a new instance will be created and the same one will be returned for each Unity container, and another one for each child container (more on this later).
PerResolveLifetimeManager (perresolve): a new instance will be created every time, but it will be reused in the same build process if it is needed as a dependency somewhere in the list of dependencies of the current resolve process.
ExternallyControlledLifetimeManager (external): the actual instance was created and is managed elsewhere, like when you supply your own instance.
If you want to do it by configuration:
That’s it for now. In the next post: dependency injection.