Spring service and repository layer convention

2019-06-26 09:32发布

问题:

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?

回答1:

  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



回答2:

Check also answer:

It explains the Persistence Bussines and Presentation layers

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