title: Using automapper to map a child property to same model
body: <p><strong>The problem</strong></p>
  
  <p>I have a domain model that has a revisioning system build in to it. In concept it looks like this:</p>
  
  <pre><code>Model -&gt; ModelDetails
  </code></pre>
  
  <p>The Model holds all the relations to other objects and has an <code>ICollection</code> of the <code>ModelDetails</code>. It also has a <code>details</code> property that shows the latest version. Until now I've mapped everything by manual coding to the viewmodels. Those are basically a combination of the <code>Model</code> and the <code>ModelDetails</code> (details property) object. The problem lies in defining that the mapping of the viewmodel come's both from the <code>Model</code> as the <code>Details</code> property (<code>ModelDetails</code> model). </p>
  
  <p>With the example shown below I can make it with by using an extra call, but then I would need to place that call on every conversion place instead of just one mapping call. Another complexity is that because the relation are being mapped, I can't handle the revisioning of the relations atm.</p>
  
  <p><strong>The domain models</strong></p>
  
  <pre><code>public class Model : RevisionBasedModel&lt;Revisions.ModelDetails&gt;
  {
      public int Id { get; set; }
      public virtual ICollection&lt;ModelTwo&gt; RelationExample { get; set; } 
               = new HashSet&lt;ModelTwo&gt;();
  }
  
  public abstract class RevisionBasedModel&lt;TRevision&gt; : IRevisionBasedModel&lt;TRevision&gt; 
  {
      public TRevision Details
      {
          get { return Revisions.OrderByDescending(x =&gt; x.TimeStamp).First();
      }
  
      public virtual ICollection&lt;TRevision&gt; Revisions { get; set; } 
              = new HashSet&lt;TRevision&gt;();
  }
  
  public class ModelDetails {
      public int Id { get; set; }
      public string PropertyOne { get; set; }
      public string PropertyTwo { get; set; }
  }
  </code></pre>
  
  <p><strong>The viewmodel</strong></p>
  
  <pre><code>public class ModelVM {
      public int Id { get; set; }
      public int RevisionId { get; set; }
      public string PropertyOne { get; set; }
      public string PropertyTwo { get; set; }
      public ICollection&lt;ModelTwoVM&gt; RelationExample { get; set; }
              = new HashSet&lt;ModelTwoVM&gt;
  }
  </code></pre>
  
  <p><strong>Current mapping configuration</strong></p>
  
  <pre><code>CreateMap&lt;Model, ModelVM&gt;()
      .ForMember(x =&gt; x.RelationExample, opt =&gt; opt.MapFrom(y =&gt; y.RelationExample));
  CreateMap&lt;ModelDetails, ModelVM&gt;()
      .ForMember(x =&gt; x.RevisionId, opt =&gt; opt.MapFrom(y =&gt; y.Id));
  
  CreateMap&lt;ModelTwo, ModelTwoVM&gt;();
  CreateMap&lt;ModelTwoDetails, ModelTwoVM&gt;()
      .ForMember(x =&gt; x.RevisionId, opt =&gt; opt.MapFrom(y =&gt; y.Id));
  </code></pre>
  
  <p><strong>Current conversion</strong></p>
  
  <pre><code>var viewModel = this._mapper.Map&lt;Model, ModelVM&gt;(model);
  viewModel = this._mapper.Map&lt;ModelDetails, ModelVM&gt;(model.Details, viewModel);
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE 24-01-2017 19:26</strong></p>
  
  <p>In the comments, the suggestion was made for a possible 'crude' way, by using a Custom type converter. I was able to make the revision part working, but because the default mapping is overwritten I lose my relation mapping.</p>
  
  <pre><code>public class RevisionConverter&lt;TModel, TRevision, TViewModel&gt; : ITypeConverter&lt;TModel, TViewModel&gt;
      where TModel : IRevisionBasedModel&lt;int, TRevision&gt;
  {
      public TViewModel Convert(TModel source, TViewModel destination, ResolutionContext context)
      {
          return context.Mapper.Map&lt;TRevision, TViewModel&gt;(source.Details, destination);
      }
  }
  </code></pre>