Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Requirements version 1.0
- need return {"name": "JohnDoe"}
- === API version 1.0 ===
- @Entity
- class Model {
- @Column
- String name;
- }
- @Json
- class ModelJson {
- String name;
- }
- @Service
- class ModelService {
- Dao dao;
- ModelToJsonMapper mapper;
- @Transactional
- ModelJson getModelJsonBy(id) {
- Model model = dao.getBy(id);
- ModelJson json = mapper.toJson(model);
- return json;
- }
- }
- Requirements version 1.1 (!)
- need return {"name": "JohnDoe", "qty": 2}
- === API version 1.1 ===
- @Entity
- class Model {
- @Column
- String name; // ! remains the same
- }
- @Entity
- class SomeQty { // another entity
- int qty;
- }
- @Json
- class ModelJson {
- String name;
- int qty // ! field added
- }
- @Service
- class ModelService {
- Dao dao;
- ModelToJsonMapper mapper;
- SomeQtyService qtyService; !!! EVIL solution No1
- @Transactional
- ModelJson getModelJsonBy(id) {
- Model model = dao.getBy(id);
- SomeQty qty = qtyService.getBy(...); !!! EVIL solution No2
- ModelJson json = mapper.toJson(model, qty); !!! EVIL solution No3
- return json;
- }
- }
- _Model_Service (!! not Qty) became dependent on other service of other entity, not even connected with each other. Reason is just miserable json changes. Conclusion - you are NOT controling your internal architecture. It is out of your own control. Json format rules over your service architecture instead of you.
- You've increased service layer complexity.
- You've increased services strong coupling.
- What it MIGHT be:
- === API version 1.0 ===
- @RestController
- class Controller {
- ModelService modelService;
- ModelToJsonMapper mapper;
- @Request
- JsonModel getBy(id) {
- Model model = modelService.getBy(id);
- ModelJson json = mapper.toJson(model);
- return json;
- }
- }
- === API version 2.0 ===
- @RestController
- class Controller {
- ModelService modelService;
- QtyService qtyService; // 1 !!!
- ModelToJsonMapper mapper;
- @Request
- JsonModel getBy(id) {
- Model model = modelService.getBy(id, FULL);
- SomeQty qty = qtyService.get(..., FULL); // 2.1
- ModelJson json = mapper.toJson(model, qty); // 2.2
- return json;
- }
- }
- 1 - Presentation layer became dependent on service layer, not vice versa, in opposition to your approach.
- 2 - json changes affects ONLY presentation layer complexity, and NOT a service layer complexity.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement