title: Is it bad practice to filter by ID within the repository pattern
body: <p>I am using ASP.NET <strong>MVC4</strong> with <strong>Entity Framework 5</strong>.</p>
  
  <p>Essentially every controller action result <strong>filters the db results by the logged in Users' Company ID</strong>. I have just begun implementing a repository pattern to return the models rather than directly filtering the DbContext from the controller. (Passing the companyID into the repository to filter results of methods)</p>
  
  <p>I have a funny feeling that it is bad practice to do this, but have been unable to find any information on the subject. I will insert a basic version of my current code below, I would appreciate any information about whether or not it is bad practice, and why so.</p>
  
  <p><strong>IBookingSystemRepository.cs</strong></p>
  
  <pre><code>public interface IBookingSystemRepository : IDisposable
  {
      IEnumerable&lt;Appointment&gt; GetAppointments();
      IEnumerable&lt;Appointment&gt; GetAppointments(bool includeDeleted);
      IEnumerable&lt;Client&gt; GetClients();
      IEnumerable&lt;Client&gt; GetClients(bool includeDeleted);
      void Save();
  }
  </code></pre>
  
  <p><strong>BookingSystemRepository.cs</strong></p>
  
  <pre><code>public class BookingSystemRepository : IBookingSystemRepository
  {
      private BookingSystemEntities db;
      int CompanyID;
  
      public BookingSystemRepository(BookingSystemEntities context, int companyID)
      {
          this.db = context;
          this.CompanyID = companyID;
      }
  
      public IEnumerable&lt;Appointment&gt; GetAppointments()
      { return GetAppointments(false); }
  
      public IEnumerable&lt;Appointment&gt; GetAppointments(bool includeDeleted)
      {
          return includeDeleted
              ? db.Appointments.Where(a =&gt; a.User.CompanyID == CompanyID)
              : db.Appointments.Where(a =&gt; a.User.CompanyID == CompanyID &amp;&amp; a.Deleted.HasValue);
      }
  
      public IEnumerable&lt;Client&gt; GetClients()
      { return GetClients(false); }
  
      public IEnumerable&lt;Client&gt; GetClients(bool includeDeleted)
      {
          return includeDeleted
              ? db.Clients.Where(c =&gt; c.CompanyID == CompanyID)
              : db.Clients.Where(c =&gt; c.CompanyID == CompanyID &amp;&amp; c.Deleted.HasValue);
      }
  
      public void Save()
      {
          db.SaveChanges();
      }
  
      public void Dispose()
      {
          if (db != null)
              db.Dispose();
      }
  }
  </code></pre>
  
  <p><strong>TestController.cs</strong></p>
  
  <pre><code>public class TestController : Controller
  {
      private BookingSystemEntities db = new BookingSystemEntities();
  
      public ActionResult AppointmentsList()
      {
          var user = db.Users.Single(u =&gt; u.Email == User.Identity.Name);
          IBookingSystemRepository rep = new BookingSystemRepository(db, user.CompanyID);
          return View(rep.GetAppointments());
      }
  }
  </code></pre>
  
  <p>Thankyou in advance for your assistance :)</p>