[Unity] Registration

Registering type maps (from and to) is the first step in DI patterns. Unity provides a couple of ways to register types.

 

1. Instance Registration

If you want to use an singleton instance as a dependence, you need to use the “UnityContainer.RegisterInstance()”

var container = new UnityContainer();
IAnimal animal = new Dog();
container.RegisterInstance(animal);

 

2. Simple Type Registration

It is the most commonly used type of registration. It maps 2 types(usually the abstract interface and the concrete class).
Whenever the interface type is requested, the instance of concrete type will be created and returned.

var container = new UnityContainer();
container.RegisterType<IAnimal, Dog>();

 

3. Constructors and Named Registrations

What if the constructor of the concrete class needs more parameters? You can provide the parameter values during registration using the “InjectionConstructor” class. Please look at the sample code.

interface ILogger
{
  void Log();
}
class Logger : ILogger
{
  public string Extension { get; set; }
  public Logger(string ext)
  {
    Extension = ext;
  }
  public void Log()
  {
    Console.WriteLine(string.Format("Log to the myLog.{0}", Extension));
  }
}

static void Main(string[] args)
{
  var container = new UnityContainer();
  container.RegisterType<ILogger, Logger>("Csv",
     new InjectionConstructor("csv"));
  container.RegisterType<ILogger, Logger>("Txt",
     new InjectionConstructor("txt"));

  container.Resolve("Csv").Log(); // Log to the myLog.csv
  container.Resolve("Txt").Log(); // Log to the myLog.txt
}

Note that how names are used to register types and resolve them. It is pretty easy to use.

 

4. ParameterOverride

There is a problem in the previous example. You need to provide the value during the registration. What if you do not know the value in compile time?

var container = new UnityContainer();

container.RegisterType<ILogger, Logger>
  (new InjectionConstructor(typeof(string)));

container.Resolve(new ParameterOverride("ext", "csv")).Log(); // Log to the myLog.csv
container.Resolve(new ParameterOverride("ext", "txt")).Log(); // Log to the myLog.txt
container.Resolve(new ParameterOverride("ext", "xls")).Log(); // Log to the myLog.xls

 

5. Viewing Registration Information

For debugging purposes, it is useful to know what the Unity container has.

Console.WriteLine("Container has {0} Registrations:",
  container.Registrations.Count());
foreach (ContainerRegistration item in container.Registrations)
{
  Console.WriteLine(
    String.Format("{0} - {1} : {2}",
      item.Name,
      item.RegisteredType,
      item.MappedToType));
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s