One to many relationship in OOP

N

Nemisis

hi everyone,

hope u r all ok, and looking forward to xmas, well the xmas holidays!!
lol

In my backend database i have a table that has a foreign key column in
it.

Is it best practice that when i load my object from the table, to also
load the foreign key object as part of the parent object??

Table1 (Parent)
Id
Name
Table2 foreign key

Table2 (Child, but also a parent in other areas of the system)
Id
Name

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

Can anyone give me there opinion? I really need it, its driving me
crasy! lol
 
G

Guest

I generally tend to have an object property (strongly typed) which I load
(and cache) the first time the get is called. This can result in a large
number of queuries on the database but with connection pooling it isnt really
that bad. I have written very high performance apps which work this way in
the majority but also have some retreive methods will load all in one go if
it is always going to happen and speed is needed.

Ciaran O'Donnell
 
C

Chris Dunaway

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.
}
}
 
N

Nemisis

Chris said:
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.
}
}

I like the idea, i am using the csla framework and it is a bit new to
me, but i will see about defining it as you said above. it was a bit
confusing, cos sometimes, we need to child objects when dealing with
the parents, and sometimes we dont.

Thanks for the info, from you both
 

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

Top