title: How to map self-reference with Mapster?
body: <p>I have list with items contained self reference. 
  For example:<br>
  Model  </p>
  
  <pre><code>public class Stock{
    public int StockId { get; set; }
    public int? ParentStockId { get; set; }
    public int Level { get; set; }
    public string Name { get; set; }
  }
  </code></pre>
  
  <p>ViewModel  </p>
  
  <pre><code>public class StockViewModel {
    public int Level { get; set; }
    public string Name { get; set; }
    public IEnumerable&lt;StockViewModel&gt; SubStock { get; set; }
  }
  </code></pre>
  
  <p>Sample data</p>
  
  <pre><code>var stocks = new List&lt;Stock&gt; {
      new Stock{ StockId = 1, Level = 10, Name = "Root1" },
      new Stock{ StockId = 2, Level = 10, Name = "Root2" },
      new Stock{ StockId = 3, ParentStockId = 1, Level = 20, Name = "Area1" },
      new Stock{ StockId = 4, ParentStockId = 2, Level = 20, Name = "Area2" },
      new Stock{ StockId = 5, ParentStockId = 3, Level = 30, Name = "Box11" },
      new Stock{ StockId = 6, ParentStockId = 3, Level = 30, Name = "Box12" },
      new Stock{ StockId = 7, ParentStockId = 4, Level = 30, Name = "Box21" }
  };
  </code></pre>
  
  <p>I use a similar code to achieve the desired result. But I'm sure it's wrong. I have to make a configuration before each new request.</p>
  
  <pre><code>        var mapper = new Mapper(); 
          TypeAdapterConfig&lt;Stock, StockViewModel&gt;
              .NewConfig()
              .Map(dst =&gt; dst.SubStock, src =&gt; stocks.Where(x =&gt; x.ParentStockId == src.StockId).ToList());
          var viewStocks = mapper.Map&lt;IEnumerable&lt;Stock&gt;, IEnumerable&lt;StockViewModel&gt;&gt;(stocks.Where(x =&gt; x.ParentStockId == null));
          // Sample output
          foreach (var stock in viewStocks){
              Console.WriteLine(stock.Level.ToString() + " " + stock.Name);
              if (stock.SubStock != null) 
                  foreach (var stock1 in stock.SubStock){
                      Console.WriteLine("  " + stock1.Level.ToString() + " " + stock1.Name);
                      if (stock1.SubStock != null) 
                          foreach (var stock2 in stock1.SubStock){
                              Console.WriteLine("    " + stock2.Level.ToString() + " " + stock2.Name);
                          }
                  }
          }
  </code></pre>
  
  <p><a href="https://dotnetfiddle.net/hUhmUZ" rel="nofollow noreferrer">Fiddle</a><br>
  How to map <code>viewStocks</code> hierarhical?
  Same as  </p>
  
  <pre><code>10 Root1
    20 Area1
      30 Box11
      30 Box12
  10 Root2
    20 Area2
      30 Box21
  </code></pre>