使用AutoMapper自动映射

安装Nuget包 通过.Net CLI安装 dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection --version 8.1.0 该包会自动安装AutoMapper的依赖包。 配置AutoMapper 在Startup.cs的ConfigureServices方法中配置: services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //也可以如下指定项目: //services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.Location.Contains("ExampleSolution.Model"))); 接下来在ConfigureServices中添加: services.AddAutoMapperProfiles(Configuration); 然后为每个需要映射的类之间添加配置,如: public class ExmapleProfile : Profile { public ExmapleProfile() { //这里的映射方向是单向的,如不需要从ExmapleViewModel转Exmaple,则第二条就可以不用写 CreateMap<Exmaple, ExmapleViewModel>(); CreateMap<ExmapleViewModel, Exmaple>(); } } 使用 通过构造函数注入: private readonly IMapper _mapper; public ExmapleController(IMapper mapper) { _mapper = mapper; } 使用: public ActionResult ExmapleMethod() { ...... Exmaple entity = new Exmaple(); ExmapleViewModel entityVM = _mapper.Map<ExmapleViewModel>(entity); ...... }

January 15, 2021 · 1 分钟 · Remo