0

We use automapper with ASP.NET Core 2.0.

We like to create a mapping configuration once, which will be used from each mapper. We create a mapper per request that we will not have problems because I read once automapper is not thread safe.

We like to precompile the mappingconfiguration at application startup (See in Code MapperConfigruation.CompileMappings();

If I measure the time how long the mapping takes, i see that the first mapping need more time than the other mappings. Is there a reason for that or do I have bug?

Code

In ConfigureService of Startup class:

 services.AddSingleton<MyMapperConfiguration>();
 services.AddScoped<IObjectMapper, MyMapper>();     

Mapperconfiguration

public class MyMapperConfiguration
    {
        public MapperConfiguration MapperConfiguration { get; private set; }

        public MappingDefinition MappingDefinition { get; }

        public MapperConfiguration(IOptions<MappingDefinition> mappings)
        {
            // MappingDefinitions hold some information where to search mappings  
            MappingDefinition = mappings.Value;
        }


        public void Configure()
        {
            List<Type> mappingDefinitionClasses = new List<Type>();

             // Search Types with special attribute and add it to the typelist

            MapperConfiguration = new MapperConfiguration(cfg =>
            {
                cfg.AddProfiles(mappingDefinitionClasses.ToArray());
            });            
            MapperConfiguration.CompileMappings(); // <-- THIS SHOULD COMPILE THE MAPPING I THNIK?!

        }

`

Mapper

 public class MyMapper : IObjectMapper
    {      
        public IMapper Mapper { get; }

        public Mapper(MapperConfiguration mappingConfiguration)
        {           
            Mapper = mappingConfiguration.MapperConfiguration.CreateMapper();
        }

        public TDestination Map<TSource, TDestination>(TSource source)
        {
            return Mapper.Map<TSource, TDestination>(source);
        }
    }

IObjectMapper:

   public interface IObjectMapper
    {        
        TDestination Map<TSource, TDestination>(TSource source);        
    }

Measure time inside a webApi

Stopwatch sw = new Stopwatch();
sw.Start();
destObj = _mapper.Map<Source, Destination>(sourceObj);
sw.Stop();
Debug.WriteLine($"Duration of mapping: {sw.ElapsedMilliseconds}");

In the Configruate methode of Startup I also get an instance of the mapping configuration and call Configure() that this instance lifes.

8
  • You should look at the extensions package to ASP.NET Core for AutoMapper: github.com/AutoMapper/… that makes all this much easier to set up. Commented Nov 18, 2017 at 8:19
  • We do not like to have a direct dependeny to automapper. It should be possible to exchange the mapper implementantion Commented Nov 18, 2017 at 11:00
  • What? Why? That'll be a tooooon of work without much benefit. If you want to change mapper implementations, don't abstract, just learn regex. Commented Nov 27, 2017 at 14:20
  • Regex and mapping? Commented Nov 27, 2017 at 14:48
  • Main goal it to have te possibility to exchange the mapper implementation on one place. Commented Nov 27, 2017 at 15:17

1 Answer 1

1

AM is thread safe. The mapper itself is not expensive, you can share it or not. It does allocate a few things, so it's cheaper to share it if you can. CompileMappings changed a bit, so upgrade to the latest. But other than that, it's probably the JIT compiler you're seeing. Until the mapping is executed, the code doesn't get compiled. CompileMappings just compiles an expression to IL. The JIT compiles IL to machine code. You can profile and verify what happens.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.