BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sharing objects across lists

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
Mark Reichert
Guest





PostPosted: Sat Mar 03, 2007 4:55 am    Post subject: Sharing objects across lists Reply with quote



I have a object that can be in two different lists at the same time, like an
Address object might be both in a Person item in a list of persons, and in a
list of addresses.

One could get that information twice from a database, once for each list,
and duplicate the information in memory, or is there a standard elegent way
of allowing the exact same object to reside in both lists.

Or is it a fool's errand if say there are addresses with no associated
persons?
Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Sat Mar 03, 2007 8:07 am    Post subject: Re: Sharing objects across lists Reply with quote



Mark Reichert wrote:
Quote:
I have a object that can be in two different lists at the same time,
like an Address object might be both in a Person item in a list of
persons, and in a list of addresses.

One could get that information twice from a database, once for each
list, and duplicate the information in memory, or is there a standard
elegent way of allowing the exact same object to reside in both lists.

If the multiple objects are all read-only then it doesn't matter outside the
inefficiency of duplicating the objects. If any of them could be updated
then that could be a problem since the duplicates will then be out of date.
If more than one could be updated then you have a mess. <g>

The standard solution for this is an object cache. The layer that transforms
data queried from the database into objects would first look in the cache to
see if the object already exists and, if so, includes that in the new list
rather than creating a new instance.

Quote:
Or is it a fool's errand if say there are addresses with no associated
persons?

There could be scenarios where dealing with address objects on their own
might be legitimate, but in most cases you would be querying addresses in
the context of something else, e.g. searching for customers with a
particular address attribute like city or street. However, for purposes of
an object cache, this doesn't matter, no matter how the address objects come
to be retrieved, they would go into that cache.

--
Wayne Niddery - Winwright, Inc (www.winwright.ca)
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson
Back to top
David Smith
Guest





PostPosted: Sat Mar 03, 2007 9:13 am    Post subject: Re: Sharing objects across lists Reply with quote



Use a Tobject list. It stores a pointer to the object in a list (It's just
like a stringlists Objects[] property without the strings). It manages its
own memory too. Create your objects along with the objectlist at startup
while reading from the database, then all non db components can refer to the
same objects in memory. That's how I handle all my lookup tables.

Dave


"Mark Reichert" <mark (AT) messagelink (DOT) com> wrote in message
news:45e8aa37$1 (AT) newsgroups (DOT) borland.com...
Quote:
I have a object that can be in two different lists at the same time, like
an Address object might be both in a Person item in a list of persons, and
in a list of addresses.

One could get that information twice from a database, once for each list,
and duplicate the information in memory, or is there a standard elegent
way of allowing the exact same object to reside in both lists.

Or is it a fool's errand if say there are addresses with no associated
persons?
Back to top
Peter Morris
Guest





PostPosted: Sat Mar 03, 2007 4:05 pm    Post subject: Re: Sharing objects across lists Reply with quote

If you have a TAddress class then a single instance of that object may
already exist in multiple lists, you just make sure it is a one-way
reference so that the TAddress class doesn't have properties such as
"Person", and "Building". It's only when you come to save it to disk that
you need to know which association it belongs.



--
Pete

Blessed are the geek, for they shall public class GeekEarth : Earth {}
====
Audio compression components, DIB graphics controls, ECO extensions,
FastStrings : http://www.droopyeyes.com
My blog : http://mrpmorris.blogspot.com
Back to top
Leonardo M. Ramé
Guest





PostPosted: Mon Mar 05, 2007 6:04 pm    Post subject: Re: Sharing objects across lists Reply with quote

You can map your Address and Person classes to a database and reference
each other by an AddressObj field in the Person class.

TAddress = class
private
FAddressId: Integer;
FNumber: Integer;
FCity: string;
..
public
property AddressId: Integer read FAddressId write FAddressId;
..
end;

TPerson = class
private
FPersonId: Integer;
FAddressObj: TAddress; // Pointer to an Address object.
FName: string;
..
public
property PersonId: Integer read FPersonId write FPersonId;
property AddressObj: TAddress read FAddress write FAddress;
..
end;

Then, make a list (TCollection or TObjectList) of addresses and link by
TPerson's AddressObj field.

To store the relation, just add an AddressId field in the Person table.

Leonardo M. Ramé
http://leonardorame.blogspot.com


Mark Reichert escribió:
Quote:
I have a object that can be in two different lists at the same time, like an
Address object might be both in a Person item in a list of persons, and in a
list of addresses.

One could get that information twice from a database, once for each list,
and duplicate the information in memory, or is there a standard elegent way
of allowing the exact same object to reside in both lists.

Or is it a fool's errand if say there are addresses with no associated
persons?

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.