Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Define Product Class

Status
Not open for further replies.

nicole08

Programmer
Joined
Apr 29, 2002
Messages
2
Location
CA
Hi,

I have this Product class which is related to several other classes, e.g. Category, Designer, etc. In database, I have column CategoryID, DesignerID. When defining a Product class, shall I have Integer property for CategoryID, DesignerID? Or, shall Ihave String property for CategoryName, and DesignerName?

Solution 1: Integer Property
When I display a product, I'll initialize a Product class, get the CategoryID and DesignerID, and then initialize Category class and Designer class, to get the Category name and Designer name to display. That means I will access database 3 times.

Solution 2: String Property
When I display a product , I'll initialize a Product class, use a JOIN SQL statement, and set Category name and Designer name directly.

So, which is the proper solution? I think Solution 1 is the correct one. But what can I do to eliminate 3-time database accessing?

Thanks,
Nicole
 
What I do in such a case is to provide the IDs to the object, even if only internally. This allows me to do a lazy fetch of the related records. In my case, a Product instance would have both a CategoryID property and an (often lazy) getCategory method.

My code is usually structured as follows: I have a "backend" object that has persistent collections ("table wrappers"). These collections provide the persistent objects ("record wrappers").
This can impose a problem: each Product instance has to know both the Categories collection and the Designers collection to make the lazy getters work. What I do in such a case is to pass the Backend object around, which provides all the collections the object needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top