سؤال

I have below classes

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;  

}

I want to do auto-map from Source to Destination class. The challenge I see is to convert the list of contact numbers into independent fields in destination class. Can anyone please suggest the ways? Thanks in advance.

هل كانت مفيدة؟

المحلول

It is probably easiest to do a custom mapping function, which keeps things simple and readable:

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

Destination MapContactToDestination(Contact c)
{
    //logic here for handling conversion
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top