標籤: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 小記