title: Can AutoMapper mappings be composed?
body: <p>The models I'm working with include an entry object which I'd like to map as if its child object were the entire object.</p>
  
  <p>Here is a simplified version of the problem. I'd like an instance of <code>OurWrappedSource</code> to map directly to <code>OurTarget</code>.</p>
  
  <pre><code>class OurTarget
  {
      public Guid Id { get; set; }
      public string Notes { get; set; }
      public int Quantity { get; set; }
  }
  
  class OurSource
  {
      public Guid Id { get; set; }
      public string Notes { get; set; }
      public int Quantity { get; set; }
  }
  
  class OurWrappedSource
  {
      public OurSource Source { get; set; }
  }
  
  private static void TestUnwrapUsingConfig(MapperConfiguration config)
  {
      config.AssertConfigurationIsValid();
      IMapper mapper = new Mapper(config);
  
      var wrappedSource = new OurWrappedSource
      {
          Source = new OurSource
          {
              Id = new Guid("123e4567-e89b-12d3-a456-426655440000"),
              Notes = "Why?",
              Quantity = 27
          }
      };
  
      var target = mapper.Map&lt;OurTarget&gt;(wrappedSource);
  
      Assert.Equal(wrappedSource.Source.Id, target.Id);
      Assert.Equal(wrappedSource.Source.Notes, target.Notes);
      Assert.Equal(wrappedSource.Source.Quantity, target.Quantity);
  }
  </code></pre>
  
  <p>The following configuration works, but is unwieldy for more than a couple of members:</p>
  
  <pre><code>// Works, but isn't *auto* enough
  TestUnwrapUsingConfig(new MapperConfiguration(cfg =&gt;
  {
      cfg.CreateMap&lt;OurWrappedSource, OurTarget&gt;()
          .ForMember(src =&gt; src.Id, opts =&gt; opts.MapFrom(wrappedSource =&gt; wrappedSource.Source.Id))
          .ForMember(src =&gt; src.Notes, opts =&gt; opts.MapFrom(wrappedSource =&gt; wrappedSource.Source.Notes))
          .ForMember(src =&gt; src.Quantity, opts =&gt; opts.MapFrom(wrappedSource =&gt; wrappedSource.Source.Quantity));
  }));
  </code></pre>
  
  <p>What I'd like to be able to do is define two intermediate mappings an then compose them:</p>
  
  <ol>
  <li>Map <code>OurWrappedSource</code> directly to <code>OurSource</code></li>
  <li>Map <code>OurSource</code> directly to <code>OurTarget</code></li>
  <li>Map <code>OurWrappedSource</code> to <code>OurTarget</code> by composing mapping 1 with mapping 2</li>
  </ol>
  
  <p>After some hammering, I have this configuration:</p>
  
  <pre><code>// Works, but #3 probably isn't ProjectTo-friendly
  TestUnwrapUsingConfig(new MapperConfiguration(cfg =&gt;
  {
      // 1
      cfg.CreateMap&lt;OurWrappedSource, OurSource&gt;()
          .ConvertUsing(wrappedSource =&gt; wrappedSource.Source);
  
      // 2
      cfg.CreateMap&lt;OurSource, OurTarget&gt;();
  
      // 3
      cfg.CreateMap&lt;OurWrappedSource, OurTarget&gt;()
          .ConstructUsing((wrappedSource, ctx) =&gt;
              ctx.Mapper.Map&lt;OurTarget&gt;(ctx.Mapper.Map&lt;OurSource&gt;(wrappedSource))
              )
          .ForAllOtherMembers(opts =&gt; opts.Ignore());
  }));
  </code></pre>
  
  <p>This works exactly as specified, but mapping 3 seems perhaps a little more explicit and/or kludgey than it should. It involves code in a <code>Func</code> (rather than an expression), which makes me think it probably won't optimize well when used with <code>ProjectTo()</code>. Is there a way to rewrite mapping 3 to address these issues?</p>