Automapper 说 Mapper.Map 已过时,全局映射?

Automapper says Mapper.Map is obsolete, global mappings?

我在我的项目中定义了一个全局 Automapper 配置,它允许我在我的代码中使用 Mapper.Map<targetType>(sourceObject);。 (请参阅下面我的配置。)

我更新了 NuGet 包,我看到 Mapper.Map 是 obsolete/depreciated 的消息。我在 GitHub 回到 Automapper,看到这样的例子:

[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");
}

我是否必须在每个使用映射的方法中创建一个配置?

我当前的全局配置:

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 的一些指导,我创造了一个 class 这样的:

    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 class 中应该有 'MapperConfiguration'。我开始做这样的事情:

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;
    }
}

我走在正确的轨道上吗?

我就是这样处理的。

在 Profile 中创建地图,注意使用 Profile 的 CreateMap 方法而不是 Mapper 的同名静态方法:

internal class MappingProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<Project, ProjectCreate>();
    }
}

然后,在连接依赖项的任何地方(例如: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>();

现在,在任何需要映射的地方,声明对 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);
    }
}

您可以在我的 ASP.NET MVC 模板项目中找到已配置的 AutoMapper 4.2:https://github.com/NikolayIT/ASP.NET-MVC-Template

  1. 创建这些 类:https://github.com/NikolayIT/ASP.NET-MVC-Template/tree/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web.Infrastructure/Mapping

  2. IMapFrom<> 注释视图模型:https://github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20Core/Web/AspNetCoreTemplate.Web/ViewModels/Settings/SettingViewModel.cs

  3. 用作.To<SomeViewModel>()。示例:https://github.com/NikolayIT/ASP.NET-MVC-Template/blob/master/ASP.NET%20MVC%205/Web/MvcTemplate.Web/Controllers/HomeController.cs#L27

这是 AutoMapper 4.2 中的新功能。 Jimmy Bogard 关于此的博客 post:Removing the static API from AutoMapper。它声称

The IMapper interface is a lot lighter, and the underlying type is now just concerned with executing maps, removing a lot of the threading problems...

新语法:(从博客post粘贴)

var config = new MapperConfiguration(cfg => {
  cfg.CreateMap<User, UserDto>();
});

如果您只是想要 "old way" 这样做。最新的 版本 4.2.1 带回了一些传统。只需使用

CreateMap<Project, ProjectCreate>();

而不是

Mapper.CreateMap<Project, ProjectCreate>();

旧代码可以正常工作。

Mapper.Initialize(cfg => {
    cfg.CreateMap<Source, Dest>();
});

我使用的是 5.2.0 版本,支持在构造函数中创建映射而不是覆盖配置。

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Project, ProjectDto>();
    }
}

在Global.asax.cs中调用如下:

Mapper.Initialize(c=>c.AddProfile<MappingProfile>());

希望对您有所帮助。