lpf*_*pfx 8 c# performance automapper
我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
我试着像这样改进它:
var objects = // get all items (returns a collection of MyObj)
IEnumerable<MyViewModel> collection = mapper.Map<IEnumerable<MyObj>, IEnumerable<MyViewModel>>(objects);
Run Code Online (Sandbox Code Playgroud)
它00:02:25.4527961绘制了一切.
所以它没有那么多帮助.
我的对象的属性都不是null.
这是我配置映射器的方式:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<MyObj, MyViewModel>();
cfg.CreateMap<MyObjOtherObj, MyViewModelOtherObj>();
});
mapper = config.CreateMapper();
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
Run Code Online (Sandbox Code Playgroud)
编辑:
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; }
}
Run Code Online (Sandbox Code Playgroud)
类型:
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; }
}
Run Code Online (Sandbox Code Playgroud)
Jim*_*ard 12
5.0版本的AutoMapper具有显着的性能提升.在我们的基准测试中,使用您在此处显示的非常相似的类型,我们可以在一秒钟内映射一百万个项目.在即将发布的5.1版本中,我们的收缩甚至比手工映射慢大约3倍,这主要是因为无法检查手工映射是不行的.
我升级了.
| 归档时间: |
|
| 查看次数: |
12115 次 |
| 最近记录: |