从 NHibernate 3.2 获取/构建 SQL(带参数) [英] Getting/building the SQL (with parameters) from NHibernate 3.2

查看:43
本文介绍了从 NHibernate 3.2 获取/构建 SQL(带参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个需要以下内容的项目:

I am working on a project that requires the following:

  • 从特定的 NHibernate 3.2 会话中提取完整的 SQL 查询.
  • 对查询执行特定操作(即不必记录)
  • 不影响整个系统的NHibernate,避免引入性能问题

我检查了几种方法,其中许多方法已经出现在 StackOverflow 上.这是我现在看到的选项:

I checked several approaches, many of them already appearing on StackOverflow. Here are my options as I see now:

手册

在最幼稚和烦人的解决方案中,我可以只遵循业务逻辑并自己构建查询 - 例如,如果 BL 构建了一个对 ID=5 进行限制的条件,我将使用 选择 ... WHERE ID = 5.由于我们有一个相当复杂的 BL,我真的很想避免这种情况.

In the most naive and annoying solution, I can just follow the business logic and build the query myself - E.g, if the BL builds a criteria that does a restrictions on ID=5, I'd build a query with SELECT ... WHERE ID = 5. Since we have quite a complex BL, I'd really like to avoid that.

NHibernate 拦截

最初,使用 OnPrepareStatement 似乎是最好的选择.但是,我很快发现查询没有被记录,这使得它非常无用.

Originally, using the OnPrepareStatement seemed like the best bet. However, I soon discovered that the parameters of the query aren't logged which renders it quite useless.

内省 NHibernarnate 的 ICriteria

当使用 NHibernate 执行查询时,我们使用包含限制、排序和聚合定义的 ICreteria 对象来执行.当 CriteriaWalker 描述为 这里.但是,它似乎对复杂的查询感到困惑.此外,在某些情况下,我们使用 NHibernate 3 新的QueryOver"语法,但此解决方案对我没有帮助.

When performing a query with NHibernate, we do it with an ICreteria object that contains the restrictions, the sorting and the aggregations definitions. It seems I could interspect it when a CriteriaWalker that is described here. However, it seems that it gets confused on complex queries. Also, in some cases we use NHibernate 3 new "QueryOver" syntax for which this solution doesn't help me.

使用 ILoggerFactory

从 NHibernate 3 开始,您可以编写自定义日志工厂(sample).这将获得完整的 SQL,但是,它也会影响整个 NHibernate 系统并且它 好像不可能 有工厂申请特定的ISession,甚至是ISessionFactory.

Since NHibernate 3, you can write custom log factories (sample). This gets the full SQL, however, it also affects the whole NHibernate system and it seems it is impossible to have a factory to apply for a specific ISession, or even ISessionFactory.

自定义 NHibernate 驱动程序

我考虑过编写代理 NHibernate 驱动程序并将其分配给特定的 SessionFactory(如此处所述).然而,一个友好的评论警告说它在 Nhibernate 3.2 中更有效.

I've considered writing a proxy NHibernate driver and assigning it to a specific SessionFactory (as described here). However, a friendly comment warns that it longer works in Nhibernate 3.2.

使用动态代理

此代码使用 Castle 的动态代理将自身注入 ISession.我还没有尝试在我的服务器上运行它,但我对使用这种激烈的措施有点谨慎.但是,如果其他方法无效,我想这是需要考虑的.

This code uses Castle's Dynamic proxies to inject itself inside ISession. I haven't tried running it yet with my server, but I am a bit wary of using such drastic measures. If nothing else works, however, I guess it is something to consider.

建议?

现在我有点坚持选择最好的方式,因为似乎没有任何东西可以发挥作用,非常正确.如果还有其他建议,我很想听听.

Right now I am a bit stuck on choosing the best way to go with since nothing seems to be doing its job, quite right. If there are other suggestions, I'd love to hear them.

推荐答案

我会使用标准或自定义日志记录框架,并应用自定义过滤器从线程数据(例如)中检索标志,以确定是否应该记录会话.

I'd use a standard or custom logging framework, and apply a custom filter to retrieve a flag from thread data (for example) in order to determine whether the session should be logged.

这样,您根本不会弄乱 NH 内部结构,而且只要您不设置标志,就不会记录任何内容.

This way, you don't mess with NH internals at all, and as long as you don't set the flag, nothing gets logged.

这篇关于从 NHibernate 3.2 获取/构建 SQL(带参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