简体   繁体   中英

Implementing CQRS with OOP in Java

I have an API where representation of query and entity model is represented by the same class. A simplified version :

class Model {
  private Long id;
  private Double a;
  private Double b;
  ...
}

The problem is that those properties together make no sense as query object. It does not need id altogether, and a , b are exclusive. Code is hard to understand without proper separation. My plan involves creating separate query classes: A and B for each type of key.

What makes me worry though is that in order to avoid using instanceof (any code assuming knowledge of all query types) I will need to implement all cross cutting concerns in query objects. For example, mapping to WHERE clause as part of actual SQL query.

To avoid client having implicit dependency on implementation my initial plan is to create server side classes that extend each POJO A and B and implement details there. This requires additional mapping layer to map from A to some other class that will implement a sqlQuery() operation (simplifying matters).

Question: Since this seems is complex enough, is there some standard approach for this kind of problem? References, patterns welcome.

As you sayed implementing separate query classes seems to be a good idea. The GOF's strategy pattern could be a good point to start at, see http://en.wikipedia.org/wiki/Strategy_pattern

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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