c++сlass设计数据库模式

时间:2022-08-25 12:56:42

I am writing a perl script to parse a mysql database schema and create C++ classes when necessary. My question is a pretty easy one, but us something I haven't really done before and don't know common practice. Any object of any of classes created will need to have "get" methods to populate this information. So my questions are twofold:

我正在编写一个perl脚本来解析mysql数据库模式,并在必要时创建c++类。我的问题很简单,但我们是我以前没有做过的,也不知道常见的做法。创建的任何类的任何对象都需要有“get”方法来填充此信息。我的问题有两个:

  1. Does it make sense to call all of the get methods in the constructor so that the object has data right away? Some classes will have a lot of them, so as needed might make sense too. I have two constrcutors now. One that populates the data and one that does not.
  2. 调用构造函数中的所有get方法,使对象立即拥有数据,这样做有意义吗?有些类会有很多这样的类,所以根据需要可能也有意义。现在我有两个约束条件。一个填充数据,另一个不填充数据。
  3. Should I also have a another "get" method that retrieves the object's copy of the data rather that the db copy.
  4. 我还应该有另一个“get”方法来检索该对象的数据副本,而不是检索db副本。

I could go both ways on #1 and am leaning towards yes on #2. Any advice, pointers would be much appreciated.

我可以两种方法都在#1上,并且倾向于#2。如果有任何建议,我将非常感激。

5 个解决方案

#1


2  

Ususally, the most costly part of an application is round trips to the database, so it would me much more efficient to populate all your data members from a single query than to do them one at a time, either on an as needed basis or from your constructor. Once you've paid for the round trip, you may as well get your money's worth.

通常,应用程序中最昂贵的部分是对数据库的往返,因此从一个查询填充所有数据成员要比一次只做一个查询要高效得多,无论在需要的基础上还是在构造函数中。一旦你付了往返旅费,你的钱还是物有所值的。

Also, in general, your get* methods should be declared as const, meaning they don't change the underlying object, so having them go out to the database to populate the object would break that (which you could allow by making the member variables mutable, but that would basically defeat the purpose of const).

同时,在一般情况下,你得到*方法应该声明为常量,这意味着他们不改变底层对象,所以让他们去数据库填充对象将打破(你可以允许通过成员变量的可变的,但这将基本上失败const的目的)。

To break things down into concrete steps, I would recommend:

要把事情分解成具体的步骤,我建议:

  • Have your constructor call a separate init() method that queries the database and populates your object's data members.
  • 让构造函数调用一个独立的init()方法,该方法查询数据库并填充对象的数据成员。
  • Declare your get* methods as const, and just have them return the data members.
  • 将get*方法声明为const,并让它们返回数据成员。

#2


1  

First realize that you're re-inventing the wheel here. There are a number of decent object-relational mapping libraries for database access in just about every language. For C/C++ you might look at:

首先,你要意识到你正在重新发明*。在几乎每种语言中,都有许多像样的对象关系映射库用于数据库访问。对于C/ c++,你可以看看:

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

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

Ok, with that out of the way, you probably want to create a static method in your class called find or search which is a factory for constructing objects and selecting them from the database:

好的,顺便说一下,你可能想在你的类中创建一个静态方法,叫做查找或搜索,它是一个构建对象并从数据库中选择它们的工厂:

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

Note the save method which then takes the modified object and stores it back into the database. If you actually want to create a new record, then you'd use the new method which would instantiate an empty object:

注意保存方法需要修改的对象,并将其存储回数据库。如果您实际想要创建一个新记录,那么您将使用一个新方法来实例化一个空对象:

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

Note the set and get methods here just set and get the values from the object, not the database. To actually store elements in the database you'd need to use the static Find method or the object's save method.

注意这里的set和get方法只是设置并从对象(而不是数据库)获取值。要在数据库中实际存储元素,您需要使用静态查找方法或对象的保存方法。

#3


0  

there are existing tools that reverse db's into java (and probably other languages). consider using one of them and converting that to c++.

有一些工具可以将db反向转换成java(可能还有其他语言)。考虑使用其中之一,并将其转换为c++。

#4


0  

I would not recommend having your get methods go to the database at all, unless absolutely necessary for your particular problem. It makes for a lot more places something could go wrong, and probably a lot of unnecessary reads on your DB, and could inadvertently tie your objects to db-specific features, losing a lot of the benefits of a tiered architecture. As far as your domain model is concerned, the database does not exist.

我不建议您的get方法进入数据库,除非对于您的特定问题是绝对必要的。它会使很多地方出现问题,可能会在DB上产生大量不必要的读取,并可能不经意地将对象绑定到特定于DB的特性上,从而失去分层架构的许多好处。就域模型而言,数据库不存在。

