I'm a little bit confused. I can't find out the difference between PreserveReferences
and MaxDepth
.
Let's suppose we have the following DTOs and models.
public class PersonEntity
{
public PersonEntity InnerPerson { get; set; }
}
public class PersonModel
{
public PersonModel InnerPerson { get; set; }
}
As written in the documentation:
Previously, AutoMapper could handle circular references by keeping track of what was mapped, and on every mapping, check a local hashtable of source/destination objects to see if the item was already mapped. It turns out this tracking is very expensive, and you need to opt-in using PreserveReferences for circular maps to work. Alternatively, you can configure MaxDepth.
My mappings:
cfg.CreateMap<PersonModel, PersonEntity>().MaxDepth(1);
cfg.CreateMap<PersonEntity, PersonModel>();
Program:
var personModel = new PersonModel();
personModel.InnerPerson = personModel;
var entity = Mapper.Map<PersonEntity>(personModel);
That is what I expect to get:
That is what I actually get:
I can use both of them (PreserveReferences
and MaxDepth
) for resolving circular references, but I don't see the difference. When I should use different number of depth in the MaxDepth
method? So, could someone provide it? Thanks in advance.