I’m developing a relatively complex Spring Boot application following a classic layered architecture
Example:
project-root/
├── controller/
│ ├── SampleController.java
│ └── ...
├── service/
│ ├── SamplService.java
│ └── ...
├── model/
│ ├── SampleModel.java
│ └── ...
└── repository/
├── SampleRepository.java
└── ...
My question is about organizing the class structure and naming conventions in the service layer.
There are many services in the application, and I need a way to distinguish between primary services (those invoked directly by controllers) and secondary services (utility services that are only consumed by one or many other services).
I’m considering a couple of different approaches:
- Placing all service layer classes in a single service package, without distinguishing between whether they are invoked directly by controllers or are helper services for other services.
Example:
service/
├── AService.java
├── BService.java
├── FirstHelperService.java ## has Service suffix in name
├── SecondHelperService.java ## has Service suffix in name
├── ...
└── ServiceZ.java
- A classic helpers directory for secondary services. But I don’t really like this approach, because helpers are going to grow up to a heap of rubbish.
Example:
service/
├── AService.java
├── BService.java
├── ...
└── ServiceZ.java
helpers/
├── FirstHelper.java ## has Helper suffix in name
├── SecondHelper.java ## has Helper suffix in name
- Organizing helpers according to common problem, that they are trying to solve.
Example:
project-root/
├── validator/
│ └── SampleValidator
├── mapper/
│ └── SampleMapper
├── builder/
│ └── SampleBuilder
My goal is to achieve maximum maintainability of the code. The code should be as intuitively understandable as possible.
Which approach would you choose, or do you know of any other approaches?