Accessing Parent object from Child

Accessing Parent object from Child

Old forum URL: forums.lhotka.net/forums/t/267.aspx


Pradeep posted on Sunday, June 04, 2006

Hi,
I have a Company Parent Object and this has a Contact List, which is a collection of Contact objects.
 
Company and Contact are root level objects (similar to project and resource in the book sample)  and Contact object has a company name property.  When I try to add a contact to a company, I want to have a validation rule, making sure that the contact's company name is same as the Company's name property.
 
To keep it short. I want to get the Parent of Contact object (ContactList) and then get to its parent (Company) and check for the Name property. But BusinessCollectionBase does not have a parent property.
 
Could any one tell me, how to get to the parent object (in this case, Company).
 
Thank You
 
 
 
 

xal replied on Sunday, June 04, 2006

Declare a parent field inside your collection like this:

<NonSerialized(), NotUndoable()> Private mParent as Company

Now, you can have a method like:
SetParent(parent as Company)
You should call that method when you make an instance of the collection and when your root object is desserialized.

You can expose mParent through a readonly property.

Andrés

xal replied on Sunday, June 04, 2006

You could also just have a string field (that would use the same technique for setting / reading) inside your collection that you can assign from the parent whenever the company name changes. This would of course be much simpler.


Andrés

Pradeep replied on Monday, June 05, 2006

Thanks for your suggestion. I tried the first one. But I didn't set the parent before adding the child to the collection. So it was throwing error. Now, I am just using one more property and comparing with it (as your second sugg.).
 
I posted this here, just to make sure that I didn't miss any obvious method and doing it in a round about way. Also in VB6.0 version of CSLA it had parent object.
 
Thanks again.
 
 
 

guyroch replied on Wednesday, June 07, 2006

Take a look at this thread in the old forum.

http://www.searchcsla.com/Details.aspx?msn_id=22214

Crestline replied on Tuesday, June 20, 2006

In our scenario we need access to the parent's ID so that when the child's data is saved out, one of the values needed to store in the Child's table is the related ID of the parent.

How should we go about getting the parent's ID?

Outline:
Categories   EditableRootList
Category     EditableRoot
SubCategories   EditableChildList
SubCategory    EditableChild

UI
We have a form containing 2 datagrids.  The Category grid and the related SubCategory grid.  Both Categories and SubCategories are editable on this form.  In the underlying data tables on the server, there is an CategoryID field in the SubCategory table so that we can see the given subcategories for any particular category.  This is pretty straight forward stuff, nothing out of the ordinary.

Question
When save is clicked on the form, the Categories.Save() method is called which of course saves both Categories and SubCategories.  How do we set the CategoryID value of the SubCategories if we can't get to the parent object to find it?

Maybe our model is wrong but we are definately looking for some suggestions.  Any thoughts?

Thanks,
BR

JoeFallon1 replied on Tuesday, June 20, 2006

In my collections I use:

Protected Friend Overridable Sub Update(ByVal tr As IDbTransaction, ByVal parent As MyParent)

....

     For Each child In List
       child.Update(tr, parent)
     Next

  ....

 

Then in the child BO I use:

The only method that requires the Parent is the Insert.

Protected Friend Overridable Sub Update(ByVal tr As IDbTransaction, ByVal parent As MyParent)
...

  (I call a method like InsertData rather than write all the code in one large Update section.)

Protected Overridable Sub InsertData(ByVal tr As IDbTransaction, ByVal parent As MyParent)
      DAL.ExecuteNonQuery(tr, CommandType.Text, SQL.Insert(parent.key,... ))

I use Codesmith templates to generate all this for me.

HTH

Joe

 


   

 

 

 

 

 

 

 

Crestline replied on Wednesday, June 21, 2006

Thanks Joe,

We implelemented something before I read your reply.  I'll take a look at your suggestion.

BR

Copyright (c) Marimer LLC