473,508 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

One to many relationship in OOP

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

Nov 14 '06 #1
4 1740
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

"Nemisis" wrote:
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

Nov 14 '06 #2
On Nov 14, 5:34 am, "Nemisis" <darrens2...@hotmail.comwrote:
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.
}
}

Nov 14 '06 #3

Chris Dunaway wrote:
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

Nov 14 '06 #4
PS

"Nemisis" <da*********@hotmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>
Chris Dunaway wrote:
>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,
This may help you. The CSLA framework supports lazy loading.

http://www.lhotka.net/Article.aspx?i...4-9476b8f77600

PS
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
Nov 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2672
by: Megan | last post by:
hello everybody, i know this is a very long post, but i wanted to provide as much detail as possible. quick overview- i want to create a couple of many to many relationships and am wondering...
2
375
by: Megan | last post by:
hello everybody, i know this is a very long post, but i wanted to provide as much detail as possible. quick overview- i want to create a couple of many to many relationships and am wondering...
13
1866
by: the other john | last post by:
The trouble currently with 3 tables. I'm excluding non-relevant fields... tbl_users PK_user_ID tbl_developers PK_developer_ID FK_developer_user_ID FK_developer_project_ID
2
2531
by: paulcjcross | last post by:
How to set up a many to many relationship. I know you need a third table to join the other two. I need one table (table1) with stock_numbers(unique), one table (table2) with job-numbers(unique) and...
6
4047
by: NicoleCartrette | last post by:
Going back to school is easier said than done.. This was posted to an older thread earlier but I don't think it got any attention. Your help is appreciated Professor requires we create a...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.