C ++ class design from database schema

I am writing a perl script to parse the mysql database schema and if necessary create C ++ classes. My question is quite simple, but we are something that I did not do before, and I do not know the general practice. Any object of any of the created classes will have to “receive” methods for filling this information. Therefore, my questions are twofold:

  • Does it make sense to call all get methods in the constructor so that the object immediately has data? Some classes have a lot of them, so maybe that also makes sense. Now I have two constructors. One that fills the data, and one that does not.
  • Should I have another get method that fetches an object copy of the data, not a copy of db.

I could go both ways at # 1, and I tend to yes at # 2. Any advice, pointers would be much appreciated.

+3
source share
5 answers

Usually, the most expensive part of the application is round trips to the database, so it’s much more efficient for me to fill out all your data from one request than to do it one at a time, either as needed or from your constructor. Once you pay for the round trip, you can also get your money.

In addition, in general, your get * methods should be declared as const, which means that they do not modify the underlying object, so if they go to the database to populate the object, it will violate (what you could allow, variables- the members are changing, but that basically defeats the goal of const).

, :

  • init(), .
  • get * const .
+2

, . - . C/++ :

http://trac.butterfat.net/public/StactiveRecord
http://debea.net/trac

, , , find search, factory :

Artist MJ = Artist::Find("Michael Jackson");
MJ->set("relevant", "no");
MJ->save();

, . , new, :

Artist StackOverflow = Artist->new();
StackOverflow->set("relevant", "yes");
StackOverflow->save();

, get , . , Find save.

+1

, db java (, , ). ++.

0

, get , . , - , , , , db, . , .

edit - # 2 (). # 1 , .

0

, , , , .

I don’t know how many tables we are talking about, so this can explode the volume of your project.

0
source

Source: https://habr.com/ru/post/1697054/


All Articles