This is the Part-3 of our article. If you haven't read part-2 yet then click Here to go.
In Part-2, We created the API project and used Entity Framework (Code First Approach). Now we need a mechanism to access the context from our API. Directly accessing the context methods from the API controller is a bad practice and we should avoid that. We will inject interface into our API Controller and API will communicate with the data context using the interface. Now follow the steps to do this.
Step 1: Create a new folder under the Models folder and name it Repository and Create a new Interface under the repository folder and called it IStudentRepository.
Copy the below code and change this code as per your requirement:
Step 2: Now create a new folder under the Models folder, called it DataManager and then create a new class under this folder named it StudentManager. This class will implement IStudentRepository interface.
copy the below code and change it as per your requirement:
GetAll(): Used to gets all the students from DB.
Get(): used to Get a specific student by passing GUID of student
Add(): used to create a new student in DB.
Delete(): used o delete the student from DB.
Update(): used to update student information.
Here we not directly accessing the context file in controller. Now this class(student class) which access the context file will perform all the CRUD operations instead of API controller.
Strep 3: Go to Startup.cs file. Here we will configure the repository using dependency injection. This can be done in the ConfigureServices method in the Startup.cs.
Step 4: Now we will Create an API controller for Student.
Go to controller folder and create a API controller in it.
Now copy the blow code and change it as per your requirement.
Now do your testing.
We have Successfully performed CRUD operations in .NET CORE API.
In this article, I will explain you how to create a WEB API in .NET Core and also perform CRUD operations.
In Part-2, We created the API project and used Entity Framework (Code First Approach). Now we need a mechanism to access the context from our API. Directly accessing the context methods from the API controller is a bad practice and we should avoid that. We will inject interface into our API Controller and API will communicate with the data context using the interface. Now follow the steps to do this.
Step 1: Create a new folder under the Models folder and name it Repository and Create a new Interface under the repository folder and called it IStudentRepository.
Copy the below code and change this code as per your requirement:
using System;
using System.Linq;
namespace NET_Core_API.Models.Repository
{
public interface IStudentRepository<TEntity>
{
IQueryable GetAll();
TEntity Get(Guid id);
TEntity Add(TEntity entity);
void Update(TEntity entityToUpdate, TEntity entity);
void Delete(TEntity entity);
}
}
using System.Linq;
namespace NET_Core_API.Models.Repository
{
public interface IStudentRepository<TEntity>
Step 2: Now create a new folder under the Models folder, called it DataManager and then create a new class under this folder named it StudentManager. This class will implement IStudentRepository interface.
copy the below code and change it as per your requirement:
using NET_Core_API.Models.Repository;
using System;
using System.Linq;
namespace NET_Core_API.Models.DataManager
{
public class StudentManager : IStudentRepository<Student>
{
readonly EntityContext _Context;
public StudentManager(EntityContext context)
{
_Context = context;
}
public IQueryable GetAll()
{
return _Context.Students;
}
public Student Get(Guid id)
{
return _Context.Students.FirstOrDefault(e => e.ID == id);
}
public Student Add(Student entity)
{
_Context.Students.Add(entity);
_Context.SaveChanges();
return entity;
}
public void Update(Student studentToUpdate, Student newStudent)
{
studentToUpdate.FirstName = newStudent.FirstName;
studentToUpdate.LastName = newStudent.LastName;
_Context.SaveChanges();
}
public void Delete(Student student)
{
_Context.Students.Remove(student);
_Context.SaveChanges();
}
}
}
using System;
using System.Linq;
namespace NET_Core_API.Models.DataManager
{
public class StudentManager : IStudentRepository<Student>
GetAll(): Used to gets all the students from DB.
Get(): used to Get a specific student by passing GUID of student
Add(): used to create a new student in DB.
Delete(): used o delete the student from DB.
Update(): used to update student information.
Here we not directly accessing the context file in controller. Now this class(student class) which access the context file will perform all the CRUD operations instead of API controller.
Strep 3: Go to Startup.cs file. Here we will configure the repository using dependency injection. This can be done in the ConfigureServices method in the Startup.cs.
services.AddScoped<IStudentRepository<Student>, StudentManager>();
Step 4: Now we will Create an API controller for Student.
Go to controller folder and create a API controller in it.
Now copy the blow code and change it as per your requirement.
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using NET_Core_API.Models;
using NET_Core_API.Models.Repository;
namespace NET_Core_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
private readonly IStudentRepository<Student> _dataRepository;
public StudentController(IStudentRepository<Student> dataRepository)
{
_dataRepository = dataRepository;
}
// GET: api/Student
[HttpGet]
public IActionResult Get()
{
IQueryable Students = _dataRepository.GetAll();
return Ok(Students);
}
// GET: api/Student/{GUID}
[HttpGet("{id}")]
public IActionResult Get(Guid id)
{
Student Student = _dataRepository.Get(id);
if (Student == null)
{
return NotFound("The Student record couldn't be found.");
}
return Ok(Student);
}
// POST: api/Student
[HttpPost]
public IActionResult Post([FromBody] Student Student)
{
if (Student == null)
{
return BadRequest("Student is null.");
}
Student student = _dataRepository.Add(Student);
return Ok(student);
}
// PUT: api/Student/{GUID}
[HttpPut("{id}")]
public IActionResult Put(Guid id, [FromBody] Student Student)
{
if (Student == null)
{
return BadRequest("Student is null.");
}
Student StudentToUpdate = _dataRepository.Get(id);
if (StudentToUpdate == null)
{
return NotFound("The Student record couldn't be found.");
}
_dataRepository.Update(StudentToUpdate, Student);
return NoContent();
}
// DELETE: api/Student/{GUID}
[HttpDelete("{id}")]
public IActionResult Delete(Guid id)
{
Student Student = _dataRepository.Get(id);
if (Student == null)
{
return NotFound("The Student record couldn't be found.");
}
_dataRepository.Delete(Student);
return NoContent();
}
}
}
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using NET_Core_API.Models;
using NET_Core_API.Models.Repository;
namespace NET_Core_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
private readonly IStudentRepository<Student>
Now do your testing.
We have Successfully performed CRUD operations in .NET CORE API.
1 comment:
Distributor Locations in Mumbai
Distributors Profile in Delhi
Distributor Lists in Gurugram
Geo-locations in Gurugram
Post a Comment