Avoid loading unnecessary data from db into objects (web pages)

Posted by GmGr on Stack Overflow See other posts from Stack Overflow or by GmGr
Published on 2010-03-31T20:08:02Z Indexed on 2010/03/31 20:23 UTC
Read the original article Hit count: 266

Really newbie question coming up. Is there a standard (or good) way to deal with not needing all of the information that a database table contains loaded into every associated object. I'm thinking in the context of web pages where you're only going to use the objects to build a single page rather than an application with longer lived objects.

For example, lets say you have an Article table containing id, title, author, date, summary and fullContents fields. You don't need the fullContents to be loaded into the associated objects if you're just showing a page containing a list of articles with their summaries. On the other hand if you're displaying a specific article you might want every field loaded for that one article and maybe just the titles for the other articles (e.g. for display in a recent articles sidebar).

Some techniques I can think of:

  1. Don't worry about it, just load everything from the database every time.
  2. Have several different, possibly inherited, classes for each table and create the appropriate one for the situation (e.g. SummaryArticle, FullArticle).
  3. Use one class but set unused properties to null at creation if that field is not needed and be careful.
  4. Give the objects access to the database so they can load some fields on demand.
  5. Something else?

All of the above seem to have fairly major disadvantages.

I'm fairly new to programming, very new to OOP and totally new to databases so I might be completely missing the obvious answer here. :)

© Stack Overflow or respective owner

Related posts about object-oriented-design

Related posts about databases