Skip to content

配置AutoMapper

前置条件:增加Profile文件

根目录下增加AutoMapperProfile文件

c#
namespace JingJian.Package.SimpleAService
{
    public class AutoMapperProfile : Profile
    {
        public AutoMapperProfile()
        {
            CreateMap<BasicEmployeeFunction, BasicEmployeeFunctionDto>();
            CreateMap<BasicEmployeeFunctionDto, BasicEmployeeFunction>();
        }
    }
}

方案一 注入方式

c#
private readonly IMapper _mapper;
public TestController(IMapper mapper)
{
    _mapper = mapper;
}

[HttpGet("dto")]
public async object TestAutoMapper()
{
	return _mapper.Map<BasicEmployeeFunctionDto>(entity);
}

方案二 扩展方式

c#
[HttpGet("dto")]
public async Task<object> TestAutoMapper()
{
    BasicEmployeeFunction entity = new();
    entity.FunctionName = "测试功能";

    var dtos = entity.ToMapper<BasicEmployeeFunctionDto>();

    return await Task.FromResult(dtos);
}

广州宝点数字化科技