因此,除了通常的 DTO 到业务映射器之外,我还有一些东西,我正在尝试用最少的映射代码来映射它们。
设置
public class Target {
public string propA { get; set; }
public string propB { get; set; }
public string propC { get; set; }
public string propD { get; set; }
public string propE { get; set; }
public List<KeyValuePair> Tokens { get; set; }
}
public class Source {
public SomeClass SomeClass { get; set; }
public AnotherClass AnotherClass { get; set; }
}
public class SomeClass {
public string propA { get; set; }
public string propB { get; set; }
public string propDifferent { get; set; }
public List<KeyValuePair> Tokens { get; set; }
}
public class AnotherClass {
public string propC { get; set; }
public string propD { get; set; }
public List<KeyValuePair> Tokens { get; set; }
}
映射器配置
Mapper.CreateMap<SomeClass, Target>()
.ForMember(dest => dest.propE, opt => opt.MapFrom(src => src.propDifferent));
Mapper.CreateMap<AnotherClass, Target>();
Mapper.CreateMap<Source, Target>()
.ForMember(dest => dest, opt => opt.MapFrom(src => src.SomeClass))
.ForMember(dest => dest, opt => opt.MapFrom(src => src.AnotherClass));
这样做会抛出
错误:AutoMapper.AutoMapperConfigurationException:成员的自定义配置仅支持类型上的顶级单个成员。
而且我还需要服用AnotherClass.Tokens
,SomeClass.Tokens
并将其添加到Target.Tokens
.
我知道我可以使用.ConvertUsing
,但是我必须为每个属性定义映射,并且我失去了基于约定的映射来匹配属性的优势。
有没有其他方法可以实现这一点(除了.ConvertUsing
手动映射每个属性)?
如果不是通过Automapper,是否可以通过EmitMapper?我想通过 EmitMapper's 添加到 Tokens 列表可能是可行的PostProcessing
。
更新
经过一番黑客攻击,我找到了一种方法:
public static IMappingExpression<TSource, TDestination> FlattenNested<TSource, TNestedSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var sourceType = typeof(TNestedSource);
var destinationType = typeof(TDestination);
var sourceProperties = sourceType.GetProperties().ToDictionary(x => x.Name.ToLowerInvariant());
var childPropName = typeof (TSource).GetProperties().First(x => x.PropertyType == sourceType).Name;
var mappableProperties = destinationType.GetProperties()
.Where(p => sourceProperties.ContainsKey(p.Name.ToLowerInvariant()) &&
sourceProperties[p.Name.ToLowerInvariant()].PropertyType ==
p.PropertyType)
.Select(p => new {DestProperty = p.Name, SrcProperty = sourceProperties[p.Name.ToLowerInvariant()].Name});
foreach (var property in mappableProperties)
{
expression.ForMember(property.DestProperty,
opt => opt.MapFrom(src => src.GetPropertyValue(childPropName).GetPropertyValue(property.SrcProperty)));
}
return expression;
}
注意:我这样做是Name.ToLowerInvariant()
为了能够匹配AccountID
->AccountId
和类似的。
用法
AutoMapper.Mapper.CreateMap<Source, Target>()
.FlattenNested<Source, SomeClass, Target>()
.FlattenNested<Source, AnotherClass, Target>()
.ForMember(dest => dest.propE, opt => opt.MapFrom(src => src.propDifferent));
我发现了一些其他属性IMappingExpression
,我也许可以使用和清理很多这些属性。当我找到它们时会更新。