Skip to content

Instantly share code, notes, and snippets.

@Whistler092
Last active May 9, 2021 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Whistler092/0870c00fe99e3e653ee977891f9df653 to your computer and use it in GitHub Desktop.
Save Whistler092/0870c00fe99e3e653ee977891f9df653 to your computer and use it in GitHub Desktop.
Configurar AutoMapper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using peliculasAPI.DTOs;
using peliculasAPI.Entidades;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace peliculasAPI.Controllers
{
[Route("api/generos")]
[ApiController]
public class GenerosController : ControllerBase
{
private readonly ILogger<GenerosController> logger;
private readonly ApplicationDbContext context;
private readonly IMapper mapper;
public GenerosController(ILogger<GenerosController> logger, ApplicationDbContext context, IMapper mapper)
{
this.logger = logger;
this.context = context;
this.mapper = mapper;
}
[HttpGet]
public async Task<ActionResult<List<GeneroDTO>>> Get()
{
var generos = await context.Generos.ToListAsync();
return mapper.Map<List<GeneroDTO>>(generos);
}
}
}
using System;
using AutoMapper;
using peliculasAPI.DTOs;
using peliculasAPI.Entidades;
namespace API.Utilidades
{
public class AutoMapperProfiles : Profile
{
public AutoMapperProfiles()
{
CreateMap<Genero, GeneroDTO>();
// CreateMap<Genero, GeneroDTO>().ReverseMap();
}
}
}
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAutoMapper(typeof(Startup));
/// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment