.NET Core 中依赖注入 AutoMapper 小记

最近在 review 代码时发现同事没有像其他项目那样使用 AutoMapper.Mapper.Initialize() 静态方法配置映射,而是使用了依赖注入 IMapper 接口的方式

services.AddSingleton<IMapper>(new Mapper(new MapperConfiguration(cfg =>
{
    cfg.CreateMap<User, MentionUserDto>();
})));

于是趁机学习了解一下,在 github 上发现了 AutoMapper.Extensions.Microsoft.DependencyInjection ,使用它只需通过 AutoMapper.Profile 配置映射

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<User, MentionUserDto>();
    }
}

然后通过 AddAutoMapper() 进行依赖注入,它会在当前程序集自动找出所有继承自 Profile 的子类添加到配置中

services.AddAutoMapper();

后来发现在使用 ProjectTo 时

.Take(10)
.ProjectTo<MentionUserDto>()
.ToListAsync(); 

发现如果自己使用 AddSingleton<IMapper>() ,会出现下面的错误(详见博问):

Mapper not initialized. Call Initialize with appropriate configuration.

使用 AddAutoMapper() 并且将 UseStaticRegistration 为 false 时也会出现同样的问题。

解决方法是给 ProjectTo 传参 _mapper.ConfigurationProvider (注:传 _mapper 不行)

.ProjectTo<MentionUserDto>(_mapper.ConfigurationProvider)

对于自己依赖注入的操作方式,后来参考  AutoMapper.Extensions.Microsoft.DependencyInjection 的实现

services.AddSingleton(config);
return services.AddScoped<IMapper>(sp => new Mapper(sp.GetRequiredService<IConfigurationProvider>(), sp.GetService));

采用了下面的方式,如果不想使用 AddAutoMapper()  通过反射自动找出 Profile ,建议使用这种方式

AutoMapper.IConfigurationProvider config = new MapperConfiguration(cfg =>
{
    cfg.AddProfile<MappingProfile>();
});
services.AddSingleton(config);
services.AddScoped<IMapper, Mapper>();
posted @ 2018-01-13 17:42  dudu  阅读(4267)  评论(1编辑  收藏  举报