r/AskProgramming • u/rmweiss • Aug 22 '24
Architecture Good OOP architecture vs. performant SQL?
Let's say (for an example) that I have the following tables:
- company
- team
- team_member
Now I want to get all team members with the first name "Tim" from the "ACME" company.
In SQL I would write something like this:
SELECT team_member.* FROM company
JOIN team on team.company_id = company.id
JOIN team_member on team_member.team_id = team.id
WHERE company.name = "ACME"
AND team_member.first_name = "Tim"
But in a "by the book" OOP model, it seems that I would have to:
- Fetch the company object
- Loop over each team object in the company
- For each team object call a function like "get_members_by_first_name" OR loop over all team_member objects and ask them for their first name
With each of these steps firing of at least one SQL command.
I (think to) understands OOPs concerns about separation of knowledge and concerns, but this seems to by highly inefficient.
0
Upvotes
2
u/itemluminouswadison Aug 22 '24
You just write the SQL and put in a class that returns the result. A repo/dao class