Need to speed up automapper…It takes 32 seconds to do 113 objects

后端 未结 7 1995
[愿得一人]
[愿得一人] 2021-01-30 09:25

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



        
7条回答
  •  清酒与你
    2021-01-30 09:53

    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 => 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.

提交回复
热议问题