Guest User

Untitled

a guest
May 13th, 2019
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Requirements version 1.0
  2. need return {"name": "JohnDoe"}
  3. === API version 1.0 ===
  4.  
  5. @Entity
  6. class Model {
  7. @Column
  8. String name;
  9. }
  10.  
  11. @Json
  12. class ModelJson {
  13. String name;
  14. }
  15.  
  16. @Service
  17. class ModelService {
  18. Dao dao;
  19. ModelToJsonMapper mapper;
  20.  
  21. @Transactional
  22. ModelJson getModelJsonBy(id) {
  23. Model model = dao.getBy(id);
  24. ModelJson json = mapper.toJson(model);
  25. return json;
  26. }
  27. }
  28.  
  29. Requirements version 1.1 (!)
  30. need return {"name": "JohnDoe", "qty": 2}
  31. === API version 1.1 ===
  32.  
  33. @Entity
  34. class Model {
  35. @Column
  36. String name; // ! remains the same
  37. }
  38.  
  39. @Entity
  40. class SomeQty { // another entity
  41. int qty;
  42. }
  43.  
  44. @Json
  45. class ModelJson {
  46. String name;
  47. int qty // ! field added
  48. }
  49.  
  50. @Service
  51. class ModelService {
  52. Dao dao;
  53. ModelToJsonMapper mapper;
  54. SomeQtyService qtyService; !!! EVIL solution No1
  55.  
  56. @Transactional
  57. ModelJson getModelJsonBy(id) {
  58. Model model = dao.getBy(id);
  59. SomeQty qty = qtyService.getBy(...); !!! EVIL solution No2
  60. ModelJson json = mapper.toJson(model, qty); !!! EVIL solution No3
  61. return json;
  62. }
  63. }
  64.  
  65. _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.
  66. You've increased service layer complexity.
  67. You've increased services strong coupling.
  68.  
  69. What it MIGHT be:
  70.  
  71. === API version 1.0 ===
  72.  
  73. @RestController
  74. class Controller {
  75. ModelService modelService;
  76. ModelToJsonMapper mapper;
  77.  
  78. @Request
  79. JsonModel getBy(id) {
  80. Model model = modelService.getBy(id);
  81. ModelJson json = mapper.toJson(model);
  82. return json;
  83. }
  84. }
  85.  
  86. === API version 2.0 ===
  87.  
  88. @RestController
  89. class Controller {
  90. ModelService modelService;
  91. QtyService qtyService; // 1 !!!
  92. ModelToJsonMapper mapper;
  93.  
  94. @Request
  95. JsonModel getBy(id) {
  96. Model model = modelService.getBy(id, FULL);
  97. SomeQty qty = qtyService.get(..., FULL); // 2.1
  98. ModelJson json = mapper.toJson(model, qty); // 2.2
  99. return json;
  100. }
  101. }
  102.  
  103. 1 - Presentation layer became dependent on service layer, not vice versa, in opposition to your approach.
  104. 2 - json changes affects ONLY presentation layer complexity, and NOT a service layer complexity.
RAW Paste Data