【发布时间】:2016-12-01 20:30:35
【问题描述】:
我正在使用 Automapper 将我的业务模型映射到 ViewModel。
可以,但是速度很慢。
我有一个包含 6893 个对象和 23 个属性的集合(测试环境,生产应该有更多)。
使用循环需要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();
我的对象:
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; }
}
编辑:
来源类型:
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; }
}
【问题讨论】:
-
能否请您发布两个对象的代码?
-
那么你的问题是什么?您是否尝试过寻找热点?其他对象是如何创建的?
-
您是否检查过它们是完全加载的对象,即 ORM 不会因为延迟加载附加对象而减慢速度?
-
@RichLinnell 是的,对象已完全加载。
-
你用的是什么版本?
标签: c# performance automapper