ASP.NET Core 2.0 - How To Use Dependency Injection

Introduction

In this article, I'll show you the new feature introduced in ASP .NET Core that allows you to inject dependencies directly into a View using the dependency injection container.

ASP .NET Core has introduced a feature called "View Injection" that allows you to inject the dependencies into a View using the inject keyword.

Previously, to retrieve the data from a View, we needed to pass it from the Controller using the Controller properties, such as ViewBag, ViewData, or Model properties.

In ASP.NET Core MVC, this task became very simple using the Inject directive that allows you to inject the dependencies directly into the View and retrieve the data.

Let's see this working in practice!

STEP1 - Creating the ASP .NET Core Project in VS 2017

Open VS 2017 and on the File menu, click New Project. Then, select the Visual C # -> Web template and check ASP .NET Core Web Application (.NET Core). 

ASP.NET Core

Then, enter the name AspNetCoreDemo and click OK.

In the next window, choose the ASP .NET Core 2.0 version and mark the Web Application (Model-View-Controller) template without authentication and click the OK button.

ASP.NET Core

STEP2 - Creating a new service in the application

Let's define a very simple service just for testing in our application.

ASP.NET Core

Create the Services folder in the project (Project -> New Folder), and then add the CountriesService.cs file to this folder containing the following code.

  1. using System.Collections.Generic;  
  2. namespace AspNetCoreDemo.Services  
  3. {  
  4.     public class CountriesService  
  5.     {  
  6.         public List<string> GetCountries()  
  7.         {  
  8.             return new List<string>() { "Portugal""Spain""France" };  
  9.         }  
  10.     }  
  11. }  

STEP 3 - Injecting the service into View

We will now have how we can inject the service created in the project View using the @Inject directive. Think of the @Inject directive as a property being added in your View and populating the property using dependency injection.

The basic syntax used is: @inject <service> <name>

At where,

  1.  @ inject - is the directive used to inject dependencies;
  2.  <service> - is the class of service.
  3.  <name> - is the service injection name through which we can access the service's methods.
Let's open the Index.cshtml file from the /Views/Home folder and include the code below in this file.
  1. @inject AspNetCoreDemo.Services.CountriesService Countries  
  2. @foreach (var countryName in Countries.GetCountries())  
  3.         {  
  4.             <ul>  
  5.                 <li>@countryName</li>  
  6.             </ul>  
  7.         } 

STEP4 - Registering the service in the Startup.cs file

Open the Startup.cs file and add the code shown below to this file that registers the service created for dependency injection in the ConfigureServices method.

  1. public void ConfigureServices(IServiceCollection services)  
  2.         {  
  3.             services.AddMvc();  
  4.             services.AddTransient<CountriesService>();  
  5.         }  

STEP5 - Result

Executing the project, we will obtain the following result.

ASP.NET Core
Resources