title: Using AutoMapper to unflatten a DTO
body: <p>I've been trying to use AutoMapper to save some time going from my DTOs to my domain objects, but I'm having trouble configuring the map so that it works, and I'm beginning to wonder if AutoMapper might be the wrong tool for the job.</p>
  
  <p>Consider this example of domain objects (one entity and one value):</p>
  
  <pre><code>public class Person
  {
      public string Name { get; set; }
      public StreetAddress Address { get; set; }
  }
  
  public class StreetAddress
  {
      public string Address { get; set; }
      public string City { get; set; }
      public string State { get; set; }
  }
  </code></pre>
  
  <p>My DTO (from a Linq-to-SQL object) is coming out looking roughly like this:</p>
  
  <pre><code>public class PersonDTO
  {
      public string Name { get; set; }
      public string Address { get; set; }
      public string City { get; set; }
      public string State { get; set; }
  }
  </code></pre>
  
  <p>I'd like to be able to do this in my repository:</p>
  
  <pre><code>return Mapper.Map&lt;PersonDTO, Person&gt;(result);
  </code></pre>
  
  <p>I've tried configuring AutoMapper every way I can figure, but I keep getting the generic <strong>Missing type map configuration or unsupported mapping</strong> error, with no details to tell me where I'm failing.</p>
  
  <p>I've tried a number of different configurations, but here are a few:</p>
  
  <pre><code>Mapper.CreateMap&lt;PersonDTO, Person&gt;()
      .ForMember(dest =&gt; dest.Address, opt =&gt; opt.MapFrom(Mapper.Map&lt;Person, Domain.StreetAddress&gt;));
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Mapper.CreateMap&lt;Person, Domain.Person&gt;()
      .ForMember(dest =&gt; dest.Address.Address1, opt =&gt; opt.MapFrom(src =&gt; src.Address))
      .ForMember(dest =&gt; dest.Address.City, opt =&gt; opt.MapFrom(src =&gt; src.City))
      .ForMember(dest =&gt; dest.Address.State, opt =&gt; opt.MapFrom(src =&gt; src.State));
  </code></pre>
  
  <p>I've read that <em>flattening</em> objects with AutoMapper is easy, but <em>unflattening</em> them isn't easy...or even possible.  Can anyone tell me whether I'm trying to do the impossible, and if not what I'm doing wrong?</p>
  
  <p>Note that my actual objects are a little more complicated, so it's possible I'm leaving out info that is the key to the error...if what I'm doing looks right I can provide more info or start simplifying my objects for testing.</p>