ForAllPropertyMaps 如何在配置文件中工作?
How do ForAllPropertyMaps work inside a Profile?
我正在尝试制作一个 gRPC API 并且偶然发现了一个奇怪的错误(或者我可能不知道它是如何工作的)。
如果我们想将所有 IEnumerables 映射到 RepeatedFields,我们遵循以下答案:
(函数IsToRepeatedField(PropertyMap pm)
可以在上面看到link)
尝试将 ForAllPropertyMaps 移动到配置文件时,我们的测试失败了。
public class ToRepeatedFieldProfile : Profile
{
public ToRepeatedFieldProfile()
{
ForAllPropertyMaps(IsToRepeatedField, (propertyMap, opts) => opts.UseDestinationValue());
}
}
public ProfileTests()
{
_mapperConfiguration =
new MapperConfiguration(cfg =>
{
cfg.AddProfile<ToRepeatedFieldProfile>();
});
_mapper = _mapperConfiguration.CreateMapper();
}
上面的代码不起作用,而下面的代码起作用:
public ProfileTests()
{
_mapperConfiguration =
new MapperConfiguration(cfg =>
{
cfg.ForAllPropertyMaps(IsToRepeatedField, (propertyMap, opts) => opts.UseDestinationValue());
});
_mapper = _mapperConfiguration.CreateMapper();
}
这是我们要执行的测试:
public void AutoMapper_Map_Success_Response()
{
var updatedIds = new List<Guid>
{
new Guid("53c909f8-9803-406a-921f-965ef2cf6301"),
};
var response = new Result { UpdatedIds = updatedIds }
var reply = _mapper.Map<Reply>(response);
Assert.Equal(1, reply.UpdatedIds.Count);
}
你知道我哪里想错了吗?
PS。抱歉代码混乱,我试图删除所有不重要的东西。
问题是我 运行 使用的是 9.0。随着 9.1 的夜间构建,问题得到了解决。
我正在尝试制作一个 gRPC API 并且偶然发现了一个奇怪的错误(或者我可能不知道它是如何工作的)。
如果我们想将所有 IEnumerables 映射到 RepeatedFields,我们遵循以下答案:
(函数IsToRepeatedField(PropertyMap pm)
可以在上面看到link)
尝试将 ForAllPropertyMaps 移动到配置文件时,我们的测试失败了。
public class ToRepeatedFieldProfile : Profile
{
public ToRepeatedFieldProfile()
{
ForAllPropertyMaps(IsToRepeatedField, (propertyMap, opts) => opts.UseDestinationValue());
}
}
public ProfileTests()
{
_mapperConfiguration =
new MapperConfiguration(cfg =>
{
cfg.AddProfile<ToRepeatedFieldProfile>();
});
_mapper = _mapperConfiguration.CreateMapper();
}
上面的代码不起作用,而下面的代码起作用:
public ProfileTests()
{
_mapperConfiguration =
new MapperConfiguration(cfg =>
{
cfg.ForAllPropertyMaps(IsToRepeatedField, (propertyMap, opts) => opts.UseDestinationValue());
});
_mapper = _mapperConfiguration.CreateMapper();
}
这是我们要执行的测试:
public void AutoMapper_Map_Success_Response()
{
var updatedIds = new List<Guid>
{
new Guid("53c909f8-9803-406a-921f-965ef2cf6301"),
};
var response = new Result { UpdatedIds = updatedIds }
var reply = _mapper.Map<Reply>(response);
Assert.Equal(1, reply.UpdatedIds.Count);
}
你知道我哪里想错了吗?
PS。抱歉代码混乱,我试图删除所有不重要的东西。
问题是我 运行 使用的是 9.0。随着 9.1 的夜间构建,问题得到了解决。