Dependency Injection To The Core

Learn how dependency injection mechanism has evolved from ASP.NET to ASP.NET Core.

Okay ! Since I’m writing blogs mostly on ASP.NET Core lately, you might be wondering why am I writing a blog on a topic which is already available in ASP.NET Core official documentation.

Well, in this post, I’m not only going to talk about how you can achieve DI (Dependency Injection) in .NET Core but will also discuss about DI itself (what, why and how). This article reflects how I actually learned (still learning) DI. So, if you don’t want to follow my approach or don’t want a deep dive into DI,  then I would suggest you to follow the official link

If you are reading this paragraph, then maybe you are interested in a deep dive into the wonderful world of DI with me. So, are you ready? I guess you are. Let’s get started!

An application is made of different components coupled together. Coupling is good but what about strict coupling? That’s definitely bad. According to the second principle of the very well-known software design principles (simply called SOLID), "Software entities (components/class/module/functions) should be open for extension, but closed for modifications (ca be achieved with abstract classes/interfaces)."

Since we need to make our application extensible over time, we need to make sure that different components in our application are loosely coupled together. DI is a way in which we can achieve loose coupling in our application.

On Enterprise level, we often work with n-tier applications. For our example,  let’s assume that we have this 3-tier application.

It is basically a Web API application. So, all I did is I exposed some data over the client (presentation layer) using APIs. Here, we have AngularJS on the client-side (presentation layer). We have our API Controllers in the business layer and we have our Entity Framework database context to do the dirty works in the data access layer.

In this case, we really don’t have to worry about DI in the presentation layer because we can do DI in client side separately. Frameworks like AngularJS provide their own way of implementing DI, client-side. Notice that I said frameworks (AngularJS, EmberJS, BackboneJS), not libraries (jQuery, Knockout). You can use libraries and make your own framework where people can implement DI in your provided way.

I’m making things clear so that you can’t poke me later saying, “you didn’t do DI in the presentation layer”. DI is really just a concept and a technique to learn, upon learning, you can use the same concept and technique in any kind of application framework of your like. This can be either server side framework or client-side framework.

So, don’t mix things up.

Today, I’ll show you how you can achieve dependency in ASP.NET Core. So, that’s server-side. But before that, let’s see what is the current state of our application. Let’s take a look into the business layer and data access layer.

A snapshot from the data access layer would be (don’t try to understand the code.)

  1. public class TodoRepository  
  2. {  
  3.     private readonly TodoContext _context = new TodoContext();  
  5.     public IEnumerable<Todo> GetAll()  
  6.     {  
  7.         return _context.Todos;  
  8.     }  
  10.     public void Add(Todo item)   
  11.     {   
  12.         _context.Todos.Add(item);  
  13.         _context.SaveChanges();  
  14.     }  
  16.     public Todo Find(int id)  
  17.     {  
  18.         Todo todo = _context.Todos.AsNoTracking().FirstOrDefault(t => t.Id == id);  
  19.         return todo;  
  20.     }  
  22.     public Todo Remove(int id)  
  23.     {  
  24.         Todo todo = _context.Todos.FirstOrDefault(t => t.Id == id);  
  25.         _context.Todos.Remove(todo);  
  26.         _context.SaveChanges();  
  28.         return todo;  
  29.     }  
  31.     public void Update(int id, Todo item)  
  32.     {  
  33.         Todo todo = _context.Todos.FirstOrDefault(t => t.Id == id);  
  34.         todo.Title = item.Title;  
  35.         todo.IsDone = item.IsDone;  
  36.         _context.SaveChanges();  
  37.     }  
  38. }  
Notice that here, we are creating a new instance of TodoContext every time we are calling the TodoRepository class. Our TodoRepository is working as a wrapper which eventually talks to a specific DbSet defined in the TodoContext. Here is how our TodoContext looks like.
  1. class TodoContext : DbContext  
  2. {  
  3.     public TodoContext() : base("TodoDbConnectionString")  
  4.     {  
  6.     }  
  7.     public DbSet<Todo> Todos { get; set; }  
  8. }  

TodoDbConnectionString is the connection string name.

