Complex Model

Test Classes

public class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

public class AddressDTO
{
    public int Id { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal? Credit { get; set; }
    public Address Address { get; set; }
    public Address HomeAddress { get; set; }
    public Address[] Addresses { get; set; }
    public ICollection<Address> WorkAddresses { get; set; }
 }

 public class CustomerDTO
 {
    public int Id { get; set; }
    public string Name { get; set; }
    public Address Address { get; set; }
    public AddressDTO HomeAddress { get; set; }
    public AddressDTO[] Addresses { get; set; }
    public List<AddressDTO> WorkAddresses { get; set; }
    public string AddressCity { get; set; }
 }


Test Data

private Customer GetCustomer()
{
	Customer customer = new Customer()
	{
		Id = 1,
		Name = "Timucin Kivanc",
		Credit = 234.7m,
		Address = new Address() { 
			City = "Istanbul", 
			Country = "Turkey", 
			Id = 1, 
			Street = "Istiklal cad." 
		},
		HomeAddress = new Address() { 
			City = "Istanbul", 
			Country = "Turkey", 
			Id = 2, 
			Street = "Istiklal cad." 
		},
		WorkAddresses = new List<Address>() { 
			new Address() { 
				   City = "Istanbul", 
				   Country = "Turkey", 
				   Id = 5, 
				   Street = "Istiklal cad." 
			},
			new Address() { 
				   City = "Izmir", 
				   Country = "Turkey", 
				   Id = 6, 
				   Street = "Konak" 
			}
		},
		Addresses = new List<Address>() { 
			new Address() { 
				   City = "Istanbul", 
				   Country = "Turkey", 
				   Id = 3, 
				   Street = "Istiklal cad." 
			},
			new Address() { 
				   City = "Izmir", 
				   Country = "Turkey", 
				   Id = 4, 
				   Street = "Konak" 
			}
		}.ToArray()
	};

	return customer;
}


Map

var customerDto = TypeAdapter.Adapt<Customer, CustomerDTO>(GetCustomer());

//result : 
// customerDto.Id == 1
// customerDto.Name == "Timucin Kivanc"
// customerDto.AddressCity == "Istanbul"
...
//All properties mapped

Last edited Mar 28, 2014 at 11:28 AM by timucinkivanc, version 6