I’ve read the nested mapping wiki page but it appears to not like multiple levels of nesting. I’ve got the following maps created and classes defined.
AutoMapper.Mapper.CreateMap<Address, AddressDTO>();
AutoMapper.Mapper.CreateMap<MatchCompanyRequest, MatchCompanyRequestDTO>();
public class MatchCompanyRequest
{
Address Address {get;set;}
}
public class MatchCompanyRequestDTO
{
public CompanyInformationDTO {get;set;}
}
public class CompanyInformationDTO {get;set;}
{
public string CompanyName {get;set;}
public AddressDTO Address {get;set;}
}
But the following code…
// works
matchCompanyRequestDTO.companyInformationDTO.Address =
AutoMapper.Mapper.Map<Address, AddressDTO>(matchCompanyRequest.Address);
// fails
matchCompanyRequestDTO =
AutoMapper.Mapper
.Map<MatchCompanyRequest, MatchCompanyRequestDTO>(matchCompanyRequest);
Does this deep nesting work and I have it configured improperly? Or is this kind of nesting not yet supported?
— Edit
For anyone interested, I am not in control of the DTOs.
It lacks the mapping from Address to
CompanyInformationDTO, as those objects are on the same nest-level.The map is created for
MatchCompanyRequest->MatchCompanyRequestDTO, but it is unable to figure out whether it can mapAddresstoCompanyInformationDTO.So your
MatchCompanyRequestDTOcould in fact have same declaration as yourCompanyInformationDTO:This of course only affects you if you want to use automatic mapping. You still can configure your maps manually, but it seems like the DTOs should be fixed instead, let’s try anyway: