You don't always need a service layer. Especially if your APIs are just simple CRUD operations, for example, with no real logic required or calculations.
However, if you have an API which performs some logic before querying your repository then this should be in a separate service class. This is good practice arising from what is known as the single responsibility principle.
https://en.wikipedia.org/wiki/Single_responsibility_principle
- Your Controller's single responsibility should be to handle the
incoming request.
- The Service layer's single responsibility is to do any logic required with the data received by the Controller.
- The repository's single responsibility is to query the data base.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…