2021-01-31 11:05:37 +01:00
|
|
|
# ASP .NET REST API
|
|
|
|
|
|
|
|
## Data Transfer Objects (DTOs)
|
|
|
|
|
|
|
|
A **DTO** is an object that defines how the data will be sent and recieved over the network (usually as JSON).
|
|
|
|
Without a DTO the JSON response (or reqest) could contain irrilevant, wrong or unformatted data.
|
|
|
|
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-05-21 19:23:10 +02:00
|
|
|
In `EntityDTO.cs`:
|
2021-01-31 11:05:37 +01:00
|
|
|
|
|
|
|
```cs
|
|
|
|
namespace <Namespace>.DTOs
|
|
|
|
{
|
2021-05-21 19:23:10 +02:00
|
|
|
// define the data to be serialized in JSON (differs from model)
|
|
|
|
public class EntityDTO
|
2021-01-31 11:05:37 +01:00
|
|
|
{
|
|
|
|
// only properties to be serialized
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-21 19:23:10 +02:00
|
|
|
### DTO mapping with Automapper
|
|
|
|
|
|
|
|
Required NuGet Packages:
|
|
|
|
|
|
|
|
- `AutoMapper`
|
|
|
|
- `AutoMapper.Extensions.Microsoft.DependencyInjection`
|
|
|
|
|
|
|
|
A good way to organize mapping configurations is with *profiles*.
|
|
|
|
|
2021-01-31 11:05:37 +01:00
|
|
|
In `EntitiesProfile.cs`:
|
|
|
|
|
|
|
|
```cs
|
|
|
|
using AutoMapper;
|
|
|
|
using <Namespace>.DTOs;
|
|
|
|
using <Namespace>.Model;
|
|
|
|
|
|
|
|
namespace <Namespace>.Profiles
|
|
|
|
{
|
|
|
|
public class EntitiesProfile : Profile
|
|
|
|
{
|
|
|
|
public EntitiesProfile()
|
|
|
|
{
|
2021-05-21 19:23:10 +02:00
|
|
|
CreateMap<Entity, EntityDTO>(); // map entity to it's DTO
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-21 19:23:10 +02:00
|
|
|
In `StartUp.cs`:
|
2021-01-31 11:05:37 +01:00
|
|
|
|
|
|
|
```cs
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
{
|
2021-05-21 19:23:10 +02:00
|
|
|
// other services
|
|
|
|
|
|
|
|
// let AutoMapper know in what assemblies are the profiles defined
|
|
|
|
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
|
|
|
|
|
|
|
// or create a MapperConfiguration
|
|
|
|
services.AddAutoMapper(cfg => {
|
|
|
|
cfg.CreateMap<Foo, Bar>();
|
|
|
|
cfg.AddProfile<FooProfile>();
|
|
|
|
})
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-21 19:23:10 +02:00
|
|
|
### Controller with DTOs
|
2021-01-31 11:05:37 +01:00
|
|
|
|
|
|
|
```cs
|
|
|
|
using <App>.Model;
|
|
|
|
using <App>.Repo;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace <App>.Controllers
|
|
|
|
{
|
|
|
|
[Route("api/endpoint")]
|
|
|
|
[ApiController]
|
|
|
|
public class EntitiesController : ControllerBase // MVC controller w/o view
|
|
|
|
{
|
2021-05-21 19:23:10 +02:00
|
|
|
// service or repo (DAL) injection
|
|
|
|
|
2021-01-31 11:05:37 +01:00
|
|
|
private readonly IMapper _mapper; // AutoMapper class
|
|
|
|
|
|
|
|
[HttpGet] // GET api/endpoint
|
2021-05-21 19:23:10 +02:00
|
|
|
public ActionResult<EntityDTO> SelectAllEntities()
|
2021-01-31 11:05:37 +01:00
|
|
|
{
|
2021-05-21 19:23:10 +02:00
|
|
|
...
|
2021-01-31 11:05:37 +01:00
|
|
|
|
2021-05-21 19:23:10 +02:00
|
|
|
return Ok(_mapper.Map<EntityDTO>(entity));
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-21 19:23:10 +02:00
|
|
|
## Simple API Controller
|
2021-01-31 11:05:37 +01:00
|
|
|
|
|
|
|
```cs
|
2021-05-21 19:23:10 +02:00
|
|
|
[Route("api/endpoint")]
|
|
|
|
[ApiController]
|
|
|
|
public class EntitiesController : ControllerBase
|
2021-01-31 11:05:37 +01:00
|
|
|
{
|
2021-05-21 19:23:10 +02:00
|
|
|
// service or repo (DAL) injection
|
2021-01-31 11:05:37 +01:00
|
|
|
|
2021-05-21 19:23:10 +02:00
|
|
|
[HttpGet]
|
|
|
|
public ActionResult<TEntity> SelectAll()
|
|
|
|
{
|
|
|
|
...
|
|
|
|
return Ok(entity);
|
2021-01-31 11:05:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|