Re: Class Structure Question



dweesie wrote:
This is an opinion question, with background first.

Let's say I have a table
<u>aaaa</u>
a_id (pk)
a_name
a_descrip

and another table
<u>bbbb</u>
b_id (pk)
b_name
b_descrip

and another table to create a many to many
<u>aabb</u>
a_id (fk)
b_id (fk)

If I create a class for aaaa, obviously I would include fields for id,
name, and description. My data access object would have sql to
populate those fields for certain. Now here's my opinion question.
Does your aaaa class also include a list of foreign keys to bbbb? Do
you create an aabb class? Should you include any data from bbbb in
the aaaa class (this one seems an obvious no)? When is the
appropriate time to get related data? I don't have a particular
scenario. I'm just looking for opinions.


Here's what I do, FWIW. I claim no perfection for the idiom.

Object models should be made based on the object model, not the data model. In other words, design the objects to represent the class domain. Design the database as a normalized representation of the data in the objects. An object model is not a data model, and a data model is not an object model.

That said, object relationships tend to be modeled through composition. So let's say instead of "aaaaa" and "bbbbb" you have "students" and "courses" in a school, a standard pedagogical example. A Student would take Courses, and a Course would have Students that take it:

class Student
{
private String name;
private String info;
private Set<Course> courseLoad;
// can't take the same course twice at once
...
}

class Course
{
private String name;
private String info;
private Set<Student> roster;
// can't have the same student twice at once
...
}

When retrieving a list (or any collection) of Student or Course for, say, a dropdown I will usually not fill in the details such as those Sets, because that information isn't usually needed yet. (Usually.) In most cases such a query will only fill the 'name' field of the backing objects.

When the user drills down into a particular item from the list I fill in the details with a separate query.

So I fill the parts of such objects at need, lazily. (In certain circumstances, like with JSF backing beans, I find a query repeated multiple times to handle the same request. In such cases I'll cache the first result and re-use it until the request is handled.)

--
Lew
.



Relevant Pages

  • Re: Class Structure Question
    ... Object models should be made based on the object model, not the data model. ... A Student would take Courses, and a Course would have Students that take it: ... private String name; ... When the user drills down into a particular item from the list I fill in the details with a separate query. ...
    (comp.lang.java.databases)
  • Generic document manipulation routine
    ... Due to the printing demand, the printing PC has installed several ... to print any posible file that a student wanted to print. ... Word object model, and so on with other apps. ... application specific references or object models. ...
    (microsoft.public.dotnet.general)