How to store an object to disk in all its glory? My object is derived from TObjectList so it holds other objects.
Which is the fastest and easiest way? Which is the compatible way?
Serialization IS NOT a solution since I want to save also non-public properties and the list of objects it holds!
For the moment I trying to save every object independently as a binary file which then are packed together. This is a lengthy process but allows me to load old versions of the object with a newer version of the program (compatibility with previous saved projects). Anyway, the complexity started to grow and it is not looking good anymore.
To save an object to disk with Python, we can use the pickle module. We have the Company class that we instantiated and assigned to company . Then we define the save_object function that opens filename and call pickle. dump with obj , the outp file and pickle.
Yes, since objects are also considered as datatypes (reference) in Java, you can create an array of the type of a particular class and, populate it with instances of that class.
If you are using Delphi 2010, things will be much easier because of the new RTTI unit, Robert Love has written a nice unit to serialize the objects to XML called XMLSerial.
You can read about it at his blog : Xml Serialization - Basic Usage
I also mostly use handcrafted serialization for my own datastructures. The multiple version angle is one of the main reasons.
However in your case that is difficult, since not all your objects (tobjectlist) derive from an own hierarchy that contain virtual abstract methods to load/store.
D2010 serialization (which afaik allows nearly everything to RTTI) could be a solution, but probably requires a new delphi version, and worse, it spells the end to manually dealing with versioning. (e.g. copying values from old fields into new ones when the format changes)
If the manual streaming is getting out of hand, a different approach could be to have abstract definitions for the data part of your objects, and generate sourcecode (the field declarations and the streaming code) from these abstract definitions. The advantage is that you might be able to slip in some custom code here and there when you need, or patch your generator for versioning issues.
I did this once for a business object to SQL mapping with over 800 objects. Since it was the time before generics in Delphi, I generated a typesafe container type for each object too, as well as other helper and converter objects/routines.
It is a lot of work to setup though, and only is worth it if you have a project with really a lot of objects and fields (hundreds if not thousands) and are sure that you will need to maintain it with significant mutations for quite some time to come.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With