We have the API Controller in the business layer (middle layer). If you are still trying to understand the code, then don’t. We are here to learn DI not how to make an n-tier application.
  1. public class TodoController : ApiController  
  2. {  
  3.     private readonly TodoRepository _todoRepository = new TodoRepository();  
  4.     // GET: api/Todo  
  5.     public IEnumerable<Todo> Get()  
  6.     {  
  7.         return _todoRepository.GetAll();  
  8.     }  
  10.     // GET: api/Todo/5  
  11.     public Todo Get(int id)  
  12.     {  
  13.         return _todoRepository.Find(id);  
  14.     }  
  16.     // POST: api/Todo  
  17.     public void Post([FromBody]Todo todo)  
  18.     {  
  19.          _todoRepository.Add(todo);  
  20.     }  
  22.     // PUT: api/Todo/5  
  23.     public void Put(int id, [FromBody]Todo todo)  
  24.     {  
  25.         _todoRepository.Update(id, todo);  
  26.     }  
  28.     // DELETE: api/Todo/5  
  29.     public void Delete(int id)  
  30.     {  
  31.         _todoRepository.Remove(id);  
  32.     }  
  33. }  
Notice that, here again, we are creating a new instance of TodoRepository every time we are calling the API Controller. Always remember that creating a new instance means gluing stuff together. And that’s exactly what we are doing in the TodoController and TodoRepository class. What if we change our mind and want to use another repository (for example, a repository that can do CRUD operations against a file system) in the Controller instead of the current one? Again, what if we want to use a different data access library in the repository instead of the current one. Here’s what we will do when no one is watching.

We will go to those individual classes and then replace the instantiations with the new types. But doing this will break the second rule of the SOLID design principle. (Here, not only we are making our application inextensible but we are also modifying it each time our requirement is changing).

Say for example, our client changed their requirement and now wants us to read and write comma separated data (CSV) from a file system instead of a database. Suppose, I’ve searched and downloaded a cool library that can work with CSV files. So, we have a CSV library configured and ready to talk with our CSV files in the data access layer.

If you are following me, then you would say that we have a new data access layer.  We also need a new repository. Right? Exactly! Since we want to do the exact CRUD operations, now against a CSV file, our method signatures will be the same as we have for the TodoRepository but the implementation will be different (because now we are using a new library instead of EF to talk to a CSV file).
Let us not go into the implementation details; rather, just create the repository and declare the methods. Let’s say, the name of our new repository is TodoCSVRepository.
  1. public class TodoCSVRepository  
  2. {  
  3.     private readonly SomeCSVLibary _someCSVLibrary = new SomeCSVLibary();  
  5.     public IEnumerable<Todo> GetAll()  
  6.     {  
  7.         /* Use _someCSVLibrary library instance and get all the todo */  
  8.     }  
  10.     public void Add(Todo item)   
  11.     {  
  12.          /* Use _someCSVLibrary library instance to add a new todo */  
  13.     }  
  15.     public Todo Find(int id)  
  16.     {  
  17.         /* Use _someCSVLibrary library to find a todo by id */  
  18.     }  
  20.     public Todo Remove(int id)  
  21.     {  
  22.         /* Use _someCSVLibrary library to remove a todo by id */  
  23.     }  
  25.     public void Update(int id, Todo item)  
  26.     {  
  27.         /* Use _someCSVLibrary library to update a todo */  
  28.     }  
  29. }  
Suppose, later our client changed the requirement again and now he/she provided us with a new library which can talk to an in-memory database system. So, we require another repository (TodoInMemoryepository). But wait! Don’t you think every time when we are creating a new repository, we are declaring the same sets of methods which do some CRUD operations against our preferred data storage?
That’s not a big issue here but the issue is that every time when our client is changing the requirement, we are creating a new repository and instantiating that repository in the Controller (modifying code, not making the code extensible). So, let’s stop from modifying the Controller code once and for all. We have to take care of this culprit line which is new-ing up a repository.
  1. private readonly TodoRepository _todoRepository = new TodoRepository();  
We can solve this problem of ours easily by creating an abstraction for our repositories. What I meant is that we can extract an interface (abstraction) out of the repository and use that instead of the concrete type of a preferred repository. Since every repository has the exact sets of methods, we can extract an interface out of them which basically looks like this.
  1. public interface ITodoRepository  
  2. {  
  3.     void Add(Todo item);  
  4.     Todo Find(int id);  
  5.     IEnumerable<Todo> GetAll();  
  6.     Todo Remove(int id);  
  7.     void Update(int id, Todo item);  
  8. }  
We can now make the repositories implement that simple interface (abstraction) like this.

  1. public class TodoRepository : ITodoRepository { ... }  
  1. public class TodoCSVRepository : ITodoRepository { ... }  
With this simple abstraction of ours now we can modify the controller code and make it depend on an abstraction rather than a concreate type. Remember that culprit of ours? It should now look like this,
  1. private readonly ITodoRepository _todoRepository = new TodoRepository();  
