Mapping fields to collection

May 15, 2010 at 8:03 PM

I have an source address object that has individual address line properties, AddressLine1, AddressLine2, AddressLine3.  The target address object has a collection of lines.

Source object:

    public class Address
    {
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string AddressLine3 { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
        public string PostalCode { get; set; }
        public string Province { get; set; }
        public AddressType Type { get; set; }
    }

Target object:

    public class Address
    {
        public AddressType Type { get; set; }
        public List<string> Lines { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
    }

My handwritten mapper would look like,

                target.Lines = new List<string>();

                if (!string.IsNullOrEmpty(source.AddressLine1)) target.Lines.Add(source.AddressLine1);
                if (!string.IsNullOrEmpty(source.AddressLine2)) target.Lines.Add(source.AddressLine2);
                if (!string.IsNullOrEmpty(source.AddressLine3)) target.Lines.Add(source.AddressLine3);

Any recommendations how to configure this in EmitMapper?