Automapper将一个属性映射到多个

JAV*_*AVH 5 c# visual-studio-2010 automapper automapper-2 automapper-3

我在源对象和目标对象之间遇到了AutoMapper的挑战.我会尝试解释这种情况.在我的src对象上,我有一个字符串,根据它的长度,它应该映射到我的目标对象的多个属性.

class source
{
   public int Id {get; set;}
   /* some other properties */
   public string Value {get; set;}
}

class destination
{
   public int Id {get; set;}
   /* some other properties with the same name as the source */
   public string Value1 {get; set;}
   public string Value2 {get; set;}
   public string Value3 {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

预期的最大长度为30个字符(可能小于仅映射到两个属性或一个属性的字符).因此,每10个将映射到每个目标属性.我试图使用AutoMapper中的ResolveUsing方法,但是没有办法让函数知道我应该带回哪个段.所以我想忽略这些属性的映射,并在Automapper完成其他属性的工作后手动执行此操作

And*_*ker 8

在途中,你可以做到这一点是利用.ConstructUsing告诉AutoMapper如何创建对象,您可以创建一个映射功能Value1,Value2,Value3手动,然后让AutoMapper映射剩余的属性.例如:

static destination ConstructDestination(source src)
{
    List<string> chunked = src.Value
        .Select((ch, index) => new { Character = ch, Index = index })
        .GroupBy(
            grp => grp.Index / 10,
            (key, grp) => new string(grp.Select(itm => itm.Character).ToArray()))
        .ToList();

    var dest = new destination
    {
        Value1 = chunked.Count > 0 ? chunked[0] : null,
        Value2 = chunked.Count > 1 ? chunked[1] : null,
        Value3 = chunked.Count > 2 ? chunked[2] : null
    };

    return dest;
}

Mapper.CreateMap<source, destination>()
    .ConstructUsing(ConstructDestination)
    .ForMember(dest => dest.Value1, opt => opt.Ignore())
    .ForMember(dest => dest.Value2, opt => opt.Ignore())
    .ForMember(dest => dest.Value3, opt => opt.Ignore());
    /* Id is mapped automatically. */
Run Code Online (Sandbox Code Playgroud)

当然,如果在实际场景中你有三个以上的Value字段,这可能会变得很讨厌 - 在这种情况下你可以使用反射来设置属性.


小智 6

您可以使用ForMember规范创建映射函数

private void CreateMap(){
    Mapper.CreateMap<source,destination>()
        .ForMember(v=> v.Value1,
            opts => opts.MapFrom( src=> src.Value.Substring(0,10)))
        .ForMember(v=> v.Value2,
            opts => opts.MapFrom( src=> src.Value.Substring(10,10)))
        .ForMember(v=> v.Value3,
            opts => opts.MapFrom( src=> src.Value.Substring(20,10)));
}
Run Code Online (Sandbox Code Playgroud)

您可以只评估原始字符串包含适当长度的每个映射,否则返回string.Empty或填充它以满足您的需要.

用法(使用LinqPad):

void Main(){
   CreateMap();
   var source = new source();
   source.Id=1;
   source.Value="123454678901234546789012345467";
   var res = Mapper.Map<destination>(source);
   res.Dump();
}
Run Code Online (Sandbox Code Playgroud)