Recursive relationships in data objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lithoman@gmail.com

    Recursive relationships in data objects

    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_O ptionsOptionLis t;
    Customer Owner;

    /* Assorted Methods */...
    }

    public class Customer
    {
    int ID;
    string Name;
    List<AircraftAi rcraftList;

    /* 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

    #2
    Re: Recursive relationships in data objects

    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


    Comment

    • Marc Gravell

      #3
      Re: Recursive relationships in data objects

      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


      Comment

      • lithoman@gmail.com

        #4
        Re: Recursive relationships in data objects

        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!

        Comment

        • Kevin Spencer

          #5
          Re: Recursive relationships in data objects

          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
          Thoughts and Ideas about programming, philosophy, science, arts, life, God, and related subjects.


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



          <lithoman@gmail .comwrote in message
          news:1163783692 .730024.302240@ h48g2000cwc.goo glegroups.com.. .
          >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_O ptionsOptionLis t;
          Customer Owner;
          >
          /* Assorted Methods */...
          }
          >
          public class Customer
          {
          int ID;
          string Name;
          List<AircraftAi rcraftList;
          >
          /* 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?
          >

          Comment

          Working...