简体   繁体   English

Delphi 对象持久化,最好的方法是什么

[英]Delphi object persistence, what is the best way

I have developed application for drawing some shapes (lines mostly) , now i need to be able to store sketch to a file, I know that delphi has build in routines for object persistence, but I have never used it.我开发了用于绘制一些形状(主要是线条)的应用程序,现在我需要能够将草图存储到文件中,我知道 delphi 已经内置了用于对象持久性的例程,但我从未使用过它。

Can someone tell me can object persistence be used if i have to persist object that have also references to other objects (that will be stored to), I have TLine object which can be connected to other TLine object etc.有人可以告诉我,如果我必须保留也引用其他对象(将被存储到)的对象,我可以使用对象持久性,我有可以连接到其他 TLine 对象的 TLine 对象等。

Is it better to use this feature or write custom procedure to store/read object to/from file.使用此功能或编写自定义过程来将对象存储到文件中/从文件中读取对象是否更好。

The built in object persistance is primarily designed for use in streaming components to a dfm, the work that you would need to do to persist your sketch would not benefit very much from that mechanism.内置对象持久性主要设计用于将组件流式传输到 dfm,你需要做的工作来持久化你的草图不会从该机制中受益很多。

I think that you would be better off coming up with a custom scheme.我认为你最好提出一个自定义方案。

One method that I have used in the past is to store my object properties in an XML file writing a custom "save" routine which adds to a passed IXMLNode, and a new constructor which reads from a passed IXMLNode.我过去使用的一种方法是将我的对象属性存储在一个 XML 文件中,编写一个自定义的“保存”例程,该例程添加到传递的 IXMLNode,以及一个从传递的 IXMLNode 读取的新构造函数。 I store component as a node, and the properties as attributes (unless the property is another object, then it would be a child node).我将组件存储为一个节点,将属性存储为属性(除非该属性是另一个对象,否则它将是一个子节点)。

I believe there are some routines in the Delphi Jedi project which will handle component streaming for you, but I have not used them directly as most of my XML streaming has been done by hand since my objects were extremely simple and could be rendered with only a handful of properties.我相信 Delphi Jedi 项目中有一些例程可以为你处理组件流,但我没有直接使用它们,因为我的大部分 XML 流都是手工完成的,因为我的对象非常简单,只能用一个少数属性。

I use the TI Object persistence framework (tiopf.com).我使用 TI 对象持久性框架 (tiopf.com)。 I wrote the overview at http://tiopf.sourceforge.net/Doc/overview/index.shtml .我在http://tiopf.sourceforge.net/Doc/overview/index.shtml写了概述。

It will let you save objects and lists to xml, csv, databases etc. It handles child objects automatically.它将让您将对象和列表保存到 xml、csv、数据库等。它会自动处理子对象。

If you are interested, use the svn version, not the sourceforge download as it has more features (inc partial D 2009 support).如果您有兴趣,请使用 svn 版本,而不是 sourceforge 下载,因为它具有更多功能(包括部分 D 2009 支持)。

You also can use the famous hibernate know from java.您也可以使用从 java 知道的著名的 hibernate。

the delphi port you can find here: dHibernate您可以在此处找到的 delphi 端口: dHibernate

JSON is a new and very compact way to store objects. JSON是一种新的、非常紧凑的对象存储方式。 Two libraries are available for Delphi: SuperObject and lkJSON . Delphi 有两个可用的库: SuperObjectlkJSON

With NativeXML from http://www.simdesign.nl/xml.html I accomplished to read/write delphi in-memory objects from/to XML code.使用来自http://www.simdesign.nl/xml.html 的NativeXML,我完成了从/到 XML 代码读取/写入 delphi 内存中的对象。 Very nice, very easy, with demo included here: http://www.simdesign.nl/forum/download/file.php?id=236非常好,非常简单,这里包含演示: http : //www.simdesign.nl/forum/download/file.php?id=236

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

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