I have an controller that call three services :
public class ProductController() {
@Autowired
private AccountService accountService;
@Autowired
private ProcessService processService;
@Autowired
private releaseService releaseService;
@RequestMapping("/process")
public Product process(@RequestParam(value="name", defaultValue="docs") ProductProcessed process) {
accountService.notify();
releaseService.sendRelease(process);
return processService.process(process);
}
}
What is the best approach to encapsulate this service calls??
What you are looking for is possibly some design patterns. I approach could be to create a coarse-grained facade over the fine-grained services (Account, Process and Release). (see also Coarse-grained vs fine-grained)
The Facade will basically have these 3 services injected in them and encapsulate the behavior you are making your controller perform currently. This way you will minimize the business logic to invoking the coarse grained service in your controller thus further encapsulating the guts of the system.
You already have them marked as private, so they cannot be called outside of this class. This is encapsulated.
A common practice is to autowire them so the implementation of the service can be modified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With