自动映射器性能

编程入门 行业动态 更新时间:2024-10-28 17:22:29
本文介绍了自动映射器性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Automapper将我的业务模型映射到ViewModel。

它可以工作,但速度非常慢。

我有一个具有23个属性的6893个对象的集合(测试环境、生产环境应该还有更多属性)。

使用循环映射所有内容需要00:02:32.8118534。

var objects = // get all items (returns a collection of MyObj) List<MyViewModel> collection = new List<MyViewModel>(); foreach (MyObj obj in objects) { MyViewModel vm = Mapper.Map<MyObj, MyViewModel>(obj); collection.Add(vm); }

我尝试这样改进:

var objects = // get all items (returns a collection of MyObj) IEnumerable<MyViewModel> collection = mapper.Map<IEnumerable<MyObj>, IEnumerable<MyViewModel>>(objects);

并且需要00:02:25.4527961来映射所有内容。

所以没有多大帮助。

我的对象的任何属性都不能为null。

我是这样配置映射器的:

var config = new MapperConfiguration(cfg => { cfg.CreateMap<MyObj, MyViewModel>(); cfg.CreateMap<MyObjOtherObj, MyViewModelOtherObj>(); }); mapper = config.CreateMapper();

MyObj:

public partial class MyObj { public MyObj() { this.MyObjOtherObj= new HashSet<MyObjOtherObj>(); } public long a{ get; set; } public short b{ get; set; } public string c{ get; set; } public string d{ get; set; } public string e{ get; set; } public string f{ get; set; } public string g{ get; set; } public string h{ get; set; } public string i{ get; set; } public string j{ get; set; } public string k{ get; set; } public string l{ get; set; } public string m{ get; set; } public bool n{ get; set; } public bool o{ get; set; } public bool p{ get; set; } public bool q{ get; set; } public virtual ICollection<MyObjOtherObj> MyObjOtherObj{ get; set; } public virtual Types Types { get; set; } }

MyViewModel:

public class MyViewModel { public long a{ get; set; } public short b{ get; set; } public string c{ get; set; } public string d{ get; set; } public string e{ get; set; } public string f{ get; set; } public string g{ get; set; } public string h{ get; set; } public string i{ get; set; } public string j{ get; set; } public string k{ get; set; } public string l{ get; set; } public string m{ get; set; } public bool n{ get; set; } public bool o{ get; set; } public bool p{ get; set; } public bool q{ get; set; } public string TypesDescription { get; set; } public List<MyViewModelOtherObj> MyObjOtherObj { get; set; } }

MyObjOtherObj:

public partial class MyObjOtherObj { public long id{ get; set; } public long MyObjId { get; set; } public short x{ get; set; } public string z{ get; set; } public virtual MyObj MyObj{ get; set; } public virtual SourceTypes SourceTypes { get; set; } }

MyViewModelOtherObj:

public class MyViewModelOtherObj { public long Id { get; set; } public long MyObjId { get; set; } public short x{ get; set; } public string z{ get; set; } public string SourceTypesDescription { get; set; } }

编辑:

SourceTypes:

public partial class SourceTypes { public SourceTypes() { this.MyObjOtherObj = new HashSet<MyObjOtherObj>(); } public short SourceTypeId { get; set; } public string Description { get; set; } public virtual ICollection<MyObjOtherObj> MyObjOtherObj { get; set; } }

类型:

public partial class Types { public Types() { this.MyObj = new HashSet<MyObj>(); } public short TypeId { get; set; } public string Description { get; set; } public virtual ICollection<MyObj> MyObj{ get; set; } } 推荐答案

为了响应我们的评论,您需要立即加载集合对象。请看下面的文章,这应该可以解决您的问题:

Loading Related Entities

更多推荐

自动映射器性能

本文发布于:2023-11-04 12:01:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:性能   映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!