473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Parent and child relationships in objects

Hi everyone,

If i have 3 objects as below.

objectA
A_id
A_Name
B_id

objectB
B_id
B_Name

objectC
C_id
C_Name

If object a is linked to object B via an id, is it a good idea to write
class A as follows??

objectA
A_id
A_Name
childB as objectB

Then i can set the ID of objectB, within object A as follows

objectA.childB.B_id = 9

What happens when i try to call a save method and the id hasnt been
set??

Sep 26 '06 #1
1 1538
Hi,

It sounds to me like you're looking for a way to convert a logical data model into a physical model, in code. Most applications do
not benefit from an entirely one-to-one relationship between logical entities and physical entities. Instead, think about the
aggregation of multiple objects into a single object if it will always be used as such in code.

For instance, if you expect to use an instance of objectB every time you are using an instance of objectA then there may not be a
need for objectB (even if it exists in your logical data model). You can then combine the objects into one entity. The physical
model would appear as such in C# (fields should be properties, but I wanted to reduce the amount of code):

public class ObjectA
{
public int AID;
public string AName;
public int BID;
public string BName;
}

Your data access code must be aware of this aggregation and convert a single update into two separate updates: One to table objectA
and one to table objectB.

If objectB may be used separately from objectA then you could use two classes as in your second example:

public class ObjectA
{
public int ID;
public string Name;
public ObjectB B; // reference an instance of an object
}

public class ObjectB
{
public int ID;
public string Name;
}

If you don't want to allow null references in the B property of ObjectA then you can enforce this in a constructor:

public class ObjectA
{
...
public ObjectA(ObjectB b)
{
if (b == null)
throw new ArgumentNullException("b");

this.B = b;
}
}

Your data access code could then update table objectA and table objectB separately from each respective object, without having to
check for null ObjectB references.

The problem in your first example is that you aren't enforcing the dependency to an instance of ObjectB, except through the logical
model's B_id attribute. This is a problem in a physical model since there is no way of knowing where, and if at all, an instance of
ObjectB is located that has the referenced B_id value. In that case you'd have to have a globally-accessible (static) list of
ObjectB instances that you would maintain separately from your ObjectAs. It's just much easier, and makes more sense IMO, to
reference ObjectB directly, as in your second example.
What happens when i try to call a save method and the id hasnt been
set??
If you require ObjectA always to have a reference to an instance of an ObjectB, as in my third example, then your data access code
that creates and initializes the objects must meet that requirement. By using the constructor mechanism as I described you'll
enforce this constraint at runtime. If your data access code doesn't load the objects properly then your application will fail with
an ArgumentNullException.

The data access code that saves the objects' values to the database won't need to check for null ObjectB references since it has
been enforced that the ObjectA.B property will never contain a null reference.

If you require that ObjectA can have a null ObjectB reference then your data access code that saves the object's values must account
for this by checking if ObjectA.B is a null reference. In the case that ObjectA.B is null, your data access code should either
ignore the update to the objectB table, since there is nothing to update, throw an exception or notify the user through the UI that
they are missing data (the latter should probably be in combination with an exception so that the data access layer throws an
exception and somewhere back up the stack it is caught and handled, and the user is notified)

--
Dave Sexton

"Nemisis" <da*********@hotmail.comwrote in message news:11*********************@h48g2000cwc.googlegro ups.com...
Hi everyone,

If i have 3 objects as below.

objectA
A_id
A_Name
B_id

objectB
B_id
B_Name

objectC
C_id
C_Name

If object a is linked to object B via an id, is it a good idea to write
class A as follows??

objectA
A_id
A_Name
childB as objectB

Then i can set the ID of objectB, within object A as follows

objectA.childB.B_id = 9

What happens when i try to call a save method and the id hasnt been
set??

Sep 26 '06 #2

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

Similar topics

2
by: jerrygarciuh | last post by:
Hello, Is it possible to instantiate a child class within the constructor of its parent? eg Class DBI extends DB { function DBI() { // explicit parent constructor call
9
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
0
by: JJ | last post by:
Hi All, I am trying to Add nodes in code behind and am having problems with the parent child relationships. I get a Use of unassigned local variable error . I have the local variables defined...
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
1
by: Mr Newbie | last post by:
Sorry to bother you guys but I have another questions related to DataSets. I'm almost there now, so I dont expect to bug you much more ( Hopefully ). I have Master / Details tables with...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
3
by: reachsamdurai | last post by:
Is it possible to determine the list of child/parent tables for a particular table from any system catalog tables? Using the syscat.tables I'm able to retrieve the no of dependent parent/child...
4
by: mikemiller.innova | last post by:
In Visual Studio 2005 SP1, I added a DataSet item into my project. I added 3 tables from SQL that have data and relationships. I added 2 lookup columns (lkp) to the DllVersions table in the...
8
by: Rick | last post by:
VS 2005 I' m setting up a parent/child datagridviews in a form. I am doing a lot of this by hand coding in order to get the feel of things. I want a change in the parent table to trigger a...
2
by: ChaoticDALO | last post by:
Hello, I am wondering if this is possible. If I have a parent class and a child that inherits the parent, the child can access a parents member function right? Well let's say we are in the parent...
0
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.