装饰一个 IQueryOver使用 NHibernate 3.0
尝试使用一些条件逻辑来装饰 IQueryOver。考虑以下代码,它是表示客户搜索的对象上的私有方法。
private void addCustomerCriterion<T>(IQueryOver<T, T> query) where T : Customer
{
if (!string.IsNullOrEmpty(Name))
query = query
.Where(x => x.FirstName.ToUpper().Contains(Name.ToUpper()) ||
x.LastName.ToUpper().Contains(Name.ToUpper()));
if (HasOpenTicket.HasValue)
query = query
.Inner.JoinQueryOver<ServiceTicket>(c => c.ServiceTickets)
.Where(t => t.StatusName == "Open")
}
if (HasOpenInvoice.HasValue)
query = query
.Inner.JoinQueryOver<Invoice>(c => c.Invoices)
.Where(i => i.StatusName == "Open");
}
不幸的是,这不能编译。这是有道理的,因为在第二个和第三个 if 语句中,我破坏了流畅接口的类型安全性。根据 http://nhforge .org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx,JoinQueryOver 将我的查询从 QueryOver(of Customer, Customer) 转换为 QueryOver(of Customer) 、服务票)。当我尝试使用 query = query 习惯用法进行装饰时,类型现在不匹配。
所以这里真正的问题是我已经从 Customer 对象向下遍历到 ServiceTicket 关系,并且它更改了 QueryOver 对象的类型。 如何遍历树,以便可以继续向原始根 Customer 对象添加内部联接?
Trying to decorate an IQueryOver by using some conditional logic. Consider the following code which is a private method on an object representing a customer search.
private void addCustomerCriterion<T>(IQueryOver<T, T> query) where T : Customer
{
if (!string.IsNullOrEmpty(Name))
query = query
.Where(x => x.FirstName.ToUpper().Contains(Name.ToUpper()) ||
x.LastName.ToUpper().Contains(Name.ToUpper()));
if (HasOpenTicket.HasValue)
query = query
.Inner.JoinQueryOver<ServiceTicket>(c => c.ServiceTickets)
.Where(t => t.StatusName == "Open")
}
if (HasOpenInvoice.HasValue)
query = query
.Inner.JoinQueryOver<Invoice>(c => c.Invoices)
.Where(i => i.StatusName == "Open");
}
Unfortunately, this doesn't compile. Makes sense, because in the second and third if-statement, I am breaking the fluent interface's type safety. According to http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx, the JoinQueryOver converts my query from QueryOver(of Customer, Customer) to a QueryOver(of Customer, ServiceTicket). Types don't match up now when I try to decorate using the query = query idiom.
So the real problem here is that I've traversed down into the ServiceTicket relationship from the Customer object, and it's changed the type of my QueryOver object. How can I traverse back up the tree so that I can keep adding inner joins to the original root Customer object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您拥有的原始 IQueryOver 是可变的,并注册对 JoinQueryOver 的调用,因此您不需要存储返回值(除非您需要从连接实体添加更多条件连接)。
I think the original IQueryOver you have is mutable and registers the call to JoinQueryOver so you shouldn't need to store the return value (unless you need to add more conditional joins from the joined entity).