Ignore properties dynamically at time of mapping using AutoMapper2019 Community Moderator Electionautomapper how to ignore property in source item that does not exist in destinationIgnore mapping one property with AutomapperAutomapper ignoring ignore for propertiesGetting Automapper to ignore non-scalar and nav properties in EFCan I ignore Automapper properties on a one-off basis?Automapper ignore properties while mappingAutoMapper ignore properties while mapping a listMap Parent/Child Interface Properties using AutoMapperC# AutoMapper from DTO to Destination using dynamic property name?automapper convert typeof object to enum
Why is the President allowed to veto a cancellation of emergency powers?
If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?
Why do passenger jet manufacturers design their planes with stall prevention systems?
Are ETF trackers fundamentally better than individual stocks?
Why do Australian milk farmers need to protest supermarkets' milk price?
Interplanetary conflict, some disease destroys the ability to understand or appreciate music
My adviser wants to be the first author
How to change two letters closest to a string and one letter immediately after a string using notepad++
Is it possible to upcast ritual spells?
What options are left, if Britain cannot decide?
How to simplify this time periods definition interface?
Look at your watch and tell me what time is it. vs Look at your watch and tell me what time it is
What approach do we need to follow for projects without a test environment?
How to terminate ping <dest> &
Min function accepting varying number of arguments in C++17
Opacity of an object in 2.8
Gravity magic - How does it work?
How could a scammer know the apps on my phone / iTunes account?
A sequence that has integer values for prime indexes only:
How to use of "the" before known matrices
Employee lack of ownership
How do I hide Chekhov's Gun?
How can you use ICE tables to solve multiple coupled equilibria?
A limit with limit zero everywhere must be zero somewhere
Ignore properties dynamically at time of mapping using AutoMapper
2019 Community Moderator Electionautomapper how to ignore property in source item that does not exist in destinationIgnore mapping one property with AutomapperAutomapper ignoring ignore for propertiesGetting Automapper to ignore non-scalar and nav properties in EFCan I ignore Automapper properties on a one-off basis?Automapper ignore properties while mappingAutoMapper ignore properties while mapping a listMap Parent/Child Interface Properties using AutoMapperC# AutoMapper from DTO to Destination using dynamic property name?automapper convert typeof object to enum
I'm using AutoMapper in my .net-core application to create DTOs from my Nhibernate entities to send to the client as a JSON. However, there are some requests that I need to omit certain properties on my entity. For example, assume I have the following classes:
public class Person
public ICollection<Friend> Friends get; set;
public class PersonDTO
public ICollection<FriendDTO> Friends get; set;
Now, there are cases where I need to send to the client a Person with all of their friends, however sometimes I just need the person. I have a automapper configuration that defines a simple mapping between Person and PersonDTO with no omitted properties because that's the most common case. However, rather than create two separate DTOS (one with the Friends property mapped and the other with it ignored), it would be nice to somehow specify this at the time of mapping. I've tried the following, but it doesn't seem to work:
var personDtoWithoutFriends = _mapper.map<Person, PersonDTO>(person,
opts => opts.ConfigureMap()
.ForMember(dest => dest.Friends, act => act.Ignore()))
I know I can solve this problem with multiple DTOs, however this is an extremely common pattern in my application and I would end up creating a large number of DTOs. Ignoring certain properties in the few cases I need to seems like a much more elegant solution. Am I doing something wrong or is this just not possible using AutoMapper?
c# .net-core automapper
add a comment |
I'm using AutoMapper in my .net-core application to create DTOs from my Nhibernate entities to send to the client as a JSON. However, there are some requests that I need to omit certain properties on my entity. For example, assume I have the following classes:
public class Person
public ICollection<Friend> Friends get; set;
public class PersonDTO
public ICollection<FriendDTO> Friends get; set;
Now, there are cases where I need to send to the client a Person with all of their friends, however sometimes I just need the person. I have a automapper configuration that defines a simple mapping between Person and PersonDTO with no omitted properties because that's the most common case. However, rather than create two separate DTOS (one with the Friends property mapped and the other with it ignored), it would be nice to somehow specify this at the time of mapping. I've tried the following, but it doesn't seem to work:
var personDtoWithoutFriends = _mapper.map<Person, PersonDTO>(person,
opts => opts.ConfigureMap()
.ForMember(dest => dest.Friends, act => act.Ignore()))
I know I can solve this problem with multiple DTOs, however this is an extremely common pattern in my application and I would end up creating a large number of DTOs. Ignoring certain properties in the few cases I need to seems like a much more elegant solution. Am I doing something wrong or is this just not possible using AutoMapper?
c# .net-core automapper
I'm not sure if this will work for you but what about creating two different mappers, one for mapping with friends and one for mapping without and switch between the two when either is needed?
– amura.cxg
Mar 6 at 22:47
docs.automapper.org/en/latest/Conditional-mapping.html
– Lucian Bargaoanu
Mar 7 at 2:31
add a comment |
I'm using AutoMapper in my .net-core application to create DTOs from my Nhibernate entities to send to the client as a JSON. However, there are some requests that I need to omit certain properties on my entity. For example, assume I have the following classes:
public class Person
public ICollection<Friend> Friends get; set;
public class PersonDTO
public ICollection<FriendDTO> Friends get; set;
Now, there are cases where I need to send to the client a Person with all of their friends, however sometimes I just need the person. I have a automapper configuration that defines a simple mapping between Person and PersonDTO with no omitted properties because that's the most common case. However, rather than create two separate DTOS (one with the Friends property mapped and the other with it ignored), it would be nice to somehow specify this at the time of mapping. I've tried the following, but it doesn't seem to work:
var personDtoWithoutFriends = _mapper.map<Person, PersonDTO>(person,
opts => opts.ConfigureMap()
.ForMember(dest => dest.Friends, act => act.Ignore()))
I know I can solve this problem with multiple DTOs, however this is an extremely common pattern in my application and I would end up creating a large number of DTOs. Ignoring certain properties in the few cases I need to seems like a much more elegant solution. Am I doing something wrong or is this just not possible using AutoMapper?
c# .net-core automapper
I'm using AutoMapper in my .net-core application to create DTOs from my Nhibernate entities to send to the client as a JSON. However, there are some requests that I need to omit certain properties on my entity. For example, assume I have the following classes:
public class Person
public ICollection<Friend> Friends get; set;
public class PersonDTO
public ICollection<FriendDTO> Friends get; set;
Now, there are cases where I need to send to the client a Person with all of their friends, however sometimes I just need the person. I have a automapper configuration that defines a simple mapping between Person and PersonDTO with no omitted properties because that's the most common case. However, rather than create two separate DTOS (one with the Friends property mapped and the other with it ignored), it would be nice to somehow specify this at the time of mapping. I've tried the following, but it doesn't seem to work:
var personDtoWithoutFriends = _mapper.map<Person, PersonDTO>(person,
opts => opts.ConfigureMap()
.ForMember(dest => dest.Friends, act => act.Ignore()))
I know I can solve this problem with multiple DTOs, however this is an extremely common pattern in my application and I would end up creating a large number of DTOs. Ignoring certain properties in the few cases I need to seems like a much more elegant solution. Am I doing something wrong or is this just not possible using AutoMapper?
c# .net-core automapper
c# .net-core automapper
asked Mar 6 at 19:49
user115188user115188
5027
5027
I'm not sure if this will work for you but what about creating two different mappers, one for mapping with friends and one for mapping without and switch between the two when either is needed?
– amura.cxg
Mar 6 at 22:47
docs.automapper.org/en/latest/Conditional-mapping.html
– Lucian Bargaoanu
Mar 7 at 2:31
add a comment |
I'm not sure if this will work for you but what about creating two different mappers, one for mapping with friends and one for mapping without and switch between the two when either is needed?
– amura.cxg
Mar 6 at 22:47
docs.automapper.org/en/latest/Conditional-mapping.html
– Lucian Bargaoanu
Mar 7 at 2:31
I'm not sure if this will work for you but what about creating two different mappers, one for mapping with friends and one for mapping without and switch between the two when either is needed?
– amura.cxg
Mar 6 at 22:47
I'm not sure if this will work for you but what about creating two different mappers, one for mapping with friends and one for mapping without and switch between the two when either is needed?
– amura.cxg
Mar 6 at 22:47
docs.automapper.org/en/latest/Conditional-mapping.html
– Lucian Bargaoanu
Mar 7 at 2:31
docs.automapper.org/en/latest/Conditional-mapping.html
– Lucian Bargaoanu
Mar 7 at 2:31
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031133%2fignore-properties-dynamically-at-time-of-mapping-using-automapper%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031133%2fignore-properties-dynamically-at-time-of-mapping-using-automapper%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm not sure if this will work for you but what about creating two different mappers, one for mapping with friends and one for mapping without and switch between the two when either is needed?
– amura.cxg
Mar 6 at 22:47
docs.automapper.org/en/latest/Conditional-mapping.html
– Lucian Bargaoanu
Mar 7 at 2:31