2016-10-27 12 views
2

AutoMapperを使用してオブジェクトのフラット化を試みています。AutoMapper:複数のソースプロパティからのコレクションへのマップ

public class Source 
{ 
    public string Name {get;set;} 
    public string Child1Property1 {get;set;} 
    public string Child1Property2 {get;set;} 
    public string Child2Property1 {get;set;} 
    public string Child2Property2 {get;set;} 
} 

私は私のマッピング設定

public static class AutoMapperConfiguration 
{ 
    public static MapperConfiguration Configure() 
    { 
     var config = new MapperConfiguration(
      cfg => 
      { 
       cfg.CreateMap<Source, Destination>() 
        .ForMember(dest => dest.Children, /* What do I put here?*/)) 
       // I don't think this is correct 
       cfg.CreateMap<Source, Child>() 
        .ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child1Property1)) 
        .ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child1Property2)) 
        .ForMember(dest => dest.Property1, opt => opt.MapFrom(src => src.Child2Property1)) 
        .ForMember(dest => dest.Property2, opt => opt.MapFrom(src => src.Child2Property2)); 

      }); 
     return config; 
    } 
} 

先にこれに

public class Destination 
{ 
    public string Name {get;set;} 
    public List<Child> Children {get;set;} 
} 

public class Child 
{ 
    public string Property1 {get;set;} 
    public string Property2 {get;set;} 
} 

をマップするを次のように私は今、私は自分のコードをテストするときに私が得るソースを持っていますmapper.Map<List<Child>>(source)を使用して私はAutoMapperMappingException: Missing type map configuration or unsupported mapping.を得ます。これは、マッピングcがないので意味がありますList<Child>に設定されています。 mapper.Map<Child>(source)を実行すると、プロパティの値がすべてnullChildインスタンスになります。

私は残念ながらSourceクラスを変更する立場にはありません。

AutoMapperでこれはまったく可能ですか?もしそうなら、どのように?

答えて

0

Sourceクラスにメソッドを追加して、子リストを取得できます。 それで、マップするのがとても簡単です。

+2

簡単な例を示してください – liorsolomon

1

少なくとも2つのオプションがあります。単純な拡張方法を使用してマッピングを簡略化するか、カスタムタイプコンバータを作成することができます。

public class ConvertSourceToDestination : ITypeConverter<Source, Destination> 
{ 
    public Destination Convert(Source source, Destination destination, ResolutionContext context) 
    { 
     destination = destination ?? new Destination(); 
     destination.Children = destination.Children ?? new List<Child>(); 
     destination.Children.Add(new Child() { Property1 = source.Child1Property1, Property2 = source.Child1Property2 }); 
     destination.Children.Add(new Child() { Property1 = source.Child2Property1, Property2 = source.Child2Property2 }); 
     destination.Name = source.Name; 

     return destination; 
    } 
} 

public static class SourceExtension 
{ 
    public static IEnumerable<Child> Children(this Source source) 
    { 
     yield return new Child() { Property1 = source.Child1Property1, Property2 = source.Child1Property2 }; 
     yield return new Child() { Property1 = source.Child2Property1, Property2 = source.Child2Property2 }; 
    } 

    public static MapperConfiguration CreateMapping() 
    { 
     var config = new MapperConfiguration(
      cfg => 
      { 
       cfg.CreateMap<Source, Destination>() 
       .ForMember(dest => dest.Children, opt => opt.MapFrom(src => src.Children())); 
      }); 

     return config; 
    } 

    public static MapperConfiguration CreateMapping2() 
    { 
     var config = new MapperConfiguration(
      cfg => 
      { 
       cfg.CreateMap<Source, Destination>().ConvertUsing(new ConvertSourceToDestination()); 

      }); 

     return config; 
    } 
} 
関連する問題