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:

public class Person {
     private String name;
     private String surname;
     private Dog dog;
    //.... gets sets ...

public class Dog {
   private String name;
   //.... gets sets ...

And try to persist the domain(JPA api):

Person p = new Person();
p.setDog(new Dog());

EntityManager 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:

	ObjectQuery<Person? query = new GenericObjectQuery<Person>(Person.class);

good this object can be the start of my proxy, just do it

	ObjectQuery<Person> query = new GenericObjectQuery<Person>();
	Person target =;

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 ;)


First Week Librem 5
Tags: librem5

Rust lib error management, multiple enum approach
Tags: rust

Setup Gitlab runner for run ci tests locally
Tags: self_hosting

Check What You Run
Tags: sh

Self Hosting Feed Reader Server
Tags: self_hosting




Object Query
URL Freezer