0

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.

techspider
  • 3,370
  • 13
  • 37
  • 61

1 Answers1

0

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
}
Rob West
  • 5,189
  • 1
  • 27
  • 42
  • I'm sorry for missing clarity... I have amended the original post to give correct picture on what I require. Thanks for re-look. I also feel that writing logic using Resolver or ConvertUsing would be almost equivalent to do manual mapping of each field. I would like to see if there are any other options also available... – techspider Mar 14 '13 at 12:52
  • There is no way to do this with a fully automatic mapping, for such a case it is just so much easier and quicker to do a custom conversion. – Rob West Mar 15 '13 at 09:10