question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UseDestinationValue is not inherited

See original GitHub issue

UseDestinationValue doesn’t works when using Include<,> or IncludeBase<,>. Looks similar like here #2188, also used IncludeBase, but option is PreserveReferences.

Source/destination types

// Model
sealed class Root { public List<Element> Elements { get; } = new List<Element>(); }
class Element { public List<Item> Items { get; } = new List<Item>(); }
class ElementX : Element { }
sealed class Item { }

// Dto
sealed class RootDto { public List<ElementDto> Elements { get; } = new List<ElementDto>(); }
class ElementDto { public List<ItemDto> Items { get; } = new List<ItemDto>(); }
class ElementXDto : ElementDto { }
sealed class ItemDto { }

Mapping configuration

CreateMap<Root, RootDto>().ForMember(_ => _.Elements, _ => _.UseDestinationValue());
CreateMap<Element, ElementDto>().ForMember(_ => _.Items, _ => _.UseDestinationValue());
CreateMap<ElementX, ElementXDto>().IncludeBase<Element, ElementDto>();
CreateMap<Item, ItemDto>();

Version: 6.1.1

Expected behavior

When mapping Root to RootDto, for each source Element in Elements will be created ElementDto (ElementXDto for ElementX), and it’s Items collection will be populated by mapped objects from source collection.

Actual behavior

For ElemenDto colelction populated, but for ElemenXDto collection is empty.

Steps to reproduce

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<Root, RootDto>().ForMember(_ => _.Elements, _ => _.UseDestinationValue());
    cfg.CreateMap<Element, ElementDto>().ForMember(_ => _.Items, _ => _.UseDestinationValue());
    cfg.CreateMap<ElementX, ElementXDto>().IncludeBase<Element, ElementDto>();
    cfg.CreateMap<Item, ItemDto>();
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();

var source = new Root
{
    Elements =
        {
            new Element { Items = { new Item(), new Item(), new Item() } }, // 3 items
            new ElementX { Items = { new Item(), new Item(), new Item() } } // 3 items
        }
};

var target = mapper.Map<RootDto>(source);

As result:

source.Elements[0]
{MapperTestApp.Program.Element}
    Items: Count = 3
source.Elements[1]
{MapperTestApp.Program.ElementX}
    Items: Count = 3

target.Elements[0]
{MapperTestApp.Program.ElementDto}
    Items: Count = 3
target.Elements[1]
{MapperTestApp.Program.ElementXDto}
    Items: Count = 0

Note

Same if exact map ElementX to ElementXDto. Problem can be resolved by adding UseDestinationValue into ElementX to ElementXDto mapping, but why??? I almost configured it for Element to ElementDto mapping, and ElementX to ElementXDto mapping should include it. It looks like I need to configure same things twice, and this is simple example with just one derived class. In this case Include or IncludeBase looks useless, or works incorrect. Same in #2188, but used option is PreserveReferences.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
lbargaoanucommented, Nov 18, 2018

Let’s start simple with a PR to inherit the setting and DontUseDestinationValue. Please open a new issue.

0reactions
lock[bot]commented, May 5, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - AutoMapper UseDestinationValue is ignored when ...
UseDestinationValue tells AutoMapper not to create a new object for some member, but to use the existing property of the destination object.
Read more >
5.0 Upgrade Guide
UseDestinationValue tells AutoMapper not to create a new object for some member, but to use the existing property of the destination object.
Read more >
10.0 Upgrade Guide
UseDestinationValue is now inherited by default¶. You can override that with DoNotUseDestinationValue . AllowNull allows you to override per member ...
Read more >
AutoMapper Documentation
30.4 UseDestinationValue isnowinheritedbydefault . ... 31.2 AutoMapper no longer creates maps automatically (CreateMissingTypeMaps and ...
Read more >
How to get AutoMapper to use interface mappings and ...
UseDestinationValue is only really used for collection types, not primitives. For collections/complex types it means "use the destination ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found