I want to ask about Architectural pattern. I write two snippet code to demo what I ask.
The first way is:
//a method on controller layer (in Spring framework)
@RequestMapping(...)
public ShopDTO findShop(final Long shopId) {
Shop shop = shopService.getShopById(shopId);
ShopDTO shopDTO = shopMapper.toShopDTO(shop);
return shopDTO;
}
//A method on service layer
@Transactional
public Shop getShopById(final Long shopId) {
//some code to find an entity by id
}
- Note: the code what maps from shop entity to
ShopDTO
in controller layer.
The second way is:
//a method on controller layer (in Spring framework)
@RequestMapping(...)
public ShopDTO findShop(final Long shopId) {
ShopDTO shopDTO = shopService.getShopById(shopId);
return shopDTO;
}
//A method on service layer
@Transactional
public ShopDTO getShopById(final Long shopId) {
Shop shop = shopRepository.findById(shopId);
ShopDTO shopDTO = shopMapper.toShopDTO(shop);
return shopDTO;
}
- Note: the code what maps from shop entity to
ShopDTO
in service layer.
I use Spring framework code for example.
My question is: Which is the best layer to place mapper code. And can you tell me why?
By the way, what type logic should place on controller layer and what should place on service layer?.