2
votes

I would like to clarify the role of the service layer in my mind. I have a classic architecture for my app : Controller => Service => DAO (JpaRepository).

The service perform my business logic and are annotated with @Transactional.

Let's take a simple example and say that I have two entities:

  • Company
  • Project

A company can have several project and a project can be in one company. Let's assume that there is one controller for each entity. For example, if I want to get all the projects of one company, I have two options.

  1. Calling ProjectService in my CompanyController and create a method in the ProjectService that get all projects by a company ID (by a query in the DAO). Then, I will have several @Autowired (for all services) in my controller but only one in my service.
  2. Adding a FindAllProject in my CompanyService that will call the project DAO method. Then, I will have only one @Autowired in my controller but several in my service.

What would be the best approach ?

Thanks in advance for your answer.

Seb

1
Perhaps ProjectService, CompanyService, and CompanyController have too much responsibility? Maybe there should be finer grained implementations such as ProjectSearchService, CompanySearchController, and CompanySearchService.Andrew S

1 Answers

0
votes

I'm not sure whether there is a best practice for this. I would like to go for the 1st option. The reason is:

Service is responsible for business logic. Your requirement is get the Projects List from a Company, the returned result is a List of Projects.

The ProductService layer then just need to call one ProductDAO which is responsible for Product only

With that in mind, when the methods increase, just remember: What do you want to get as returned result?

  • A Project so you go for ProjectService
  • A Company so you go for CompanyService