Help needed with child persistence
Help needed with child persistenceOld forum URL: forums.lhotka.net/forums/t/8479.aspx
jpm posted on Thursday, February 04, 2010
I have a parent BO which contains a list of child BO’s.
The primary key of the parent BO is an integer (the real value is returned from the database when the object is persisted – like the Resource BO in the project tracker sample).
The child object’s primary key is a complex key made up of the parent key and another integer that’s value is determined by the DB on persistence. Both values are stored as read-only properties of the child object.
When DataPortal_Insert() is called on the parent BO it persists the parent data, updates its ID property then calls FieldManager.UpdateChildren(this) to persist the child objects.
My problem is how to I go about updating the matching id property on the children prior to calling FieldManager.UpdateChildren without having to make that property editable on the child.
I’m sure I’m being very dense here but any help would be greatly appreciated.
rxelizondo replied on Thursday, February 04, 2010You should not need to store the ID value of the parent on the child object to begin with. Thats why you are passing the "this" pointer to FieldManager.UpdateChildren() method call.
When Child_Create is called, it will be called with a reference to the parent (this) and you can get the parent ID value using that reference.
Not sure if this is what you where asking, hope I did't misunderstand your question.
Marjon1 replied on Thursday, February 04, 2010I think rxelizondo meant Child_Insert() instead of Child_Create, just in-case anyone was confused.
Your method signature should probably look like this: Child_Insert(ByVal parent as RootObjectType)
From there you could access the root level's id property, which would be populated from the original dataportal_insert().
ajj3085 replied on Friday, February 05, 2010I'd probably just pass the key value directly, and not a reference to the parent to avoid coupling the child objects to the parent.
JoeFallon1 replied on Friday, February 05, 2010
I'd probably just pass the key value directly, and not a reference to the parent to avoid coupling the child objects to the parent.
I do this.
But I recall reading that Rocky prefers to do it the opposite way! He likes to pass the Parent object instead of just the key.
In all the years I have been doing this I have never needed anything from the parent other than the key so I stopped passing the parent a long time ago.
ajj3085 replied on Friday, February 05, 2010Ya, in the end I think it comes down to personal preference. It'd be unusual to reuse one child between two different root objects (although I do have such a case).
rsbaker0 replied on Thursday, February 04, 2010I have an intermediate base class between BusinessBase and my concrete object classes.
I chose to solve this problem by implementing a virtual SetChildForeignKey() method (which the default implementation does nothing). The parent object invokes this for all child objects as part of it's persistence implementation.
This allows the child object to use the parent object's key in any way it sees fit before it is actually saved.
jpm replied on Sunday, February 07, 2010
Thanks everyone, I was being a bit dense but this is exactly what I was after. I my case my child objects may end up having more than one valid parent so I'll probably go for the loosly coupled option and just pass the key to the child_insert() method.
Copyright (c) Marimer LLC