在 NHibernate 中,如何组合两个 DetachedCriteria 实例
我的场景是这样的:我有一个基本的 NHibernate 查询要运行该表单(我使用 DetachedCriteria 对其进行编码,但在此处使用 SQL 语法进行描述):
SELECT * FROM Items I INNER JOIN SubItems S on S.FK = I.Key
显示此连接结果的用户界面允许用户指定其他标准:说:
I.SomeField = 'UserValue'.
现在,我需要最终的加载命令为:
SELECT * FROM Items I INNER JOIN SubItems S on S.FK = I.Key
WHERE I.SomeField = 'UserValue'
我的问题是:我创建了一个具有查询的“静态”方面(顶部连接)的 DetachedCriteria,并且 UI 创建了一个具有“动态”的 DetachedCriteria查询的组成部分。我需要将两者组合成一个可以在 NHibernate 会话上执行的最终查询。
DefaultCriteria.Add() 接受一个 ICriterion(它是使用 Expression 类创建的,也许还有我不知道的其他类可以解决我的问题)。
有谁知道我怎样才能做我想做的事?
My scenario is this: I have a base NHibernate query to run of the form (I've coded it using DetachedCriteria , but describe it here using SQL syntax):
SELECT * FROM Items I INNER JOIN SubItems S on S.FK = I.Key
The user interface to show the results of this join allows the user to specify additional criteria: Say:
I.SomeField = 'UserValue'.
Now, I need the final load command to be:
SELECT * FROM Items I INNER JOIN SubItems S on S.FK = I.Key
WHERE I.SomeField = 'UserValue'
My problem is: I've created a DetachedCriteria with the 'static' aspect of the query (the top join) and the UI creates a DetachedCriteria with the 'dynamic' component of the query. I need to combine the two into a final query that I can execute on the NHibernate session.
DefaultCriteria.Add() takes an ICriterion (which are created using the Expression class, and maybe other classes I don't know of which could be the solution to my problem).
Does anyone know how I might do what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 GetExecutableCriteria 将独立的条件转换为特定会话的可执行形式:
但是,我认为您的设计有点缺陷。用户界面应该补充标准,而不是创建自己的标准。最坏的情况是,它应该生成
ICriterion
,然后在执行之前将其添加到您的条件中。最好的情况是,您可以将过滤功能抽象到一个完全独立于 ORM 技术的层中,然后将 UI 中的过滤器应用到您的基础标准。You can use
GetExecutableCriteria
to turn a detached criteria into an executable form for a specific session:However, I think your design is a little flawed. The UI should be supplementing the criteria, not creating its own. At worst, it should be generating
ICriterion
that are then added to your criteria prior to execution. At best, you would abstract away filtering capabilities into a layer completely independent of your ORM technology and then apply filters from the UI to your underlying criteria.