AddNew Child object - best way to intercept the process and initialize values after I have a parent

AddNew Child object - best way to intercept the process and initialize values after I have a parent

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


DCottle posted on Thursday, January 22, 2009

I have been using CSLA.NET for a few years now, but in my previous assignment our children were being added via a grid on the UI, and therefore the AddNewCore and OnAddNew methods were being fired by the grid.  I could simply put the initialization code that I needed inside one of those methods and all was good with the world.

I have since moved to a different assignment and we have the need to do something like this:

Order.OrderDetails.Add(OrderDetail.NewOrderDetail());

 

What is the best way to "hook" into this process and initialize some values on the OrderDetail object AFTER it has a reference to its parent object.  For example, I want to traverse up my parent tree and get the key from the Order object to set that on my child.

Thanks in advance,

 

David

simon_may replied on Thursday, January 22, 2009

This may not be the conventional way in the CSLA world but I have the parent BO handle the BLB's AddingNew event and provide a new child with various properties initialised.

HTH

Simon

rsbaker0 replied on Thursday, January 22, 2009

I have this exact same scenario, but I still take (partial) advantage of AddNewCore() to do this (that's were I would be calling your equivalent of OrderDetail.NewOrderDetail())

However, I think you can override either InsertItem() or OnAddingNew() to hook into the process of the item being added to the list.

I vaguely recall difficulties with trying to supply the new object using OnAddingNew(), so I create the item in AddNewCore() and do additional property setting in InsertItem()

 

Copyright (c) Marimer LLC