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

后端 未结 7 2121
[愿得一人]
[愿得一人] 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 10:01

    Another thing to look for is mapping code that throws exceptions. AutoMapper will catch these silently, but catching exceptions in this way impacts performance.

    So if SomethingThatMightBeNull is often null, then this mapping will perform poorly due to the NullreferenceExceptions :

    .ForMember(dest => dest.Blah, c.MapFrom(src=>src.SomethingThatMightBeNull.SomeProperty))
    

    I've found making a change like this will more than half the time the mapping takes:

    .ForMember(dest => dest.Blah, c.MapFrom(src=> (src.SomethingThatMightBeNull == null
        ? null : src.SomethingThatMightBeNull.SomeProperty)))
    

    Update: C# 6 syntax

    .ForMember(dest => dest.Blah, c.MapFrom(src => (src.SomethingThatMightBeNull?.SomeProperty)))
    

提交回复
热议问题