繁体   English   中英

使用自动映射器按惯例展开

[英]Unflatten by convention with automapper

在我的项目中,我尝试使用 automapper 尽可能按照约定将我的命令对象展开为我的域对象。
当我明确 map 映射配置文件中的两个成员时,它可以工作,但根据 automapper 文档,我认为这也应该按惯例工作。 我创建了一个 dotnetfiddle来演示最小的情况。

相关问题最终会导致人们明确添加映射,但这与 Automapper 的构建目的和文档相矛盾,不是吗? 它也不适用于展平,所以我认为反向图是一个红鲱鱼。

映射

public class Mapping: Profile
{
    public Mapping()
    {
        this.CreateMap<CreateSelectionCommand, Selection>();
        // .ForMember(selection => selection.Name, opt => opt.MapFrom(x => x.SelectionName))
        .reverseMap()
    }
}

我期望的工作

[Fact]
public void ShouldMapName()
{
    var cmd = new CreateSelectionCommand {SelectionName = "selectionName"};
    var selection = _mapper.Map<Selection>(cmd);

    Assert.Equal(cmd.SelectionName, selection.Name); <== selection.Name == ""
}

上下文类

public class Selection
{
    public string Name { get; set; }
}

public class CreateSelectionCommand
{
    public string SelectionName { get; set; }
}

我是误读了文档还是遗漏了什么?

展平是关于将嵌套的“复杂”对象映射到“更高”级别的属性,即在您的情况下,如果CreateSelectionCommand具有具有Name属性的类型的属性Selection ,它将被映射到目标类型中的SelectionName (参见this fiddle )。

您可以尝试通过添加以下内容来使用前缀

cfg.RecognizePrefixes("Selection");

到您的配置(请参阅this fiddle ),但我怀疑它是否适合基于约定的处理。

此外,您似乎可以使用ISourceToDestinationNameMapperAddMemberConfiguration添加自定义名称约定:

class TypeNamePrefixedSourceToDestinationNameMapper : ISourceToDestinationNameMapper
{
    public MemberInfo GetMatchingMemberInfo(IGetTypeInfoMembers getTypeInfoMembers, TypeDetails typeInfo,
        Type destType,
        Type destMemberType, string nameToSearch)
    {
        return getTypeInfoMembers.GetMemberInfos(typeInfo)
            .FirstOrDefault(mi => mi.Name == destType.Name + nameToSearch);
    }
}

var config = new MapperConfiguration(cfg =>
{
    cfg.AddMemberConfiguration().AddName<TypeNamePrefixedSourceToDestinationNameMapper>();

    // ...
}

至少它适用于这种简单的情况,请参阅this fiddle

暂无
暂无

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

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