0

I have a situation that requires to use CreateMissingTypeMaps and manual mappings at the "same time" (or at least at the same configuration).

Scenario: The Domain and View Model classes are manually mapped using profiles. The CreateMissingTypeMaps property is necessary because I have an anticorruption layer to access a legacy system wich returns anonymous objects.

The issue is that the manual mapping has it's mapping overhidden by CreateMissingTypeMaps option when it is set to true and I can't map anonymous objects when it is false.

I tried to set CreateMissingTypeMaps inside the MapperConfiguration, inside a profile and also inside a profile with a mapping condition but all of them failed.

The code below is my attempt to do a conditional profile that should be applied just for anonymous objects.

    public class AnonymousProfile : Profile
    {
        public AnonymousProfile()
        {
            AddConditionalObjectMapper().Where((s, d) => s.GetType().IsAnonymousType());
            CreateMissingTypeMaps = true;
        }
    }

   // inside my MapperConfiguration
   cfg.AddProfile(new AnonymousProfile()); // also tried cfg.CreateMissingTypeMaps = true;

[EDIT:] The original question didn't mention EF but I discovered that its proxy classes are part of the problem.

Douglas Gandini
  • 827
  • 10
  • 24
  • Could you provide a sample code reproducing the issue? – Ivan Stoev Mar 16 '17 at 20:40
  • @IvanStoev I was writing a short demo code when I discovered that the problem just happens when EF Proxy Classes are used. Meanwhile, my question was answered on Github and now I'm writing a answer to my own question. Thank you by your interest on my question. – Douglas Gandini Mar 17 '17 at 14:12

1 Answers1

1

I refactored my code following these directions pointed by Tyler on Github.

  1. My anonymous type check had a bug (I should not use GetType)
  2. Objects from System.Data.Entity.DynamicProxies have to be ignored

My rewritten AnonymousProfile class:

public class AnonymousProfile : Profile
{
    public AnonymousProfile()
    {
        AddConditionalObjectMapper().Where((s, d) => 
            s.IsAnonymousType() && s.Namespace != "System.Data.Entity.DynamicProxies");
    }
}
Community
  • 1
  • 1
Douglas Gandini
  • 827
  • 10
  • 24