Automapper说Mapper.Map是过时的全局映射? [英] Automapper says Mapper.Map is obsolete, global mappings?
问题描述
我在项目中定义了一个全局Automapper配置,该配置使我可以在代码中使用Mapper.Map<targetType>(sourceObject);
. (请参见下面的配置.)
I had defined in my project a global Automapper configuration that would allow me to use Mapper.Map<targetType>(sourceObject);
in my code. (See my configuration below.)
我更新了NuGet程序包,并且看到消息Mapper.Map已过时/已弃用.我回到了GitHub上的Automapper,然后看到了这样的示例:
I updated the NuGet package, and I see the message that Mapper.Map is obsolete/depreciated. I went back to Automapper on GitHub and see examples like this:
[Test]
public void Example()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source1, SubDest1>().FixRootDest();
cfg.CreateMap<Source2, SubDest2>().FixRootDest();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var subDest1 = mapper.Map<Source1, SubDest1>(new Source1 {SomeValue = "Value1"});
var subDest2 = mapper.Map<Source2, SubDest2>(new Source2 {SomeOtherValue = "Value2"});
subDest1.SomeValue.ShouldEqual("Value1");
subDest2.SomeOtherValue.ShouldEqual("Value2");
}
我是否必须在使用映射的每种方法中创建一个配置?
Am I going to have to create a configuration in EVERY method that uses a mapping?
我当前的全局配置:
namespace PublicationSystem.App_Start
{
public class AutoMapperConfig
{
public static void CreateMaps()
{
CreateProjectMaps();
}
private static void CreateProjectMaps()
{
Mapper.CreateMap<Project, ProjectCreate>();
Mapper.CreateMap<Project, ProjectSelectable>();
//...
}
}
}
更新: :由于斯科特·张伯伦(Scott Chamberlain)的一些指导,我创建了一个像这样的课程:
UPDATE: Thanks to some coaching from Scott Chamberlain I have created a class like this:
public class MkpMapperProfile : AutoMapper.Profile
{
protected override void Configure()
{
this.CreateMap<Project, ProjectCreate>();
this.CreateMap<Project, ProjectSelectable>();
this.CreateMap<Project, ProjectDetails>();
// Many Many other maps
}
}
我在想我的BaseController类中应该有"MapperConfiguration".我开始做这样的事情:
I'm thinking I should have the 'MapperConfiguration' in my BaseController class. I started to do something like this:
public partial class BaseController : Controller
{
private MapperConfiguration mapConfig;
public BaseController()
{
db = new MkpContext();
SetMapperConfig();
}
private void SetMapperConfig()
{
mapConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile<MkpMapperProfile>();
});
}
public BaseController(MapperConfiguration config)
{
db = new MkpContext();
this.mapConfig = config;
}
}
我在正确的轨道上吗?
推荐答案
这就是我的处理方式.
在配置文件中创建地图,请谨慎使用配置文件的CreateMap方法,而不要使用Mapper的同名静态方法:
Create maps in a Profile, taking care to use the Profile's CreateMap method rather than Mapper's static method of the same name:
internal class MappingProfile : Profile
{
protected override void Configure()
{
CreateMap<Project, ProjectCreate>();
}
}
然后,无论连接了哪些依赖项(例如Global.asax或Startup),请创建一个MapperConfiguration,然后使用它来创建IMapper.
Then, wherever dependencies are wired-up (ex: Global.asax or Startup), create a MapperConfiguration and then use it to create an IMapper.
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile());
});
然后,使用配置生成IMapper:
Then, use the configuration to generate an IMapper:
var mapper = mapperConfiguration.CreateMapper();
然后,向依赖项生成器注册该映射器(我在这里使用Autofac)
Then, register that mapper with the dependency builder (I'm using Autofac here)
builder.RegisterInstance(mapper).As<IMapper>();
现在,无论您需要在哪里映射东西,都声明对IMapper的依赖:
Now, wherever you need to map stuff, declare a dependency on 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, ProjectCreate>(project);
}
}
这篇关于Automapper说Mapper.Map是过时的全局映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!