In this article, we will see in detail how to use Dependency Injection in ASP.NET Core 2.0.
create our first Angular 5 and ASP.NET Core application.
Step 1- Create Angular5 Template Core
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.
create our first Angular 5 and ASP.NET Core application.
Step 1- Create Angular5 Template Core
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.
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 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”.
In Studentdetails class, we need to create properties as student name, Subjects, and grade for each student like below.
public class StudentDetails
{
public string StudentName { get; set; }
public int RollNumber { get; set; }
public string Subject1 { get; set; }
public string Subject2 { get; set; }
public string Subject3 { get; set; }
public string Grade { get; set; }
}
Step 3 – Creating Interface and Service
First, let us create a folder named as Services in the project, right-click your project, and create a new folder as “Services”.
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 Services folder and name the class as “IStudentService”.
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.
using AspNetCoreDependencyInjections.Data;
namespace AspNetCoreDependencyInjections.Services
{
public interface IStudentService
{
IEnumerable GetAllStudentDetails();
}
}
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 IStudentService. 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.
using System.Collections.Generic;
using AspNetCoreDependencyInjections.Data;
namespace AspNetCoreDependencyInjections.Services
{
public class StudentService : IStudentService
{
public IEnumerable GetAllStudentDetails()
{
return new List
{
new StudentDetails {StudentName = "Nikhil",RollNumber = 1, Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Grade="A"},
new StudentDetails {StudentName = "Rushita",RollNumber = 2, Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Grade="A" },
new StudentDetails {StudentName = "Sandip",RollNumber = 3, Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Grade="A" },
new StudentDetails {StudentName = "Yash",RollNumber = 4, Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",Grade="B" },
new StudentDetails {StudentName = "Hardik",RollNumber = 5, Subject1 = ".Net Programming",Subject2="Operating System",Subject3="Web Programming",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 the service to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient();
services.AddMvc();
}
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
In the “Index.cshtml”, we inject our StudentService and bind all the results inside the table.
@inject AspNetCoreDependencyInjections.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.RollNumber
</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.Grade
</span>
</td>
</tr>
}
</table>
}
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.
Comments
Post a Comment