AutoMapper hurting performance in .net application

2019-07-26 22:57发布

问题:

We are using AutoMapper 3.1.1.0 in our Dot net application. We are having lots of classes which neeed to map. Time required to initialize mapping is almost 22 seconds. We are having almost 1327 DTO which need to mapped.

And we can say that each DTO having average 8 properties.

My concern is for each message we check in list of 1327 mapped DTO,
and then use

 if (MappingManager.MessageMappings.ContainsKey(message.GetType()))
            {
                var myMessage = Mapper.Map(message, message.GetType(), MappingManagerFile.MessageMappings[message.GetType()]);

So it hurts performance. Do we need to Dispose after use, or automapper take care itself? In task manager the component which do this conversion is taking lots of memory.

So please suggest what alterantives we need to use to improve performance.

回答1:

Later versions of AutoMapper lazily compile the configuration. There's still some startup time, discovering and mapping types, but compiling the runtime mapping function is done lazily.

I would suggest trying the 5.0 release and comparing the numbers.



回答2:

Having that many entities mapped with automapper is going to take some time. Are you eager loading your entities or using lazy loading? I have seen these issues in the past when using lazy loading as Automapper generates a large number of database hits when getting all of the relational data.

Eager loading may be your best bet here, or I would recommend only loading exactly what you need. Seems like a lot of data to load at once.