DDD Query Way

In last few years I was lucky and I had the opportunity to work with some new and powerful java frameworks and with the newest software design approaches, and in particular I specialised in DDD (Domain Driven Design).

When you try to develop a software following DDD principles you need to use some frameworks that help you to thinking only to domain, example the ORM for the persistence or user interface generator for the user interaction.
Now I want focus on persistence because in my work experience I found some gap in the persistence frameworks.

Explain the gap:

if you are using java probably you want define all rules and all behaviour of your domain using only java, and this is almost possible,

Just define a domain:

 1 public class Person {
 2     private String name;
 3     private String surname;
 4     private Dog dog;
 5    //.... gets sets ...
 8public class Dog {
 9   private String name;
10   //.... gets sets ...

And try to persist the domain(JPA api):

1 Person p = new Person();
3p.setDog(new Dog());
5EntityManager manager=...

OK you can do all CRUD(create read update delete) only with java, this is good!! but what happen if you try to search something??

"select * from Person where name=:name"

I have to use another SQL-like language (JPQL,JDOQL,HQL. etc...) this is not java ... not good.

The reasons why this is not good are many:

  • if you want refactoring your code you have to check all SQL-like everywhere
  • You can't check at compile time if something is wrong like types field names
  • have to learn another language
  • at least i broken my DDD principles ;)

OK now we need something of different but before start an hard work I'll try to check on Internet if someone did it!

Yes i found it:

JPA type safe query
JDO type safe query
SQL type safe query

Yes good, someone(of big) have the same my problem and they try to solve it... but i don't like this kind of solution! you have to define manually or generate automatically another static class that expose a reflection of your domain end then if you change something you have to update it!

OK ... Is my time ! I try find a better solution!

I worked with java for years and I had discovered there is three main way to write implementation independent code:

  • Statically with simple java interface(all know it)
  • Dynamically with reflection (almost all know it)
  • Dynamically with proxy (almost all use it, but just someone know it)
so the first solution is not for me because a can't force people to adapt to my interfaces, the second one is good but i need something of more powerful, proxy is your time ;)

usually when we want execute a query in a DDD approach we want an object that describe the query, where we set the target of query the conditions and all we need:
1        ObjectQuery<Person> query = new GenericObjectQuery<Person>(Person.class);
good this object can be the start of my proxy, just do it
1        ObjectQuery<Person> query = new GenericObjectQuery<Person>();
2        Person target =;
3        query.eq(target.getName(),"test");
Yeeeaaa I like it, and I have done it:ObjectQuery

But ... is not finished here .... to complete it I have to fix lot of problems... but this is another post ;)


Java Shell Script
13 May 2015
Tags: ,

Self Hosted Calendar Server With Radicale
2 May 2015

New Blog
14 April 2014

Primitive Boxing Problem
20 November 2012
Tags: ,

DDD Query Way
20 October 2012
Tags: , ,




Object Query