Skip to main content

Dependency Injection In ASP.NET Core 2.0

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.



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

Popular posts from this blog

How To See Logs Of Dropped Tables From The Database in MS SQL.

Here, I will explain you how you can see logs of users. Step 1 : First, create a new database with name "test". Step 2 : Create a new table. Step 3 : Now, go and drop the table by running the following command. Step 4 : Now, select your database under Object Explorer and go to Reports >> Standard Reports >> Schema Changes History. Step 5 : You will then see the schema change history. The report will show you who has dropped this table. Finally, you can locate the user activity with the help of log.

How To Deploy .net Core Application On Linux

Here, I can explain steps to deploy .net core application on linux machine. Step 1 - Publish your .net Core application: First, create a .net core application on VS; you can make an MVC project or Web API project and if you already have an existing project, then open it. Right Click on your project Click on publish Now create a new publish profile, and browse the folder where you want to publish your project dll Click on publish so it will create your dll in the folder Step 2 - Install required .net Module on Linux: Now we have our web application dll and now we need to host it on the Linux environment. First, we need to understand how the deployment works in Linux. .Net applications run on Kestrel servers and we run Apache or Nginx server in Linux environments, which acts as a proxy server and handles the traffic from outside the machine and redirects it to the Kestrel server so we will have Apache or Nginx server as the middle layer. In this article, we will use Apache as a proxy ser

List Of Commonly Used Angular Commands

1) To get the npm version,    npm -v 2) To get the node version,    node -v 3) To get the Angular version,    ng v  4) To get the Jasmine version,    jasmine -v  5) To get the Karma version,    karma --version  6) To install Angular CLI,    npm install @angular/cli -g   npm install @angular/cli 7) To install the next version of Angular CLI, v   npm install @angular/cli@next  8) To get help in the terminal,    ng help 9) To create a new project in Angular,    ng new project-name  10) To skip external dependencies while creating a new project,    ng new project-name --skip-install  11) To run the Angular project,   ng serve (or) npm start (or) ng serve --force  12) Dry Run,   ng new project-name --dry-run 13) To create a new component in the Angular Project,   ng generate component component-