I'm using Automapper to map my business model to a ViewModel.
It works, but it's very slow.
I have a collection with 6893 objects with 23 properties (test environment, production should have much more).
With a loop it takes 00:02:32.8118534
to map 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 like this:
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 map everything.
So it didn't help that much.
None of my object's properties can be null
.
This is how I configured the 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; }
}
The 5.0 version of AutoMapper has significant performance increases. In our benchmarks, using a very similar type that you've shown here, we can map one million items in a little over a second. In the upcoming 5.1 version, that shrinks even more that we're only about 3x slower than hand-mapping, mainly due to null checking that hand mapping won't do.
I'd upgrade.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With