I have the following data returned from my database:
Number, AddressStreet, AddressCity, AddressState
My domain objects are as follows:
public class Order
{
public Address Address { get; set; }
public String Number { get; set; }
}
public class Address
{
public String City { get; set; }
public String State { get; set; }
public String Street { get; set; }
}
Using the built-in DataReaderMapper, mapping from the IDataReader containing the database data is simple except I can't get the Address object populated.
Ideally, I'd like to use DynamicMap as follows:
var order = Mapper.DynamicMap<Order>(dataReader);
When I do this, order.Number is populated but order.Address is null. When I create a new instance of the Address class in the Order constructor it's not null (as expected) but none of the properties are set.
I've tried:
Mapper.CreateMap<IDataReader, Order>()
.ForMember(target => target.Address, opt => opt.MapFrom(src => src));
and changed the column names to simple "Street", "City" and "State" with no luck.
I've tried:
Mapper.CreateMap<IDataReader, Address>();
Mapper.CreateMap<IDataReader, Order>()
.ForMember(target => target.Address, opt => opt.UseDestinationValue());
and all combinations there of. I even specified ForMembers for each property on Address. Nothing has worked.
I know AutoMapper supports flattening by convention, I can only assume from what I am finding that projection by convention (where it recognizes that AddressStreet should be mapped to Address.Street) is not possible. Am I wrong?
How do I get the above example working???
UPDATE
We've been able to get it to work using:
Mapper.CreateMap<IDataReader, Order>()
.ForMember(target => target.Address,
opt => opt.MapFrom(src => Mapper.Map<Address>(src)));
Seems kinda cludgy, though.