Dependency Injection in Asp.Net Core

Asp.Net Core has the built-in support for Dependency Injection. Earlier on dot net framework, we used to use third-party dependency injector like Ninject, Autofac, and Unity etc. Being a game changer on Microsoft technology, Asp.Net core has its own simple built-in dependency injection container.

In this article, I am going to demonstrate how dependency Injection works with its own default container.

What is Dependency Injection?

Dependency Injection is also known as DI is the design pattern in software engineering. In simple term, DI is the concept of designing the software in such a way that the components of the software communicate with each other without depending on the concrete implementation.  The technique of DI decouple the different components of software and further helps to design the loosely coupling software.

Until the day of writing this article, It is Asp.Net Core V2.1. Dependency Injection became already a vital part since day one.

Implementing Dependency Injection in Asp.Net Core

Now I will create a simple Asp.Net Core application. I have Visual Studio 2017 and Asp.Net Core V2.1 already installed. If you have Visual Studio 2015, make sure you have .net Core installed.

  1. Open Visual Studio
  2. Click, File -> New -> Project
  3. on the left section select, Visual C# -> Web
  4. on the middle section select, ASP.NET Core Web Application
  5. Specify the name and location of your project and click OK.

For the sake of the demo, I will create a simple car service that will provide the detail of the cars along with their price. Once I register the car service on DI container, later I can use that service throughout the project wherever needed.

First Step: Create required classes.

Now create a simple POCO class which contains few properties describing the car object.

Now create an Interface named ICarService. This interface contains only one method named GetAll(), which will return the collection of CarModel. The CarService class will implement this interface later on the demo.

The CarService class implements the ICarService interface as shown below.

Register Service of Asp.Net Core DI Container

To use the carService later on the application, we need to register it on Dependency Injection Container. As mentioned earlier, we are using the built-in DI container of Asp.Net Core itself. The Startup.cs class of the project contains ConfigureServices function where we will register our service as shown below.

As shown above, we can register as many services as we need but for the sake of making our code cleaner, we can also use an extension method like this.

Now instead of registering all the services at the same place, we can call RegisterVehicleServices extension method in our ConfigureServices as shown below.

If you have a huge chunk of services to register, this way of registering services helps you to write clean and maintainable code.

Asp.Net Core Dependency Lifetime

In this article, I am using the two-term ‘service’ and ‘dependency’ interchangeably. The AddScoped, as we used above implies the lifetime of the service. The lifetime of the service gives us feasibility to register and use our service instance according to our requirement. In Asp.net Core there are 3 options to register service.

  1. Singleton
    The services created using singleton lifetime will have at most one instance until the container is disposed of.
  2. Scoped
    The services created using scoped lifetime will have one instance per scope.
  3. Transient
    The services created using transient lifetime will be created each time they are requested. This lifetime works best for lightweight services.

You can read this Microsoft doc for the more detailed view about the asp.net core service lifetime.

In this article, we have registered our service using transient type. The same service can also be registered using scoped and singleton lifetime as below.

Consume our registered service in our Asp.Net Core application via Dependency Injection

Now we can use the registered service throughout our project. The service contains the collection of functions like GetAll in our project. All the functions implemented inside the service can be called from anywhere in the project. For that first, we need to create the instance of the service.

Here I am going to consume the CarService in CarController as shown below.

Here the IServiceProvider is injected into the constructor of CarController which can help us to retrieve the service object. The carService object gets created using the instance of IServiceProvider.

Finally, the Index method returns the IEnumerable of CarModel where we have utilized the instance of CarService.

About dipneupane

Dip is a passionate Software Developer based in Kathmandu, Nepal. He chew code and smash keyboard for living. Apart from that, he writes about almost everything on buzzedcode.com.

View all posts by dipneupane →

Leave a Reply

Your email address will not be published. Required fields are marked *