Not done yet! We are still new-ing up a specific concreate repository. You can’t create an instance of a interface so still we are dependent on a specific repository. Why don’t we modify further? Let’s create an ITodoRepository field and instantiate it from the controller like this,
  1. private readonly ITodoRepository _todoRepository;  
  3. public TodoController()  
  4. {  
  5.     _todoRepository = new TodoRepository();  
  6. }  
As you can see now that our Controller's constructor is dealing with the task of creating a concrete type but we can delegate this task of creating a concreate type to someone else who will call this Controller of ours. What I meant is, if someone wants to use this controller, then he/she has to pass a new instance of the type of the concrete repository of their like as a parameter in the Controller’s constructor.
  1. private readonly ITodoRepository _todoRepository;  
  3. public TodoController(ITodoRepository todoRepository)  
  4. {  
  5.     _todoRepository = todoRepository;  
  6. }  
In this way, we can pass the responsibility upward and make our controller safe from further code modification. This kind of design (passing the type of instance through constructor from somewhere else) is also known as the constructor injection pattern.

This also follows the fifth principle (Dependency Inversion Principle) of SOLID design principles which states that, "High-level modules should not depend on low-level modules. Both should depend on abstractions."

Everything looks good but at this point, your program won’t run as you expected because it's expecting a instance of a repository type to be passed in the Controller. So, we can do some pure man’s DI here (passing an instance of an concrete repository type from the default constructor) ,
  1. private readonly ITodoRepository _todoRepository;  
  3. public TodoController() : this(new TodoRepository())  
  4. {  
  6. }  
  8. public TodoController(ITodoRepository todoRepository)  
  9. {  
  10.     _todoRepository = todoRepository;  
  11. }  
Still we have to modify the controller code when we want to use a different repository. But we can go further and build a composition root where we can do this kind of type initializations. The composition root is a simple class which will get called when the application is first initialized. In that class, we can resolve the specific types for our abstractions.
  1. public class CompositionRoot : IHttpControllerActivator  
  2. {  
  3.     public IHttpController Create(  
  4.         HttpRequestMessage request,  
  5.         HttpControllerDescriptor controllerDescriptor,  
  6.         Type controllerType)  
  7.     {  
  8.         if (controllerType == typeof(TodoController))  
  9.             return new TodoController(  
  10.                 new TodoRepository());  
  12.         return null;  
  13.     }  
  14. }  
As you can see here, we have configured our HTTPRequest in such a way that if someone requests the TodoController we will instantiate a new instance of TodoRepository and pass it to the controller's constructor. Likewise, we can change it to TodoCSVRepository or TodoInMemoryepository whenever we feel like. Now we have a single place to do all our dirty works of type initializations. In Web API projects we have to register this composition root in the Global.asax.cs file like this,
  1. GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator),new CompositionRoot());  
One thing to remember here is, a composition root implementation varies from framework to framework. Here we are working with Web API so this won’t work for MVC (may work) and WPF applications. You have to find a way to implement those but don’t worry because the internet has a lot of code snippets that can ease up your task of creating a composition root for a specific framework. Tell you what, I learned how to make a composition root like this for Web API projects after reading a blog from Mark Seeman. Here is the link for it,

That’s good since we have to deal with resolving tiny dependencies for this small project of ours. What if we have a large project where hundreds of dependencies are scattered around? In that cases, composition root won’t be a good idea. That is why in enterprise level, we use a well-known IoC (Inversion of Control) container to make our job easy. IoC containers can resolve dependencies recursively and they are also pretty much easy to configure. They allows us to work with dependency injection lifecycle easily.

There are many IoC containers available and most of them do the same things somewhat differently. Let’s use one of them in our current project. Let’s pick Autofac which has a great documentation online. Here is the link where you can know all about the Autofac integration related stuff with Web API projects,

Since we are just beginners wondering around the world of dependency injection, we will go slow and easy. The Autofac library for Web API projects is available to download to from NuGet.

Install-Package Autofac.WebApi2

I’ve downloaded it in my Techtalkers.WEB project. It is time to configure it. I’ve created a class in the App_Startfolder and added this method where I’ve configured Autofac like this.
  1. public class AutofacConfig  
  2. {  
  3.     public static void RegisterAutofac()  
  4.     {  
  5.         var builder = new ContainerBuilder();  
  7.         var config = GlobalConfiguration.Configuration;  
  8.         builder.RegisterApiControllers(Assembly.GetExecutingAssembly());  
  9.         builder.RegisterType<TodoRepository>().As<ITodoRepository>().InstancePerRequest();  
  11.         var container = builder.Build();  
  12.         config.DependencyResolver = new AutofacWebApiDependencyResolver(container);  
  13.     }  
  14. }  
