Introduction To ASP.NET Core, WEB API And Repository Class- Part One

This will be a series of articles. In this series, we will see one by one in detail about:

  1. Introduction to ASP.NET Core, WEB API and Repository Class
  2. ASP.NET Core, WEB API and Repository Class display using MVC View
  3. ASP.NET Core CRUD using WEB API and Repository Class
  4. ASP.NET Core CRUD with Database using WEB API and Repository Class

In this article, we will see in detail about how to create ASP.NET Core with Repository pattern in the WEB API.

This will be the series of articles--  in the first series will see how to create a simple ASP.NET Core application and how to create our first WEB API with Repository Class and view our JSON output in our browser.,

WEB API

Web API is a simple and easy way to build HTTP Services for Browsers and Mobiles. It has the following four methods as Get/Post/Put and Delete where.

  • Get is used to request for the data. (Select)
  • Post is used to create a data. (Insert)
  • Put is used to update the data.
  • Delete used is to delete the data.

Reference Link

Repository Class

Repository Patten allows us to create a new layer for our business logics and Database operations. We can use repository to store our data. To know more about repository check this link

Prerequisites

  • Visual Studio 2015: You can download it from here.
  • .NET Core 1.0.1: download link,link2.

Code Part

Step 1 Create our ASP.NET Core 1.0.1 Web Application.

After installing both Visual Studio 2015 and ASP.NET Core 1.0.1. Click Start, then choose Programs and select Visual Studio 2015 - Click Visual Studio 2015. Click New, then Project, select Web and select ASP.NET Core Web Application. Enter your Project Name and click OK.

ASP.NET

Next select WEB API. Click OK.

ASP.NET

Step 2 Creating Modules

To create our module class first, we create one folder inside our solution project.

Right click our solution > Click Add > Click New Folder

ASP.NET

Name the folder as Models.

ASP.NET

Creating Model Class

