如何使用数据访问对象进行序列化和关系数据库数据访问

时间:2022-12-08 13:06:25

I am developing a C++ domain model class library which should provide some facilities or a framework (i.e. interface classes etc) for writing/reading class instance data to/from both a binary file and a RDBMS. The basis for this library is an application that uses a RDBMS, and there are several methods which instantiate a class by performing a sequence of database retrieve and update calls to fetch collections of member data. The serialized data access has a different way of organizing its data, so I want the domain model to be completely ignorant of primary/foreign keys, IDs etc.

我正在开发一个C ++域模型类库,它应该提供一些工具或框架(即接口类等),用于向/从二进制文件和RDBMS写入/读取类实例数据。此库的基础是使用RDBMS的应用程序,有几种方法通过执行一系列数据库检索和更新调用来获取成员数据集合来实例化类。序列化数据访问有一种不同的组织数据的方式,所以我希望域模型完全不知道主/外键,ID等。

To solve this problem, I consider using the Data Access Object (DAO) pattern, and would like to have some advice on the 'granularity', lifetime and use of DAO objects (in your replies, please note that I'll be using C++, not Java, and that the domain class cannot hold any ID/key info from the RDBMS or binary file store):

为了解决这个问题,我考虑使用数据访问对象(DAO)模式,并希望对“粒度”,生命周期和DAO对象的使用有一些建议(在你的回复中,请注意我将使用C ++ ,而不是Java,并且域类不能保存来自RDBMS或二进制文件存储的任何ID /密钥信息):

  1. Does each Foo instance of a domain object have its own FooDAO instance, or is there a single FooDAO instance for all instances of class Foo?
  2. 域对象的每个Foo实例是否都有自己的FooDAO实例,或者Foo类的所有实例都有一个FooDAO实例?

  3. Is the FooDAO created once for each Foo instance, or would the FooDAO instance be created only when access to data is needed, and destroyed immediately afterwards?
  4. FooDAO是为每个Foo实例创建一次,还是仅在需要访问数据时创建FooDAO实例,并在之后立即销毁?

  5. The J2EE page on DAO introduces a DTO in addition to the DAO. Why can't the DAO transfer the data?
  6. 除了DAO之外,DAO上的J2EE页面还引入了DTO。为什么DAO不能传输数据?

  7. For a complex domain class Foo that has instances of other domain classes Bar, it seems inevitable that the FooDAO class uses the BarDAO class to retrieve data. This would lead to parallel hierarchies/dependencies in the domain class structure and the DAO class structure. How can this be managed best?
  8. 对于具有其他域类Bar实例的复杂域类Foo,FooDAO类似乎不可避免地使用BarDAO类来检索数据。这将导致域类结构和DAO类结构中的并行层次结构/依赖关系。如何才能最好地管理?

Thanks for your help!

谢谢你的帮助!

2 个解决方案

#1


1  

I don't have a good solution for you, but I can tell you what I have, and some thoughts and experiences. I have built something very similar, based on a model I had seen used before, as a C++ library.

我没有一个很好的解决方案,但我可以告诉你我有什么,以及一些想法和经验。基于我之前看到过的模型,我已经构建了一些非常相似的东西作为C ++库。

Some thoughts, in no particular order:

一些想法,没有特别的顺序:

  • Have a separate instance of the DAO object for each instance in the DB. If you have a shared instance, thread synchronization may be a problem, and you'll be forced into doing a lot of copies.
  • 为DB中的每个实例分配一个单独的DAO对象实例。如果你有一个共享实例,线程同步可能是一个问题,你将*做很多副本。

  • My library DAO classes use types closely associated with the RDBMS types, for a couple of reasons. First, the library supports automatic creation and update of storage in the underlying data store, so the classes need to have enough information to create the tables. Second, it makes data transition much easier, and optimizable (you can do direct ODBC/OLEDB data copies using the native interfaces, for example). The downside is that you can't have "nice" class types in the DAO objects (eg: string abstractions with more data than the actual string buffer).
  • 我的库DAO类使用与RDBMS类型密切相关的类型,原因有两个。首先,库支持在底层数据存储中自动创建和更新存储,因此类需要有足够的信息来创建表。其次,它使数据转换更容易,并且可以优化(例如,您可以使用本机接口直接执行ODBC / OLEDB数据复制)。缺点是您不能在DAO对象中使用“nice”类类型(例如:字符串抽象,其数据多于实际的字符串缓冲区)。

  • I create on demand, certainly, because there's potentially much more data in the store than would be practical to put in memory.
  • 当然,我是按需创建的,因为商店中的数据可能比放入内存中的数据要多得多。

  • I try to keep the DAO classes simple, with minimal accessor functionality, and "close" to the underlying data structures. That means no inheritance from other DAO classes, instances have key variable members, etc.
  • 我尝试保持DAO类简单,具有最小的访问器功能,并“关闭”底层数据结构。这意味着没有来自其他DAO类的继承,实例具有关键变量成员等。

On top of the DAO classes I build more accessible classes which represent the data in my application, and may or may not map 1-1 to a DAO class. These are allowed to have any type of members and structure, are supposed to be what the app uses, and have methods to copy data to/from the DAO classes which underlie them.

在DAO类之上,我构建了更多可访问的类,这些类表示我的应用程序中的数据,并且可能会或可能不会将1-1映射到DAO类。允许它们具有任何类型的成员和结构,应该是应用程序使用的,并且具有将数据复制到DAO类中的方法,这些方法是构成它们的DAO类。

Hope that helps.

希望有所帮助。

#2


0  

I don't know the best implementation, but here's what I've seen done:

我不知道最好的实现,但这是我见过的:

  1. Separate for each instance.
  2. 每个实例分开。

  3. Created right before it is needed and destroyed right after.
  4. 在需要之前创建并在之后立即销毁。

  5. Don't know.
  6. Combine the data outside of the DAO instances, thereby avoiding the coupling.
  7. 将数据组合在DAO实例之外,从而避免耦合。

Disclaimer: This is just what I've seen done.

免责声明:这就是我所见过的。

#1


1  

I don't have a good solution for you, but I can tell you what I have, and some thoughts and experiences. I have built something very similar, based on a model I had seen used before, as a C++ library.

我没有一个很好的解决方案,但我可以告诉你我有什么,以及一些想法和经验。基于我之前看到过的模型,我已经构建了一些非常相似的东西作为C ++库。

Some thoughts, in no particular order:

一些想法,没有特别的顺序:

  • Have a separate instance of the DAO object for each instance in the DB. If you have a shared instance, thread synchronization may be a problem, and you'll be forced into doing a lot of copies.
  • 为DB中的每个实例分配一个单独的DAO对象实例。如果你有一个共享实例,线程同步可能是一个问题,你将*做很多副本。

  • My library DAO classes use types closely associated with the RDBMS types, for a couple of reasons. First, the library supports automatic creation and update of storage in the underlying data store, so the classes need to have enough information to create the tables. Second, it makes data transition much easier, and optimizable (you can do direct ODBC/OLEDB data copies using the native interfaces, for example). The downside is that you can't have "nice" class types in the DAO objects (eg: string abstractions with more data than the actual string buffer).
  • 我的库DAO类使用与RDBMS类型密切相关的类型,原因有两个。首先,库支持在底层数据存储中自动创建和更新存储,因此类需要有足够的信息来创建表。其次,它使数据转换更容易,并且可以优化(例如,您可以使用本机接口直接执行ODBC / OLEDB数据复制)。缺点是您不能在DAO对象中使用“nice”类类型(例如:字符串抽象,其数据多于实际的字符串缓冲区)。

  • I create on demand, certainly, because there's potentially much more data in the store than would be practical to put in memory.
  • 当然,我是按需创建的,因为商店中的数据可能比放入内存中的数据要多得多。

  • I try to keep the DAO classes simple, with minimal accessor functionality, and "close" to the underlying data structures. That means no inheritance from other DAO classes, instances have key variable members, etc.
  • 我尝试保持DAO类简单,具有最小的访问器功能,并“关闭”底层数据结构。这意味着没有来自其他DAO类的继承,实例具有关键变量成员等。

On top of the DAO classes I build more accessible classes which represent the data in my application, and may or may not map 1-1 to a DAO class. These are allowed to have any type of members and structure, are supposed to be what the app uses, and have methods to copy data to/from the DAO classes which underlie them.

在DAO类之上,我构建了更多可访问的类,这些类表示我的应用程序中的数据,并且可能会或可能不会将1-1映射到DAO类。允许它们具有任何类型的成员和结构,应该是应用程序使用的,并且具有将数据复制到DAO类中的方法,这些方法是构成它们的DAO类。

Hope that helps.

希望有所帮助。

#2


0  

I don't know the best implementation, but here's what I've seen done:

我不知道最好的实现,但这是我见过的:

  1. Separate for each instance.
  2. 每个实例分开。

  3. Created right before it is needed and destroyed right after.
  4. 在需要之前创建并在之后立即销毁。

  5. Don't know.
  6. Combine the data outside of the DAO instances, thereby avoiding the coupling.
  7. 将数据组合在DAO实例之外,从而避免耦合。

Disclaimer: This is just what I've seen done.

免责声明:这就是我所见过的。