手动添加映射
在 Controllers
目录下找到所需要添加映射的 xxxcontroller.cs
文件,然后手动添加数据的映射:
using System;
using System.Linq;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using TestWebAPI.DTOs;
using TestWebAPI.Services;
namespace TestWebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TouristRoutesController : Controller
{
private ITouristRouteRepository _touristRouteRepository; // 创建数据仓库的私有变量_
public TouristRoutesController(ITouristRouteRepository touristRouteRepository) // 注入旅游仓库的实例
{
_touristRouteRepository = touristRouteRepository; // 给私有仓库赋值
}
[HttpGet("{touristRouteId:Guid}")]
public IActionResult GetTouristRouteById(Guid touristRouteId) // 单一
{
var touristRouteFromRepo = _touristRouteRepository.GetTouristRoute(touristRouteId);
if (touristRouteFromRepo == null)
{
return NotFound($"旅游路线{touristRouteId}找不到");
}
// 关键代码*************
// ↑ 从数据库拿到数据后
// 对数据做一个从model->DTO的映射
var touristRouteDto = new TouristRouteDto() // 只给用户显示我们想要显示的字段
{
Id = touristRouteFromRepo.Id,
Title = touristRouteFromRepo.Title,
Description = touristRouteFromRepo.Description,
Price = touristRouteFromRepo.OriginalPrice * (decimal) (touristRouteFromRepo.DiscountPresent ?? 1),
CreateTime = touristRouteFromRepo.CreateTime,
UpdateTime = touristRouteFromRepo.UpdateTime,
Features = touristRouteFromRepo.Features,
Fees = touristRouteFromRepo.Fees,
Notes = touristRouteFromRepo.Notes,
Rating = touristRouteFromRepo.Rating,
TravelDays = touristRouteFromRepo.TravelDays.ToString(),
TripType = touristRouteFromRepo.TripType.ToString(),
DepartureCity = touristRouteFromRepo.DepartureCity.ToString()
};
//*************
// return Ok(touristRouteFromRepo);
return Ok(touristRouteDto);
}
}
}
自动添加映射
自动映射数据模型和DTO,我们需要用到AutoMapper这个包
-
安装AutoMapper包(基于dotnet可以衔接IOC反转容器),不要安装错了:
-
依赖注入:在
Startup.cs
的ConfigureServices
方法中注册AutoMapper
的服务依赖(把AutoMapper的服务依赖注册进入系统的IOC容器中)
// 自动扫描程序集里所有包含映射关系的profile文件(profile:映射配置)
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
- 在
profiles
目录下新建一个xxxProfile.cs
的文件:
using AutoMapper;
using TestWebAPI.DTOs;
using TestWebAPI.Models;
namespace TestWebAPI.Profiles
{
/// <summary>
/// 映射配置
/// </summary>
public class TouristRouteProfile : Profile
{
/// <summary>
/// 添加构建函数
/// </summary>
public TouristRouteProfile()
{
CreateMap<TouristRoute, TouristRouteDto>() // 创建一个映射
.ForMember(
dest => dest.Price,
opt => opt.MapFrom(src => src.OriginalPrice * (decimal) (src.DiscountPresent ?? 1))
)
.ForMember(
dest => dest.TravelDays,
opt => opt.MapFrom(src => src.TravelDays.ToString())
)
.ForMember(
dest => dest.TripType,
opt => opt.MapFrom(src => src.TripType.ToString())
)
.ForMember(
dest => dest.DepartureCity,
opt => opt.MapFrom(src => src.DepartureCity.ToString())
);
}
}
}
- 修改对应
xxxcontroller.cs
文件:给控制器注入AutoMapper的服务依赖
using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using TestWebAPI.DTOs;
using TestWebAPI.Services;
namespace TestWebAPI.Controllers
{
[Route("api/[controller]")] // api/touristroute
[ApiController]
public class TouristRoutesController : Controller
{
private ITouristRouteRepository _touristRouteRepository;
private readonly IMapper _mapper;
public TouristRoutesController(ITouristRouteRepository touristRouteRepository, IMapper mapper)
{
_touristRouteRepository = touristRouteRepository;
_mapper = mapper;
}
[HttpGet("{touristRouteId:Guid}")]
public IActionResult GetTouristRouteById(Guid touristRouteId)
{
var touristRouteFromRepo = _touristRouteRepository.GetTouristRoute(touristRouteId);
if (touristRouteFromRepo == null)
{
return NotFound($"旅游路线{touristRouteId}找不到");
}
// ↑ 从数据库拿到数据后
// 对数据做一个从model->DTO的映射
var touristRouteDto = _mapper.Map<TouristRouteDto>(touristRouteFromRepo);
return Ok(touristRouteDto);
}
}
}
标签:src,映射,touristRouteRepository,touristRouteFromRepo,Controller,AutoMapper,using,N
From: https://www.cnblogs.com/huangkenicole/p/16770857.html