Spring service and repository layer convention

2019-06-26 09:05发布

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?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-26 09:35
  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

查看更多
戒情不戒烟
3楼-- · 2019-06-26 09:40

Check also answer:

It explains the Persistence Bussines and Presentation layers

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

查看更多
登录 后发表回答