源码之家

  • 首页
  • 文章
  • 问答
  • 下载
您的位置: 首页  >  技术问答  >  AutoMapper:class属性,映射ISet 到HashSet

AutoMapper:class属性,映射ISet 到HashSet
分类: 技术问答 • 2022-09-16 20:46:57
问题描述:

我已经定义了映射到/从我的DTO对象,其中一个对另一个的属性与其他匹配激动人心,除了DTO对象具有集合定义为ISet和非DTO对象将那些collectiond定义为HashSet。我注意到了DTO - > Non DTO映射的重要性能指标,相比之下。AutoMapper:class属性,映射ISet <object>到HashSet <Object>

AutoMapper似乎有麻烦从接口从具体类,我想知道如果我在映射丢失的东西,或配置某处更明确。这种模式在我们的代码库中存在,但我的问题我对象可以2K对象从的DTO 8秒映射,而我可以在大约0.1秒映射完全相同的对象的DTO

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>(); 
+0

进一步测试,以确保我们的代码库并没有造成麻烦,我使用了上述目的创建了一个测试,并发现ExampleDTO->示例映射发生在约11.5秒内,反向花费了2.29秒。 –

答

我们发现,升级Automapper(6.0.2版本)将在我们的例子要走的路。使用更新后的AutoMapper和上面列出的相同对象和映射,我们看到ExampleDTO-> Example对象在1.57秒内映射,相反的情况在1.86秒内映射。我并不是非常高兴的发布了一个说明使用升级的答案,所以我会发布一些提供一些适度收益的选项,如果其他人有实际的答案,我会很乐意标记这一点。

我试着创建一个ISet HashSet的映射,这是没有指定映射的两倍,我记不得正是我在这里做了什么,但我发现它在Google上。

我试过的另一个选项是在简单地返回ISet的DTO对象上创建Non Mappable HashSets。这大约快了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>(); 

相关推荐

  • AutoMapper:class属性,映射ISet 到HashSet
  • 使用AutoMapper到映射列表到ConfigurationElementCollection中
  • 如何初始化IList >?
  • 将IList 转换为IList
    • 网站免责声明 网站地图 最新文章 用户隐私 版权申明
    本站所有数据收集于网络,如果侵犯到您的权益,请联系网站进行下架处理。   

    Copyright © 2018-2021   Powered By 源码之家    备案号:   粤ICP备20058927号