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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2068478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:13:48  来源:igfitidea点击:

Query Object Pattern (Design Pattern)

javadesign-patterns

提问by Buhake Sindi

I need to implement a Query Object Pattern in Java for my customizable search interface (of a webapp I'm writing).

我需要在 Java 中为我的可自定义搜索界面(我正在编写的 web 应用程序)实现一个查询对象模式。

Does anybody know where I can get an example/tutorial of Query Object Pattern (Martin Fowler's QoP)?

有谁知道我可以在哪里获得查询对象模式(Martin Fowler 的 QoP)的示例/教程?

Thanks in Advance

提前致谢

ADDITIONHow to add a Query Pattern to an existing DAO pattern?

ADDITION如何将查询模式添加到现有的 DAO 模式?

回答by BalusC

The word "pattern" in the "Query Object Pattern" is (IMHO) misplaced. It's not a real design pattern. The "Query Object" is just another example of the Interpreter Pattern. The legacy Hibernate Criteria APIand the modern JPA2 Criteria APIare 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

回答by Abdur Rahman

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. 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. It also leads to duplication in the SQL statements should the database schema change.

SQL 可能是一种复杂的语言,许多开发人员并不特别熟悉它。此外,您需要知道形成查询的数据库模式是什么样的。您可以通过创建将 SQL 隐藏在参数化方法中的专用查找器方法来避免这种情况,但这会使形成更多的即席查询变得困难。如果数据库架构更改,它还会导致 SQL 语句重复。

A Query Object is an interpreter [Gang of Four], that is, a structure of objects that can form itself into a SQL query. 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.

查询对象是一个解释器 [四人组],即可以将自身形成为 SQL 查询的对象结构。您可以通过引用类和字段而不是表和列来创建此查询。通过这种方式,编写查询的人可以独立于数据库模式执行此操作,并且可以将模式更改本地化在一个地方。

回答by Shayne

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_threadhttp://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