【问题标题】:Automapper 5.0 global configurationAutomapper 5.0 全局配置
【发布时间】:2016-05-20 14:04:42
【问题描述】:

我在App_Start 文件夹中的AutoMapperConfig.cs 中使用以下代码。我 在Global.asax 中将其初始化为AutoMapperConfiguration.Configure()

但我无法在我的 控制器。它抛出一个没有定义映射的异常。它 在支持的 Automapper 的早期版本中工作 Mapper.CreateMap<> 方法。我很困惑如何使用 MapperConfiguration 实例。

public static class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(
            cfg =>
                {
                    cfg.AddProfile<HospitalProfile>();
                }
        );
        Mapper.AssertConfigurationIsValid();
    }
}

public class HospitalProfile : Profile
{
    protected override void Configure()
    {
        var config = new MapperConfiguration(
            cfg =>
                {
                    cfg.CreateMap<Hospital, MongoHospital>()
                        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.ToString()));
                });
        config.CreateMapper();
    }
}

尝试如下访问此地图

Mapper.Map<IEnumerable<Hospital>, IEnumerable<MongoHospital>>(hospitalsOnDB);

【问题讨论】:

  • 您的配置文件应该调用 CreateMap,而不是创建整个配置对象。
  • MapperConfiguration 有什么用?它在哪里完美契合?

标签: c# model-view-controller automapper


【解决方案1】:

在这种情况下您真的需要使用个人资料吗?如果你不这样做,你可以尝试像这样初始化 Mapper:

public static class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(
            config =>
            {
                config.CreateMap<Hospital, MongoHospital>()
                    .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.ToString()));
            });
    }
}

但是,如果您仍想注册个人资料,您可以这样做:

public static class AutoMapperConfiguration
{
    public static void Configure()
    {
        Mapper.Initialize(
            cfg =>
            {
                cfg.AddProfile<HospitalProfile>();
            }
        );
    }
}

public class HospitalProfile : Profile
{
    protected override void Configure()
    {
        CreateMap<Hospital, MongoHospital>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.ToString()));
    }
}

希望这会有所帮助。如果您使用的是 AutoMapper 5.0,请记住此时它仍处于 beta-1。

【讨论】:

  • 此解决方案有效。但我只是想知道 MapperConfiguration 类的目的是什么?它在哪里有用?
  • 我认为 AutoMapper 一直支持它是 Mapper 的静态实现,如上所用 - 每个 AppDomain 注册/初始化一次,并且可以通过静态 Mapper 类引用。配置方法允许在容器上配置和注册 IMapper 以进行依赖注入。这可能不是 100% 正确,但这是我的理解。
  • 是的,差不多就是这样。或者,如果您想提供分段配置,比如说共享库提供了一些配置等。
【解决方案2】:

您可以在 AutoMapper 5.2 中使用它。

您的个人资料类如下

public class MapperProfile: Profile
{
    public MapperProfile()
    {
        CreateMap<Hospital, MongoHospital>().ReverseMap();
    }

}

然后在你的 Global.asax 中

     protected void Application_Start()
     {
       //Rest of the code 
       Mapper.Initialize(c => c.AddProfiles(new string[] { "DLL NAME OF YOUR PROFILE CLASS" }));
     }

现在当你需要创建一个实例时

AutoMapper.Mapper.Instance.Map<MongoHospital, Hospital>(source, new Hospital());

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2014-11-18
    • 2021-11-11
    • 2020-04-18
    相关资源
    最近更新 更多