I have an Entity and Dto like this:
public class Animal
{
public int Legs { get; set; }
}
public class Tiger : Animal
{
public string Description1 { get; set; }
}
public class AnimalDto
{
public int Legs { get; set; }
}
public class TigerDto : AnimalDto
{
public string Description1 { get; set; }
}
Here is the code for mapping:
public void MyTest2()
{
//add child to parent
var tiger = new Tiger() { Description1 = "Tiger", Legs = 4 };
var animals = new List<Animal>();
animals.Add(tiger);
//mapping
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Animal, AnimalDto>();
cfg.CreateMap<Tiger, AnimalDto>();
cfg.CreateMap<Tiger, TigerDto>();
});
var mapper = config.CreateMapper();
//transfer to dto
var animalsDto = mapper.Map<List<AnimalDto>>(animals);
var tigerDto = mapper.Map<List<TigerDto>>(animals);
var tigerDto2 = mapper.Map<List<TigerDto>>(animalsDto);
}
The purpose is:
- Create list Animal with a child (animals.Add(tiger))
- map information from Entity (Animal) to Dto (AnimalDto)
- Send out AnimalDto (via API)
- Get AnimalDto and retrieve Child (here is TigerDto)
With mapping above, I can get correct data for tigerDto.
Q1. But how I can get data of tigerDto2?
Q2. How to send out Dto with child data?
Any advise will be appriciated.