次のプロファイルを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(...)
がコードの可読性と保守性に役立つより複雑な表現があります。
誰もが同じ問題を抱えていた、または私が間違っていることを見ることができますか?