Dependency Injection in ASP.NET Core

Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at Dependency Injection in ASP.NET Core. It's our 7th tutorial in ASP.NET Core series. In previous versions of ASP.NET, dependency injection was not its core element, although we could have achieved it using third party packages i.e. Ninject, StructureMap etc. As ASP.NET Core is built from scratch so it was made sure that Dependency Injection should become its essential part. In our previous lecture, we have created Controller & Model for our core project. Now there's a need to connect them together and that will be done by Dependency Injection. So, let's get started with Dependency Injection in ASP.NET Core:

Dependency Injection in ASP.NET Core

  • Dependency Injection in ASP.NET Core is used to inject dependencies in Web Application and these dependencies are mentioned in IoC Container, implemented by IServiceProvider.
  • In Startup.cs file, the ConfigureServices Method is dependency injection container and if you check it's parameters, then you will find IServiceProvider in it.
  • There are different types of Dependency Injections available and the most commonly used one is Constructor Injection.
  • In our Startup.cs file, we have seen two Methods: ConfigureServices and Configure.
  • So far, we have only discussed Configure Method, which is used to set up Request Processing Pipeline and we add our Middlewares in it.
  • But we haven't yet discussed the ConfigureServices Method, which is an IoC Container and is used for service configuration required by our application.
  • We can configure both builtin and custom services in this ConfigureServices Method.
  • In our previous lecture, we have added an MVC Service (builtin) in this IoC container, as shown in below figure:
  • Similarly, we can also create custom dependencies in ASP.NET Core and these custom dependencies will also be registered in this ConfigureServices Method.
  • In our previous lecture on MVC architecture, we have created an Interface named IEngineersRepository and a class implementing this interface named EngineersRepository.
  • In our future coding, we will never invoke this class, as its an implementation and we don't want to directly call this class as it will make our code rigid. Instead, I will always call this Interface as it's just a definition. ( If you are not getting this point yet, don't worry we will cover it in detail later)
  • So, now we have to insert this dependency so that if someone wants to use any method from Interface IEngineersRepository, compiler should know that it has to invoke the method from class EngineersRepository, without actually calling the class.
  • So, let's add this dependency using AddSingleton Method, as shown in below figure:
  • You can see in above code that in a single line, we have declared our dependency i.e. our interface IEngineersRepository is dependent on class EngineersRepository.
  • Now in our project, whenever we invoke this interface IEngineersRepository, compiler will know that I have to look in EngineersRepository class.
We have added the dependency condition in our Dependency Injection Container. But remember one thing that we haven't yet connected Model and Controller. We have just connected a Model Class with a Model Interface. So, now let's have a look at Constructor Injection in ASP.NET Core:

Constructor Injection in ASP.NET Core

  • When we want to inject one service into another then we use Constructor Injection in ASP.NET Core.
  • The service, we want to inject, is provided as a parameter to the Constructor of the service, in which it is being injected.
  • In our example, we want to inject our Interface IEngineersRepository in our Home Controller so that we could use its methods, as shown in below figure:
  • I have encircled the constructor injection code with red line, and you can see that I have created the Constructor of Home Controller and then passed interface as a parameter.
  • Finally, simply saved the value in private variable _engineersRepository.
  • In the action method index(), I have used this variable and then called the method GetEngineers, which is implemented in my class and will provide me the data based on Id, which I have provided 1. ( Rite now value is hard coded, we will automate it soon )
  • So, I am getting the university name of engineer with Id = 1, and if you remember we have hard coded the data and the university of first student was MIT.
  • So, now let's run this project and if everything goes fine then you will get similar results:
  • So, let's summarize our lecture, we have used dependency injection and then called Model function from home controller.

Why we need Dependency Injection ???

  • When you are working on some complex application, then we want it to be as flexible as we can and here dependency injection comes quite handy.
  • With Dependency Injection, we can bound different parts of our application together.
  • Let's say you have created some Interface named "IEngineersData" and then a class implementing it named "EngineersProfile".
  • Now in order to implement Constructor Injection, you first have to configure it in the ConfigureServices Method so that compiler could know that this class is implementing that interface.
  • In future, if you want to change the implementation of your Interface, you just need to change the name in this IoC Container.
I know it's not making much sense rite now and is a bit difficult to understand, but no need to worry as in our upcoming lecture, we will have a look at practical implementation of these concepts. So, that's all for today, in our next lecture, we will have a look at Views in ASP.NET Core. Till then take care & have fun !!!
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir