装饰一个 IQueryOver使用 NHibernate 3.0

发布于 2024-09-30 17:22:35 字数 1332 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2024-10-07 17:22:35

我认为您拥有的原始 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文