Vipercold
Vipercold

Reputation: 609

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

Upvotes: 14

Views: 11659

Answers (2)

Y123
Y123

Reputation: 987

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.

Upvotes: 11

riddle_me_this
riddle_me_this

Reputation: 9155

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.

Upvotes: 2

Related Questions