 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Shawn Oster Guest
|
Posted: Sun Nov 23, 2003 1:26 am Post subject: Restoring object references after loading from stream? |
|
|
I have a few objects that are basically data-aware. Each object has a
TDataReference that contains a reference to my custom TDataProvider and a
TDataField. This is roughly the same kind of mechanism Delphi uses with
it's data-aware controls, except instead of backing into a database I back
into a BO.
I stream my objects to disk, using an XML-based format and when I restore
them I'm faced with the challenge of reconnecting each object's
TDataReference to the TDataProvider. When I save out an object I save the
name of the TDataProvider (it can be one of a fixed number) and TDataField
as a string. Currently I do something like this:
procedure TModel.LoadFromXml(xml: string);
begin
// Creates objects, from xml
FStructure := LoadStructure(xml);
// Creates DataProvider objects, from xml
FDataProvider := LoadDataProvider(xml);
// Uses the string name of the provider and field to reconnect object
references
FStructure.ResolveDataProvider(FDataProvider);
end;
The part that is bugging me is that now I have this method,
ResolveDataProvider() that my TStructure class and any contained classes
must implement. I don't mind adding the method, or descending from a base
class, but now I have this public method that is used in an "internal" way,
only called from the LoadFromXml call. I have a few contained classes as
well:
TStructure
contains TElementList
contains TElement
contains TElementPropertyList
contains TElementProperty
Any thoughts? Perhaps this isn't the best way to achieve roll-your-own
data-aware objects? Pattern gurus have some pearls of wisdom?
Thanks,
Shawn
|
|
| Back to top |
|
 |
Constantine Yannakopoulos Guest
|
Posted: Mon Nov 24, 2003 8:47 am Post subject: Re: Restoring object references after loading from stream? |
|
|
Author := "Shawn Oster";
| Quote: |
I have a few objects that are basically data-aware. Each object has a
TDataReference that contains a reference to my custom TDataProvider
and a TDataField. This is roughly the same kind of mechanism Delphi
uses with it's data-aware controls, except instead of backing into a
database I back into a BO.
|
Take a look at how the VCL handles component references. The idea is
this: At save time you store the referenced object's (unique) name
inside your namespace (MS would use a moniker) in the storage (in your
case the XML document). At load time you do two steps. The first step
is to create the objects stored in your XML and assign any "normal"
properties. At that time when processing stored reference properties
you don't do anything else with them except adding some information in
a list (teh object, the property name and the name of the referenced
object will do). When you are done with processing all the objects in
your namespace you iterate the list of pending reference properites and
for each of then you resolve the name to the object (which will have
been loaded for sure) and set the property.
--
Constantine
|
|
| Back to top |
|
 |
Shawn Oster Guest
|
Posted: Mon Nov 24, 2003 10:42 pm Post subject: Re: Restoring object references after loading from stream? |
|
|
"Constantine Yannakopoulos" <kyan (AT) deltasingular (DOT) remove.this.gr> wrote in
message news:3fc1d3ad (AT) newsgroups (DOT) borland.com...
| Quote: | Author := "Shawn Oster";
|
| I have a few objects that are basically data-aware. Each object has a
| TDataReference that contains a reference to my custom TDataProvider
| and a TDataField. This is roughly the same kind of mechanism Delphi
| uses with it's data-aware controls, except instead of backing into a
| database I back into a BO.
Take a look at how the VCL handles component references. The idea is
this: At save time you store the referenced object's (unique) name
inside your namespace (MS would use a moniker) in the storage (in your
case the XML document).
|
Which I'm currently doing.
| Quote: | object will do). When you are done with processing all the objects in
your namespace you iterate the list of pending reference properites and
for each of then you resolve the name to the object (which will have
been loaded for sure) and set the property.
|
Just to be redundant... whenever you come across a property that you know is
an object reference you add it to an internal "to-do" list of objects that
need to be resolved. Think I got it. Now for the details, such as who owns
that list and how it gets accessed (passed in during streaming or a global).
Any examples of where to look in the VCL for examples?
Thanks for your reply,
Shawn
|
|
| Back to top |
|
 |
Constantine Yannakopoulos Guest
|
Posted: Wed Nov 26, 2003 7:41 am Post subject: Re: Restoring object references after loading from stream? |
|
|
Author := "Shawn Oster";
| Quote: | whenever you come across a property that you
know is an object reference you add it to an internal "to-do" list of
objects that need to be resolved.
|
Exactly.
| Quote: | Now for the
details, such as who owns that list and how it gets accessed (passed
in during streaming or a global).
|
Typically it is global (be careful to protect it for multithreaded
access) or the streamer object owns it.
| Quote: | Any examples of where to look in the VCL for examples?
|
Take a look at TReader.FixUpReferences in Classes.pas. It uses two
fixup lists, a local owned by the TReader object and a global one
protected by the GlobalNamcespace locking object.
--
Constantine
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|