title: Specific generic interfaces
body: <p>I'm refactoring all my repository interfaces of various types. Most of them contain very similar methods like Add, Update but some have methods which only makes sense for a specific type. This is a best practices question.</p>
  
  <p>I thought about using generics to straighten things up.</p>
  
  <pre><code> public interface IRepository&lt;T&gt;
   {
        T Get(int id);
        void Add(T x);
   }
  </code></pre>
  
  <p>But now for the specific methods. I could ofcourse "subclass" the interface, but then I'm not better off than before. I would have code like:</p>
  
  <pre><code> IUserRepository&lt;User&gt; users;
  </code></pre>
  
  <p>One neat way would be if I could have multiple constraints like:</p>
  
  <pre><code> public partial interface IRepository&lt;T&gt;
   {
        T Get(int id);
        void Add(T x);
   }
  
   public partial interface IRepository&lt;T&gt; where T: User
   {
        T Get(Guid id);
   }
  
   public partial interface IRepository&lt;T&gt; where T: Order
   {
        T Get(string hash);
   }
  </code></pre>
  
  <p>But the compiler complains about conflicting inheritance. Annother way would be contraints on the methods:</p>
  
  <pre><code> public partial interface IRepository&lt;T&gt;
   {
        T Get(int id);
        void Add(T x);
  
        T Get(Guid id) where T: User;
        T Get(string hash) where T: Order;
   }
  </code></pre>
  
  <p>But that's not quite the way these this work is it. Compiler fathoms not my intentions and wants a type definition on the method, of course.</p>
  
  <p>Right now I just have methods that throw NotImplemented. Ugly.</p>
  
  <p>I'm looking for a solution that will make me kick myself.</p>