edit - this is for #2 (obviously). For #1 I would say no, for many of the same reasons.

编辑-这是#2(显然)。对于第一条,我会说不,因为很多相同的原因。

#5


0  

Another alternative would be to not automate creating the classes, and instead create separate classes that only contain the data members that individual executables are interested in, so that those classes only pull the necessary data.

另一种选择是不自动创建类,而是创建单独的类,这些类只包含单个可执行程序感兴趣的数据成员,以便这些类只提取必要的数据。

Don't know how many tables we're talking about, though, so that may explode the scope of your project.

但是,不要知道我们正在讨论多少个表,这样可能会破坏项目的范围。

#1


2  

Ususally, the most costly part of an application is round trips to the database, so it would me much more efficient to populate all your data members from a single query than to do them one at a time, either on an as needed basis or from your constructor. Once you've paid for the round trip, you may as well get your money's worth.

通常,应用程序中最昂贵的部分是对数据库的往返,因此从一个查询填充所有数据成员要比一次只做一个查询要高效得多,无论在需要的基础上还是在构造函数中。一旦你付了往返旅费,你的钱还是物有所值的。

Also, in general, your get* methods should be declared as const, meaning they don't change the underlying object, so having them go out to the database to populate the object would break that (which you could allow by making the member variables mutable, but that would basically defeat the purpose of const).

同时,在一般情况下,你得到*方法应该声明为常量,这意味着他们不改变底层对象,所以让他们去数据库填充对象将打破(你可以允许通过成员变量的可变的,但这将基本上失败const的目的)。

To break things down into concrete steps, I would recommend:

要把事情分解成具体的步骤,我建议:

  • Have your constructor call a separate init() method that queries the database and populates your object's data members.
  • 让构造函数调用一个独立的init()方法,该方法查询数据库并填充对象的数据成员。
  • Declare your get* methods as const, and just have them return the data members.
  • 将get*方法声明为const,并让它们返回数据成员。

#2


1  

First realize that you're re-inventing the wheel here. There are a number of decent object-relational mapping libraries for database access in just about every language. For C/C++ you might look at:

首先,你要意识到你正在重新发明*。在几乎每种语言中,都有许多像样的对象关系映射库用于数据库访问。对于C/ c++,你可以看看:

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

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

Ok, with that out of the way, you probably want to create a static method in your class called find or search which is a factory for constructing objects and selecting them from the database:

好的,顺便说一下,你可能想在你的类中创建一个静态方法,叫做查找或搜索,它是一个构建对象并从数据库中选择它们的工厂:

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

Note the save method which then takes the modified object and stores it back into the database. If you actually want to create a new record, then you'd use the new method which would instantiate an empty object:

注意保存方法需要修改的对象,并将其存储回数据库。如果您实际想要创建一个新记录,那么您将使用一个新方法来实例化一个空对象:

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

Note the set and get methods here just set and get the values from the object, not the database. To actually store elements in the database you'd need to use the static Find method or the object's save method.

注意这里的set和get方法只是设置并从对象(而不是数据库)获取值。要在数据库中实际存储元素,您需要使用静态查找方法或对象的保存方法。

#3


0  

there are existing tools that reverse db's into java (and probably other languages). consider using one of them and converting that to c++.

有一些工具可以将db反向转换成java(可能还有其他语言)。考虑使用其中之一,并将其转换为c++。

#4


0  

I would not recommend having your get methods go to the database at all, unless absolutely necessary for your particular problem. It makes for a lot more places something could go wrong, and probably a lot of unnecessary reads on your DB, and could inadvertently tie your objects to db-specific features, losing a lot of the benefits of a tiered architecture. As far as your domain model is concerned, the database does not exist.

我不建议您的get方法进入数据库,除非对于您的特定问题是绝对必要的。它会使很多地方出现问题,可能会在DB上产生大量不必要的读取,并可能不经意地将对象绑定到特定于DB的特性上,从而失去分层架构的许多好处。就域模型而言,数据库不存在。

edit - this is for #2 (obviously). For #1 I would say no, for many of the same reasons.

编辑-这是#2(显然)。对于第一条,我会说不,因为很多相同的原因。

#5


0  

Another alternative would be to not automate creating the classes, and instead create separate classes that only contain the data members that individual executables are interested in, so that those classes only pull the necessary data.

另一种选择是不自动创建类,而是创建单独的类,这些类只包含单个可执行程序感兴趣的数据成员,以便这些类只提取必要的数据。

Don't know how many tables we're talking about, though, so that may explode the scope of your project.

但是,不要知道我们正在讨论多少个表,这样可能会破坏项目的范围。