AutoMapper 依赖注入 Profile

安装 AutoMapper.Extensions.Microsoft.DependencyInjection 包

依赖注入

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
public class Startup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        // AutoMapper
        builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly(), typeof([其他需要反射的程序集的某一个class引用]).Assembly);
    }
}
using AutoMapper;
public class XXXXXXXServiceBus{
    private readonly IMapper mapper;
    public XXXXXXXServiceBus(IMapper _mapper)
    {
        mapper = _mapper;
    }
}

配置Profile

using AutoMapper;
public class AccountMappingProfile : Profile
{
    public AccountMappingProfile()
    {
        CreateMap<AccountTable, MqAccountModel>();
    }
}

使用

var mqAccountModel = mapper.Map<MqAccountModel>(accountTable);

参考:

标签: AutoMapper

评论已关闭