开发者

Query Object Pattern (Design Pattern)

开发者 https://www.devze.com 2022-12-16 23:10 出处:网络
I need to implement a Query Object Pattern in Java for my customizable search interface (of a webapp I\'m writing).

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

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

Thanks in Advance

ADDITION How to add a Query开发者_JS百科 Pattern to an existing DAO pattern?


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 API and the modern JPA2 Criteria API are an excellent examples which combines it with the Builder Pattern.

As to your question:

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

I would recommend to have a look at JPA2.


I wrote a C# implementation for NHibernate here: 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:

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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号