title: Generic method where T implements Interface<T>
body: <p>I'm trying to create a generic data retrieval process. What I have currently works, but there is a part of it that doesn't seem right and I'm hoping there is a better way to accomplish it.</p>
  
  <p>So the idea is that I have classes for each table in the database, here is an example of a class:</p>
  
  <pre><code>public class CMCGRGRGROUP : IFacetsObject&lt;CMCGRGRGROUP&gt;
  {
      public int GRGR_CK { get; set; }
      public string GRGR_NAME { get; set; }
      public string GRGR_ADDR1 { get; set; }
  
      public IEnumerable&lt;CMCGRGRGROUP&gt; ToObject(DataTable table)
      {
          return table.AsEnumerable().Select(row =&gt;
          {
              return new CMCGRGRGROUP
              {
                  GRGR_CK = Convert.ToInt32(row["GRGR_CK"]),
                  GRGR_NAME = row["GRGR_NAME"].ToString(),
                  GRGR_ADDR1 = row["GRGR_ADDR1"].ToString()
              };
          });
      }
  }
  </code></pre>
  
  <p>You'll notice that the class implements an interface of its own type. The interface simply defines a method called <code>ToObject</code>, which is used to convert a datatable to a class of that particular type:</p>
  
  <pre><code>public interface IFacetsObject&lt;T&gt;
  {
      IEnumerable&lt;T&gt; ToObject(DataTable obj);
  }
  </code></pre>
  
  <p>Now, here is the method that I am using to execute a query:</p>
  
  <pre><code>public IEnumerable&lt;T&gt; ExecuteQuery&lt;T&gt;(string sql, IFacetsObject&lt;T&gt; obj) where T : new()
  {
      using (var conn = new AseConnection(_conn))
      {
          conn.Open();
          var cmd = new AseCommand(sql, conn);
  
          var dt = new DataTable();
          var da = new AseDataAdapter(sql, conn);
          da.Fill(dt);                
  
          return obj.ToObject(dt); //this is the interface method
      }
  }
  </code></pre>
  
  <p><strong>So the main question is:</strong>
  How can the generic method know that T should implement <code>IFacetsObject&lt;T&gt;</code>? That way I don't have to pass <code>IFacetsObject&lt;T&gt;</code> as a parameter. Ideally, I could change the return line to be something like this:</p>
  
  <pre><code>return T.ToObject(dt);
  </code></pre>
  
  <p>And call it like this:</p>
  
  <pre><code>var result = ExecuteQuery&lt;CMCGRGRGROUP&gt;(sql).Take(5);
  </code></pre>
  
  <p>Instead of like this:</p>
  
  <pre><code>var result = ExecuteQuery&lt;CMCGRGRGROUP&gt;(sql, new CMCGRGRGROUP()).Take(5);
  </code></pre>
  
  <p>I'll admit that I'm not terribly familiar with generics yet so there may be something within the implementation that isn't right.</p>