
基本上每个控制器 *** 作结果都按登录的用户公司ID过滤数据库结果.我刚刚开始实现一个存储库模式来返回模型,而不是直接从控制器中过滤DbContext. (将companyID传递到存储库以过滤方法的结果)
我有一种有趣的感觉,这样做是不好的做法,但一直无法找到有关该主题的任何信息.我将在下面插入我当前代码的基本版本,我将不胜感激任何关于它是否是不良做法的信息,以及为什么如此.
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()); }} 谢谢你提前帮助:)
解决方法 这是一个多租户应用程序.需要过滤以保持每个公司的数据分开.你的方法很合理;如果可能,提供已经过滤的上下文,而不是单独过滤下游存储库方法. 总结以上是内存溢出为你收集整理的c# – 在存储库模式中按ID过滤是不好的做法全部内容,希望文章能够帮你解决c# – 在存储库模式中按ID过滤是不好的做法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)