2016-07-03 8 views
0

次のプロファイルをAutomapper 4.2.1から5.0に移植しました。プロファイルのコンストラクタで今使われていないConfigure()メソッドを移動し、式のConvertメソッド。私がコメントする場合、AutoMapperをバージョン5.0にアップデートすると、ITypeConverterとプロファイルが矛盾しています

var mapperConfiguration = new MapperConfiguration(c => 
{ 
    c.AddProfile<BookModelToDtoProfile>(); 
    c.AddProfile<AuthorModelToDtoProfile>(); 
    c.CreateMissingTypeMaps = true; 
}); 

不思議:

public class AuthorModelToDtoProfile : Profile 
{ 
    public AuthorModelToDtoProfile() { 
     CreateMap<Author, AuthorDto>() 
      .ForMember(a => a.AuthorId, o => o.MapFrom(m => m.Id)) 
      .ForMember(a => a.FullName, o => o.MapFrom(m => $"{m.FirstName} {m.Surname}")); 
    } 
} 

public class BookModelToDtoProfile : Profile 
{ 
    public BookModelToDtoProfile() { 
     CreateMap<IEnumerable<Genre>, IEnumerable<int>>() 
      .ConvertUsing(new GenreExpression()); 
    } 
} 

public class GenreExpression : ITypeConverter<IEnumerable<Genre>, IEnumerable<int>> 
{ 
    public IEnumerable<int> Convert(IEnumerable<Genre> source, ResolutionContext context) 
     => source.Select(g => g.Id); 
} 

は今、これらの変更後、次のような構成では、それはタイプIEnumerable<Genre>のパラメータの文字列型の式を使用することはできませんと言って、ArgumentExceptionスロー私がコメントした内容に基づいて、AuthorからAuthorDtoまたはBookからBookDtoに正しくマッピングされているため、プロファイルのすべてのプロファイルが私自身正しい。

提示された状況ではTypeConverterの使用を避けることができますが、.ConvertUsing(...)がコードの可読性と保守性に役立つより複雑な表現があります。

誰もが同じ問題を抱えていた、または私が間違っていることを見ることができますか?

答えて

0

バージョン5.0.2で修正されました。

関連する問題