You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AutoMapper配置复用:如何复用已有Contact映射避免重复配置

AutoMapper复用已有映射解决非1:1结构转换问题

你需要将Vendor中的主、次Contact包装到ContactResponse对象中,同时复用已定义的Contact到CommunicationContactResponse的映射逻辑,避免重复编写字段映射。以下是两种可行的解决方案:

方案一:使用映射上下文调用已有转换

在MapFrom中通过AutoMapper的上下文对象context.Mapper直接调用已配置的Contact到CommunicationContactResponse映射,无需重复编写字段赋值逻辑:

public class VendorResponseMappingProfile : Profile 
{
    public VendorResponseMappingProfile()
    {
        this.CreateMap<Contact, CommunicationContactResponse>()
            .ForMember(dest => dest.Phone, opt => opt.MapFrom(src => src.Phone))
            .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email));

        this.CreateMap<Vendor, VendorResponse>()
            .ForMember(dest => dest.id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
            .ForMember(dest => dest.Memo, opt => opt.MapFrom(src => src.Memo))
            .ForMember(dest => dest.Active, opt => opt.MapFrom(src => src.Active))
            .ForMember(dest => dest.Contact, opt => opt.MapFrom((src, dest, _, context) => 
                new ContactResponse
                {
                    PrimaryContact = src.PrimaryContact != null 
                        ? context.Mapper.Map<CommunicationContactResponse>(src.PrimaryContact) 
                        : null,
                    SecondaryContact = src.SecondaryContact != null 
                        ? context.Mapper.Map<CommunicationContactResponse>(src.SecondaryContact) 
                        : null
                }));
    }
}

方案二:单独定义ContactResponse的映射配置

先为Vendor到ContactResponse创建独立的映射规则,再在Vendor到VendorResponse的映射中直接复用该规则,结构更清晰,便于后续维护:

public class VendorResponseMappingProfile : Profile 
{
    public VendorResponseMappingProfile()
    {
        this.CreateMap<Contact, CommunicationContactResponse>()
            .ForMember(dest => dest.Phone, opt => opt.MapFrom(src => src.Phone))
            .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email));

        // 单独定义Vendor到ContactResponse的映射
        this.CreateMap<Vendor, ContactResponse>()
            .ForMember(dest => dest.PrimaryContact, opt => opt.MapFrom(src => src.PrimaryContact))
            .ForMember(dest => dest.SecondaryContact, opt => opt.MapFrom(src => src.SecondaryContact));

        // 在VendorResponse映射中复用ContactResponse的转换逻辑
        this.CreateMap<Vendor, VendorResponse>()
            .ForMember(dest => dest.id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
            .ForMember(dest => dest.Memo, opt => opt.MapFrom(src => src.Memo))
            .ForMember(dest => dest.Active, opt => opt.MapFrom(src => src.Active))
            .ForMember(dest => dest.Contact, opt => opt.MapFrom(src => src));
    }
}

两种方案都能实现映射逻辑的复用,方案二更符合AutoMapper的配置设计思想,当ContactResponse的结构或映射规则需要修改时,只需调整一处配置即可。

内容的提问来源于stack exchange,提问作者Androme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 19:07:30