NHibernate:创建适用于表上所有查询的条件
使用 Castle ActiveRecord / NHibernate:有没有一种方法可以对表上的所有查询强制使用 ICriterion?
例如,我的很多表都有“UserId”列。我可能想确保始终为登录用户选择行。我可以轻松创建一个 ICriterion 对象,但我被迫为不同的方法提供它:FindAll()、FindFirst()、FindLast() 等。
有没有办法对 Castle ActiveRecord 的所有查询强制使用 WHERE 子句?
Using Castle ActiveRecord / NHibernate: Is there a way you can force an ICriterion on all queries on a table?
For example, a good amount of of my tables have a "UserId" column. I might want to ensure that I am always selecting rows for the logged in user. I can easily create an ICriterion object, but I am forced to supply it for different methods: FindAll(), FindFirst(), FindLast() etc.
Is there a way to force a WHERE clause on all queries to a Castle ActiveRecord?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于找到了一个很好的解决方案(使用过滤器)。由于 Castle AR 没有任何用于映射到 NHibernate 过滤器的本机 API,因此这部分几乎没有文档记录。所以就这样吧。
此示例过滤器将确保您永远不会收到超过一年的新闻,无论您在 ActiveRecord 上使用哪种查询。您可能可以为此想到更实际的应用。
首先,创建一个 ActiveRecord“新闻”。
在初始化 ActiveRecordStarter 之前使用以下代码。
然后,添加缺少的函数和类:
瞧!新闻查询,例如 FindAll()、DeleteAll()、FindOne()、Exists() 等,绝不会触及一年以上的条目。
I finally found a great solution (using filters). Since Castle AR does not have any native API for mapping to NHibernate filters, this part was pretty much undocumented. So here goes.
This example filter, will make sure you will never get news more than a year old, no matter what kind of query you use on the ActiveRecord. You can probably think of more practical applications for this.
First, create an ActiveRecord "News".
Use the following code before you initialize ActiveRecordStarter.
Then, add the missing function and class:
And voila! Queries on News, e.g. FindAll(), DeleteAll(), FindOne(), Exists(), etc. will never touch entries more than a year old.
最接近的是使用过滤器。请参阅http://ayende.com/Blog/archive/2009 /05/04/nhibernate-filters.aspx
The closest thing would be using filters. See http://ayende.com/Blog/archive/2009/05/04/nhibernate-filters.aspx