473,738 Members | 8,397 Online
Bytes | Software Development & Data Engineering Community
+ 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 1556
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 ArgumentNullExc eption("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 ArgumentNullExc eption.

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*********@ho tmail.comwrote in message news:11******** *************@h 48g2000cwc.goog legroups.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
2481
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
2084
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
2739
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 and in my switch statement I build the parent Nodes and have children created off of them. What am I doing wrong? Here is the code:
4
4569
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 adding to the child collection, eg: parent.Children.Add(child); child.Parent = parent;
1
1463
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 cascading relationships between them. When I Update the master using the DataAdapter we get a new ID for the new master row and all the child row foreign keys are updated to the newly aquired ID.
10
4026
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
3
11373
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 tables but unable to determine the list of dependent table name(s) Example : For a simple department-employee relationship, the following query gives the count of parent/child tables but not the table names(s). db2 => select tabname,...
4
1887
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 dataset ProjectID
8
5976
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 change in the child. Where is the best place (if I cannot set it up to be automatic through relationships) to catch the change in the parent to refetch the child rows?
2
6426
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 member function that has just been accessed by a child. The program goes through some condition statements, comes true to a situation where it's true, and now needs to tell the child to do child member function x. Is that possible? class parent{ ...
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8210
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.