[英]Automapper custom value resolver reuse for multiple types
I have a project which I am trying to use AutoMapper to map from multiple classes in each of these classes there are properties where I would like to use some custom logic to parse the source value to the destination.我有一个项目,我试图使用 AutoMapper 从每个这些类中的多个类进行映射,有一些属性我想使用一些自定义逻辑来解析源值到目标。 I have tried to use custom resolver methods as documented on the AutoMapper docs.
我尝试使用 AutoMapper 文档中记录的自定义解析器方法。
Here is my code:这是我的代码:
public class CustomDateTextHandler : IValueResolver<object, object, string>
{
public string Resolve(object source, object destination, string destMember, ResolutionContext context)
{
string txt = source.ToString();
txt.Replace("AM/PM", "tt");
txt.Replace("HH:MM", "hh:mm");
if (txt.Contains("format"))
{
txt.Replace("mmm", "MMM");
}
return txt;
}
}
public class SMapping : Profile
{
public SMapping()
{
CreateMap<SourceForm, s_form>()
.ForMember(dest => dest.id, opt => opt.Ignore())
.ForMember(dest => dest.cell_text, opt => opt.MapFrom<CustomDateTextHandler>())
.ForMember(dest => dest.fn_def, opt => opt.MapFrom<CustomCodeTextResolver>());
}
What I am trying to get is the cell_text value processed with my replace logic in the resolver method but the issue I am facing is that what is being passed to the resolver is the entire SMapping instance, I would like to be able to reuse the resolver code across different classes where the property names will be different, however looking at what it going on at the moment I could not really use the resolver code across my different classes.我想要得到的是在解析器方法中用我的替换逻辑处理的 cell_text 值,但我面临的问题是传递给解析器的是整个 SMapping 实例,我希望能够重用解析器跨不同类的代码,其中属性名称会有所不同,但是看看目前发生的事情,我无法真正在不同的类中使用解析器代码。
Can someone help me?有人能帮我吗?
Thank you in advance.先感谢您。
Use IMemberValueResolver instead of IValueResolver.使用 IMemberValueResolver 而不是 IValueResolver。
Compared to IValueResolver, its Resolve function gets one more parameter: value.与 IValueResolver 相比,它的 Resolve 函数多了一个参数:value。
Registering mapping with IMemberValueResolver requires you to pass 1 extra parameter - not the 'value' directly, but a lambda that will produce a 'value' from given source object.使用 IMemberValueResolver 注册映射需要您传递 1 个额外参数 - 不是直接传递“值”,而是一个将从给定源对象生成“值”的 lambda。
public class CustomDateTextHandler :
IMemberValueResolver< // note: different interface
object, object,
string, string // note: 1 more parameter
>
{
public string Resolve(
object source, object destination,
string sourceValue, string destMember, // note: 1 more parameter
ResolutionContext context
)
{
// here, see the difference:
// source - source object, whole
// sourceMember - value produced by extra lambda passed in mapping
}
}
public class SMapping : Profile
{
public SMapping()
{
CreateMap<SourceForm, s_form>()
...
.ForMember(
dest => dest.cell_text,
opt => opt.MapFrom<CustomDateTextHandler, string>(source => source.PROPERTY11)) // note: this produces that sourceValue
.ForMember(
dest => dest.fn_def,
opt => opt.MapFrom<CustomCodeTextResolver, string>(source => source.PROPERTY22)); // note: this produces that sourceValue
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.