Nested Mappings

As the mapping engine executes the mapping, it can use one of a variety of methods to resolve a destination member value. One of these methods is to use another type map, where the source member type and destination member type are also configured in the mapping configuration. This allows us to not only flatten our source types, but create complex destination types as well. For example, our source type might contain another complex type:

public class OuterSource
{
	public int Value { get; set; }
	public InnerSource Inner { get; set; }
}

public class InnerSource
{
	public int OtherValue { get; set; }
}

We could simply flatten the OuterSource.Inner.OtherValue to one InnerOtherValue property, but we might also want to create a corresponding complex type for the Inner property:

public class OuterDest
{
	public int Value { get; set; }
	public InnerDest Inner { get; set; }
}

public class InnerDest
{
	public int OtherValue { get; set; }
}

In that case, we would need to configure the additional source/destination type mappings:

Mapper.CreateMap<OuterSource, OuterDest>();
Mapper.CreateMap<InnerSource, InnerDest>();
Mapper.AssertConfigurationIsValid();

var source = new OuterSource
	{
		Value = 5,
		Inner = new InnerSource {OtherValue = 15}
	};

var dest = Mapper.Map<OuterSource, OuterDest>(source);

dest.Value.ShouldEqual(5);
dest.Inner.ShouldNotBeNull();
dest.Inner.OtherValue.ShouldEqual(15);

A few things to note here:
  • Order of configuring types does not matter
  • Call to Map does not need to specify any inner type mappings, only the type map to use for the source value passed in
With both flattening and nested mappings, we can create a variety of destination shapes to suit whatever our needs may be.

Last edited Mar 5, 2009 at 12:09 AM by jbogard, version 1

Comments

shipon_eye Apr 27, 2011 at 8:48 PM 
Hello!
Can I configure the case sensitiveness for mapping?
How can i handle it?
Please suggest me.

Thanks

FredrikRofors Jan 6, 2011 at 11:47 AM 
@chrispadgett

AutoMapper.Mapper.CreateMap<SongDTO, Song>().ConstructUsing((songDto) => new Song {Artist = new Artist
{Name = songDto.ArtistName}});

DeeMon Jun 18, 2010 at 7:18 AM 
Hi is there a solution to chrisadgett's problem?

chrispadgett Apr 26, 2010 at 11:52 PM 
Hi
If I have:
1. A Song class that contains an Artist property of type Artist, which contains a Name property of type string
2. A SongDTO class that contains an ArtistName property of type string
How do I map from SongDTO.ArtistName to Song.Artist.Name, where Song.Artist must be constructed during mapping?
Regards,
Chris

jbogard Feb 18, 2010 at 4:38 PM 
@soulja

Are the Publisher and Author values available on the Title object, or would have to go look for them somewhere else?

soulja Feb 14, 2010 at 9:22 PM 
I'll be very thankful for answer!

soulja Feb 14, 2010 at 9:21 PM 
Hello!
I have a question:
If I have a class "Title"(with some fields: Name, PubDate, Publisher, Author, etc), where Publisher and Author are objects of another classes.
Can you tell me how can do mapping in this case(To another class "TitleDto" with the same fields)?