Re: One to many relationship in OOP

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Nov 14, 5:34 am, "Nemisis" <darrens2...@xxxxxxxxxxx> wrote:

If i make an object, called Object1, i have a property called
Object1.ChildId, and another property called Object1.HasChild, which
checks the Id value and says whether it is linked or not.

Should i have a property called Object1.ChildObject, which would load
the data of the child object? I dont need to object all the time, and
thus i am wondering whether i should just load this child object into a
variable when i need it rather then everytime i load the object??

We generally design our objects like your second method above. We have
the "parent" object and as a property we have the child object. Then
when retrieving the data from the database, he have a boolean parameter
that indicates whether we should populate all child object or not.

For example, we might have a class called Parent which has a Child
object. Our data layer might have a method called GetParentObject and
we pass in a bool to indicate if we want the child object retrieved
from the db as well, depending on the circumstance. This code is
pseudo-code and can probably be refined, but it should illustrate the
point.

class Parent
{

public Parent()
{
//Create an empty object here
_child = New Child();
}

private Child _child;
public Child Child
{
get;
set;
}
}

public class DataLayer
{
//This method takes the parentid and a bool to indicate if we get
the child object.
public Parent GetParentObject(int parentid, bool deep)
{
//Code to get parent object here
_child.Id = ....; //assign child id here.
if (deep)
FillChildObject();
}

public void FillChildObject()
{
//get child data here using child id in the _child instance.
}
}

.



Relevant Pages

  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)