访问要在 IValueResolver 中映射的目标 属性 名称

Access the destination property name to be mapped in IValueResolver

我想为同一个 class 的几个属性重用同一个 IValueResolver,所有三个要映射的属性都通过同一个过程,所以我不想重复一个代码几乎一样。

我想做这样的事情:

CreateMap<AlertLine, AlertLineDto>()
                .ForMember(dest => dest.BusinessArea, opt => opt.MapFrom<MyResolver>())

CreateMap<AlertLine, AlertLineDto>()
                .ForMember(dest => dest.Division, opt => opt.MapFrom<MyResolver>())

CreateMap<AlertLine, AlertLineDto>()
                .ForMember(dest => dest.SubDivision, opt => opt.MapFrom<MyResolver>())


public class MyResolver : IValueResolver<AlertLine, AlertLineDto, string>
    {
        private readonly ICatalogService _catalogService;
        public BusinessAreaResolver(ICatalogService catalogService)
        {
            _catalogService = catalogService;
        }
        public string Resolve(AlertLine source, AlertLineDto destination, string destMember, ResolutionContext context)
        {
            // How to access the destination property name to be mapped?
            // BusinessArea, Division, SubDivision, and so on...

            string val = string.Empty;
            var variant = JsonSerializer.Deserialize<AddlInformation>(source.AddlDimension);
            if (variant.BUSINESS_AREA_ID > 0)
            {
                var businessArea = _catalogService.GetCachedBusinessAreaById(variant.BUSINESS_AREA_ID);
                val = businessArea?.Description;

            }
            return val;
        }

    }

尝试使用 IMemberValueResolver 而不是 IValueResolver

如果不同成员的逻辑不同,请发送 属性 名称而不是实际源值。

CreateMap<AlertLine, AlertLineDto>()
.ForMember(
    dest => dest.BusinessArea,
    opt => opt.MapFrom<MyResolver, string>(p => "BusinessArea"))) 
    //opt => opt.MapFrom<MyResolver, string>(source => source.BusinessArea))
.ForMember(
    dest => dest.Division,
    opt => opt.MapFrom<MyResolver, string>(p => "Division")))
   //opt => opt.MapFrom<MyResolver, string>(source => source.Division))
.ForMember(
    dest => dest.SubDivision,
    opt => opt.MapFrom<MyResolver, string>(p => "SubDivision"));
    //opt => opt.MapFrom<MyResolver, string>(source => source.SubDivision))

propertyName参数将帮助您根据参数值识别当前调用了哪个映射。 returnValue 将映射到适当的目标成员

public class MyResolver : IMemberValueResolver<AlertLine, AlertLineDto, string, string>
{    
    public string Resolve(AlertLine source, AlertLineDto destination, string propertyName, string destMember, ResolutionContext context)
    {
        string sourceValue = string.Empty;
        string returnValue = string.Empty;

        switch (propertyName)
        {
            case "BusinessArea":
                 sourceValue = source.BusinessArea;
                 //returnValue = Some Logic here
                break;
            case "Division":
                sourceValue = source.Division;
                //returnValue = Some Logic here
                break;
            case "SubDivision":
                sourceValue = source.SubDivision;
                //returnValue = Some Logic here
                break;

            default:
                break;
        }          
        return returnValue;
    }    
}