1

Let's say I've designed a class A and also I've designed a DTO class called Dto.

If A is a dynamic proxy generated with Castle DynamicProxy, AutoMapper won't fire AfterMap:

mappingConfig.CreateMap<Dto, A>()
.AfterMap
(
     (dto, a, resolutionContext) => 
     {
          // Stuff to do after automatic mapping has been already completed
     }
);

// This won't fire AfterMap
Mapper.Map(dto, aProxy);

In the other hand, if I run the following code, AfterMap will be called:

var a = new A();
Mapper.Map(dto, a);

Also, MappingConfiguration is configured to automatically register types:

config.CreateMissingTypeMaps = true;

Currently I'm using AutoMapper 6.0.2. I can't figure out why this isn't working.

Update

After some trial-errors, It started to work once I commented out config.CreateMisstingTypeMaps or I set it to false.

I suspect that AutoMapper is creating a map between the DTO and AProxy (which is a type generated during run-time) and prioritizes that mapping over the Dto and A as is.

Thus, how may I configure AutoMapper to both create missing type maps and also support my scenario?

4

0 回答 0