自动映射器,将一种对象成员类型映射到多种具体类型 [英] Automapper, Mapping one object member type to multiple concrete type

查看:97
本文介绍了自动映射器,将一种对象成员类型映射到多种具体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Party类,其中包含来自服务的object数据类型.它可以为Item属性包含两种不同的成员类型.

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; }
}

和此DTO

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

如何将Item的输出映射到BusinessItemIndividualItem. 我知道这是行不通的. Mapper.CreateMap<Party, PartyDTO>(); 我不知道条件映射是否可以解决这个问题或像这样的解析器 one .

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.

推荐答案

嘿,也许这对您有帮助!我已经测试过了,但是我只用了两天的AutoMapper!

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

好吧,这是您的备课!

    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;
    }
}

在这里,这是当前方案的MapperConfiguration!

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.

这篇关于自动映射器,将一种对象成员类型映射到多种具体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