0

Good morning, in my project I'm using three-tier architecture, I have one package for controller, and one for service, and another one for data model and repositories (I'm using spring data).

So I need to create some classes just for the web services in the controllers package.

My question is, it is a best practice to create those classes in the controllers package or in the model package or the model package can just have the types to be persisted?

Thank you in advance.

Ismail
  • 2,322
  • 1
  • 12
  • 26
  • It mostly depends on project scale(small, large, distributed), re-usability. You can use same controller package or inner controller package or whole new model package. – Haripriya Apr 05 '19 at 10:39
  • 1
    Hope this answer - https://stackoverflow.com/a/8510896/10099219 would help you decide. – Haripriya Apr 05 '19 at 10:52

1 Answers1

1

After many years working with multilayer architectures, the best practice is to group the classes in a way that is meaningful to you and your developers. See the following example:

├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── mycompany
│   │   │           └── myproject
│   │   │               └── mymodule
│   │   │                   ├── Application.java
│   │   │                   ├── data
│   │   │                   │   ├── bo
│   │   │                   │   │   ├── Account.java
│   │   │                   │   │   ├── Customer.java
│   │   │                   │   │   ├── User.java
│   │   │                   │   └── repository
│   │   │                   │       ├── AccountRepository.java
│   │   │                   │       ├── CustomerRepository.java
│   │   │                   │       └── UserRepository.java
│   │   │                   ├── exception
│   │   │                   │   ├── DuplicatedRecordException.java
│   │   │                   │   ├── NoResultException.java
│   │   │                   │   └── UnknownErrorException.java
│   │   │                   ├── web
│   │   │                   │   └── controller
│   │   │                   │       ├── AccountController.java
│   │   │                   │       ├── CustomerController.java
│   │   │                   │       └── UserController.java
│   │   │                   └── service
│   │   │                       ├── AccountService.java
│   │   │                       ├── CustomerService.java
│   │   │                       └── UserService.java
│   │   └── resources
│   │       ├── application.yml
│   │       ├── bootstrap.yml
│   │       ├── logback-spring.xml
│   │       └── messages
│   │           ├── message_es.properties
│   │           └── message.properties

The previous example group classes in packages by:

BO (Business Object) Entities that represents data

Exception Your custom exception classes

Web Controller If you are going to build rest all your controllers should be here.

Service Your services classes if needed.

Resources All your application resources

ELavicount
  • 429
  • 3
  • 16