Home » C# » Automapper » Automapper Ignore vs UseDestinationValue, what is the difference?

Automapper Ignore vs UseDestinationValue, what is the difference?

Automapper gives you big leverage when it’s the time to transfer values from one object into another object. While most of the transfer is done automatically with AutoMapper, sometimes, specific values must not be transferred. Imagine the case for a destination class that has not the property or for a class that has do not want the value from the source but will compute the value in its constructor.

Automapper gives the property Ignore which tells the mapper to not take the value of a property from the source class. Ignore not only ignore the mapping for the property, but also ignore the mapping of all inner properties. It means that if the property is not a primitive type, but another class, that if you are using Ignore, this class properties won’t be mapped. So, A.B.C would be A.Null.

But, if you have a scenario that you must not map a property, but map all inner properties should remain then you should use UseDestinationValue. That will end to be A.B.C -> A.B.C (where B is from the destination class, not the source class, probably initialized in the destination’s constructor).

To conclude, Ignore method will not map any property, neither the inner properties of the property defined to be ignored. UseDestinationValue will not map the property. It keeps the existing value of the property but will map the value of this one. It’s the same with a collection. The reference of the collection stays the same while the object inside is mapped.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

10 Responses so far.

  1. Emanuel says:

    I was looking for exactly this, the difference between both wasn’t that clear for me, thank you.

  2. vpurush says:

    yes.. useful.. thanks

  3. Ed Eaglehouse says:

    As a native English-speaker, I found this article was vague and confusing. While the author may understand what he’s talking about, that knowledge certainly didn’t make it into this article.

    • Hi Ed,
      Sorry to hear that you didn’t understand this article. Some of my articles are written rapidly as a reminder of something I discover. I do not double check or spell proof. I read this article again, I you are right that it is far from being stellar. I’ll try to take the time to proof read more in the future as well as edit this one as soon as I find some time. Thank you for your positive critic.

      • I updated the article. I think a next step would be to have some example and add more text. Nevertheless, I think it’s one step better than the original version. Feel free to give your suggestion and thank you for you feedback.

  4. Mojtaba says:

    Patrick,

    Thanks for the article.

    A few points; first as Ed pointed out the article is quite vague (though I am not a native English speaker myself). It is worth reading again and correct mistakes. For example, “Ignore not also ignore the mapping” should be “Ignore not only ignore the mapping”. That simple grammatical mistake confused me for a few minutes.

    Also, I will become a great article if you add more clear code samples.

  5. It took me a while to decipher what you said. I think I can summarize it a little more concisely:
    * Ignore() will completely prevent mapping the source member to the destination.
    * UseDestinationValue() will not replace the destination member with a new object but will map the source member on the existing object.

    I think it would have been more obvious if the latter had been more appropriately named as MapExistingMemberFrom() because UseDestinationValue() to me means use the destination value _without modification_, which is incorrect according to your explanation.

    The wording in some of your sentences was very confusing. For example, “UseDestinationValue will not map the property. It keeps the existing value of the property but will map the value of this one.” Those two sentences are contradictory (“will not map” vs. “will map”) and missing context (“this one?”). I suppose your implication is that UseDestinationValue() _will map the source property to the destination_ but, instead of using the member type’s default constructor to create a new object for it, it will modify whatever value is already in the destination member.

    AutoMapper is a nice tool, but its documentation is poor and full of unresolved references.

    Thanks for you article. It forced me to make the effort to think it through. The difference between those two functions is a little more clear to me now.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.