“注射”使用 PetaPoco 动态地实现 WHERE 子句
我正在使用 .NET MVC 3 和 PetaPoco 构建一个带有共享数据库的多租户应用程序。 租户 ID(以及其他信息)在登录时保存在 FormsAuth cookie 中,并可通过 BaseController 属性供所有控制器使用。大多数表(即除了主“租户”表之外)都包含 TenantId 列。
有没有办法可以在执行查询之前将其动态添加到查询中,而不是手动将“WHERE TenantId = X”添加到功能表上的所有 CRUD 中?换句话说,也许维护一个表列表,如果查询是针对这些表之一,那么动态添加 TenantId 过滤器?
当然,这样做的好处是,它无需手动添加过滤器,从而减少了遗漏的机会。我确实找到了 使用 NHibernate 的示例,我怀疑它可以被重新利用。我正在使用 Ninject,以防有所作为。
I'm building a multi-tenant app with a shared database using .NET MVC 3 and PetaPoco.
The tenant id (along with other info) is saved in a FormsAuth cookie on login and is available to all controllers via a BaseController property. Most tables, (i.e. apart from apart the main 'Tenants' table) include a TenantId column.
Instead of manually adding a 'WHERE TenantId = X' to all CRUD on the feature tables, is there a way I can dynamically add this to the query just before its executed? In other words, maybe maintain a list of tables, and if the query is for one of those tables, then dynamically add in the TenantId filter?
The benefit of course is that it removes the need to add in the filter manually thus reducing the chances its left out. I did find an example using NHibernate, which I doubt can be repurposed. I am using Ninject in case that makes a difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Database 类中有一个 OnCommandExecuting 方法,您可以在自己的子类中重写该方法,并在执行之前根据需要修改 sql。我们使用此功能在 Sql Server 和 Oracle 之间转换 isnull/nvl。
您可以在 sql 中留下一个标记并在此处替换它。
There is an OnCommandExecuting method on the Database class which you can override in your own sub class, and modify the sql as you wish just before it gets executed. We use this feature for converting isnull/nvl between Sql Server and Oracle.
You could just leave a marker in your sql and replace it here.