简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM