简体   繁体   English

AutoMapper:PreserveReferences和MaxDepth有什么区别?

[英]AutoMapper: What is the difference between PreserveReferences and MaxDepth?

I'm a little bit confused. 我有点困惑。 I can't find out the difference between PreserveReferences and MaxDepth . 我找不到PreserveReferencesMaxDepth之间的区别。

Let's suppose we have the following DTOs and models. 假设我们有以下DTO和模型。

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. 以前,AutoMapper可以通过跟踪已映射的内容来处理循环引用,并在每个映射上检查源/目标对象的本地哈希表,以查看该项目是否已被映射。 It turns out this tracking is very expensive, and you need to opt-in using PreserveReferences for circular maps to work. 事实证明,这种跟踪非常昂贵,您需要使用PreserveReferences选择启用圆形地图。 Alternatively, you can configure MaxDepth. 或者,您可以配置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. 我可以将它们( PreserveReferencesMaxDepth )都用于解析循环引用,但是我看不出有什么区别。 When I should use different number of depth in the MaxDepth method? 什么时候应该在MaxDepth方法中使用不同数量的深度? So, could someone provide it? 那么,有人可以提供吗? Thanks in advance. 提前致谢。

MaxDepth doesn't consider object values at runtime. MaxDepth在运行时不考虑对象值。 It simply stops mapping after the depth of the mapping tree reaches the configured value. 在映射树的深度达到配置的值之后,它只是停止映射。

PreserveReferences doesn't help with ProjectTo , MaxDepth does. PreserveReferencesProjectTo MaxDepthMaxDepth If somehow, with Map , you have a mapping tree that might overflow the stack, but objects instances are not duplicated, then PreserveReferences won't help, MaxDepth will. 如果使用Map ,以某种方式,您有一个可能会在堆栈中溢出的映射树,但是对象实例没有重复,那么PreserveReferences将无济于事, MaxDepth将会帮助MaxDepth

MaxDepth is predictable, it stops at a hardcoded value, PreserveReferences stops only when an object instance is duplicated. MaxDepth是可预测的,它停止在硬编码值上, PreserveReferences仅在复制对象实例时停止。

Thanks @Lucian Bargaoanu for the answer. 感谢@Lucian Bargaoanu的回答。 I just want to add a simple example of MaxDepth . 我只想添加一个简单的MaxDepth示例。

I recently changed my DTOs and models. 我最近更改了我的DTO和模型。

public class SelfEntity
{
    public string Id { get; set; }
    public string Name { get; set; }
    public int Number;
    public SelfEntity InnerSelfEntity { get; set; }
}

public class SelfModel
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public int Number;
    public SelfModel InnerSelfModel { get; set; }
}

Mappings: 映射:

cfg.CreateMap<SelfModel, SelfEntity>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(x => x.Id.ToString()))
.MaxDepth(3);

Program: 程序:

SelfModel firstSelfModel = new SelfModel();
SelfModel prev = firstSelfModel;

for (int i = 0; i < 100; i++)
{
   SelfModel newModel = new SelfModel
   {
      Id = Guid.NewGuid(),
      Name = "Test name" + i.ToString(),
      Number = i
   };

   prev.InnerFirstSelf = newModel;
   prev = newModel;
}

var entity = Mapper.Map<FirstSelfEntity>(firstSelfModel);

Starts from the 3-th level of depth we will get null for the InnerSelfModel . 从3级深度开始,我们将为InnerSelfModel获得null

PreserveReferences doesn't help with ProjectTo, MaxDepth does. PreserveReferences对ProjectTo无效,MaxDepth有用。 If somehow, with Map, you have a mapping tree that might overflow the stack, but objects instances are not duplicated, then PreserveReferences won't help, MaxDepth will. 如果通过Map以某种方式,您有一个可能会溢出堆栈的映射树,但是对象实例没有重复,那么PreserveReferences将无济于事,MaxDepth会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM