简体   繁体   English

AutoMapper 8.0构造使用重大更改

[英]AutoMapper 8.0 ConstructUsing breaking change

I use AutoMapper in my code and I have a generic wrapper that initializes the mapper. 我在代码中使用了AutoMapper,并且有一个用于初始化映射器的通用包装器。 The code runs since long without any problems. 该代码运行了很长时间没有任何问题。 After upgrading to AutoMapper 8 my custom converters are ignored. 升级到AutoMapper 8后,我的自定义转换器将被忽略。 The responsible code for setting the converters is the following: 负责设置转换器的代码如下:

new MapperConfiguration(cfg =>
   cfg.CreateMap(mySource, myTarget).ConstructUsing(myConverter);

This piece of code does not build with AutoMapper 8, because I have to use expressions. 这段代码不是使用AutoMapper 8构建的,因为我必须使用表达式。 There is even an upgrade guid provided, but I cant manage to get it running again. 甚至提供了升级指南 ,但我无法设法使其再次运行。

I just expected to convert it to 我只是希望将其转换为

new MapperConfiguration(cfg =>
   cfg.CreateMap(mySource, myTarget).ConstructUsing(expr => myConverter);

but the converter is ignored. 但转换器将被忽略。

I resolved a similar issue by extracting the myConverter method into a separate class that inherits from ITypeConverter: 我通过将myConverter方法提取到一个从ITypeConverter继承的单独的类中,解决了一个类似的问题:

public class MyConverter : ITypeConverter<MySourceType, MyDestinationType>
{
    public MyDestinationType Convert(MySourceType source, MyDestinationType destination, ResolutionContext context)
    {
        // Conversion logic here
    }
}

You can then replace the mapping config with the following: 然后,您可以使用以下内容替换映射配置:

cfg.CreateMap(mySource, myTarget).ConvertUsing<MyConverter>();

or 要么

cfg.CreateMap<MySourceType, MyDestinationType>().ConvertUsing<MyConverter>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM