Recursive relationships in data objects

  • Thread starter Thread starter lithoman
  • Start date Start date
L

lithoman

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?
 
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
 
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
 
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!
 
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.

--
HTH,

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top