Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

c# - Conditionally mapping one source type to two destination types

I have a source DTO like this

public class Member 
{
    public string MemberId {get;set;}
    public string MemberType {get;set;}
    public string Name {get;set;}
}

The member type can be "Person" or "Company".

And two destination classes like this

public class PersonMember 
{
    public int PersonMemberId {get;set;}
    public string Name {get;set;}
}

public class CompanyMember 
{
    public int CompanyMemberId {get;set;}
    public string Name {get;set;}
}

I want to use Automapper to check what the value of MemberType is in the source class and depending on that type, map to one of the two destination types.

I saw the example of conditionally mapping, but it maps the field it performs the conditional check on. I want to check the condition and map a different field.

var config = new MapperConfiguration(cfg => {
  cfg.CreateMap<Foo,Bar>()
    .ForMember(dest => dest.baz, opt => opt.Condition(src => (src.baz >= 0))); 
});

My goal is something like this -

cfg.CreateMap<Member, PersonMember>()
.ForMember(dest => PersonMember.PersonMemberId, opt => if the source.MemberType == "Person" perform mapping from MemberId, otherwise do nothing);

cfg.CreateMap<Member, CompanyMember>()
.ForMember(dest => CompanyMember.CompanyMemberId, opt => if the source.MemberType == "Company" perform mapping from MemberId, otherwise do nothing);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Introduce some base class Member. Inherit PersonMember, CompanyMember from the new base class.

Then define these mappings:

cfg.CreateMap<Dto.Member, Member>()
    .ConstructUsing((memberDto, context) => {
    switch (memberDto.MemberType)
    {
        case "PersonMember":
            return context.Mapper.Map<PersonMember>(memberDto);
        case "CompanyMember":
            return context.Mapper.Map<CompanyMember>(memberDto);
        default:
            throw new ArgumentOutOfRangeException($"Unknown MemberType {memberDto.MemberType}");
    }
});

cfg.CreateMap<Dto.Member, PersonMember>()
    .ForMember(dest => PersonMember.PersonMemberId,
               opt => opt.MapFrom(src => src.MemberId));

cfg.CreateMap<Dto.Member, CompanyMember>()
    .ForMember(dest => CompanyMember.CompanyMemberId,
               opt => opt.MapFrom(src => src.MemberId));

Now you can map using _mapperInstance.Map<Member>(memberDto);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...