Automapper Performance

I use Automapper to map my business model to ViewModel.

It works, but it is very slow.

I have a collection with 6893 objects with 23 properties (test environment, production should have much more).

Using a loop requires 00:02:32.8118534 to display everything.

 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); } 

I tried to improve it as follows:

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

And it took 00:02:25.4527961 to display everything.

So it didn’t help.

None of my object properties can be null .

This is how I configured mapper:

 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; } } 

EDIT:

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; } } 

Types:

 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; } } 
+5
source share
2 answers

In response to our comments, you need Eager to load your collection objects. Take a look at the following article to solve your problem:

Download related objects

+2
source

AutoMapper 5.0 has a significant increase in performance. In our tests, using the very similar type that you showed here, we can match one million elements in a few seconds. In the next version 5.1, this was further reduced, since we are only 3 times slower than hand-mapping, mainly due to zero verification that the manual display will not be performed.

I would update.

+8
source

All Articles