Inversion of Control Using Unity

This article assumes a basic understanding of the de-coupled architecture and various types of Dipendency Injection patterns like:

  • Setter injection
  • Constructor injection
  • Function injection

In this article we will implement all of them with an example using Unity Framework. Now, let us discuss what Unity is and what its advantages are.

Unity is an IoC container released by Microsoft and is very simple, flexible and easy to use. Though there are many IoC containers available in the market much stronger than Unity, for simplicity and to understand the basic concepts, it's one of the best choices.

So, the Unity framework is a lightweight and extensible dependency container. It facilitates building loosely-coupled applications and provides developers with the following advantages.

  • Simplified object creation, especially for hierarchical object structures and dependencies
  • Abstraction of requirements; this allows developers to specify dependencies at run time or in configuration and simplify

And one more advantage of an IoC container is that, once we implement it we can plug it into any application so it is pluggable and does not need configuration again and again.

In this example we are implementing an example within a console application, so I will expect you to create one console application an add the following package from the NuGet Package Manager.



Once the package has been successfully installed in the application, you will find the following references.



Our application is now ready to use Unity and we will implement Dependency Injection using it. As we said, there are three types of the DI pattern and we will implement them one by one, all of them.

Dependency injection by Property injection

Here we will inject dependencies using properties. Let's understand the following implementation. We have one Employee class and one Company class and the Employee class is dependent on the Company class. When we try to print the salary of an employee by calling the DisplaySalary() function, internally it will call the ShowSalary() function of the Company class.

Since this is property injection, we need to use the [Dependency] attribute over the property.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Net.Http;  
  7. using System.Net;  
  8. using System.Net.Http.Headers;  
  9. using Microsoft.Practices.Unity;  
  10.   
  11. namespace Client  
  12. {  
  13.     public interface IEmployee  
  14.     {  
  15.     }  
  16.     public class Employee : IEmployee  
  17.     {  
  18.         private ICompany _Company;  
  19.         [Dependency]  
  20.         public ICompany Company  
  21.         {  
  22.             get { return _Company; }  
  23.             set { _Company = value; }  
  24.         }  
  25.         public void DisplaySalary()  
  26.         {  
  27.             _Company.ShowSalary();  
  28.         }  
  29.     }  
  30.   
  31.     public interface ICompany  
  32.     {  
  33.         void ShowSalary();  
  34.     }  
  35.     public class Company : ICompany  
  36.     {  
  37.         public void ShowSalary()  
  38.         {  
  39.             Console.WriteLine("Your salary is 40 K");  
  40.         }  
  41.     }  
  42.   
  43.     class Program  
  44.     {  
  45.         static void Main(string[] args)  
  46.         {  
  47.             IUnityContainer unitycontainer = new UnityContainer();  
  48.             unitycontainer.RegisterType<ICompany, Company>();  
  49.   
  50.             Employee emp = unitycontainer.Resolve<Employee>();  
  51.             emp.DisplaySalary();  
  52.   
  53.   
  54.             Console.ReadLine();  
  55.         }  
  56.     }  
  57. }  
Now, let's try to understand the Main() function, first of all we are creating an object of IUnityContainer with the following line:

  1. IUnityContainer unitycontainer = new UnityContainer();  
Then we will register the class on which the main class is dependent. In our example, we said that the Employee class is dependent on the Company class. So we need to add the Company class to the container.
  1. unitycontainer.RegisterType<ICompany, Company>();  
Fine, now the container is ready for use to solve dependency of another class. We will now create one object of the Employee class and solve it's dependency using IoC container as in the following:
  1. Employee emp = unitycontainer.Resolve<Employee>();  
And magically it will solve the dependency. Now we can call the DisplaySalary() function of the Employee class that will call the ShowSalary() of the Company class.

Now, let's see that we did not create an object of the Company class, we have just added the Company class to the dependency container and the container has taken care of everything else. Here is the output of the above example.



Dependency injection using constructor injection

This is another pattern or way to solve dependency, in this technique we will inject a dependency using a constructor. In the case of constructor injection we need to add an [InjectionConstructor] attribute over the constructor and that's all to make the preceding example work.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Net.Http;  
  7. using System.Net;  
  8. using System.Net.Http.Headers;  
  9. using Microsoft.Practices.Unity;  
  10.   
  11. namespace Client  
  12. {  
  13.     public interface IEmployee  
  14.     {  
  15.     }  
  16.     public class Employee : IEmployee  
  17.     {  
  18.         private ICompany _Company;  
  19.   
  20.         [InjectionConstructor]  
  21.         public Employee(ICompany tmpCompany)   
  22.         {  
  23.             _Company = tmpCompany;  
  24.         }  
  25.   
  26.         public void DisplaySalary()  
  27.         {  
  28.             _Company.ShowSalary();  
  29.         }  
  30.     }  
  31.   
  32.     public interface ICompany  
  33.     {  
  34.         void ShowSalary();  
  35.     }  
  36.     public class Company : ICompany  
  37.     {  
  38.         public void ShowSalary()  
  39.         {  
  40.             Console.WriteLine("Your salary is 40 K");  
  41.         }  
  42.     }  
  43.   
  44.     class Program  
  45.     {  
  46.         static void Main(string[] args)  
  47.         {  
  48.             IUnityContainer unitycontainer = new UnityContainer();  
  49.             unitycontainer.RegisterType<ICompany, Company>();  
  50.   
  51.             Employee emp = unitycontainer.Resolve<Employee>();  
  52.             emp.DisplaySalary();  
  53.             Console.ReadLine();  
  54.         }  
  55.     }  
  56. }  
The remaining portion is the same as the previous example and if everything is fine then we will get the same output.

Dependency injection using method injection

The is another way to implement dependency injection. As the name suggests we will inject a dependent object using a function. Have a look at the following example.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Net.Http;  
  7. using System.Net;  
  8. using System.Net.Http.Headers;  
  9. using Microsoft.Practices.Unity;  
  10.   
  11. namespace Client  
  12. {  
  13.     public interface IEmployee  
  14.     {  
  15.     }  
  16.     public class Employee : IEmployee  
  17.     {  
  18.         private ICompany _Company;  
  19.   
  20.         [InjectionMethod]  
  21.         public void SetObject(ICompany tmpCompany)  
  22.         {  
  23.             _Company = tmpCompany;  
  24.         }  
  25.   
  26.         public void DisplaySalary()  
  27.         {  
  28.             _Company.ShowSalary();  
  29.         }  
  30.     }  
  31.   
  32.     public interface ICompany  
  33.     {  
  34.         void ShowSalary();  
  35.     }  
  36.     public class Company : ICompany  
  37.     {  
  38.         public void ShowSalary()  
  39.         {  
  40.             Console.WriteLine("Your salary is 40 K");  
  41.         }  
  42.     }  
  43.   
  44.     class Program  
  45.     {  
  46.         static void Main(string[] args)  
  47.         {  
  48.             IUnityContainer unitycontainer = new UnityContainer();  
  49.             unitycontainer.RegisterType<ICompany, Company>();  
  50.   
  51.             Employee emp = unitycontainer.Resolve<Employee>();  
  52.             emp.DisplaySalary();  
  53.   
  54.   
  55.             Console.ReadLine();  
  56.         }  
  57.     }  
  58. }  
The example is very similar to the previous two, we have just have changed a little in the Employee class. We are now setting a dependent object using a function and for that we need to add an InjectionMethod attribute over the method. The output is just like the output of the first example.

Conclusion

In this example we have learned how to implement an IoC container using Unity that is very simple and flexible to use. I hope you have understood the basic concepts.

 


Similar Articles