码迷,mamicode.com
首页 > 移动开发 > 详细

.NET Core 中依赖注入 AutoMapper 小记

时间:2018-01-13 20:43:45      阅读:1679      评论:0      收藏:0      [点我收藏+]

标签:enc   ide   tom   pen   list   single   ext   问题   creat   

最近在 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>();

.NET Core 中依赖注入 AutoMapper 小记

标签:enc   ide   tom   pen   list   single   ext   问题   creat   

原文地址:https://www.cnblogs.com/dudu/p/8279114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!