Automapper说Mapper.Map是过时的全局映射?
我在我的项目中定义了一个全局Automapper配置,允许我使用Mapper.Map(sourceObject);
在我的代码中。 (参见下面的配置。)
我更新了NuGet包,我看到Mapper.Map已经过时/折旧的消息。 我在GitHub上回到Automapper,看到这样的例子:
[Test] public void Example() { var config = new MapperConfiguration(cfg => { cfg.CreateMap().FixRootDest(); cfg.CreateMap().FixRootDest(); }); config.AssertConfigurationIsValid(); var mapper = config.CreateMapper(); var subDest1 = mapper.Map(new Source1 {SomeValue = "Value1"}); var subDest2 = mapper.Map(new Source2 {SomeOtherValue = "Value2"}); subDest1.SomeValue.ShouldEqual("Value1"); subDest2.SomeOtherValue.ShouldEqual("Value2"); }
我是否必须在使用映射的EVERY方法中创建配置?
我目前的全局配置:
namespace PublicationSystem.App_Start { public class AutoMapperConfig { public static void CreateMaps() { CreateProjectMaps(); } private static void CreateProjectMaps() { Mapper.CreateMap(); Mapper.CreateMap(); //... } } }
更新:感谢Scott Chamberlain的一些指导,我创建了一个这样的课程:
public class MkpMapperProfile : AutoMapper.Profile { protected override void Configure() { this.CreateMap(); this.CreateMap(); this.CreateMap(); // Many Many other maps } }
我想我应该在我的BaseController类中使用’MapperConfiguration’。 我开始做这样的事情:
public partial class BaseController : Controller { private MapperConfiguration mapConfig; public BaseController() { db = new MkpContext(); SetMapperConfig(); } private void SetMapperConfig() { mapConfig = new MapperConfiguration(cfg => { cfg.AddProfile(); }); } public BaseController(MapperConfiguration config) { db = new MkpContext(); this.mapConfig = config; } }
我是在正确的轨道上吗?
这就是我处理它的方式。
在Profile中创建地图,注意使用Profile的CreateMap方法而不是Mapper的同名静态方法:
internal class MappingProfile : Profile { protected override void Configure() { CreateMap(); } }
然后,无论何处连接依赖项(例如:Global.asax或Startup),创建MapperConfiguration,然后使用它来创建IMapper。
var mapperConfiguration = new MapperConfiguration(cfg => { cfg.AddProfile(new MappingProfile()); });
然后,使用配置生成IMapper:
var mapper = mapperConfiguration.CreateMapper();
然后,使用依赖关系构建器注册该映射器(我在这里使用Autofac)
builder.RegisterInstance(mapper).As();
现在,无论你需要映射什么东西,都要声明对IMapper的依赖:
internal class ProjectService : IProjectService { private readonly IMapper _mapper; public ProjectService(IMapper mapper) { _mapper = mapper; } public ProjectCreate Get(string key) { var project = GetProjectSomehow(key); return _mapper.Map(project); } }
我正在使用5.2.0版,支持在构造函数中创建映射而不是覆盖配置。
public class MappingProfile : Profile { public MappingProfile() { CreateMap(); } }
在Global.asax.cs中调用如下:
Mapper.Initialize(c=>c.AddProfile());
希望这有帮助。
这是AutoMapper 4.2中的新function。 有关Jimmy Bogard的博客文章: 从AutoMapper中删除静态API 。 它声称
IMapper接口要轻得多,底层类型现在只关心执行映射,消除了很multithreading问题……
新语法:(从博客文章粘贴)
var config = new MapperConfiguration(cfg => { cfg.CreateMap(); });
如果你只是想要这样做的“老方法”。 最新版本4.2.1带来了一些传统。 只是用
CreateMap();
代替
Mapper.CreateMap();
旧代码可以正常工作。
您可以在我的ASP.NET MVC模板项目中找到已配置的AutoMapper 4.2: https : //github.com/NikolayIT/ASP.NET-MVC-Template
-
创建这些类: https : //github.com/NikolayIT/ASP.NET-MVC-Template/tree/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web.Infrastructure/Mapping
-
使用
IMapFrom<>
注释视图模型: https : //github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20Core/Web/AspNetCoreTemplate.Web/ViewModels/Settings/SettingViewModel.cs -
将它用作
.To
。 示例: https : //github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web/Controllers/HomeController.cs#L27()
Mapper.Initialize(cfg => { cfg.CreateMap