Using the book as analogy, suppose I have a book resource with the following API (there are also update and delete etc but for simplicity only show two here)
GET /book/{id}
POST /book
Each of these API would call other APIs to get the result, rather than the typical CRUD database operation. And based on the requirement / existing framework constraint, there are separate two controller class GetBookController
and CreateBookController
. The controller is to handle the request and response. So the actual business logic and retrieve / create book are in the service layer.
The question then is, should there be a separate interface for each of book operation(GetBookService
and CreateBookService
), or to have just only one (BookService
)?
Based on the Interface Segregation Principle which states "Clients should not be forced to depend upon interfaces that they do not use". Here the GetBookController
class is the client, where it only has to query book without creating it, so it only requires GetBookService
. If it is to use BookService
, it doesn't use the method createBook
, which seems to violate ISP principle. However, if using separate interface, it would result in many interface and implementation classes being created. Am I misunderstanding the ISP principle?
@Controller
public class GetBookController {
@Autowired
private GetBookService getBookService;
}
@Controller
public class CreateBookController {
@Autowired
private CreateBookService createBookService;
}
public interface GetBookService {
Book getBook(long id);
}
public interface CreateBookService {
Boolean createBook(Book request);
}
public interface BookService {
Book getBook(long id);
Boolean createBook(Book request);
}