Mai Hữu Lợi
Mai Hữu Lợi

Reputation: 569

Spring service and repository layer convention

I start working with Spring and have some confusions about its conventions.

  1. Is it fine to put Repositories in Controller?
  2. In a Service class, If I want to reuse code could I inject other Services and other Reposities?
  3. is it the best practice to name Service And Repository class is based on Entity name i.e: User -> UserRepository->UserService?

Upvotes: 6

Views: 3093

Answers (2)

andreyro
andreyro

Reputation: 985

Check also answer:

It explains the Persistence Bussines and Presentation layers

https://stackoverflow.com/a/34084807/2218992

Upvotes: 1

Si mo
Si mo

Reputation: 989

  1. No, don't use Repositories in the Controller. Only in the Services. And don't use Entities in your Controller. Create Dto (Data Transfer Objects) Object from the Entities and work with this in your frontend

    1. Yes you can use other services and respositories in your service class

    2. Yes it is. Name the interfache UserService and the implementation UserServiceImpl

Upvotes: 7

Related Questions