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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Http;
- using System.Net;
- using System.Net.Http.Headers;
- using Microsoft.Practices.Unity;
-
- namespace Client
- {
- public interface IEmployee
- {
- }
- public class Employee : IEmployee
- {
- private ICompany _Company;
- [Dependency]
- public ICompany Company
- {
- get { return _Company; }
- set { _Company = value; }
- }
- public void DisplaySalary()
- {
- _Company.ShowSalary();
- }
- }
-
- public interface ICompany
- {
- void ShowSalary();
- }
- public class Company : ICompany
- {
- public void ShowSalary()
- {
- Console.WriteLine("Your salary is 40 K");
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- IUnityContainer unitycontainer = new UnityContainer();
- unitycontainer.RegisterType<ICompany, Company>();
-
- Employee emp = unitycontainer.Resolve<Employee>();
- emp.DisplaySalary();
-
-
- Console.ReadLine();
- }
- }
- }
Now, let's try to understand the Main() function, first of all we are creating an object of IUnityContainer with the following line:
- 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.
- 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:
- 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 injectionThis 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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Http;
- using System.Net;
- using System.Net.Http.Headers;
- using Microsoft.Practices.Unity;
-
- namespace Client
- {
- public interface IEmployee
- {
- }
- public class Employee : IEmployee
- {
- private ICompany _Company;
-
- [InjectionConstructor]
- public Employee(ICompany tmpCompany)
- {
- _Company = tmpCompany;
- }
-
- public void DisplaySalary()
- {
- _Company.ShowSalary();
- }
- }
-
- public interface ICompany
- {
- void ShowSalary();
- }
- public class Company : ICompany
- {
- public void ShowSalary()
- {
- Console.WriteLine("Your salary is 40 K");
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- IUnityContainer unitycontainer = new UnityContainer();
- unitycontainer.RegisterType<ICompany, Company>();
-
- Employee emp = unitycontainer.Resolve<Employee>();
- emp.DisplaySalary();
- Console.ReadLine();
- }
- }
- }
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 injectionThe 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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Http;
- using System.Net;
- using System.Net.Http.Headers;
- using Microsoft.Practices.Unity;
-
- namespace Client
- {
- public interface IEmployee
- {
- }
- public class Employee : IEmployee
- {
- private ICompany _Company;
-
- [InjectionMethod]
- public void SetObject(ICompany tmpCompany)
- {
- _Company = tmpCompany;
- }
-
- public void DisplaySalary()
- {
- _Company.ShowSalary();
- }
- }
-
- public interface ICompany
- {
- void ShowSalary();
- }
- public class Company : ICompany
- {
- public void ShowSalary()
- {
- Console.WriteLine("Your salary is 40 K");
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- IUnityContainer unitycontainer = new UnityContainer();
- unitycontainer.RegisterType<ICompany, Company>();
-
- Employee emp = unitycontainer.Resolve<Employee>();
- emp.DisplaySalary();
-
-
- Console.ReadLine();
- }
- }
- }
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.
ConclusionIn 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.