NHibernate:创建适用于表上所有查询的条件

发布于 2024-08-03 06:14:17 字数 250 浏览 9 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2024-08-10 06:14:17

我终于找到了一个很好的解决方案(使用过滤器)。由于 Castle AR 没有任何用于映射到 NHibernate 过滤器的本机 API,因此这部分几乎没有文档记录。所以就这样吧。

此示例过滤器将确保您永远不会收到超过一年的新闻,无论您在 ActiveRecord 上使用哪种查询。您可能可以为此想到更实际的应用。

首先,创建一个 ActiveRecord“新闻”。

在初始化 ActiveRecordStarter 之前使用以下代码。

ActiveRecordStarter.MappingRegisteredInConfiguration += MappingRegisteredInConfiguration;
Castle.ActiveRecord.Framework.InterceptorFactory.Create = () => { return new EnableFiltersInterceptor(); };

然后,添加缺少的函数和类:

void MappingRegisteredInConfiguration(Castle.ActiveRecord.Framework.ISessionFactoryHolder holder)
{
    var cfg = holder.GetConfiguration(typeof (ActiveRecordBase));

    var typeParameters = new Dictionary<string, IType>
                                  {
                                    {"AsOfDate", NHibernateUtil.DateTime}
                                  };

    cfg.AddFilterDefinition(new FilterDefinition("Latest", "", typeParameters));

    var mappings = cfg.CreateMappings(Dialect.GetDialect(cfg.Properties));

    var newsMapping = cfg.GetClassMapping(typeof (News));
    newsMapping.AddFilter("Latest", ":AsOfDate <= Date");
}


public class EnableFiltersInterceptor : EmptyInterceptor
{
    public override void SetSession(ISession session)
    {
        session.EnableFilter("Latest").SetParameter("AsOfDate", DateTime.Now.AddYears(-1));
    }
}

瞧!新闻查询,例如 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.

ActiveRecordStarter.MappingRegisteredInConfiguration += MappingRegisteredInConfiguration;
Castle.ActiveRecord.Framework.InterceptorFactory.Create = () => { return new EnableFiltersInterceptor(); };

Then, add the missing function and class:

void MappingRegisteredInConfiguration(Castle.ActiveRecord.Framework.ISessionFactoryHolder holder)
{
    var cfg = holder.GetConfiguration(typeof (ActiveRecordBase));

    var typeParameters = new Dictionary<string, IType>
                                  {
                                    {"AsOfDate", NHibernateUtil.DateTime}
                                  };

    cfg.AddFilterDefinition(new FilterDefinition("Latest", "", typeParameters));

    var mappings = cfg.CreateMappings(Dialect.GetDialect(cfg.Properties));

    var newsMapping = cfg.GetClassMapping(typeof (News));
    newsMapping.AddFilter("Latest", ":AsOfDate <= Date");
}


public class EnableFiltersInterceptor : EmptyInterceptor
{
    public override void SetSession(ISession session)
    {
        session.EnableFilter("Latest").SetParameter("AsOfDate", DateTime.Now.AddYears(-1));
    }
}

And voila! Queries on News, e.g. FindAll(), DeleteAll(), FindOne(), Exists(), etc. will never touch entries more than a year old.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文