不确定如何使用 LINQ 实现 nHibernate 的特定扩展
在颇受欢迎的 ayende.com 网站的博客文章中 博客文章。有一个部分以非常具体的方式引起了我的注意。
在帖子接近尾声时,他发表了一条评论(引自该网站)
对于“好吧,那么我如何应用安全过滤”之类的问题,我给出的答案是,将其放入扩展方法中并执行如下操作:
var query =
(
from post in session.Query<Post>()
where post.DueDate > DateTime.Now
select post
)
.FilterAccordingToUserPermissions(CurrentUser)
.ToList();
说实话,我真的很喜欢这种方法。它对我来说看起来非常干净但由于我缺乏经验并且缺乏对扩展方法或 nHibernate 的了解,我似乎无法重现
。
它 告诉我正在使用的对象是NHibernate.Linq.Query
对象。我主要在项目中使用 QueryOver
来将它们投影到 Futures
,但我离题了......
我意识到这是非常模糊的,而且根本不是一个非常合理的请求 - 但我想知道是否有人愿意发布一个如何实现此示例代码的示例,
这是我不做的 明白
- 如何扩展
Query
或QueryOver
对象。 - 。我不 扩展影响/过滤数据库查找。
- 如果我能够影响数据库查找,我不明白它将如何影响性能。
- 我真的不明白
CurrentUser
来自哪里。在我看来,需要另一次数据库访问才能实现这一点,这又违背了 nhprof 和我读过的大多数书籍一直告诉我的内容(不要多次访问数据库)一次操作)。
第3点是最让我困惑的一点。我唯一能想到的是这个方法接受查询并运行它,然后过滤它,然后返回结果。这对我来说似乎违反直觉(而且它似乎与我在 ayende.com/blog 网站上看到的很多内容相反,因为它继承地建议了许多非必要的数据库调用)
所以你已经明白了。我很无能,而且我确实明白,对于一些更有经验的人来说,这是一个非常愚蠢的问题。但我还太新,无法理解所有事情,我花了 5 周的大部分时间来尝试做到这一点。
In the blog post by the rather popular ayende.com website blog post .There is a section that catches my eye in a very specific manner.
Close to the end of the post, he makes a comment, quoted from the site
The answer that I give for things like "well, so how can I apply security filtering” is that you throw that into an extension method and do something like this:
var query =
(
from post in session.Query<Post>()
where post.DueDate > DateTime.Now
select post
)
.FilterAccordingToUserPermissions(CurrentUser)
.ToList();
I really like this approach, to be honest. It looks very clean to me. But in my inexperience and lack of knowledge of either Extension Methods or nHibernate one, I cannot seem to reproduce it.
var query = ( from post in session.Query<Post>() ...
this tells me the object being used is an NHibernate.Linq.Query<T>
object. I mainly use QueryOver<T>
in my projects for the purpose of projecting them to Futures
, but I digress...
I realize this is very ambiguous, and not at all a very consice request - but I was wondering if anyone would be willing to post a sample of how one might achieve this example code.
Here is what I do not understand;
- I do not understand how to extend the
Query<T>
orQueryOver<T>
object. - If I do extend the objects, I do not understand how to make that extension impact/filter the database lookup.
- If I am able to impact the database lookup, I do not understand how it will influence the performance.
- I do not really understand where the
CurrentUser
is coming from. It seems to me that it would require another database trip to get that, and that again goes against what nhprof and most of the books I've been reading keep telling me (don't make multiple trips for one operation).
point 3 is the one that baffles me the most. The only thing I can think of is that this method takes the query and runs it, then filters it, then returns the results. This seems counter-intuitive to me (and it seems against a lot of the things I have seen on the ayende.com/blog site, since it inheritly suggests a lot of non-essential database calls)
So there you have it. I'm pretty clueless, and I do understand that to some of you more experienced, this is a really stupid question. But I am much too new to understand everything and I've spent the better part of 5 weeks trying to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你是对的,这个问题有点模糊,但我会尽力解决你的观点。
1)您并没有真正扩展 Query 对象。您要编写的扩展实际上是针对 IQueryable 的,所以......
显然您的安全标准会比这更复杂。
2) 扩展方法允许您将附加条件添加到传入的 IQueryable 中。然后,在执行该可查询时,NH 查询引擎会将附加条件内置到 sql 中。
3)性能显然与您编写的查询有关,更复杂的查询执行速度会更慢。然而,扩展方法本身并没有对性能造成影响。重要的是要理解,查询只有在整个查询构建完成后才会执行。这通常由 .ToList()、Single()、First() 或其他实际枚举可查询的语句触发。
4) CurrentUser 来自您,可能需要事先从数据库中获取。但无论如何,您可能都必须在您的应用程序中这样做。
Yes you are right, the question is a bit vague but I'll try to address your points.
1) You are not really extending the Query object. The extension that you would be writing would actually be against IQueryable so...
Obviously your security criteria would be more complicated than this.
2) The extension method allows you to add additional criteria into the IQueryable that was passed in. That additional criteria is then built into sql by the NH Query engine when that queryable is executed.
3) Performance is obviously tied the query that you write, a more complex query will perform slower. However there is no performance hit from the extension method in and of itself. It is important to understand that the query is not executed until the entire query has been built. This is usually triggered by a .ToList(), Single(), First() or other statements that actually enumerate of the queryable.
4) CurrentUser comes from you and it may have to be fetched from the database beforehand. But you are probably going to have to do that anyways in your application.
扩展方法与 NHibernate 没有任何关系。它仅用于过滤(或“应用安全策略”)NHibernate 查询返回的
IEnumerable
。换句话说,NHibernate 将返回一个可枚举的实体列表(来自数据库),并且扩展方法将过滤该列表。未提供
FilterAccordingToUserPermissions
的实现,因此我们无法对其进行太多说明,包括它是否会表现不佳。扩展方法将在静态类中定义,如下所示:
并且不要忘记,不存在愚蠢的问题:)
The extension method has nothing really to do with NHibernate. It is simply used to filter (or "apply a security policy to") the
IEnumerable<Post>
returned by the NHibernate query.In other words, NHibernate will return a enumerable list of entities (from the database), and the extension method will filter that list. The implementation of
FilterAccordingToUserPermissions
is not provided so we can't say much about it, including whether it will perform poorly or not.The extension method would be defined in a static class, as follows:
And don't forget, there is no such thing as a stupid question :)