开发者

Design question - loading info from DB

开发者 https://www.devze.com 2022-12-23 21:03 出处:网络
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 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 开发者_如何学GoDB, 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:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消