Right Click the Model Folder, add new class and name it as “StudentMasters.cs”
In this class, we declare our property variables.

  1. namespace ASPNETCOREWEBAPI.Models {  
  2.     public class StudentMasters {  
  3.         public string StdName {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Email {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Phone {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string Address {  
  16.             get;  
  17.             set;  
  18.         }  
  19.     }  
  20. }  
Step 3 Repository Class Creating Repository Class

Here we create a Repository class to inject into our Controllers. To create a Repository class,
Right click on Models Folder and click Add Class.

Name the class as IStudentRepository .

Here we can see that I have given the class name starting with I as this class, which  we will be using as Interface, and here we will declare only our methods to be used in our StudentRepository class.
  1. public interface IStudentRepository {  
  2.     IEnumerable < StudentMasters > GetAll();  
  3.     void Add(StudentMasters info);  
  4. }  
In this interface, we have added only Get and Add method. In our next article, we will see in detail for CRUD operations.

Creating a class to implement the interface.

Now we create one more class inside Models folder as “StudentRepository” we will

In this class we create a method to get all the student information and to add student Information.
  1. using System;  
  2. using System.Collections.Concurrent;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6. namespace ASPNETCOREWEBAPI.Models {  
  7.     public class StudentRepository: IStudentRepository {  
  8.         private static ConcurrentDictionary < string, StudentMasters > stdMaster = new ConcurrentDictionary < string, StudentMasters > ();  
  9.         public StudentRepository() {  
  10.             Add(new StudentMasters {  
  11.                 StdName = "Shanu",  
  12.                     Phone = "+821039120700",  
  13.                     Email = "syedshanumcain@gmail.com",  
  14.                     Address = "Seoul,Korea"  
  15.             });  
  16.         }  
  17.         public IEnumerable < StudentMasters > GetAll() {  
  18.             return stdMaster.Values;  
  19.         }  
  20.         public void Add(StudentMasters studentInfo) {  
  21.             stdMaster[studentInfo.StdName] = studentInfo;  
  22.         }  
  23.     }  
  24. }  
Adding Repository Class in Configure Services

To Inject our repository in Controllers we need to register the repository class with Dependency Injection Container.

To understand what Dependency Injection(DI) is check this link

Open the Stratup.cs file from our solution project

s

First we add our Models folder

using ASPNETCOREWEBAPI.Models;

Next we register our own services like the code below.

services.AddSingleton<IStudentRepository, StudentRepository>();

like this
  1. // This method gets called by the runtime. Use this method to add services to the container.   
  2. public void ConfigureServices(IServiceCollection services) {  
  3.     // Add framework services.   
  4.     services.AddMvc();  
  5.     services.AddSingleton < IStudentRepository, StudentRepository > ();  
  6. }  
Here is the complete Startup.cs class

Creating Controllers

Step 4 Creating Controllers

Right Click the Controllers Folder > Click Add > Click New Item.

Creating Controllers

Select ASP.NET from left side> Select Web API Controller Class.

Creating Controllers

Give your controller name as “StudentController.cs”, By default, our Controller class will be like this
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. // For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860   
  7. namespace ASPNETCOREWEBAPI.Controllers {  
  8.     [Route("api/[controller]")]  
  9.     public class StudentController: Controller {  
  10.         // GET: api/values   
  11.         [HttpGet]  
  12.         public IEnumerable < string > Get() {  
  13.                 return new string[] {  
  14.                     "value1",  
  15.                     "value2"  
  16.                 };  
  17.             }  
  18.             // GET api/values/5   
  19.             [HttpGet("{id}")]  
  20.         public string Get(int id) {  
  21.                 return "value";  
  22.             }  
  23.             // POST api/values   
  24.             [HttpPost]  
  25.         public void Post([FromBody] string value) {}  
  26.             // PUT api/values/5   
  27.             [HttpPut("{id}")]  
  28.         public void Put(int id, [FromBody] string value) {}  
  29.             // DELETE api/values/5   
  30.             [HttpDelete("{id}")]  
  31.         public void Delete(int id) {}  
  32.     }  
  33. }  
Remove all the default methods inside our controller and change to add our code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. // For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860   
  7. namespace ASPNETCOREWEBAPI.Controllers {  
  8.         [Route("api/[controller]")]  
  9.         public class StudentController: Controller {}  
First we add in our controller class
using ASPNETCOREWEBAPI.Models;

Next we will create object for our Models.
  1. [Route("api/[controller]")]  
  2. public class StudentController: Controller {  
  3.     private List < StudentMasters > _stdInfo;  
  4. }  
  5. Adding Sample Information  
  6. Next we add few sample student information to be get from our WEB API method.  
  7.     [Route("api/[controller]")]  
  8. public class StudentController: Controller {  
  9.     private List < StudentMasters > _stdInfo;  
  10.     public StudentController() {  
  11.             InitializeData();  
  12.         }  
  13.         //To bind initial Student Information   
  14.     private void InitializeData() {  
  15.         _stdInfo = new List < StudentMasters > ();  
  16.         var studentInfo1 = new StudentMasters {  
  17.             StdName = "Shanu",  
  18.                 Phone = "+821039120700",  
  19.                 Email = "syedshanumcain@gmail.com",  
  20.                 Address = "Seoul,Korea"  
  21.         };  
  22.         var studentInfo2 = new StudentMasters {  
  23.             StdName = "Afraz",  
  24.                 Phone = "+821000000700",  
  25.                 Email = "afraz@gmail.com",  
  26.                 Address = "Madurai,India"  
  27.         };  
  28.         var studentInfo3 = new StudentMasters {  
  29.             StdName = "Afreen",  
  30.                 Phone = "+821012340700",  
  31.                 Email = "afreen@gmail.com",  
  32.                 Address = "Chennai,India"  
  33.         };  
  34.         _stdInfo.Add(studentInfo1);  
  35.         _stdInfo.Add(studentInfo2);  
  36.         _stdInfo.Add(studentInfo3);  
  37.     }  
  38. }  
WEB API Get Method
  1. Using this get method we  
  2. return all the student information as JSON result.  
  3.     [Route("api/[controller]")]  
  4. public class StudentController: Controller {  
  5.     private List < StudentMasters > _stdInfo;  
  6.     public StudentController() {  
  7.             InitializeData();  
  8.         }  
  9.         //This will return all Student Information   
  10.         [HttpGet]  
  11.     public IEnumerable < StudentMasters > GetAll() {  
  12.         return _stdInfo;  
  13.     }  
  14. }  
Here is the complete code for our controller class with both Adding sample data and using WEB API Get method.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Mvc;  
  6. using ASPNETCOREWEBAPI.Models;  
  7. // For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860   
  8. namespace ASPNETCOREWEBAPI.Controllers {.[Route("api/[controller]")]  
  9.         public class StudentController: Controller {  
  10.             private List < StudentMasters > _stdInfo;  
  11.             public StudentController() {  
  12.                     InitializeData();  
  13.                 }  
  14.                 //This will return all Student Information   
  15.                 [HttpGet]  
  16.             public IEnumerable < StudentMasters > GetAll() {  
  17.                     return _stdInfo;  
  18.                 }  
  19.                 //To bind initial Student Information   
  20.             private void InitializeData() {  
  21.                 _stdInfo = new List < StudentMasters > ();  
  22.                 var studentInfo1 = new StudentMasters {  
  23.                     StdName = "Shanu",  
  24.                         Phone = "+821039120700",  
  25.                         Email = "syedshanumcain@gmail.com",  
  26.                         Address = "Seoul,Korea"  
  27.                 };  
  28.                 var studentInfo2 = new StudentMasters {  
  29.                     StdName = "Afraz",  
  30.                         Phone = "+821000000700",  
  31.                         Email = "afraz@gmail.com", .Address = "Madurai,India".  
  32.                 };  
  33.                 var studentInfo3 = new StudentMasters {  
  34.                     StdName = "Afreen",  
  35.                         Phone = "+821012340700",  
  36.                         Email = "afreen@gmail.com",  
  37.                         Address = "Chennai,India"  
  38.                 };  
  39.                 _stdInfo.Add(studentInfo1);  
  40.                 _stdInfo.Add(studentInfo2);  
  41.                 _stdInfo.Add(studentInfo3);  
  42.             }  
  43.         }  
  44.     } // This is just a sample script. Paste your real code (javascript or HTML) here.  
  45. if ('this_is' == /an_example/) {  
  46.     of_beautifier();  
  47. else {  
  48.     var a = b ? (c % d) : e[f];  
  49. }  
Step 5 Run the application

To see the result run the application .

When we run the application by default we can see the values controller result as values
http://localhost:64764/api/values

Creating Controllers

Change the Values with our newly created controller name as student “ http://localhost:64764/api/student “.

Here now we can see all our added student information has been displayed as JSON result.

Creating Controllers

Conclusion

This will be a series of articles;  in this first series we have seen in details about our first ASP.NET Core, WEB API and Repository Class for Get method. In next series, we will see how to bind this result in our MVC View.