Hi I have some major problems with auto mapper and it being slow. I am not sure how to speed it up.
I am using nhibernate,fluent nhibernate and asp.net mvc 3.0
Instead of:
var a = // get all items (returns a collection of Test2)
List collection = new List();
foreach (Test2 t in a)
{
MyViewModel vm = Mapper.Map(t);
vm.SetDateFormat(t.DateTimeDate, DateFilters.All.ToString());
collection.Add(vm);
}
Try:
var a = // get all items (returns a collection of Test2)
List collection = Mapper.Map, IEnumerable>(a);
which is equivalent to the first except the SetDateFormat
call which you could do at your mapping definition. It might also be faster.
If you have a mapping defined between Test2 => MyViewModel
AutoMapper automatically provides one for IEnumerable
so that you don't need to loop.
Also you have mentioned NHibernate in your question. Make sure that your source object along with its collections is eagerly loaded from the database before passing it to the mapping layer or you cannot blame AutoMapper for being slow because when it tries to map one of the collections of your source object it hits the database because NHibernate didn't fetch this collection.