title: AutoMapper with Dataset as parameter in profile
body: <p>I have some problem when using AutoMapper. I searched everywhere I could but either I don't understand the solution (when no code is provided) or it doesn't apply to my situation.</p>
  
  <p>I have my profile :</p>
  
  <pre><code>public class MyCustomProfile : Profile
  {
      public MyCustomProfile()
      {
          CreateMap&lt;DTO.Person, MyDataSet.PersonRow&gt;();
      }
  }
  </code></pre>
  
  <p>And I have my method :</p>
  
  <pre><code>public static void TestAutoMapper(DTO.Person p)
  {
      if (Mapper.Instance == null)
      {
          Mapper.Initialize(cfg =&gt; cfg.AddProfile&lt;MyCustomProfile&gt;());
      }
  
      MyDataSet.PersonRow pr = Mapper.Map&lt;MyDataSet.PersonRow&gt;(p);
  }
  </code></pre>
  
  <p>But my problem is :
  I need to add in my profile <code>.ConstructUsing(p =&gt; dts.get_XYdataset().Person.NewPersonRow());</code> where <code>dts</code> is an instance of <code>MyDataSet</code>.</p>
  
  <p>And I also need this instance of <code>MyDataSet dts</code> in the method <code>TestAutoMapper(DTO.Person p)</code> to save the result <code>MyDataSet.PersonRow pr</code> as the following :</p>
  
  <pre><code>dts.get_XYdataset().Person.AddPersonRow(pr)
  </code></pre>
  
  <p>But I don't know what to do. It works well if I put everything in the <code>TestAutoMapper()</code> method but of course it's not clean and I want to separate logics by creating a profile and calling it when initializing the mapper.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>So I modified my <code>TestAutoMapper()</code> method like this :</p>
  
  <pre><code>public static void TestAutoMapper(DTO.Person p)
  {
      if (Mapper.Instance == null)
      {
          Mapper.Initialize(cfg =&gt; cfg.AddProfile&lt;MyCustomProfile&gt;());
      }
  
      using (MyDataSet dts = new MyDataSet())
      {
          MyDataSet.PersonRow pr = Mapper.Map&lt;MyDataset.PersonRow&gt;(p, opt =&gt; opt.Items["Dataset"] = dts);
          dts.get_XYdataset().Person.AddPersonRow(pr);
      }
  </code></pre>
  
  <p>Then I tried to follow the mini tuto about Custom Resolvers and implemented this class :</p>
  
  <pre><code>public class CustomResolver: IMemberValueResolver&lt;object, object, MyDataSet, MyDataSet&gt;
  {
      public MyDataSet Resolve(object source, object destination, MyDataSet dts, MyDataSet dts2, ResolutionContext context)
      {
          if (dts != null)
          {
              return dts;
          }
          else if (dts2 != null)
          {
              return dts2;
          }
          return new MyDataSet();
      }
  }
  </code></pre>
  
  <p>But I don't think this is okay. But well, nevermind I tried anyway. But now I'm stuck in my profile constructor in the <code>CreateMap&lt;DTO.Person, MyDataSet.PersonRow&gt;()</code> statement. How to get the <code>Options.Items["Dataset"]</code> in the <code>.ConstructUsing()</code> ?</p>
  
  <p>Examples show about a CustomResolver about a member but how about specifying a constructor ?<br>
  It would be so perfect if I could do something like :<br>
  <code>CreateMap&lt;DTO.Person, MyDataSet.PersonRow&gt;()
      .ConstructUsing(Options.Items["Dataset"].get_XYdataset().Person.NewPersonRow());</code></p>
  
  <p>I know my need for help can be silly but I really don't understand even by reading the docs.</p>
  
  <p>What do you think ?</p>
  
  <p>Thanks,</p>
  
  <p>Hellcat8</p>