简体   繁体   English

在存储库模式中按ID过滤是不好的做法

[英]Is it bad practice to filter by ID within the repository pattern

I am using ASP.NET MVC4 with Entity Framework 5 . 我正在使用ASP.NET MVC4Entity Framework 5

Essentially every controller action result filters the db results by the logged in Users' Company ID . 基本上每个控制器操作结果都按登录的用户公司ID过滤db结果 I have just begun implementing a repository pattern to return the models rather than directly filtering the DbContext from the controller. 我刚刚开始实现一个存储库模式来返回模型,而不是直接从控制器中过滤DbContext。 (Passing the companyID into the repository to filter results of methods) (将companyID传递到存储库以过滤方法的结果)

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 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 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 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 :) 谢谢你提前帮助:)

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. 如果可能,请提供已过滤的上下文,而不是单独过滤下游存储库方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用实体服务/存储库模式时,在模型中引用 System.Web.Security 是不好的做法吗? - Is it bad practice to reference System.Web.Security in the model when using entity service/repository pattern? 这种不良做法/反模式的名称是什么? - What is the name of this bad practice / anti-pattern? 在属性中声明变量被认为是不好的做法吗? - Is it considered bad practice to declare variables within a property? SqlTransaction 中的 SqlDataAdapter.Fill() - 这是一个不好的做法吗? - SqlDataAdapter.Fill() within a SqlTransaction - is this a bad practice? 在地图定义中进行排序是否被认为是不好的做法? - Is sorting within a Map definition considered a bad practice? 在方法中修改变量是否是错误的做法? - Is it bad practice to modify the variable within a method? 在 class 中实例化 IConfidentialClientApplication 是不好的做法吗? - Is it bad practice to instantiate an IConfidentialClientApplication within a class? 单元测试 - 将存储库传递给基类构造函数的不良做法 - Unit testing - bad practice to pass repository into base class constructor 具有手动打开和关闭数据库连接的存储库是不好的做法吗? - Is it bad practice to have a repository that manually opens and closes a db connection? 在存储库设计模式C#中过滤数据集 - Filter dataset in repository design pattern C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM