Accessing Grandparent property...

Accessing Grandparent property...

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


rlriggs posted on Wednesday, October 08, 2008

Been struggling with a design issue (which is my biggest problem from cutting my teeth in a non-OOP world). The basic question, is it possible to update a property value in a grandparent object from the grandchild - and more importantly, should this ever be done.

My basic structure is this:
ProductInventory (location, product, Available, Reserved, etc.) - BB
--InventoryTransactions (editable collection)
-----InventoryTransaction (editable child) contains Date, Quantity, ActionType, etc.)

What I need to have happen is updating the Available, Reserved values based on the InventoryTransaction.


I had originally worked through this with my sprocs updating the values in the base tables, but am running into synch problems, also tried working through this with command objects, which probably could work.

Anyone have a simple example of accessing the grandparent properties to update values? I'm aware of the Parent property, but that doesn't get me to the root. I know it's likely something silly I'm not considering, and would appreciate the guidance.

sergeyb replied on Wednesday, October 08, 2008

To get to grand parent you just need to expose parent property in your editable collection:
Internal ProductInventory MyParent
{
Get
{
Return this.Parent As ProductInventory
}
}
My approach usually is to just signal grandparent to compute something instead of directly modifying a property the parent is responsible for maintaining.

So in your ProductInventory you would have
Internal void UpdateReservedAndAvalaible(InventoryTransaction tran)

In your InventoryTransaction you would call that from property setter for quantity:
This.Parent.MyParent. UpdateReservedAndAvalaible(this)

Sergey Barskiy
Principal Consultant
office: 678.405.0687 | mobile: 404.388.1899

Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

-----Original Message-----
From: rlriggs [mailto:cslanet@lhotka.net]
Sent: Wednesday, October 08, 2008 10:14 AM
To: Sergey Barskiy
Subject: [CSLA .NET] Accessing Grandparent property...

Been struggling with a design issue (which is my biggest problem from cutting my teeth in a non-OOP world). The basic question, is it possible to update a property value in a grandparent object from the grandchild - and more importantly, should this ever be done.

My basic structure is this:
ProductInventory (location, product, Available, Reserved, etc.) - BB
--InventoryTransactions (editable collection)
-----InventoryTransaction (editable child) contains Date, Quantity, ActionType, etc.)

What I need to have happen is updating the Available, Reserved values based on the InventoryTransaction.


I had originally worked through this with my sprocs updating the values in the base tables, but am running into synch problems, also tried working through this with command objects, which probably could work.

Anyone have a simple example of accessing the grandparent properties to update values? I'm aware of the Parent property, but that doesn't get me to the root. I know it's likely something silly I'm not considering, and would appreciate the guidance.

rlriggs replied on Wednesday, October 08, 2008

Thanks for the quick reply, looks very straight forward. However, when I try to do this in VB, the Parent property is not available in my EditableRootCollection object (BusinessListBase).

JonStonecash replied on Wednesday, October 08, 2008

You might also want to consider using events to signal the need for processing.  Directly accessing the parent of the parent creates a coupling between the grandparent, child, and grandchild.  That may be want you need, but if you can avoid it you should.

In the event model, the grandchild would do something and then raise an event.  The list of grandchildren would catch that event, do whatever it wanted to, and then raise its own event.  The child would catch that event, ya da, ya da, ya da and so on.  If this event bubbling seems like too much overhead, you could have the grandparent catch the event directly; the child would perhaps have to help out on this. 

In any case, the grandchild would not be so tightly coupled and the overall software would not be so fragile.

Just a thought.

Jon Stonecash

Copyright (c) Marimer LLC