I have been trying to learn Object Oriented Design, but I find it very difficult to model objects that access a relational database and do JOIN
s.
For example, I am building a REST API, it has User
, Admin
and Event
entities.
Admins can issue Users registration for Events.
This is the database schema:
User:
id
name
Event:
id
name
Admin:
id
name
UserEventRegistration:
user_id
event_id
issued_admin_id
I have an endpoint /participant/registered_events
which gives info of all events registered by a user.
class EventInfo {
public int id;
public String name;
public int issuedAdminId;
public String issuedAdminName;
}
EventInfo[] getRegisteredEvents(userid, participantid) {
Result result = executeSql(
"SELECT Event.id, Event.name," +
" Admin.id as admin_id, Admin.name as admin_name" +
" FROM UserEventRegistration " +
" JOIN Event ON event_id = Event.id " +
" JOIN Admin ON admin_id = Admin.id"
);
// convert result into EventInfo objects
return eventsInfo;
}
The above code is not properly object oriented, but I don't know which class to place the code.
If I put it in class RegistrationManager
, now RegistrationManager
is strongly coupled to the structure of Admin
table and Event
table.
Where is the appropriate place to put this JOIN
query?
Edit (04 Oct 2019): Made the example a bit more clearer and complete. Incorporated suggestions from answers to narrow down the scope of the question.