0

I'm not sure how I should have worded the question.

If I have a Student and Classes database, and I have queries that involve both. Where do I put my queries? In one case, someone wants student information, and I query Students and Classes in my Student class, if I put it in Classes, I cross over to the Students, [edit] violating single responsibility.

I thought I should only have queries for Students and queries for Classes in each class of this part of the Model layer, each utilized by services.

Also, What do I do when I need new queries? I have this set of queries, then someone comes along and wants some special query. I then copy and paste and change one thing or add an if then or a switch, this gets multiplied across the class in my model layer in multiple objects, giant switch statements, etc. That does not seem to be what Model layers should be composed of, giant Models with code everywhere, violating DRY.

I have a service in one object in Students, findAllClassesByStudent($student_id) I call in my View perhaps, and another in Classes, findAllStudentsInAParticularClass($class_id). Those are pretty close.

I realize I can abstract yet more layers and isolate logic in some other layer, but I can't abstract forever, sooner or later I have what appears as some violation of OOP's encapsulation. ViewModel View, Abstracted Data Access Layer, the services, etc. and so on. If I do too little, then my objects are anemic.

I've been reading a lot and while there is a lot of interpretations, I'm assuming I'm leaving all the business logic in the Model layer, not on the controller or the View.


Pseudo examples:

class Student extends Model {

private $student

public function __construct() {

}

public function getStudentByID($student_id){
...SQL
}

public function getStudentClasses($student_id){  //first time

}



}

class Classes extends Model {

public function __construct(){

}

public function findAllStudentsInAParticularClass($class_id){  //feels like bloat
...SQL
}

}

This seems like duplication to me. Perhaps I haven't thought it through enough. I just know from experience there's always someone that wants slightly different queries, and I end up copying and pasting.

2
  • Some concrete examples of "special queries" might help here - the best approach will likely depend very much on the nature of the queries, how you're accessing the data and what you actually plan to do with it.
    – Ant P
    Sep 1, 2014 at 14:46
  • My answer on SO may help you - it's C# but I've described how I'd approach structure data access in an MVC appliation stackoverflow.com/questions/25563276/…
    – Liath
    Sep 1, 2014 at 15:00

1 Answer 1

2

Your question is a bit confusing, but from what I understood, you need to refactor the code and find time to pay off the technical debt.

someone wants student information, and I query Students and Classes in my Student class, if I put it in Classes, I cross over to the Students, [edit] violating single responsibility.

Writing code is just easier part of software development. You have to think of who's responsibility is it (of Classes or Student), and implement it. Again, if you break something (like this OO principle), then try to find time and fix it.

I then copy and paste and change one thing or add an if then or a switch, this gets multiplied across the class in my model layer in multiple objects, giant switch statements, etc. That does not seem to be what Model layers should be composed of, giant Models with code everywhere, violating DRY.

Very bad approach. The solution is again refactoring. Do not let the copied code stay there. Do go back, and implement it properly.

How do I keep my classes in MVC from becoming bloated?

Again, refactoring. And proper design. But changing requirements do not affect only code. Refactoring also means fixing design and architecture problems.

If a class is getting bloated, that usually means that the design was either bad, or it wasn't adapted to the change(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.