例如,如果我有IMemberValueResolver类型:
Mapper.Initialize(cfg => {
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Total,
opt => opt.ResolveUsing<CustomResolver, decimal>(src => src.SubTotal));
cfg.CreateMap<OtherSource, OtherDest>()
.ForMember(dest => dest.OtherTotal,
opt => opt.ResolveUsing<CustomResolver, decimal>(src => src.OtherSubTotal));
});
public class CustomResolver : IMemberValueResolver<object, object, decimal, decimal> {
public decimal Resolve(object source, object destination, decimal sourceMember, decimal destinationMember, ResolutionContext context) {
// logic here
}
}
如果我的CustomResolver接受构造函数中的参数,例如:
,该怎么办?public class CustomResolver : IMemberValueResolver<object, object, decimal, decimal> {
public CustomResolver(string text){
}
public decimal Resolve(object source, object destination, decimal sourceMember, decimal destinationMember, ResolutionContext context) {
// logic here
}
}
如何为IMemberValueResolver类型的自定义解析器创建地图? 我们可以尝试直接提供实例:
Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Total,
opt => opt.ResolveUsing(new CustomResolver("Text"))
);
但是在这种情况下我们如何传递我们想要用于解析器中映射的源参数?
答案 0 :(得分:0)
这很多:
.ForMember(dest => dest.Total,
opt => opt.ResolveUsing(new CustomResolver("argument"), src => src.SubTotal));
但问题是当你在解析器中链接DI时......
答案 1 :(得分:-1)
我想我们应该使用ResolutionContext而不是构造函数来传递文本。