Automapper performance(自动映射器性能)
本文介绍了自动映射器性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用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
这篇关于自动映射器性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:自动映射器性能
基础教程推荐
猜你喜欢
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
