title: A better way to use AutoMapper to flatten nested objects?
body: <p>I have been flattening domain objects into DTOs as shown in the example below:</p>
  
  <pre><code>public class Root
  {
      public string AParentProperty { get; set; }
      public Nested TheNestedClass { get; set; }
  }
  
  public class Nested
  {
      public string ANestedProperty { get; set; }
  }
  
  public class Flattened
  {
      public string AParentProperty { get; set; }
      public string ANestedProperty { get; set; }
  }
  
  // I put the equivalent of the following in a profile, configured at application start
  // as suggested by others:
  
  Mapper.CreateMap&lt;Root, Flattened&gt;()
        .ForMember
         (
            dest =&gt; dest.ANestedProperty
            , opt =&gt; opt.MapFrom(src =&gt; src.TheNestedClass.ANestedProperty)
         );
  
  // This is in my controller:
  Flattened myFlattened = Mapper.Map&lt;Root, Flattened&gt;(myRoot);
  </code></pre>
  
  <p>I have looked at a number of examples, and so far this seems to be the way to flatten a nested hierarchy. If the child object has a number of properties, however, this approach doesn't save much coding. </p>
  
  <p>I found this example:</p>
  
  <p><a href="http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx">http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx</a></p>
  
  <p>but it requires instances of the mapped objects, required by the Map() function, which won't work with a profile as I understand it. </p>
  
  <p>I am new to AutoMapper, so I would like to know if there is a better way to do this. </p>