Dependency Injection In ASP.NET Core 2.0

ASP.NET Core
Introduction

In this article, we will see in detail how to use Dependency Injection in ASP.NET Core 2.0. Now, it's much easier to use Dependency Injection in our ASP.NET Core application and the fact is, that it's very simple and we can use the result to bind in our View page. Let’s see in detail how to create our ASP.NET Core project to use Dependency Injection for binding the student details to our View page. Hope you are aware of what Dependency Injection is. If you are curious to learn more about Dependency Injection, then start from here.

  • https://msdn.microsoft.com/en-us/library/hh323705%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396 ,  
  • https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection

Prerequisites

Make sure that you have installed all the prerequisites on your computer. If not, then download and install all, one by one.

  1. First, download and install Visual Studio 2017 from this link

Code Part

Now, it’s time to create our first Angular5 and ASP.NET Core application.

Step 1- Create Angular5TemplateCore

After installing the prerequisites listed above, click Start >> Programs >> Visual Studio 2017 >> Visual Studio 2017, on your desktop.

Click New >> Project. Select Visual C# >> Select Angular5Core2. Enter your project name and click OK.

ASP.NET Core

Select ASP.NET Core 2.0 , select  Web Application(Model-View-Controller) and click ok.

ASP.NET Core

Our project will be created and it's time for us to add a Class, Interface, and Service for using Dependency Injection in ASP.NET Core applications.

Step 2 – Creating Data Folder for Class

First, we will start with creating a class and for this, from our project, create a folder and name it as data, as shown below.

ASP.NET Core

Now, we have created a folder named Data in our project. The next step will be to create a class named “StudentDetails”. Right-click the Data folder and add a new class named “StudentDetails”.

ASP.NET Core

In Studentdetails class, we need to create properties as student name, Subjects, and grade for each student like below.

  1. public class StudentDetails  
  2.   {  
  3. public string studentName { get; set; }  
  4. public string Subject1 { get;  set; }  
  5. public string Subject2 { get;  set; }  
  6. public string Subject3 { get;  set; }  
  7. public string Subject4 { get;  set; }  
  8. public string Subject5 { get;  set; }  
  9. public string Grade { get;  set; }   

Now, it’s time for us to create an interface with method named GetAllStudentDetails() and we will be implementing this interface in our Service. For creating the Interface, as we have seen before, add a new class to your data folder and name the class as “IStudentDetailService”.

We will change the class to an interface as we are going to create an interface to implement in our service. Here in this interface, declare a method as GetAllStudentDetails() with return type as IEnumerable.
  1. public interface IStudentDetailService  
  2.   {  
  3. IEnumerable<StudentDetails> GetAllStudentDetails();  

Step 3 – Creating Service

First, let us create a folder named as Services in the project, right-click your project, and create a new folder as “Services”.

ASP.NET Core

Now, let’s add a new class to this folder and name the class as “StudentService”. In this class, we will be implementing our interface IStudentDetailService. We know that if we implement the interface, then we should declare the interface method in our class. In this service, we use the interface method and we return the list with student details. We will be directly Injecting this on our View page.

  1. using ASPNETCOREDependencyInjection.Data;  
  2.   
  3. namespace ASPNETCOREDependencyInjection.Services  
  4. {  
  5.     public class StudentService:IStudentDetailService  
  6.     {  
  7.            
  8.         public IEnumerable<StudentDetails> GetAllStudentDetails()  
  9.         {  
  10.             return new List<StudentDetails>  
  11.         {  
  12.             new  StudentDetails {studentName = "Afreen", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="A"},  
  13.             new StudentDetails {studentName = "kather", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="A" },  
  14.             new StudentDetails {studentName = "Asha", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="A" },  
  15.             new  StudentDetails {studentName = "Afraz", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="B" },  
  16.             new  StudentDetails {studentName = "Shanu", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="C" }  
  17.               
  18.         };  
  19.         }  
  20.     }  
  21. }  

Step 4 – Register the Service

We need to register our created service to the container. Open the Startup.cs from your project to add the service to the container.

ASP.NET Core

In the Startup.cs class, find the method named as ConfigureServices and add your service “StudentService” like below.

  1. // This method gets called by the runtime. Use this method to add services to the container.  
  2.         public void ConfigureServices(IServiceCollection services)  
  3.         {  
  4.             services.AddTransient<StudentService, StudentService>();  
  5.   
  6.             services.AddMvc();  
  7.         }  

Step 5 – Inject the Service in the View page

Now, it’s much simpler and easier as we can directly Inject the service in our View page and bind all the result to our view page. For injecting the Service in our View, here we will be using our existing view page from Home >> Index.cshtml

ASP.NET Core

In the “Index.cshtml”, we inject our StudentService and bind all the results inside the table.

  1. @inject ASPNETCOREDependencyInjection.Services.StudentService student  
  2.   
  3. @if (student.GetAllStudentDetails().Any())  
  4. {  
  5. <table class='table' style="background-color:#FFFFFF; border:2px#6D7B8D; padding:5px;width:99%;table-layout:fixed;" cellpadding="2" cellspacing="2">  
  6.     <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid1px#659EC7;">  
  7.         <td  align="center">  
  8.             Student Name  
  9.         </td>  
  10.         <td  align="center">  
  11.             Subject 1  
  12.         </td>  
  13.         <td   align="center">  
  14.             Subject 2  
  15.             </td>  
  16.             <td align="center">  
  17.                 Subject 3  
  18.             </td>  
  19.             <td align="center">  
  20.                 Subject 4  
  21.             </td>  
  22.             <td align="center">  
  23.                 Subject 5  
  24.             </td>  
  25.             <td   align="center">  
  26.                 Grade  
  27.             </td>  
  28.     </tr>  
  29.     @foreach (var std in student.GetAllStudentDetails().OrderBy(x => x.studentName))  
  30.     {  
  31.     <tr>  
  32.         <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">  
  33.             <span style="color:#9F000F">  
  34.                 @std.studentName  
  35.             </span>  
  36.         </td>  
  37.         <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">  
  38.             <span style="color:#9F000F">  
  39.                 @std.Subject1  
  40.             </span>  
  41.         </td>  
  42.         <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">  
  43.             <span style="color:#9F000F">  
  44.                 @std.Subject2  
  45.             </span>  
  46.         </td>  
  47.         <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">  
  48.             <span style="color:#9F000F">  
  49.                 @std.Subject3  
  50.             </span>  
  51.         </td>  
  52.         <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">  
  53.             <span style="color:#9F000F">  
  54.                 @std.Subject4  
  55.             </span>  
  56.         </td>  
  57.         <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">  
  58.             <span style="color:#9F000F">  
  59.                 @std.Subject5  
  60.             </span>  
  61.         </td>  
  62.         <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">  
  63.             <span style="color:#9F000F">  
  64.                 @std.Grade  
  65.             </span>  
  66.         </td>   
  67.     </tr>  
  68.    }  
  69. </table>  
  70. }  

Step 6 – Build and Run the project

Build and run the project. We can see that all our student details are displayed on our homepage.

ASP.NET Core

Conclusion

We can see clearly that ASP.NET Core makes our workload much simpler by using Dependency Injection directly injected to our View page and binding the results.