Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazily initializing AutoMapper

I've been doing some performance stats on an ASP.NET 4.5 Webforms app, which seems a bit sluggish on initial startup after a fresh deployment.

One of the points I noticed is that creating the AutoMapper maps does take some time.

Since those maps are only used rather rarely, I was wondering if I could possibly "delay" creating those maps until the first time they're needed - sort of a "lazy initialization".

In that case, I would have to have some "non-destructive" (e.g. without throwing an exception) way of checking whether or not a given map exists - if there something like that in AutoMapper?

Thanks!

like image 497
marc_s Avatar asked Sep 20 '26 01:09

marc_s


1 Answers

You can use FindTypeMapFor:

if (Mapper.FindTypeMapFor<TSource, TDestination>() == null)
    Mapper.CreateMap<TSource, TDestination>();

// Map object

There's also an overload that takes type parameters.

like image 66
stuartd Avatar answered Sep 23 '26 17:09

stuartd