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).
-
I can have the class Subscriber which constructor receives the Objectkey of subscriber, retrieves info from DB and initializes its members.
-
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?
I’d recommend the second approach, because it separates two concerns into two separate classes: