Automapper 11,字符串到 Uri 不再有效
Automapper 11, string to Uri no longer works
自升级到 automapper 11 后,尝试将 string
映射到 Uri
不再有效
实体
public sealed class Website
{
public int Id { get; set; }
public string Name {get; set; }
public string BaseAddress { get; set; }
}
我映射到的对象
public sealed class WebsiteDto
{
public int Id { get; set; }
public string Name { get; set; }
public Uri BaseAddress { get; set; }
}
映射配置文件包含
CreateMap<Website, WebsiteDto>();
以及我如何称呼它
_mapper.Map<List<WebsiteDto>>(listOfWebsiteEntities);
这在升级到 11 之前工作正常,我认为这可能是一个错误,但在我将其作为问题提出之前,Automapper 建议在这里提出问题,我是否遗漏了什么?
降级到自动映射器后 10.1.1
它又可以工作了。升级指南似乎没有提到任何与此相关的内容
该问题仅影响相对 URI,并且由于 11 版本中的 next breaking change 而发生:
System.ComponentModel.TypeConverter
is no longer supported
It was removed for performance reasons. So it’s best not to use it anymore. But if you must, there is a sample in the test project.
您可以将 TypeConverterMapper
添加到映射器,这样 Automapper
将像以前一样运行。请注意,此实现非常原始且性能非常差:
var cfg = new MapperConfiguration(cfg => cfg.Internal().Mappers.Insert(0, new TypeConverterMapper()))
public class TypeConverterMapper : ObjectMapper<object, object>
{
public override bool IsMatch(TypePair context)
{
return GetConverter(context.SourceType).CanConvertTo(context.DestinationType) ||
GetConverter(context.DestinationType).CanConvertFrom(context.SourceType);
}
public override object Map(object source, object destination, Type sourceType, Type destinationType, ResolutionContext context)
{
var typeConverter = GetConverter(sourceType);
return typeConverter.CanConvertTo(destinationType) ? typeConverter.ConvertTo(source, destinationType) : GetConverter(destinationType).ConvertFrom(source);
}
private TypeConverter GetConverter(Type type) => TypeDescriptor.GetConverter(type);
}
或者创建一个从字符串到 Uri
的映射,我认为这是更好的选择。快速而肮脏的模拟前一个行为可能如下所示:
cfg.CreateMap<string, Uri>().ConvertUsing(s => (Uri)new UriTypeConverter().ConvertFrom(s));
或者只是:
cfg.CreateMap<string, Uri>().ConvertUsing(s => new Uri(s, UriKind.RelativeOrAbsolute));
自升级到 automapper 11 后,尝试将 string
映射到 Uri
不再有效
实体
public sealed class Website
{
public int Id { get; set; }
public string Name {get; set; }
public string BaseAddress { get; set; }
}
我映射到的对象
public sealed class WebsiteDto
{
public int Id { get; set; }
public string Name { get; set; }
public Uri BaseAddress { get; set; }
}
映射配置文件包含
CreateMap<Website, WebsiteDto>();
以及我如何称呼它
_mapper.Map<List<WebsiteDto>>(listOfWebsiteEntities);
这在升级到 11 之前工作正常,我认为这可能是一个错误,但在我将其作为问题提出之前,Automapper 建议在这里提出问题,我是否遗漏了什么?
降级到自动映射器后 10.1.1
它又可以工作了。升级指南似乎没有提到任何与此相关的内容
该问题仅影响相对 URI,并且由于 11 版本中的 next breaking change 而发生:
System.ComponentModel.TypeConverter
is no longer supported It was removed for performance reasons. So it’s best not to use it anymore. But if you must, there is a sample in the test project.
您可以将 TypeConverterMapper
添加到映射器,这样 Automapper
将像以前一样运行。请注意,此实现非常原始且性能非常差:
var cfg = new MapperConfiguration(cfg => cfg.Internal().Mappers.Insert(0, new TypeConverterMapper()))
public class TypeConverterMapper : ObjectMapper<object, object>
{
public override bool IsMatch(TypePair context)
{
return GetConverter(context.SourceType).CanConvertTo(context.DestinationType) ||
GetConverter(context.DestinationType).CanConvertFrom(context.SourceType);
}
public override object Map(object source, object destination, Type sourceType, Type destinationType, ResolutionContext context)
{
var typeConverter = GetConverter(sourceType);
return typeConverter.CanConvertTo(destinationType) ? typeConverter.ConvertTo(source, destinationType) : GetConverter(destinationType).ConvertFrom(source);
}
private TypeConverter GetConverter(Type type) => TypeDescriptor.GetConverter(type);
}
或者创建一个从字符串到 Uri
的映射,我认为这是更好的选择。快速而肮脏的模拟前一个行为可能如下所示:
cfg.CreateMap<string, Uri>().ConvertUsing(s => (Uri)new UriTypeConverter().ConvertFrom(s));
或者只是:
cfg.CreateMap<string, Uri>().ConvertUsing(s => new Uri(s, UriKind.RelativeOrAbsolute));