C# AutoMapper:在自定义ITypeConverter中实现MaxDepth

C# AutoMapper:在自定义ITypeConverter中实现MaxDepth,c#,automapper,C#,Automapper,关于循环引用,我在AutoMapper中的自定义ITypeConverter方面遇到了一些问题。举下面的例子:假设一个房子可以有一个列表,其中每个人都属于一个房子。我们可以这样建模: public class House { public int PropertyA { get; set; } public string PropertyB { get; set; } public List<Person> People { get; set; } } p

关于循环引用,我在AutoMapper中的自定义ITypeConverter方面遇到了一些问题。举下面的例子:假设一个
房子
可以有一个
列表
,其中每个
都属于一个
房子
。我们可以这样建模:

public class House 
{
    public int PropertyA { get; set; }
    public string PropertyB { get; set; }
    public List<Person> People { get; set; }
}

public class Person
{
    public int PropertyA { get; set; }
    public string PropertyB { get; set; }
    public House House { get; set; }
}
公共类房屋
{
公共int属性{get;set;}
公共字符串属性b{get;set;}
公共列表人员{get;set;}
}
公共阶层人士
{
公共int属性{get;set;}
公共字符串属性b{get;set;}
酒店{get;set;}
}
也许我们有一些DTO看起来像这样:

public class HouseDto
{
    public string PropertyB { get; set; }
    public List<PersonDto> People { get; set; }
}

public class PersonDto
{
    public string PropertyB { get; set; }
    public HouseDto House { get;set; }
}
public class HouseConverter : ITypeConverter<House, HouseDto>
{
    private readonly IMapper _mapper;

    public HouseConverter(IMapper mapper)
    {
        _mapper = mapper;
    }

    public HouseDto Convert(House source, HouseDto destination, ResolutionContext context)
    {
        if (source == null) return null;

        destination = destination ?? new HouseDto();
        destination.PropertyB = source.PropertyB;
        destination.People = _mapper.Map<List<PersonDto>>(source.People);

        return destination;
    }
}

public class PersonConverter : ITypeConverter<Person, PersonDto>
{
    private readonly IMapper _mapper;

    public PersonConverter(IMapper mapper)
    {
        _mapper = mapper;
    }

    public PersoneDto Convert(Person source, PersonDto destination, ResolutionContext context)
    {
        if (source == null) return null;

        destination = destination ?? new PersonDto();
        destination.PropertyB = source.PropertyB;
        destination.House = _mapper.Map<HouseDto>(source.House);

        return destination;
    }
}
public class HouseDto
{
公共字符串属性b{get;set;}
公共列表人员{get;set;}
}
公共类个人
{
公共字符串属性b{get;set;}
公共房屋到房屋{get;set;}
}
因为AutoMapper基于约定的映射非常慢,所以我选择使用自定义类型转换器,以避免AutoMapper使用的所有反射。所以我有两个类似这样的转换器:

public class HouseDto
{
    public string PropertyB { get; set; }
    public List<PersonDto> People { get; set; }
}

public class PersonDto
{
    public string PropertyB { get; set; }
    public HouseDto House { get;set; }
}
public class HouseConverter : ITypeConverter<House, HouseDto>
{
    private readonly IMapper _mapper;

    public HouseConverter(IMapper mapper)
    {
        _mapper = mapper;
    }

    public HouseDto Convert(House source, HouseDto destination, ResolutionContext context)
    {
        if (source == null) return null;

        destination = destination ?? new HouseDto();
        destination.PropertyB = source.PropertyB;
        destination.People = _mapper.Map<List<PersonDto>>(source.People);

        return destination;
    }
}

public class PersonConverter : ITypeConverter<Person, PersonDto>
{
    private readonly IMapper _mapper;

    public PersonConverter(IMapper mapper)
    {
        _mapper = mapper;
    }

    public PersoneDto Convert(Person source, PersonDto destination, ResolutionContext context)
    {
        if (source == null) return null;

        destination = destination ?? new PersonDto();
        destination.PropertyB = source.PropertyB;
        destination.House = _mapper.Map<HouseDto>(source.House);

        return destination;
    }
}
公共类HouseConverter:ITypeConverter
{
专用只读IMapper\u映射器;
公共房屋转换器(IMapper映射器)
{
_映射器=映射器;
}
要转换的公共房屋(房屋来源、房屋目的地、ResolutionContext上下文)
{
if(source==null)返回null;
目的地=目的地??新房子到();
destination.PropertyB=source.PropertyB;
destination.People=\u mapper.Map(source.People);
返回目的地;
}
}
公共类PersonConverter:ITypeConverter
{
专用只读IMapper\u映射器;
公共人员转换器(IMapper映射器)
{
_映射器=映射器;
}
要转换的公共人员(人员来源、人员目的地、ResolutionContext上下文)
{
if(source==null)返回null;
目的地=目的地??新的个人目标();
destination.PropertyB=source.PropertyB;
destination.House=\u mapper.Map(source.House);
返回目的地;
}
}
显然,这将导致StackOverflowException,因为这些关系创建了一个循环引用。为了避免这种情况,在使用内置映射方法时,您可以使用AutoMapper的MaxDepth选项,但当您开始使用自己的转换器时,此功能显然会丢失。我想要一些关于在自定义转换器中实现此功能的正确方法的指导

问题是:在AutoMapper的v5.0之前,自定义转换器可以访问ResolutionContext,其中包含MaxDepth的值,而且看起来还包含当前深度。这将允许我检测是否达到深度限制,并停止任何进一步的递归。但是,随着5.0中ResolutionContext的更改,我找不到任何方法来访问MaxDepth或current depth这些值在某种程度上对ITypeConverter仍然可用,还是我们现在需要自己实现这些值?在线文档没有提供有关此特定用法的很多详细信息