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 a data model.

That said, object relationships tend to be model through composition. So let's say, instead of "aaaaa" and "bbbbb" you have "students" and "courses" in 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
    ... An object model is not a data model, and a data model is not an object 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)
  • Re: Doubling the order
    ... 1.PRODUCTID) - primary Key ... > data model or any data integrity. ... the gap in the sequence is not filled in and the sequence ... > Since a query result is a table, and a table is a set which has no ...
    (microsoft.public.sqlserver.programming)
  • Re: general question about how best to cache expensive query results
    ... The question is that when I have a query like: ... entries to those that are newer than a certain date, ... failing to meet what is the obvious business requirement. ... You may need to rethink your data model, ...
    (comp.databases)
  • Re: Use of the term "hierarchy"
    ... A simple example is where one column stores sales for one month. ... If the sales were represented in records we could easily apply our powerful query language. ... But if we make several assumption about our hypothetical data model then might draw a conclusion that data modeling *can* be carried out by means of tables and by means of dimensions. ... And then we might want to introduce something that would allow us to query those elements of the model more productively then it is now in SQL or in existing data models. ...
    (comp.databases.theory)
  • Re: Constructing a form for tracking student attendance
    ... Data model is a concept rather than an object. ... identify the student. ... attendance table - contains information about attendance with references ... >>> grid cells contain combo boxes to assign the status for the student. ...
    (microsoft.public.access.forms)