1

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.

SonOfPirate
  • 5,642
  • 3
  • 41
  • 97
  • You're not wrong when asserting that "projection by convention" (or unflattening) is not possible. Take a look at this question : http://stackoverflow.com/questions/3145062/using-automapper-to-unflatten-a-dto – Raphaël Althaus Mar 02 '12 at 08:00
  • Yea, I tried the different ways described in that article but none of them allowed by the map the child object from the data reader. – SonOfPirate Mar 02 '12 at 17:47

0 Answers0