NETCORE - AutoMappper 对象映射
NETCORE - AutoMappper 对象映射
创建项目 .net6 web api :NETCORE.TAutoMappper
引用 nuget 包 :
AutoMapper.Extensions.Microsoft.DependencyInjection
创建两个类: model 与 dto 的映射
namespace NETCORE.TAutoMappper { public class StudentModel { public int Id { get; set; } public string Name { get; set; } public int StudentId { get; set; } public int age { get; set; } } public class StudentDto { public int Id { get; set; } public string Name { get; set; } public bool? gender { get; set; } } }
创建 MapperProfile.cs 类
using AutoMapper; using static NETCORE.TAutoMappper.MapperProfile; namespace NETCORE.TAutoMappper { //要继承AutoMapper的Profile public class MapperProfile : Profile { //构造函数 public MapperProfile() { //这里写映射规则 CreateMap<StudentDto, StudentModel>(); CreateMap<StudentModel, StudentDto>(); } } }
Program.cs里添加 AutoMapper
builder.Services.AddAutoMapper(typeof(MapperProfile));
在需要使用的类的构造函数中,注入AutoMapper
创建 webapi 控制器:ValuesController.cs
using AutoMapper; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace NETCORE.TAutoMappper.Controllers { [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { public IMapper Mapper; public ValuesController(IMapper mapper) { Mapper = mapper; } [HttpGet] [Route("testc")] public void testc() { var studentDto = new StudentDto() { Id = 1, Name = "张三", gender = false }; var studentModel = Mapper.Map<StudentModel>(studentDto); var sModel = new StudentModel() { Id = 2, Name = "李四", age = 22 }; var sDto = Mapper.Map<StudentDto>(sModel); } } }
自定义映射,修改名称
样例:如需把 Dto 映射到 model
public class cModel { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } public class cDto { public int Id { get; set; } public string FullName { get; set; } public string Description { get; set; } }
配置映射关系
public class MapperProfile : Profile { public MapperProfile() { //这里写映射规则 CreateMap<cDto, cModel>().ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.FullName)); } }
测试
var dto = new cDto() { Id = 3, FullName = "张三", Description = "cccc" }; var model = Mapper.Map<cModel>(dto);
复杂映射
样例类:
public partial class Sence { public Sence() { Tilesets = new HashSet<Tileset>(); } public long Id { get; set; }
public string? Name { get; set; }
public string? TileSetUrlCesium { get; set; }
public virtual ICollection<Tileset> Tilesets { get; set; } }
public partial class Tileset { public long Id { get; set; } public string? Url { get; set; } }
public class SenceInfo { public SenceInfo() { tileSetInfos = new List<TileSetInfo>(); } public long Id { get; set; } public string Name { get; set; } public List<string?> TileSetUrlCesium { get; set; }public List<TileSetInfo> tileSetInfos { get; set; } } public class TileSetInfo { public long Id { get; set; } public string? Url { get; set; } public string? Group { get; set; } }
映射配置
public MapperProfile() { //这里写映射规则 CreateMap<Sence, SenceInfo>().ForMember(dest => dest.tileSetInfos, opt => opt.MapFrom(src => src.Tilesets)) .AfterMap((source, dest) => { dest.TileSetUrlCesium = source.Tilesets.Select(t => t.Url).Distinct().ToList(); }); CreateMap<Tileset, TileSetInfo>(); }
使用:
var resData = Mapper.Map<SenceInfo>(model);
映射时传入数据
映射:源(a_notice_renew),结果(ResNoticeRenewInfoUse)
如结果类中有isRead字段,需要在映射时写入
CreateMap<a_notice_renew, ResNoticeRenewInfoUse>().ForMember(dest => dest.isRead, opt => opt.MapFrom((src, dest, destMember, context) => context.Items["isRead"]));
映射代码
var dto = Mapper.Map<a_notice_renew, ResNoticeRenewInfoUse>(arr[0], opt => opt.AfterMap((src, dest) => dest.isRead = src.cognition.Contains("")));
引用:https://www.dongchuanmin.com/net/1398.html