Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach to use multiple services inside a resource controller?

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??

like image 383
Vipercold Avatar asked Apr 01 '15 14:04

Vipercold


2 Answers

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.

like image 141
Y123 Avatar answered Nov 14 '22 06:11

Y123


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.

like image 28
vphilipnyc Avatar answered Nov 14 '22 07:11

vphilipnyc