NHibernate 与 LINQ(谓词?)
如果我有些不清楚,请原谅我,我刚刚开始使用 NHibernate/LINQ/Lambda 表达式,而且我实际上不确定要寻找什么......
过去 4 或 5 年来我一直在使用 .NET 2.0多年来,除了我自己之外,没有机会发展,这就是为什么我现在开始学习新技术:)
我已经阅读了很多博客和帖子,并开始了一个个人小项目,我尝试使用存储库模式作为以及 可能的。
我现在处于以下情况:
- MyProject.Core.dll:我有这个核心程序集,其中包含所有业务逻辑并设置 IRepository 合约。它对存储库的实际实现一无所知,它是在运行时使用 IoC 解析的,因此该核心 dll 没有对 NHibernate dll 的引用。
- MyProject.Data.NHibernate.dll:存储库的实现包含在该程序集中,该程序集具有对 NHibernate dll 的所有必要引用。
我的存储库实现看起来像这样:
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
...
public virtual IQueryable<T> All()
{
IList<T> entities = Session
.CreateCriteria( typeof( T ) )
.List<T>();
return entities.AsQueryable<T>();
}
...
}
因此,在我的核心 dll 中,我可以获取对我的存储库的引用并执行类似的操作:
IList<Person> people = myRepository.All().ToList();
这似乎运行良好,它查询数据库并返回 Person 表中的所有行。
然而,现在我想做的是添加谓词:
IList<Person> daves = myRepository.All().Where(p => p.Name == "Dave").ToList();
这工作正常,但当然会发生的是 NHibernate 首先查询数据库以返回所有行,然后 LINQ 过滤结果以仅返回名称为“Dave”的行。
我一直在互联网上查找,但我还没有找到如何实现这一点,而且我发现很多东西似乎已经过时了,例如,我经常看到对 NH session.Linq() 方法的调用,我查遍了我的 dll,找不到这个方法...
如果有人能指出我正确的方向,也许有一个小例子或其他东西,我将非常感激。
非常感谢!
Please forgive me if I am somewhat unclear, I am just getting started with NHibernate/LINQ/Lambda expressions, and I am actually unsure of what to look for...
I've been working with .NET 2.0 for the past 4 or 5 years and had no opportunity to evolve other than by myself, which is why I am now getting to learn new tech :)
I've been reading alot of blogs and posts and started a personal little project where I try to use the Repository pattern as well as possible.
I am now in the following situation:
- MyProject.Core.dll: I have this core assembly that contains all the business logic and sets the IRepository contract. It knows nothing of the actual implementation of the repository, it is resolved at run time using IoC, so this core dll has no reference to the NHibernate dll's.
- MyProject.Data.NHibernate.dll: the implementation of the repository is contained within this assembly that has all the necessary references to the NHibernate dll's.
My Repository implementation looks something like this:
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
...
public virtual IQueryable<T> All()
{
IList<T> entities = Session
.CreateCriteria( typeof( T ) )
.List<T>();
return entities.AsQueryable<T>();
}
...
}
So in my core dll I can get a reference to my repository and do something like:
IList<Person> people = myRepository.All().ToList();
That seems to work well, it queries the database and returns all the rows from the Person table.
However, now what I want to do is add predicates:
IList<Person> daves = myRepository.All().Where(p => p.Name == "Dave").ToList();
This works fine, but of course what happens is that NHibernate first queries the database to return ALL rows, and then LINQ filters the results to only return the ones whose name is "Dave".
I've been looking all over the internet, but I haven't found how to implement this, also I found many things that seem to be outdated, for example, I often saw calls to the NH session.Linq() method, I looked all over my dll's, this method is nowhere to be found...
If anyone could point me to the right direction, maybe with a little example or something, I would be very greatful.
Thankd you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这非常简单:
使用
session.Query()
:Query
是一个扩展方法,位于命名空间NHibernate.Linq.
不再支持
session.Linq
。它是版本 3 之前的 NHibernate 的 LINQ 提供程序,已被Query
取代。It is quite simple:
Use
session.Query<T>()
:Query<T>
is an extension method that lives in the namespaceNHibernate.Linq
.session.Linq
is no longer supported. It was the LINQ provider for NHibernate prior to version 3 and has been replaced byQuery<T>
.您可能想考虑在您的存储库中使用规范模式。这里有一个使用 NHibernate 实现的示例这里< /a> 来自 NHibernate 3.0 Cookbook,这是一个值得获取的优秀资源。
You might want to look at using the specification pattern with your repository. There is an example of an implementation with NHibernate here from the NHibernate 3.0 Cookbook, which is an excellent resource worth getting.