本文介绍了自动映射器性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用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
更多推荐
自动映射器性能
发布评论