Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use CreateMissingTypeMaps option and manual mappings? #2008

Closed
dgandini opened this issue Mar 16, 2017 · 3 comments
Closed

How to use CreateMissingTypeMaps option and manual mappings? #2008

dgandini opened this issue Mar 16, 2017 · 3 comments

Comments

@dgandini
Copy link

dgandini commented Mar 16, 2017

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;
@dgandini dgandini changed the title How can I use CreateMissingTypeMaps option and manual mappings? How to use CreateMissingTypeMaps option and manual mappings? Mar 16, 2017
@TylerCarlson1
Copy link
Member

The AddConditionalObjectMapper() is the way to go so your anon types are mapped but not overwrite your default mappings. Don't use that with CreateMissingtypeMaps, because that overwrites the condition. CreateMissingTypeMaps basicly is AddConditionalObjectMapper().Where((s, d) => true);

I'm going to guess mapping overrides are from something like, if not exactly Dynamic Proxies from EntityFramework. What you do is make a conditional mapper map anything that's not dynamic proxies, or make your conditional mapper's where clause more robust to handle anonymous types being mapped better.

@dgandini
Copy link
Author

@TylerCarlson1 you guessed right! The problem just comes when EF proxy classes are used. You gave me the right directions and now it is working like a charm. I refactored the AnonymousProfile in order to don't use the CreateMissingTypeMaps and to ignore anon types from System.Data.Entity.DynamicProxies namespace.

Thank you.

@lock
Copy link

lock bot commented May 6, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants