tags:

views:

28

answers:

2

I need to build a class that will represent a row in some table in DB (lets say the table is 'Subscriber' and so is the class).

  1. I can have the class Subscriber which constructor receives the Objectkey of subscriber, retrieves info from DB and initializes its members.

  2. I add another class - SubscriberLoader which have a static method 'LoadSubscriber'. This method will receive the subscriber objectkey, retrieve info from DB, crate a Subscriber object and initialize its members. Subscriber constructor will be private and SubscirberLoader will be friend class of Subscriber - this way, client could build a Subscriber only using the loader.

which of the two is better? any other suggestions?

A: 

Hmm, Lazy loading(2nd) is much better, that way you can manage performance much wiser in the future.

MindFold
what do you mean by 'manage performance'? can you give an example?
amitlicht
Where does lazy loading enter the game?
Jens Schauder
simple, when you have another method for loading, and the loading is not done automaticly in the constructor, you can lazy load the info when needed. that way you can manage your performance in a smart manner.
MindFold
+2  A: 

I'd recommend the second approach, because it separates two concerns into two separate classes:

  • the concern of whatever a subscriber is supposed to do
  • the concern of extracting a subscriber from the database
Jens Schauder