assembler.java.ftl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package ${package.Interfaces}.assembler;
  2. import ${package.Domain}.model.aggregate.${entity?lower_case}.${entity};
  3. import ${package.Interfaces}.web.dto.request.${entity}RequestDTO;
  4. import ${package.Interfaces}.web.dto.response.${entity}ResponseDTO;
  5. import ${package.Application}.dto.${entity}DTO;
  6. import org.springframework.stereotype.Component;
  7. /**
  8. * <p>${table.comment}DTO装配器</p>
  9. *
  10. * <p>负责${table.comment}聚合根与DTO之间的转换,位于接口层。
  11. * 装配器封装了领域对象与DTO之间的转换逻辑,保持领域模型的纯净性。</p>
  12. *
  13. * <p>主要职责:
  14. * <ul>
  15. * <li>聚合根转换为DTO</li>
  16. * <li>DTO转换为聚合根</li>
  17. * <li>处理DTO与领域对象的映射关系</li>
  18. * </ul>
  19. * </p>
  20. *
  21. * @author ${author}
  22. * @since ${date}
  23. */
  24. @Component
  25. public class ${entity}Assembler {
  26. /**
  27. * <p>聚合根转换为响应DTO</p>
  28. *
  29. * <p>将${table.comment}聚合根转换为响应DTO,用于API响应。</p>
  30. *
  31. * @param ${entity?substring(0,1)?lower_case}${entity?substring(1)} ${table.comment}聚合根对象
  32. * @return ${table.comment}响应DTO对象
  33. */
  34. public ${entity}ResponseDTO toResponseDTO(${entity} ${entity?substring(0,1)?lower_case}${entity?substring(1)}) {
  35. if (${entity?substring(0,1)?lower_case}${entity?substring(1)} == null) {
  36. return null;
  37. }
  38. ${entity}ResponseDTO dto = new ${entity}ResponseDTO();
  39. // TODO: 实现聚合根到响应DTO的转换逻辑
  40. <#list table.fields as field>
  41. dto.set${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}(${entity?substring(0,1)?lower_case}${entity?substring(1)}.get${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}());
  42. </#list>
  43. return dto;
  44. }
  45. /**
  46. * <p>请求DTO转换为聚合根</p>
  47. *
  48. * <p>将请求DTO转换为${table.comment}聚合根,用于创建或更新操作。</p>
  49. *
  50. * @param requestDTO ${table.comment}请求DTO对象
  51. * @return ${table.comment}聚合根对象
  52. */
  53. public ${entity} toAggregate(${entity}RequestDTO requestDTO) {
  54. if (requestDTO == null) {
  55. return null;
  56. }
  57. ${entity} ${entity?substring(0,1)?lower_case}${entity?substring(1)} = new ${entity}();
  58. // TODO: 实现请求DTO到聚合根的转换逻辑
  59. <#list table.fields as field>
  60. ${entity?substring(0,1)?lower_case}${entity?substring(1)}.set${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}(requestDTO.get${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}());
  61. </#list>
  62. return ${entity?substring(0,1)?lower_case}${entity?substring(1)};
  63. }
  64. /**
  65. * <p>应用DTO转换为聚合根</p>
  66. *
  67. * <p>将应用层DTO转换为${table.comment}聚合根。</p>
  68. *
  69. * @param dto ${table.comment}应用DTO对象
  70. * @return ${table.comment}聚合根对象
  71. */
  72. public ${entity} toAggregate(${entity}DTO dto) {
  73. if (dto == null) {
  74. return null;
  75. }
  76. ${entity} ${entity?substring(0,1)?lower_case}${entity?substring(1)} = new ${entity}();
  77. // TODO: 实现应用DTO到聚合根的转换逻辑
  78. <#list table.fields as field>
  79. ${entity?substring(0,1)?lower_case}${entity?substring(1)}.set${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}(dto.get${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}());
  80. </#list>
  81. return ${entity?substring(0,1)?lower_case}${entity?substring(1)};
  82. }
  83. /**
  84. * <p>聚合根转换为应用DTO</p>
  85. *
  86. * <p>将${table.comment}聚合根转换为应用层DTO。</p>
  87. *
  88. * @param ${entity?substring(0,1)?lower_case}${entity?substring(1)} ${table.comment}聚合根对象
  89. * @return ${table.comment}应用DTO对象
  90. */
  91. public ${entity}DTO toDTO(${entity} ${entity?substring(0,1)?lower_case}${entity?substring(1)}) {
  92. if (${entity?substring(0,1)?lower_case}${entity?substring(1)} == null) {
  93. return null;
  94. }
  95. ${entity}DTO dto = new ${entity}DTO();
  96. // TODO: 实现聚合根到应用DTO的转换逻辑
  97. <#list table.fields as field>
  98. dto.set${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}(${entity?substring(0,1)?lower_case}${entity?substring(1)}.get${field.propertyName?substring(0,1)?upper_case}${field.propertyName?substring(1)}());
  99. </#list>
  100. return dto;
  101. }
  102. }