Software Architecture C-Sharp Unity-2.0 Second Lession

Related Post: Software Architecture C# Unity-2.0 First Lession

So in order to explore Unity more,Consider a small logging sample with a Interface as:

 public interface ILogger
        void Write(string message);

First I introduce, a dummy implementation of ILogger named WinTraccerLogger as:

class WinTraccerLogger : ILogger
        public WinTraccerLogger()
        { Console.WriteLine("WinTraccerLogger:Constructors.............\n"); }
        public void Write(string message)
    Console.WriteLine("WinTraccerLogger's Write method :" + string.Format("{0}: {1}",

Secondly, I introduced, a dummy implementation of ILogger named EmailLogger as:

 class EmailLogger : ILogger
        public EmailLogger()
        { Console.WriteLine("EmailLogger:Constructors.............\n"); }
        public void Write(string message)
      Console.WriteLine("EmailLogger's Write method: " + string.Format("{0}: {1} \n",

Consider the Test case sample below:

// create a container
UnityContainer container2 = new UnityContainer();
// Step 1: registration name examples
Console.WriteLine("Step 1: Registration name examples");                     
container2.RegisterType<ILogger, EmailLogger>("Email");
container2.RegisterType<ILogger, WinTraccerLogger>("WinTraccer");
ILogger logger2 = container2.Resolve<ILogger>("Email");
logger2.Write("Hello World....!!!");
ILogger logger22 = container2.Resolve<ILogger>("WinTraccer");                    
logger22.Write("Hello World....!!!");
//Step 2: Resolve all types
Console.WriteLine("Step 2: Resolvge all types");
IEnumerable<ILogger> lstLogger = container2.ResolveAll<ILogger>();                    
foreach (var item in lstLogger)
item.Write("Hello World....!!!");

Outcome of Test case is as:

In step 1,of above sample shows, Unity provides a method named Resolve that you can use to resolve an object by type by providing a registration name. So 2 types are registered wih name “Email” for EmailLogger and “WinTraccer” for WinTraccerLogger.Corresponding log write method is called

In Step 2, of above sample, Unity provides a ResolveAll method which return all the Types, then there is a loop to call write mehtod of all the resolved types.

Download complete source code here.
[After downloading the file, rename “unitytestapp-zip.doc” to “” and extract the files]


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: