AutoMapper: class 属性,将 ISet<object> 映射到 HashSet<Object>
AutoMapper: class properties, map ISet<object> to HashSet<Object>
我已经定义了一个映射 to/from 我的 DTO 对象,一个与另一个的属性非常匹配,除了 DTO 对象具有定义为 ISet 的集合,而非 DTO 对象具有定义为 HashSet 的集合。我注意到 DTO -> 非 DTO 与其他方式相比对性能有重大影响。
AutoMapper 似乎无法从具体的接口 class 转出接口,我想知道我是否在映射中遗漏了某些内容,或者在某处配置得更明确。这种范例存在于我们的代码库中,但对于我所讨论的对象,我可以在大约 8 秒内从 DTO 映射 2k 个对象 ,并且我可以映射完全相同的对象 到 DTO 大约需要 .1 秒
class ExampleDTO
{
public int Id;
public enum Type;
public DateTime creationTime;
public ISet<string> StringThings;
public ISet<int> IntThings;
public ISet<double> DoubleThings;
}
class Example
{
public int Id;
public enum Type;
public DateTime creationTime;
public HashSet<string> StringThings;
public HashSet<int> IntThings;
public HashSet<double> DoubleThings;
}
映射:
CreateMap<ExampleDTO, Example>();
CreateMap<Example, ExampleDTO>();
我们发现升级 Automapper(到 6.0.2 版)是我们的方法。使用更新后的 AutoMapper 和上面列出的相同对象和映射,我们看到 ExampleDTO->Example 对象在 1.57 秒内映射,相反的大小写在 1.86 秒内映射。我不太高兴 post 回答说使用升级,所以我会 post 一些能带来一些适度收益的选项,如果其他人有实际答案,我会很高兴标记那个。
我尝试为 ISet HashSet 创建映射,这大约是没有指定映射的速度的两倍,我不记得确切我在这里做了什么,但我找到了在谷歌上。
我尝试的另一个选项是在仅返回 ISet 的 DTO 对象上创建不可映射的 HashSet。这大约快了 3 倍,但仍不及升级后获得的性能。
class ExampleDTO
{
public int Id;
public enum Type;
public DateTime creationTime;
public ISet<string> StringThings;
[IgnoreMap]
public HashSet<string> StringThingsHash
{
get
{
return StringThings;
}
}
public ISet<int> IntThings;
[IgnoreMap]
public HashSet<int> IntThingsHash
{
get
{
return IntThings;
}
}
public ISet<double> DoubleThings;
[IgnoreMap]
public HashSet<double> DoubleThingsHash
{
get
{
return DoubleThings;
}
}
并且我使用了以下映射
CreateMap<ExampleDTO, Example>()
.ForMember(dest => dest.StringThings, opts => opts.MapFrom(src => src.StringThingsHash)
.ForMember(dest => dest.IntThings, opts => opts.MapFrom(src => src.IntThingsHash)
.ForMember(dest => dest.DoubleThings, opts => opts.MapFrom(src => src.DoubleThingsHash);
CreateMap<Example, ExampleDTO>();
我已经定义了一个映射 to/from 我的 DTO 对象,一个与另一个的属性非常匹配,除了 DTO 对象具有定义为 ISet 的集合,而非 DTO 对象具有定义为 HashSet 的集合。我注意到 DTO -> 非 DTO 与其他方式相比对性能有重大影响。
AutoMapper 似乎无法从具体的接口 class 转出接口,我想知道我是否在映射中遗漏了某些内容,或者在某处配置得更明确。这种范例存在于我们的代码库中,但对于我所讨论的对象,我可以在大约 8 秒内从 DTO 映射 2k 个对象 ,并且我可以映射完全相同的对象 到 DTO 大约需要 .1 秒
class ExampleDTO
{
public int Id;
public enum Type;
public DateTime creationTime;
public ISet<string> StringThings;
public ISet<int> IntThings;
public ISet<double> DoubleThings;
}
class Example
{
public int Id;
public enum Type;
public DateTime creationTime;
public HashSet<string> StringThings;
public HashSet<int> IntThings;
public HashSet<double> DoubleThings;
}
映射:
CreateMap<ExampleDTO, Example>();
CreateMap<Example, ExampleDTO>();
我们发现升级 Automapper(到 6.0.2 版)是我们的方法。使用更新后的 AutoMapper 和上面列出的相同对象和映射,我们看到 ExampleDTO->Example 对象在 1.57 秒内映射,相反的大小写在 1.86 秒内映射。我不太高兴 post 回答说使用升级,所以我会 post 一些能带来一些适度收益的选项,如果其他人有实际答案,我会很高兴标记那个。
我尝试为 ISet HashSet 创建映射,这大约是没有指定映射的速度的两倍,我不记得确切我在这里做了什么,但我找到了在谷歌上。
我尝试的另一个选项是在仅返回 ISet 的 DTO 对象上创建不可映射的 HashSet。这大约快了 3 倍,但仍不及升级后获得的性能。
class ExampleDTO
{
public int Id;
public enum Type;
public DateTime creationTime;
public ISet<string> StringThings;
[IgnoreMap]
public HashSet<string> StringThingsHash
{
get
{
return StringThings;
}
}
public ISet<int> IntThings;
[IgnoreMap]
public HashSet<int> IntThingsHash
{
get
{
return IntThings;
}
}
public ISet<double> DoubleThings;
[IgnoreMap]
public HashSet<double> DoubleThingsHash
{
get
{
return DoubleThings;
}
}
并且我使用了以下映射
CreateMap<ExampleDTO, Example>()
.ForMember(dest => dest.StringThings, opts => opts.MapFrom(src => src.StringThingsHash)
.ForMember(dest => dest.IntThings, opts => opts.MapFrom(src => src.IntThingsHash)
.ForMember(dest => dest.DoubleThings, opts => opts.MapFrom(src => src.DoubleThingsHash);
CreateMap<Example, ExampleDTO>();