WEB API Three-tier architecture.
Install :
1. Microsoft.EntityFrameworkCore(3.1.25)
2. Microsoft.EntityFrameworkCore.SqlServer(3.1.25)
3. Microsoft.EntityFrameworkCore.Tools(3.1.25)
To Scaffold one table
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir -t table-name
Scaffold-DbContext "Data Source=DESKTOP-56FAU8K\SQLEXPRESS;Database=DBTest;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Repository/Models
DESKTOP-56FAU8K\SQLEXPRESS
Create a person controller
using Data_Access_Layer.Repository.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Web_Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class PersonController : ControllerBase
{
private Business_Logic_Layer.PersonBLL _BLL;
public PersonController()
{
_BLL =new Business_Logic_Layer.PersonBLL();
}
[HttpGet]
[Route("getPersons")]
public List<Person> GetAllperson()
{
return _BLL.GetAllpersons();
}
[HttpGet]
[Route("getPersonById")]
public ActionResult<Person> GetPersonById(int id)
{
var Person = _BLL.GetPersonById(id);
if (Person == null)
{
return NotFound("Data not found");
}
return Ok(Person);
}
[Route("postPerson")]
[HttpPost]
public void postPerson([FromBody] Person p)
{
_BLL.PostPerson(p);
}
[Route("deletePerson")]
[HttpDelete]
public void deletePerson(int id)
{
_BLL.DeletePerson(id);
}
}
}
---------------------------------------------------------Business_Logic_Layer-------------------------------
Connect with data access layer:
using Data_Access_Layer.Repository.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
namespace Business_Logic_Layer
{
public class PersonBLL
{
private Data_Access_Layer.PersonDAL _DAL;
public PersonBLL()
{
_DAL = new Data_Access_Layer.PersonDAL();
}
public List<Person> GetAllpersons()
{
return _DAL.GetAllperson();
}
public Person GetPersonById(int id)
{
var data= _DAL.GetPersonById(id);
return data;
}
public void PostPerson( Person p)
{
_DAL.postPerson(p);
}
public void DeletePerson(int id)
{
_DAL.deletePerson(id);
}
}
}
---------------------------------------Data_Access_Layer----------------------------------------------------
Create a Repository folder and create a Model folder then use Scaffold
To Scaffold one table
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir -t table-name
Scaffold-DbContext "Data Source=DESKTOP-56FAU8K\SQLEXPRESS;Database=DBTest;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Repository/Models
DESKTOP-56FAU8K\SQLEXPRESS
using System;
using System.Collections.Generic;
// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disable
namespace Data_Access_Layer.Repository.Models
{
public partial class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public DateTime DateOfBirth { get; set; }
}
}
--------------------------------------------------All code---------------------------------------
Person controller:
using Data_Access_Layer.Repository.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Web_Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class PersonController : ControllerBase
{
private Business_Logic_Layer.PersonBLL _BLL;
public PersonController()
{
_BLL =new Business_Logic_Layer.PersonBLL();
}
[HttpGet]
[Route("getPersons")]
public List<Person> GetAllperson()
{
return _BLL.GetAllpersons();
}
[HttpGet]
[Route("getPersonById")]
public ActionResult<Person> GetPersonById(int id)
{
var Person = _BLL.GetPersonById(id);
if (Person == null)
{
return NotFound("Data not found");
}
return Ok(Person);
}
[Route("postPerson")]
[HttpPost]
public void postPerson([FromBody] Person p)
{
_BLL.PostPerson(p);
}
[Route("deletePerson")]
[HttpDelete]
public void deletePerson(int id)
{
_BLL.DeletePerson(id);
}
}
}
------------------------------------------Business_Logic_Layer-------------------------------
using Data_Access_Layer.Repository.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
namespace Business_Logic_Layer
{
public class PersonBLL
{
private Data_Access_Layer.PersonDAL _DAL;
public PersonBLL()
{
_DAL = new Data_Access_Layer.PersonDAL();
}
public List<Person> GetAllpersons()
{
return _DAL.GetAllperson();
}
public Person GetPersonById(int id)
{
var data= _DAL.GetPersonById(id);
return data;
}
public void PostPerson( Person p)
{
_DAL.postPerson(p);
}
public void DeletePerson(int id)
{
_DAL.deletePerson(id);
}
}
}
----------------------------------Data_Access_Layer----------------------------------------
---------------------------using post man-------------------------
Get details by id
0 Comments