Would some guru please write a streaming Tpersistent class

Respektable
Respektable used Ask the Experts™
on
Hello Delphiles:

Listen... I have scoured the web for code that generically streams a TPersistent and its properties (and its children).  There are hundreds of requests for them out there.. but the only ones that exist are either no longer supported or available (eg. spiderclasses) or talked about as proprietary solutions by braggarts -- and not available. Delphi loves to say that a Tpersistent supplies all that is required for streaming.. but it's a lie. Only a TComponent can stream its children directly without an incredible amount of effort.

I have written a simple component "carrier" that will stream a Tcollection and its Tpersistent collection items... but failed miserably at creating a generic Tpersistent that streams its own properties (to a stream.. not a DFM file). I get all flumoxed at the define properties area and just have no more time to try. But I know this is relatively trivial exercise for you gurus out there.  So why not take a few hours and write one for the good of mankind--or at least Delphi users everywhere?  The karma will come back to you!


Regards!

I'm blowing 20 whole points to make this public plea. Don
t let them be spent in vain :)

PS
There is a pretty good starting point out there called "TOList" (Search web for it).  It will work for a list of Tpersists... and could probably be used as a model, but I failed (ran out of time) at adapting it to a generic class.  Yes, I  coud save properties of the Tpersistent.. but not sub object properties of it (such as stringlists, etc.).

INcidently.. I am a D5 user. IF D6/7 already does this, I will kill myself by swallowing a repeat statement consisting of nothing but...
 REPEAT
  x:= X + 1
 UNTIL InPriseGivesADamn;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

have you already seen the comment I post in your question at http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20404178.html? Maybe it will help you. If you want, put the code of your TCarrier class in this forum... it will be more easy to help you.

Regards

Commented:
Make sure the properties are published.

Commented:
Still fighting it are you?

It might be worthwhile for you to look at refactoring your problem.

Start from the business problem, and then work your way inwards to a solution.  95% of your existing code may be good, but it may need reorganization to actually solve the problems you appear to be having.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Commented:
Actually, your TCarrier solution is a pretty well established OO pattern (separating business logic from storage functions).  

If it works then there's no reason to change it - if it is well written then it may actually make your app easier to maintain than embedding the storage logic inside the business object.  For example, if you ever decide that you need to shift to a DBMS back end instead of a simple stream, you will just write another TCarrier descendent.  Since the logic is not intertwined, there are no side effects.

I actually call my root class that holds the same place a TPersistor, and I have a persistor for every combination of business class and storage medium that I intend to use.

For example, a TChicken contains all the business logic surrounding a Chicken.  A TChickenBDEPersistor knows how to store and retrieve TChickens to/from BDE storage.  A TChickenXMLPersistor does the same for XML, and stream persistence would be handled by a TChickenStreamPersistor.

This allows me a great amount of flexibility, including asynchronous data persistence for high performance, and easy redirectability for failover and horizontal scaling.

You have stumbled on a proven model that is fundamental to good enterprise scale OO design and development.  Stick with it unless it's really going to buy you something big to move away from it.

Commented:
If you use ObjectBinaryToText to analyze the streamed data you will see there is no type information for TPersistent derivates.

If you want to use the streaming facility of the VCL, you should design your TPersistent properties not to be NULL (instantiate the properties in the constructor and use Assign in the set accessor).
This is not very handy for trees or graphs so you will have to use meta data (and make sure it is extracted from the stream before the data it describes) in order to create the structure of instantiations before the stream functions fill in the properties.

i posted a streamable TPersistent (TSerializable) implementation to the borland newsgroup
news://newsgroups.borland.com/borland.public.cppbuilder.vcl.components.writing
Commented:
another remark:

class construction can be done using TMetaClass.

you can use GetClass("ClassName") if your class is registered, if it's not registered you can only use classid(Class) (note that Class must be the explicit name of the instance you want to create - not some ancestor) to get a pointer to a TMetaClass.
from this point the steps to an instance of this class are:

1. allocate memory for your object (GetInstanceSize method to determine the size of the object)
2. call the InitInstance method (with a pointer to the memory area as parameter)

(i am only using c++ builder so i'm not sure wether it's TClass or TMetaClass in delphi - TClass is defined as a pointer to a TMetaClass in the c++ header).

hope i could help you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial