title: Building an object - static builder method vs. builder class vs. extension method
body: <p>I'm curious to hear if anyone has any suggestions or alternative patterns for building a custom object with data from another object.</p>
  
  <p>We're exploring three approaches currently.</p>
  
  <p><strong>1) Static Build Method</strong></p>
  
  <pre><code>public class MyObject
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public static MyObject Build(DataRow data)
      {
          MyObject newObject = new MyObject();
          newObject.Id = Convert.ToInt32(data["ID"]);
          newObject.Name = Convert.ToString(data["NAME"]);
          return newOjbect;
      }
  }
  </code></pre>
  
  <p><strong>2) Builder Class</strong></p>
  
  <pre><code>public class MyObject
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  public class MyObjectBuilder
  {
      public static MyObject Build(DataRow data)
      {
          MyObject newObject = new MyObject();
          newObject.Id = Convert.ToInt32(data["ID"]);
          newObject.Name = Convert.ToString(data["NAME"]);
          return newOjbect;
      }
  }
  </code></pre>
  
  <p><strong>3) Extension Method</strong></p>
  
  <pre><code>public class MyObject
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  public static class MyObjectExtensions
  {
      public static void Build(this MyObject obj, DataRow data)
      {
          obj.Id = Convert.ToInt32(data["ID"]);
          obj.Name = Convert.ToString(data["NAME"]);
      }
  }
  </code></pre>
  
  <p>One main consideration is that which ever approach we go with we need to add a <code>using</code> reference of <code>System.Data</code> to the class. At this point we're hesitant to add this dependency directly to our <code>MyObject</code> class. </p>
  
  <p>Can anyone offer advantages or disadvantages to any of these approaches or perhaps offer alternatives that would better enable extensibility and testability? </p>