ARTICLE AD BOX
I'm building a RESTful API using Spring Boot with a standard layered architecture: Controller (Resource) -> Facade -> Service -> Repository.
I have an incoming Data Transfer Object (ProductRequestDto) which uses codes/natural keys for some relationships (like imageCodes and countryCode). My internal persistence Entity (Product) uses Foreign Key IDs for these same relationships (imageIds and countryId).
Before the Product entity is passed to the ProductService for persistence, I need to:
Translate/Lookup the natural keys (e.g., countryCode) into foreign key IDs (e.g., countryId). This lookup will likely involve a separate repository/service call (e.g., countryRepository.findByCode(countryCode)).
Map the remaining properties from the ProductRequestDto to the Product entity.
In which layer/component should this lookup and translation logic reside?
Should it be in the Mapper, the Facade, or the Service layer?
If I had a validation service that needed to check and alert the user that he had conflicting or too many images, where would that belong?
Thank you!
@Getter @Setter public class ProductRequestDto { private Long id; private String code; private String name; private String description; private BigDecimal price; private Set<Long> imageCodes; //Here as codes private String countryCode; //Here as code private Boolean active; private Instant createdAt; private Instant updatedAt; } @Getter @Setter public class Product { private Long id; private String code; private String name; private String description; private BigDecimal price; private Set<Long> imageIds; //Here as image ids (fk) private Long countryId; //Here as country Id (fk) private Boolean active; private Instant createdAt; private Instant updatedAt; } @RestController @RequestMapping("/api") public class ProductResource { private final ProductFacade productFacade; @Autowired public ProductResource(ProductFacade productFacade) { this.productFacade = productFacade; } @PostMapping("/v1/products") public ProductResponseDto createProduct(@RequestBody CreateProductDto createProductDto){ return productFacade.createProduct(createProductDto); } } @Component public class ProductFacade { private final ProductService productService; private final ProductMapper productMapper; @Autowired public ProductFacade(ProductService productService, ProductMapper productMapper) { this.productService = productService; this.productMapper = productMapper; } public ProductResponseDto createProduct(ProductRequestDto dto) { Product product = productMapper.toProduct(dto); Product createdProduct = productService.createProduct(product); return productMapper.toProductDto(createdProduct); } } @Component public class ProductService { private final ProductRepository productRepository; @Autowired public ProductService(ProductRepository productRepository) { this.productRepository = productRepository; } public Product createProduct(Product product) { return productRepository.createProduct(product); } }