There is a MVC pattern. The controller accepts the request and simply jerks the methods from the model. All business logic is stored in models. The models are tied to each other, i.e. Student and Courses models can know about each other, there is no complete isolation. And the call to the database comes from the model itself, i.e. the model itself knows how to go to the database to get all students with a GPA> = 3.5. For this, the model creates a method findByGpa(Double gpa){} . In the model, we describe everything that our entity can do. For example, updateStatus (), getCourse (), retakeExam (), etc. And these methods can then be reused from the controller, from each other, or from other models.
In Spring, I encountered a different approach that is not yet completely understood.
- There is StudentDto, there are only fields in it, no methods.
- There is StudentDao, in it there are only calls to the database in the studens table, and no more. Therefore, in this class only crud-methods.
- There is a StudentRepository, it is the same as in dao, but you can apply to different tables. But it is not exactly.
- There is a StudentService - here is the logic for the Student.
- There is StudentController - it only jerks methods from Service.
- There is a view - it receives data from the controller and renders the view.
The first question is what are the differences between dao and repository. The second question is more voluminous. If an application has a new requirement, then I would consistently call the model's methods from the controller, if there are not enough of them, I would add a new “skill” to the model, but basically I would reuse its already implemented methods. And I would get the result.
In the spring framework, if I create a new method for each new task in the Service layer, where I will implement each step of the new requirement line by line, then there is no question of reusing methods. And my Service is just a code dump that grows at the slightest extension.
How do you need to organize the code to apply all the advantages of OOP and easily scale, manage the code?