Here, we get the HttpConfiguration which is basically the GlobalConfiguration.Configuration. Registered all the Api controllers available in the current assembly (Techtalkers.WEB). Then registered the TodoRepository so that a new instance of it can be served whenever we have ITodoRepository in the request scope. Notice that we can define lifetime for a specific instance.
Here,  InstancePerRequest() will create a new singleton instance of TodoRepository per request and shared across the nested parentand nested scopes. You can define different lifetimes for your service instance just by using the other extension methods available from Autofac. And this is actually a great functionality available in all the Ioc containers.
Lastly, we have built the container and passed it in the AutofacWebApiDependencyResolver. This class basically implements the IDependencyResolver interface provided by the Web API project. That’s it. From now on Autofac will resolve all the dependencies for our project. We don’t have to bother about how it will do it. Thing is it will do it. And if you are interested in the raw implementation then you can check the github repository for Autofac. This is an open-source project. Here is the github repository link,

Next, we have to call the RegisterAutofac() from the Global.asax.cs. There, the Application_Start() is called every time the application is started. So, like the other registration stuff, I’ve registered the RegisterAutofac() method there.
  1. protected void Application_Start()  
  2. {  
  3.     AreaRegistration.RegisterAllAreas();  
  4.     GlobalConfiguration.Configure(WebApiConfig.Register);  
  5.     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  6.     RouteConfig.RegisterRoutes(RouteTable.Routes);  
  8.     AutofacConfig.RegisterAutofac();  
  9. }  
Now, we are in a situation where we can easily test our TodoController. We don’t have to worry about instantiations of concrete repositories since we are no longer dependent on any of them. Instead, we can use a mock repository whenever we are in the test. A library called Moq can help us do that. A simple test for the Get()method will looks like this,
  1. public class TestTodoControlloer  
  2. {  
  3.     [TestMethod]  
  4.     public void GetAll_ShouldReturnAllTodos()  
  5.     {  
  6.         // Arrange  
  7.         var mockRepository = new Mock<ITodoRepository>();  
  9.         mockRepository.Setup(x => x.GetAll())  
  10.             .Returns(new List<Todo>()  
  11.             {  
  12.                 new Todo() {Id = 1, Title = "Test Item 1", IsDone = true},  
  13.                 new Todo() {Id = 2, Title = "Test Item 2", IsDone = true},  
  14.                 new Todo() {Id = 3, Title = "Test Item 3", IsDone = false}  
  15.             });  
  17.         var controller = new TodoController(mockRepository.Object);  
  19.         // Act  
  20.         var todoes = controller.Get();  
  22.         // Assert  
  23.         Assert.IsNotNull(todoes);  
  24.         Assert.AreEqual(3, todoes.Count());  
  25.         Assert.AreEqual(2, todoes.Count(t => t.IsDone));  
  26.     }  
  27. }  
As you can see here, we don’t have to create a concrete repository type to pass it around in the controller. We can just simply use a mock object instead.

Now that we have some get to go understanding on DI, we can make a tour to the wonderful world of .net core and see how DI is done there. .Net core by default provides some bare minimum functionality to do DI. But most of the time it’s enough for even big project that has hundreds of dependencies. For the time being .net core’s default IoC container only support construction injection.

So, just like we did with Autofac if you want to serve a concrete TodoRepository instance whenever ITodoRepository get caught in the scope, you would do something like this in the ConfigureServices()method,
  1. services.AddScoped<TodoRepository, ITodoRepository>();  
As you can see, we can define instance lifetime at the same time. Here, defining a service in Scoped lifetime means that a new instance of TodoRepository will be created per request and it will be shared in nested scopes.

The two other service lifetimes are Singleton and Transient. And they can be registered like above,
  1. services.AddSingleton<TodoRepository, ITodoRepository >();  
  2. services.AddTransient<TodoRepository, ITodoRepository >();  
In singleton lifetime, service instances are created the first time they are requested and in subsequent requests the instances will be shared though all the parent and nested scopes.

In transient lifetime, new service instances are created per requests for all the parent and nested scopes.

If you dont feel happy with the built-in functionality, you can also add third part IoC containers. Autofac, by itself, can be used with .NET Core. You can have a good idea on how to integrate Autofac at this link.

You can learn more about dependecy injection in .net core from this official link.

So, that’s it. I guess now you are pretty much comfortable with dependency injection and its related terms. But before you think you are done, let me remind you again that these are very much beginner level articles. Also, I only talked about constructor injection. But there is also property injection, method injection, interface injection and etc. I’ll let you do the honor and explore them. Other than lifetime management, you can do a lot with Ioc containers, so there are lot of things to learn too. Now, go and get your feet wet.