Automapper, Mapping one object member type to multiple concrete type

血红的双手。 提交于 2019-12-13 02:04:26

问题


I have this Party class which contains an object data type coming from a service. It can contain two different member types for the Item property.

public class Party
{
    public string DMVID {get; set;}
    public object Item { get; set; }
}

and this DTO

public class PartyDTO
{
    public string DMVID {get; set;}
    public BusinessDTO BusinessItem { get; set; }
    public IndividualDTO IndividualItem { get; set; }
}

How can I map the output of the Item to BusinessItem or IndividualItem. I know this one would not work. Mapper.CreateMap<Party, PartyDTO>(); I don't know if conditional mapping can solve this or a resolver like this one.


回答1:


Hey maybe this will help you out! I tested it, but i am using AutoMapper just for two days!

Allright here are your noted classes!!!

    public class Party
{
    public string DMVID { get; set; }
    public object Item { get; set; }
}


public class PartyDTO
{
    public string DMVID { get; set; }
    public BuisnessDTO BusinessItem { get; set; }
    public IndividualDTO IndividualItem { get; set; }
}


public class BuisnessDTO
{
    public int Number
    {
        get;
        set;
    }
}

public class IndividualDTO
{
    public string Message
    {
        get;
        set;
    }
}

and here your is the MapperConfiguration for this current scenario!

    // Edit There was no need here for some conditions
  AutoMapper.Mapper.CreateMap<Party, PartyDTO>()
                .ForMember(dto => dto.BusinessItem, map => 
                    map.MapFrom(party => party.Item as BuisnessDTO);
                )
                .ForMember(dto => dto.IndividualItem, map =>
                    map.MapFrom(party => party.Item as IndividualDTO);
                );

    // And this is another way to achive the mapping in this scenario
                AutoMapper.Mapper.CreateMap<PartyDTO, Party>()
                    .ForMember(party => party.Item, map => map.MapFrom( dto => (dto.BusinessItem != null) ? (dto.BusinessItem as object) : (dto.IndividualItem as object)));

And i created this sample for it!

            Party firstParty = new Party()
        {
            DMVID = "something",
            Item = new BuisnessDTO()
            {
                Number = 1
            }
        };


        Party secondParty = new Party()
        {
            DMVID = "something",
            Item = new IndividualDTO()
            {
                Message = "message"
            }
        };



        PartyDTO dtoWithBuisness = AutoMapper.Mapper.Map<PartyDTO>(firstParty);
        PartyDTO dtoWithIndividual = AutoMapper.Mapper.Map < PartyDTO>(secondParty);

        Party afterParty = AutoMapper.Mapper.Map<Party>(dtoWithBuisness);
        afterParty = AutoMapper.Mapper.Map < Party>(dtoWithIndividual);

Of course there are other possibility, but I think thats exactly what you wanted.



来源:https://stackoverflow.com/questions/27415739/automapper-mapping-one-object-member-type-to-multiple-concrete-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!