Introduction

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

Prerequisites

Make sure 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.

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

Step 1 - Create ASP.NET Core Application

 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.


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

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

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.

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

In Studentdetails class we create the property as student name, Subjects, and grade for each student like below.

public class StudentDetails 
    
        public string studentName { get; set; } 
        public string Subject1 { getset; } 
        public string Subject2 { getset; } 
        public string Subject3 { getset; } 
        public string Subject4 { getset; } 
        public string Subject5 { getset; } 
        public string Grade { getset; }
}

Now it’s time for us to create an interface with a method named as GetAllStudentDetails() and we will be implementing this interface in our Service. For creating the Interface as we seen before adding 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, we declare a method as GetAllStudentDetails() with return type as IEnumerable

public interface IStudentDetailService
    {
        IEnumerable<StudentDetails> GetAllStudentDetails();
    }

Step 3 – Creating The Service

 First, we create a folder named as Services in the project. Right-click your project and create a new folder as “Services”.


Now let’s add a new class in this folder and named as “StudentService” and in this class, we will be implementing our Interface IStudentDetailService. We know that as if we implement the interface then we should 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 Inject this on our View page.

using ASPNETCOREDependencyInjection.Data;
  
namespace ASPNETCOREDependencyInjection.Services
{
    public class StudentService:IStudentDetailService
    {
           
        public IEnumerable<StudentDetails> GetAllStudentDetails()
        {returnnew List<StudentDetails>
        {new  StudentDetails {studentName = "Afreen", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="A"},
            new StudentDetails {studentName = "kather", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="A"},
            new StudentDetails {studentName = "Asha", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="A"},
            new  StudentDetails {studentName = "Afraz", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="B"},
            new  StudentDetails {studentName = "Shanu", Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Subject4="Networks",Subject5="C# & OOP",Grade="C"}};
        }}}

Step 4 – Register the Service

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

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

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

Step 5 – Inject the Service in the View page

Now it’s much simple 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

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

@inject ASPNETCOREDependencyInjection.Services.StudentService student
  
@if (student.GetAllStudentDetails().Any())
{
<table class='table' style="background-color:#FFFFFF; border:2px#6D7B8D; padding:5px;width:99%;table-layout:fixed;" cellpadding="2" cellspacing="2">
    <tr style="height: 30px; background-color:#336699 ; color:#FFFFFF ;border: solid1px#659EC7;">
        <td  align="center">
            Student Name
        </td>
        <td  align="center">
            Subject 1
        </td>
        <td   align="center">
            Subject 2
            </td>
            <td align="center">
                Subject 3
            </td>
            <td align="center">
                Subject 4
            </td>
            <td align="center">
                Subject 5
            </td>
            <td   align="center">
                Grade
            </td>
    </tr>
    @foreach (var std in student.GetAllStudentDetails().OrderBy(x => x.studentName))
    {
    <tr>
        <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">
            <span style="color:#9F000F">
                @std.studentName
            </span>
        </td>
        <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">
            <span style="color:#9F000F">
                @std.Subject1
            </span>
        </td>
        <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">
            <span style="color:#9F000F">
                @std.Subject2
            </span>
        </td>
        <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">
            <span style="color:#9F000F">
                @std.Subject3
            </span>
        </td>
        <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">
            <span style="color:#9F000F">
                @std.Subject4
            </span>
        </td>
        <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">
            <span style="color:#9F000F">
                @std.Subject5
            </span>
        </td>
        <td align="center" style="border: solid1px#659EC7; padding: 5px;table-layout:fixed;">
            <span style="color:#9F000F">
                @std.Grade
            </span>
        </td
    </tr>
   }
</table>
}

Conclusion

Create your account for Cognitive Services from the link and use the API Key to run this program. Hope you all like this post and we will be seeing more related to Cognitive Services.

See Also

Download

Dependency Injection in ASP.NET Core 2.0