2021-01-31 11:05:37 +01:00
|
|
|
# ASP .NET REST API
|
|
|
|
|
|
|
|
## Data Transfer Objects (DTOs)
|
|
|
|
|
2021-09-20 19:35:32 +02:00
|
|
|
A **DTO** is an object that defines how the data will be sent and received over the network (usually as JSON).
|
|
|
|
Without a DTO the JSON response (or request) could contain irrelevant, wrong or unformatted data.
|
2021-01-31 11:05:37 +01:00
|
|
|
Moreover, by decoupling the JSON response from the actual data model, it's possible to change the latter without breaking the API.
|
|
|
|
DTOs must be mapped to the internal methods.
|
|
|
|
|
2021-10-04 22:37:01 +02:00
|
|
|
Required NuGet Packages:
|
|
|
|
|
|
|
|
- AutoMapper.Extensions.Microsoft.DependencyInjection
|
|
|
|
|
|
|
|
In `StartUp.cs`:
|
|
|
|
|
|
|
|
```cs
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
|
|
// ...
|
|
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
|
|
|
// other services
|
|
|
|
|
|
|
|
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); // set automapper service
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
In `EntityDTO.cs`:
|
2021-01-31 11:05:37 +01:00
|
|
|
|
|
|
|
```cs
|
|
|
|
namespace <Namespace>.DTOs
|
|
|
|
{
|
2021-10-04 22:37:01 +02:00
|
|
|
// define the data to be serialized in JSON (can differ from model)
|
2021-11-09 14:51:42 +01:00
|
|
|
public class EntityDTO
|
2021-01-31 11:05:37 +01:00
|
|
|
{
|
|
|
|
// only properties to be serialized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
In `EntitiesProfile.cs`:
|
|
|
|
|
|
|
|
```cs
|
|
|
|
using AutoMapper;
|
|
|
|
using <Namespace>.DTOs;
|
|
|
|
using <Namespace>.Model;
|
|
|
|
|
|
|
|
namespace <Namespace>.Profiles
|
|
|
|
{
|
|
|
|
public class EntitiesProfile : Profile
|
|
|
|
{
|
|
|
|
public EntitiesProfile()
|
|
|
|
{
|
2021-11-09 14:51:42 +01:00
|
|
|
CreateMap<Entity, EntityDTO>(); // map entity to it's DTO
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-09-19 12:10:20 +02:00
|
|
|
## Controller (No View)
|
|
|
|
|
2021-01-31 11:05:37 +01:00
|
|
|
```cs
|
2021-11-09 14:51:42 +01:00
|
|
|
[Route("api/endpoint")]
|
|
|
|
[ApiController]
|
|
|
|
public class EntitiesController : ControllerBase // MVC controller w/o view
|
2021-01-31 11:05:37 +01:00
|
|
|
{
|
2021-11-09 14:51:42 +01:00
|
|
|
private readonly ICommandRepo _repo;
|
|
|
|
private readonly IMapper _mapper; // AutoMapper class
|
2021-01-31 11:05:37 +01:00
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
public EntitiesController(IEntityRepo repository, IMapper mapper)
|
2021-01-31 11:05:37 +01:00
|
|
|
{
|
2021-11-09 14:51:42 +01:00
|
|
|
_repo = repository;
|
|
|
|
_mapper = mapper
|
|
|
|
}
|
2021-01-31 11:05:37 +01:00
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
[HttpGet] // GET api/endpoint
|
|
|
|
public ActionResult<IEnumerable<EntityDTO>> SelectAllEntities()
|
|
|
|
{
|
|
|
|
var results = _repo.SelectAll();
|
2021-10-04 22:37:01 +02:00
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
return Ok(_mapper.Map<EntityDTO>(results));
|
|
|
|
}
|
2021-10-04 22:37:01 +02:00
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
[HttpGet("{id}")] // GET api/endpoint/{id}
|
|
|
|
public ActionResult<EntityDTO> SelectOneEntityById(int id)
|
|
|
|
{
|
|
|
|
var result = _repo.SelectOneById(id);
|
2021-10-04 22:37:01 +02:00
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
if(result != null)
|
2021-01-31 11:05:37 +01:00
|
|
|
{
|
2021-11-09 14:51:42 +01:00
|
|
|
// transform entity to it's DTO
|
|
|
|
return Ok(_mapper.Map<EntityDTO>(result));
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
2021-11-09 14:51:42 +01:00
|
|
|
|
|
|
|
return NotFound();
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-10-04 22:37:01 +02:00
|
|
|
## Controller (With View)
|
2021-01-31 11:05:37 +01:00
|
|
|
|
|
|
|
```cs
|
2021-11-09 14:51:42 +01:00
|
|
|
[Route("api/endpoint")]
|
|
|
|
[ApiController]
|
|
|
|
public class EntitiesController : Controller
|
2021-10-04 22:37:01 +02:00
|
|
|
{
|
2021-11-09 14:51:42 +01:00
|
|
|
private readonly AppDbContext _db;
|
2021-10-04 22:37:01 +02:00
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
public EntitiesController(AppDbContext db)
|
|
|
|
{
|
|
|
|
_db = db;
|
|
|
|
}
|
2021-10-04 22:37:01 +02:00
|
|
|
|
2021-11-09 14:51:42 +01:00
|
|
|
[HttpGet]
|
|
|
|
public IActionResult SelectAll()
|
|
|
|
{
|
|
|
|
return Json(new { data = _db.Entities.ToList() }); // json view
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|