简体   繁体   English

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

[英]How to use Data Access Objects for serialized & relational database data access

I am developing a C++ domain model class library which should provide some facilities or a framework (ie interface classes etc) for writing/reading class instance data to/from both a binary file and a RDBMS. 我正在开发一个C ++域模型类库,该类库应提供一些工具或框架(即接口类等),用于向/从二进制文件和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. 该库的基础是使用RDBMS的应用程序,并且有几种方法可以通过执行一系列数据库检索和更新调用以获取成员数据的集合来实例化类。 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. 序列化的数据访问有一种不同的方式来组织其数据,因此我希望域模型完全不了解主键/外键,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? 域对象的每个Foo实例是否都有其自己的FooDAO实例,或者是否为类Foo的所有实例都具有一个FooDAO实例?
  2. 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? 是为每个Foo实例创建一次FooDAO,还是仅在需要访问数据时才创建FooDAO实例,然后立即销毁?
  3. The J2EE page on DAO introduces a DTO in addition to the DAO. DAO上的J2EE页面除了DAO之外还引入了DTO。 Why can't the DAO transfer the data? DAO为什么不能传输数据?
  4. 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. 对于具有其他Bar类实例的复杂域类Foo,似乎不可避免的是FooDAO类使用BarDAO类来检索数据。 This would lead to parallel hierarchies/dependencies in the domain class structure and the DAO class structure. 这将导致域类结构和DAO类结构中的并行层次结构/依赖性。 How can this be managed best? 如何最好地管理它?

Thanks for your help! 谢谢你的帮助!

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. 对于数据库中的每个实例,都有一个单独的DAO对象实例。 If you have a shared instance, thread synchronization may be a problem, and you'll be forced into doing a lot of copies. 如果您有一个共享实例,则线程同步可能是个问题,并且您将被迫进行大量复制。
  • My library DAO classes use types closely associated with the RDBMS types, for a couple of reasons. 我的库DAO类使用与RDBMS类型紧密相关的类型,这有两个原因。 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). 其次,它使数据转换变得更加容易和可优化(例如,您可以使用本机接口直接进行ODBC / OLEDB数据副本)。 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对象中不能有“不错”的类类型(例如:比实际字符串缓冲区具有更多数据的字符串抽象)。
  • 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. 我试图使DAO类保持简单,具有最少的访问器功能,并“接近”底层数据结构。 That means no inheritance from other DAO classes, instances have key variable members, etc. 这意味着没有从其他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. 在DAO类的顶部,我构建了更多可访问的类,这些类代表我的应用程序中的数据,并且可能(也可能不)将1-1映射到DAO类。 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类/从DAO类复制数据的方法。

Hope that helps. 希望能有所帮助。

I don't know the best implementation, but here's what I've seen done: 我不知道最好的实现,但是这是我已经看到的完成的事情:

  1. Separate for each instance. 每个实例都分开。
  2. Created right before it is needed and destroyed right after. 在需要之前立即创建,并在之后立即销毁。
  3. Don't know. 不知道
  4. Combine the data outside of the DAO instances, thereby avoiding the coupling. 在DAO实例之外合并数据,从而避免耦合。

Disclaimer: This is just what I've seen done. 免责声明:这就是我所看到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM