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

Is it possible to create a mapping per type discriminator? #258

Closed
abstract-matters opened this issue Oct 4, 2012 · 7 comments
Closed
Labels

Comments

@abstract-matters
Copy link

Something like this:

Mapper.CreateMap<DTO.Party, Party>();
Mapper.CreateMap<DTO.Party, SomeParty>().Where(source => source.PartyTypeDiscriminatorValue == 1);
Mapper.CreateMap<DTO.Party, SomeOtherParty>().Where(source => source.PartyTypeDiscriminatorValue == 2);

var party = new DTO.Party() { PartyTypeDiscriminatorValue = 2 };

var result = Mapper.Map<DTO.Party, Party>(party);

Assert.IsTrue(result is SomeOtherParty)

@jbogard
Copy link
Member

jbogard commented Oct 18, 2012

I wonder if it could be part of the Include option?

@abstract-matters
Copy link
Author

Yeah, having a Where() on the Include option would be excellent!
This would probably mean that the dictionary described in this post (below) should also contain the func for the discriminator. And the code for selecting the appropriate target type should evaluate the func.

http://stackoverflow.com/questions/8844959/mapping-derived-class-to-multiple-destinations-with-automapper

@ansydor
Copy link

ansydor commented Jan 15, 2014

it would be awesome. is there any solution for this situation right now?

@jbogard jbogard modified the milestones: 3.3.0, v.Next Apr 24, 2014
@jbogard jbogard added the Feature label Jun 6, 2014
@jbogard jbogard modified the milestones: v.Future, 3.3.0 Sep 12, 2014
@TylerCarlson1
Copy link
Member

Added pull request to add this feature in #590. It's not a Where() but an IncludeOnSourceCondition() right now.

Also another note about this change. You will NOT be able to use this for projection at the moment. Entity Framework at least, because it doesn't support casting return types when you project.

@julealgon
Copy link

I just posted a question on StackOverflow related to the problems I'm having with mapping based on a property of the original model in a inheritance scenario.

http://stackoverflow.com/questions/30128551/how-do-i-leverage-constructusing-in-a-mapping-with-inherited-types

Jimmy, I'm sure my problem is related to this. Could you perhaps take a look at that and suggest some workaround?

@TylerCarlson1
Copy link
Member

It is related to this and unfortunately there's nothing you can do with includes to make this work.
Issue is when you include one class the logic will then check every class that inherits from the base and use the highest derived ones for it. Regardless if you included it or not.

Fix for this issue in code shown here 3ed6d8a as part of #590

@jbogard jbogard removed this from the v.Future milestone Jan 18, 2016
@jbogard jbogard closed this as completed Jan 18, 2016
@lock
Copy link

lock bot commented May 7, 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 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants