Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Recursive relationships in data objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lithoman@gmail.com - 17 Nov 2006 17:14 GMT
I have been working with objects long enough to have the basics down,
but some things give me fits, and this is one of them.

Brief description:
I have an airplane object, and each airplane has an owner.
Likewise I have a customer object and each customer has a list of
airplanes.

The basic structure of my objects is: (ignoring public/private for
clarity)

public class Aircraft
{
   int ID;
   string Series;
   List<Aircraft_Options> OptionList;
   Customer Owner;

   /* Assorted Methods */...
}

public class Customer
{
   int ID;
   string Name;
   List<Aircraft> AircraftList;

   /* Assorted Methods */...
}

I want Aircraft to have the Customer object for reference when I grab
the Aircraft List, and obviously when I grab the Customer I want access
to all the associated aircraft. It seems to me I'll run into problems
with recursion. I considered a limited Customer class for Aircraft to
have, but that doesn't strike me as efficient.

How should I best deal with this situation?
Marc Gravell - 17 Nov 2006 17:25 GMT
Since they are classes, recursion isn't really a problem - each can see the
other, but there is still only a fixed number of acutal objects in the
system. It does become an issue e.g. with the XmlSerializer (which can only
serialize object trees, not graphs) - but you can handle that by marking one
direction with [XmlIgnore]. You also now have the issue of which is the
master... but this can also be solved (i.e. changing the customer on the
Aircraft causes it to also go to the Customer and remove itself from the
aircrafts collection, etc).

Memory islands (a common leak in non-GC systems) is not an issue; if the
graph is not visible it gets garbage collected - so you don't have any
exceptional reference-counting to consider.

Was there a specific recursive issue you were thinking of?

Marc
Marc Gravell - 17 Nov 2006 17:26 GMT
As a direction comparison: compare to winforms; a Control can see it's
parent and it's children; it is actually the exact same scenario, just with
1 base class instead of 2.

Marc
lithoman@gmail.com - 17 Nov 2006 18:03 GMT
So it's essentially not a problem as long as I am aware of its
structure and be careful with routines that recursively try to list
them.

Awesome! Thanks!
Kevin Spencer - 17 Nov 2006 18:45 GMT
First, you need to be clear whether your Aircraft class represents an
instance of a single Aircraft, or a type of Aircraft, such as a Cessna 195.
If it represents a single Aircraft, then I would assume that there is a
single Owner. If it represents a type of Aircraft, then it would have no
Owner. So, if it is a single Aircraft, you're talking about a one-to-many
relationship of Owners to Aircraft instances. If so, there would be one
Owner per Aircraft, and many Aircraft (o or more) to each Owner. If so,
there is no recursion involved. To find the Owner of a single Aircraft
instance, you look at the Owner property. To find all Aircraft for an Owner,
you look at the AircraftList property of the Owner. Similarly, to find all
Aircraft owned by the Owner of an Aircraft, you look at the AircraftList
property of the Owner property of the Aircraft.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Ministry of Software Development
http://unclechutney.blogspot.com

Any experience you can walk away from
is a good one.

>I have been working with objects long enough to have the basics down,
> but some things give me fits, and this is one of them.
[quoted text clipped - 33 lines]
>
> How should I best deal with this situation?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.