Implementing Unity Dependency Injection Framework in a Class Library


In one of my post I had provided a demonstration of how we can use Ninject Dependency Injection Framework in a class Library. In this post I am going to give a try to demonstrate how we can use Unity Dependency Injection Framework in the same class library. So let’s get started first creating a class library project having a model named “Employee”, an interface having signatures named “IEmployeeRepository” and its implementation as “EmployeeRepository”.

public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public string Department { get; set; }
}

Creating an Interface

public interface IEmployeeRepository
{
IEnumerable<Employee> GetEmployeeList();
Employee GetEmployee(string empName);
}

Having the implementation

public class EmployeeRepository : IEmployeeRepository
{
public IEnumerable<Employee> GetEmployeeList()
{
return new List<Employee>
{
new Employee{ Name="Joy", Age=37, Department="Software" },
new Employee{ Name="Chris", Age=29, Department="Itics" }
};
}

public Employee GetEmployee(string empName)
{
var employee = (from emp in GetEmployeeList()
where emp.Name == empName
select new Employee {
Name = emp.Name, Age = emp.Age, Department = emp.Department
}).FirstOrDefault();
return employee;
}
}

Next step is to create an instance of Unity Container and register the interfaces and their concrete classes. Then the container is used to resolve to get the actual instance of the concrete classes.

First we need to install Unity Dependency Injection Framework using Nuget with command “install-package Unity

PM> install-package Unity
Installing 'Unity 3.5.1404.0'.
You are downloading Unity from Microsoft, the license agreement to which is available at http://opensource.org/licenses/Apache-2.0. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Unity 3.5.1404.0'.
Adding 'Unity 3.5.1404.0' to DI.Demo.
Successfully added 'Unity 3.5.1404.0' to DI.Demo.

Next we are going to perform the above operations as we have discussed. We will go ahead to create the concrete class of implementation.

public class LoadEmployee
{
public readonly IEmployeeRepository EmployeeRepository;
public LoadEmployee(IEmployeeRepository _empRepository)
{
EmployeeRepository = _empRepository;
}
public void GetAllEmployees()
{
var iList = EmployeeRepository.GetEmployeeList();
}
}

And now the resolver.

public static void Main()
{
IUnityContainer container = new UnityContainer();
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
var instance = container.Resolve<LoadEmployee>();
instance.GetAllEmployees();
}

That’s it we are good to go. If you would like to test the implementation create a Unit Test Project for the class library with a Test method to derive the implementation

[TestClass]
public class DITest
{
[TestMethod]
public void Testing_Dependency_Injection_Using_Unity()
{
Resolver.Main();
Assert.IsTrue(true);
}
}

I hope this will help you to implement the DI using Unity Framework in a class library 

4 thoughts on “Implementing Unity Dependency Injection Framework in a Class Library

  1. Very useful, but I believe implementation is not for true class library as class library do not have “public static void Main()” as there entry point.

    Like

  2. Looks good but where does the resolver code go? i.e. when creating classes we start with a new class file that defaults to class1.cs what file type do we use for the resolver.

    Like

  3. I am thinking since this is Class Library which has no entry point the concept here is for test-ability of the exposed object(s) not so much for instancing in a project that references the class library. in that case the referencing project should still have its own DI/IOC project and resolver and not the class library.

    Like

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 )

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