title: Effective Repository in C# - Where to put methods?
body: <p>I'm trying to build a new application using the Repository pattern for the first time and I'm a little confused about using a Repository.  Suppose I have the following classes:</p>
  
  <pre><code>public class Ticket
  {
  
  }
  public class User
  {
     public List&lt;Ticket&gt;AssignedTickets { get; set; }
  }
  public class Group
  {
     public List&lt;User&gt; GroupMembers { get;set; }
     public List&lt;Ticket&gt; GroupAssignedTickets { get;set; }
  }
  </code></pre>
  
  <p>I need a methods that can populate these collections by fetching data from the database.</p>
  
  <p>I'm confused as to which associated Repository class I should put those methods in.  Should I design my repositories so that everything <em>returning</em> type T goes in the repository for type T as such?</p>
  
  <pre><code>public class TicketRepository
  {
     public List&lt;Ticket&gt; GetTicketsForGroup(Group g) { }
     public List&lt;Ticket&gt; GetTicketsForUser(User u) { }
  }
  public class UserRepository
  {
    public List&lt;User&gt; GetMembersForGroup(Group g) { }
  }
  </code></pre>
  
  <p>The obvious drawback I see here is that I need to start instantiating a lot of repositories.  What if my User also has assigned Widgets, Fidgets, and Lidgets?  When I populate a User, I need to instantiate a WidgetRepository, a FidgetRepository, and a LidgetRepository all to populate a single user.</p>
  
  <p>Alternatively, do I construct my repository so that everything requesting based on type T is lumped into the repository for type T as listed below?</p>
  
  <pre><code>public class GroupRepository
  {
      public List&lt;Ticket&gt; GetTickets(Group g) { }
      public List&lt;User&gt; GetMembers(Group g) { }
  }
  public class UserRepository
  {
     public List&lt;Ticket&gt; GetTickets(User u) { }
  }
  </code></pre>
  
  <p>The advantage I see here is that if I now need my user to have a collection of Widgets, Fidgets, and Lidgets, I just add the necessary methods to the UserRepository pattern and don't need to instantiate a bunch of different repository classes every time I want to create a user, but now I've scattered the concerns for a user across several different repositories.</p>
  
  <p>I'm really not sure which way is right, if any.  Any suggestions?</p>