Thursday, December 30, 2010

Unity Study Notes (1)

Download the unity dll's from http://unity.codeplex.com/

I. Without Registering Type/Instance


By the documents of Unity, we have:
"You can use the Unity container to generate instances of any object that has a public constructor (in other words, objects that you can create using the new operator), without registering a mapping for that type with the container. When you call the Resolve method and specify the default instance of a type that is not registered, the container simply calls the constructor for that type and returns the
result."
We write the following code to test:

class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();

MyService serviceInstance = container.Resolve< MyService>();
serviceInstance.Print("End of 2010.");
}

public class MyService
{
public void Print(string msg)
{
Console.WriteLine("Hello," + msg);
}
}
}

The Unity Application Block exposes two methods for registering types and mappings with the container:

II. Register Type


This method register a type with the container and at the appropriate time the container will build an instance of the type you specified.


Let's first Create an interface called IWriteService,

public interface IWriteService
{
void Write(string msg);
}

And we create 4 concrete classes



public class FirstService : IWriteService
{
public void Write(string msg)
{
Console.WriteLine("First Service: "+msg);
}
}
public class SecondService : IWriteService
{
public void Write(string msg)
{
Console.WriteLine("Second Service: " + msg);
}
}
public class ThirdService : IWriteService
{
public void Write(string msg)
{
Console.WriteLine("Third Service: " + msg);
}
}
public class FourthService : IWriteService
{
public void Write(string msg)
{
Console.WriteLine("Fourth Service: " + msg);
}
}

In the Main entry, we can register the Service with mapping.

IUnityContainer container = new UnityContainer();

//Do not specify the name, will be the default mapping
container.RegisterType< IWriteService, FirstService>();
//notice we use the RegisterType< TFrom, TTo >, this creats a mapping
//we can also use RegisterType< T>

//Specify the name
container.RegisterType< IWriteService, SecondService>("Second");

//We can also use register chain
container.RegisterType< IWriteService, ThirdService>("Third")
.RegisterType< IWriteService, FourthService>("Fourth");

So when resolve, we can specify the name or use the default mapping.

IWriteService serviceInstance1 = container.Resolve< IWriteService>();
serviceInstance1.Write("test");

IWriteService serviceInstance2 = container.Resolve< IWriteService>("Second");
serviceInstance2.Write("test");

IWriteService serviceInstance3 = container.Resolve< IWriteService>("Third");
serviceInstance3.Write("test");

We can also use ResolveAll method to get the IEnumerable< IWriteService> Collection:

IEnumerable< IWriteService> serviceInstances = container.ResolveAll< IWriteService>();
foreach (IWriteService service in serviceInstances)
{
service.Write("good");
}

The source code is at:

https://docs.google.com/uc?id=0BwzR-spEXHBFZjU1ZjUxMWMtNTg1ZS00MWRiLTg2M2MtY2MzZDBiNDUwMTAw&export=download&hl=en

III. Register Instance


We can also register instance to the container.

We write class the following:


public class FifthService : IWriteService
{
public void Write(string msg)
{
Console.WriteLine("Fifth Service: " + msg);
}
}

And in the Main entry, we write code to register and resolve instance like the following:


container.RegisterInstance< IWriteService>(new FifthService());
IWriteService service5 = container.Resolve< IWriteService>();
service5.Write("Register Instance");

No comments: