需要AutoMapper建议

时间:2013-03-14 10:20:59

标签: automapper automapper-2

我有以下课程

class Contact  
{  
  string FirstName;  
  string LastName;  
  List<Phone> ContactNumbers;  
}

class Phone  
{  
  string Number;  
  PhoneType Type;  
}  

enum PhoneType  
{  
  Home, Work, Fax
}  

class Source
{
  Contact Agent;
  Contact Customer;
}

class Destination  
{  
  string AgentFirstName;  
  string AgentLastName;  
  string AgentPhoneNumber1;  
  string AgentPhoneNumber2;  
  string AgentPhoneNumber3;  
  PhoneType AgentPhoneType1;  
  PhoneType AgentPhoneType2;  
  PhoneType AgentPhoneType3; 

  string CustomerFirstName;  
  string CustomerLastName;  
  string CustomerPhoneNumber1;  
  string CustomerPhoneNumber2;  
  string CustomerPhoneNumber3;  
  PhoneType CustomerPhoneType1;  
  PhoneType CustomerPhoneType2;  
  PhoneType CustomerPhoneType3;  

}

我想从来源目的地类进行自动映射。我看到的挑战是将联系号码列表转换为目标类中的独立字段。有人可以建议方法吗?提前致谢。

1 个答案:

答案 0 :(得分:0)

最简单的做一个自定义映射功能,它可以保持简单易读:

CreateMap<Contact, Destination>().ConvertUsing(c => MapContactToDestination(c));

Destination MapContactToDestination(Contact c)
{
    //logic here for handling conversion
}