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

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

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.

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 string Subject1 { get; set; }
- public string Subject2 { get; set; }
- public string Subject3 { get; set; }
- public string Subject4 { get; set; }
- public string Subject5 { get; set; }
- 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”.
- public interface IStudentDetailService
- {
- 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”.

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.
- using ASPNETCOREDependencyInjection.Data;
- namespace ASPNETCOREDependencyInjection.Services
- {
- public class StudentService:IStudentDetailService
- {
- public IEnumerable<StudentDetails> GetAllStudentDetails()
- {
- return new 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" }
- };
- }
- }
- }



Manoj KPosted Jan 8, 2020, 11:13 PM
Please correct the service injection code it should be services.AddTransient<IStudentDetailService, StudentService>()
saavi khandalPosted Aug 16, 2019, 6:46 AM
Nice article..so simple to understand..
Ravindra JagadalePosted Nov 22, 2018, 5:08 AM
I want create model from database then where I have to write connectioSstring and how to make it
Ashutosh PuspwanPosted Apr 4, 2018, 10:25 AM
Very nice article.
Gajendra JangidPosted Mar 28, 2018, 6:56 AM
Nice blog..................
Priyanka KalePosted Mar 9, 2018, 4:41 AM
Very informative and interesting.
Hiten PandyaPosted Mar 7, 2018, 6:43 AM
Such a great article. Thanks for sharing sir.