Flattening

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 Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal? Credit { get; set; }
        public Address Address { get; set; }

        public string GetFullAddress()
        {
               return string.Format("{0} / {1}", this.Address.City, this.Address.Country);
        }
    }

    public class CustomerDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string AddressCity { get; set; }
        public string FullAddress { get; set; }
    }


Test Data

public Customer GetCustomer()
{
    Customer customer = new Customer(){
        Id = 5,
        Name = "Tim",
        Address = new Address(){ City = "New Jersey", Country = "USA" }
    };

    return customer;
}


Map

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

   //result : 
   // customerDto.Id == 5
   // customerDto.Name == "Tim"
   // customerDto.AddressCity == "New Jersey"
   // customerDto.FullAddress == "New Jersey / USA"


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