从Automapper中的已解析成员映射

时间:2016-08-15 06:16:14

标签: automapper

这是一个范围,但无论如何我都会问。

我带着我的榜样:

public class PatientInfoModel : IPatientInfoModel, IHaveCustomMappings
{
    public string PatientId { get; set; }
    public string PatientIdForView { get; set; }
    public PatientEpisodeData PatientEpisode { get; set; }

    public void CreateMappings(Profile configuration)
    {
        configuration.CreateMap<PatientInfoRawDto, PatientInfoModel>()
            .ForMember(m => m.PatientIdForView, opt => opt.ResolveUsing<PatientIdResolver<PatientInfoRawDto, PatientInfoModel>>())
            .ForMember(m => m.PatientId, opt => opt.MapFrom(p => p.patID))
            .ForMember(m => m.PatientEpisode, opt => opt.MapFrom(p => new PatientEpisodeData
            {
                PatientId = p.patID,
                PatientIdForView = this.PatientIdForView
            }));
    }

    public class PatientEpisodeData
    {
        public int PatientId { get; set; }
        public string PatientIdForView { get; set; }
    }
}

正如您所看到的,对于 PatientEpisode 成员,我想从其中一个已经解析的属性( PatientIdForView )进行映射。

由于我无法弄清楚如何做到这一点,我只是在事后设置了属性。但要弄清楚这是否可能会很有趣。

注意:除非您可以将 PatientIdForView 属性传递给它,否则我对使用自定义值解析器并不感兴趣。

干杯

1 个答案:

答案 0 :(得分:1)

自定义值解析器 do 允许您将目标成员值传入其中(我假设您提到的PatientIdForView属性是目标成员值)。如果需要源成员值,可以使用成员值解析器:

http://docs.automapper.org/en/stable/Custom-value-resolvers.html

您将获得目标成员,您指定的源成员以及源/目标对象。应该是你需要的一切!