7

I am using ASP.NET MVC4 with Entity Framework 5.

Essentially every controller action result filters the db results by the logged in Users' Company ID. 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)

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.

IBookingSystemRepository.cs

public interface IBookingSystemRepository : IDisposable
{
    IEnumerable<Appointment> GetAppointments();
    IEnumerable<Appointment> GetAppointments(bool includeDeleted);
    IEnumerable<Client> GetClients();
    IEnumerable<Client> GetClients(bool includeDeleted);
    void Save();
}

BookingSystemRepository.cs

public class BookingSystemRepository : IBookingSystemRepository
{
    private BookingSystemEntities db;
    int CompanyID;

    public BookingSystemRepository(BookingSystemEntities context, int companyID)
    {
        this.db = context;
        this.CompanyID = companyID;
    }

    public IEnumerable<Appointment> GetAppointments()
    { return GetAppointments(false); }

    public IEnumerable<Appointment> GetAppointments(bool includeDeleted)
    {
        return includeDeleted
            ? db.Appointments.Where(a => a.User.CompanyID == CompanyID)
            : db.Appointments.Where(a => a.User.CompanyID == CompanyID && a.Deleted.HasValue);
    }

    public IEnumerable<Client> GetClients()
    { return GetClients(false); }

    public IEnumerable<Client> GetClients(bool includeDeleted)
    {
        return includeDeleted
            ? db.Clients.Where(c => c.CompanyID == CompanyID)
            : db.Clients.Where(c => c.CompanyID == CompanyID && c.Deleted.HasValue);
    }

    public void Save()
    {
        db.SaveChanges();
    }

    public void Dispose()
    {
        if (db != null)
            db.Dispose();
    }
}

TestController.cs

public class TestController : Controller
{
    private BookingSystemEntities db = new BookingSystemEntities();

    public ActionResult AppointmentsList()
    {
        var user = db.Users.Single(u => u.Email == User.Identity.Name);
        IBookingSystemRepository rep = new BookingSystemRepository(db, user.CompanyID);
        return View(rep.GetAppointments());
    }
}

Thankyou in advance for your assistance :)

0

1 Answer 1

4

It is a multi-tenant application. The filtering is needed to keep each company's data separate. Your approach is a sound one; if possible, provide a context that is already filtered, rather than filtering in the downstream repository methods individually.

3
  • Thanks very much for your help and speedy response :) I didn't even think of filtering the context before passing it to the repository, I think your approach will make things even simpler for me! Just to clarify though, do you mean for me to create a new DbContext class that filters all DbSets based on an ID passed to the constructor? Commented Jun 17, 2013 at 15:50
  • 1
    That would be an interesting idea. It would be nice if you could pull it off. No, I was thinking more of a repository or DBContextWrapper class that would feed your normal repository. From a security standpoint, the best way to feed the DBContext would be to provide Views in your database engine that are already filtered. But I don't know how much time you have. :) Commented Jun 17, 2013 at 15:52
  • So essentially make another repository, between this one and the DbContext, that filters everything to company level? Your idea about filtering it using database views is probably the best option, but I don't have the time to do that currently. Anyhow, many thanks for your assistance Robert. :) Commented Jun 17, 2013 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.