简体   繁体   English

查询对象模式(设计模式)

[英]Query Object Pattern (Design Pattern)

I need to implement a Query Object Pattern in Java for my customizable search interface (of a webapp I'm writing). 我需要在Java中为我的可自定义搜索界面(我正在编写的webapp)实现一个查询对象模式。

Does anybody know where I can get an example/tutorial of Query Object Pattern (Martin Fowler's QoP)? 有谁知道我在哪里可以获得查询对象模式(Martin Fowler的QoP)的示例/教程?

Thanks in Advance 提前致谢

ADDITION How to add a Query Pattern to an existing DAO pattern? 加成如何查询模式添加到现有的DAO模式?

The word "pattern" in the "Query Object Pattern" is (IMHO) misplaced. “查询对象模式”中的“模式”一词是(IMHO)放错位置的。 It's not a real design pattern. 这不是一个真正的设计模式。 The "Query Object" is just another example of the Interpreter Pattern . “查询对象”只是解释器模式的另一个例子。 The legacy Hibernate Criteria API and the modern JPA2 Criteria API are an excellent examples which combines it with the Builder Pattern . 传统的Hibernate Criteria API和现代JPA2 Criteria API是一个很好的例子,它将它与Builder Pattern结合在一起。

As to your question: 至于你的问题:

How to add a Query Pattern to an existing DAO pattern? 如何将查询模式添加到现有DAO模式?

I would recommend to have a look at JPA2 . 我建议看看JPA2

Query Object 查询对象

An object that represents a database query. 表示数据库查询的对象。

For a full description see here 有关完整说明,请参见此处

SQL can be an involved language, and many developers aren't particularly familiar with it. SQL可以是一种涉及的语言,许多开发人员并不是特别熟悉它。 Furthermore, you need to know what the database schema looks like to form queries. 此外,您需要知道数据库模式形成查询的样子。 You can avoid this by creating specialized finder methods that hide the SQL inside parameterized methods, but that makes it difficult to form more ad-hoc queries. 您可以通过创建隐藏参数化方法中的SQL的专用查找器方法来避免这种情况,但这使得难以形成更多的即席查询。 It also leads to duplication in the SQL statements should the database schema change. 如果数据库架构发生更改,它还会导致SQL语句重复。

A Query Object is an interpreter [Gang of Four], that is, a structure of objects that can form itself into a SQL query. 查询对象是一个解释器[Gang of Four],即可以将自身形成为SQL查询的对象结构。 You can create this query by referring to classes and fields rather than tables and columns. 您可以通过引用类和字段而不是表和列来创建此查询。 In this way those who write the queries can do so independently of the database schema and changes to the schema can be localized in a single place. 通过这种方式,编写查询的人可以独立于数据库模式执行此操作,并且可以将对模式的更改本地化在一个位置。

I wrote a C# implementation for NHibernate here: https://github.com/shaynevanasperen/NHibernate.Sessions.Operations . 我在这里为NHibernate编写了一个C#实现: https//github.com/shaynevanasperen/NHibernate.Sessions.Operations

It works by using an interface like this: 它通过使用这样的接口工作:

public interface IDatabases
{
    ISessionManager SessionManager { get; }

    T Query<T>(IDatabaseQuery<T> query);
    T Query<T>(ICachedDatabaseQuery<T> query);

    void Command(IDatabaseCommand command);
    T Command<T>(IDatabaseCommand<T> command);
}

Given a POCO entity class like this: 给定像这样的POCO实体类:

class Database1Poco
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

You can build query objects like this: 您可以像这样构建查询对象:

class Database1PocoByProperty1 : DatabaseQuery<Database1Poco>
{
    public override Database1Poco Execute(ISessionManager sessionManager)
    {
        return sessionManager.Session.Query<Database1Poco>().SingleOrDefault(x => x.Property1 == Property1);
    }

    public int Property1 { get; set; }
}

And then use them like this: 然后像这样使用它们:

var database1Poco = _databases.Query(new Database1PocoByProperty1 { Property1 = 1 });

You could port that to Java if you like it. 如果你喜欢,你可以将它移植到Java。

Here are some other examples: 以下是其他一些例子:

https://lostechies.com/jimmybogard/2012/10/08/favor-query-objects-over-repositories/ http://www.mrdustpan.com/command-query-objects-with-dapper#disqus_thread http://crosscuttingconcerns.com/CommandQuery-Object-pattern https://lostechies.com/jimmybogard/2012/10/08/favor-query-objects-over-repositories/ http://www.mrdustpan.com/command-query-objects-with-dapper#disqus_thread http:/ /crosscuttingconcerns.com/CommandQuery-Object-pattern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM