Performance: Why service layer matters
MEDIUM IMPACT
This concept affects the organization of backend logic which indirectly impacts frontend load speed and responsiveness by controlling data flow and processing efficiency.
public class UserService { @Autowired private UserRepository userRepository; public User getUser(Long id) { // Business logic centralized here return userRepository.findById(id).orElse(null); } } @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users/{id}") public User getUser(@PathVariable Long id) { return userService.getUser(id); } }
public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/users/{id}") public User getUser(@PathVariable Long id) { // Directly accessing repository in controller return userRepository.findById(id).orElse(null); } }
| Pattern | Backend Logic Organization | Code Duplication | Response Time Impact | Verdict |
|---|---|---|---|---|
| Controller directly accesses repository | Mixed in controller | High duplication risk | Slower API responses | [X] Bad |
| Business logic in service layer | Centralized in service | Low duplication | Faster, consistent responses | [OK] Good |