Re: Class Structure Question
- From: Lew <lew@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 04 Jun 2007 10:51:25 -0400
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
.
- References:
- Class Structure Question
- From: dweesie
- Class Structure Question
- Prev by Date: Class Structure Question
- Next by Date: Re: Class Structure Question
- Previous by thread: Class Structure Question
- Next by thread: Re: Class Structure Question
- Index(es):
Relevant Pages
|
|