Skip to content

Nested DTOs

Jon Smith edited this page Jan 20, 2015 · 1 revision

There are a few times when you might have nested DTOs. This can happen when you have relationships. A good example of this is CrudCustomerAddressDto.cs, which includes a CrudAddressDto.cs.

This will work, but you have to make sure that when you use the top level DTO, in this case CrudCustomerAddressDto, that the mapping for the related DTO, in this case CrudAddressDto is setup, otherwise you will get an exception from AutoMapper.

Setting up Associated DTOs

In the CrudCustomerAddressDto.cs code you will see the property below, which tells GenericServices to set up the CrudAddressDto mapping before it is called:

protected override Type AssociatedDtoMapping
{
    get { return typeof(CrudAddressDto); }
}

If you have more than one associated DTO you need to override the AssociatedDtoMappings (plural) property. There is an example of this in the TabNeededTagAndDelegatePostDto class in the GenericServices Unit Tests.

Update considerations

If you are updating a data item that has linked relationships the DTO copying will work, but you need to make sure all the related classes are loaded for the update to them to work. See the overriding of FindItemTrackedForUpdate in CrudCustomerAddressDto.cs for an example of this.