UseDestinationValue is not inherited
See original GitHub issueUseDestinationValue
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:
- Created 6 years ago
- Comments:9 (7 by maintainers)
Let’s start simple with a PR to inherit the setting and DontUseDestinationValue. Please open a new issue.
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.