CSLA 3.5: Root subscribing to a child eventCSLA 3.5: Root subscribing to a child event
Old forum URL: forums.lhotka.net/forums/t/4916.aspx
triplea posted on Thursday, May 29, 2008
I am struggling a bit with this... I have an editable root object say Customer and an editable child Address. I want the root Customer object to be notified when a specific property in its Address changes. But I am not sure when to subscribe the root to the event.
Here is an example (within my root Customer object):
public
Address CustomerAddress
{
get
{
if (!(FieldManager.FieldExists(CustomerAddressProperty)))
{
SetProperty<Address>(CustomerAddressProperty, Address.NewAddress());
GetProperty<Address>(CustomerAddressProperty).PostcodeChanged += new EventHandler<AddressPostcodeChangedEventArgs>(CustomerAddress_PostcodeChanged);
}
return GetProperty<Address>(CustomerAddressProperty);
}
}
The event is fired properly but Customer never gets notified. Now the question is twofold:
- It appears I am never getting in the SetProperty<>, GetProperty<> bit of code... I was under the impression that this should happen each time the property is interrogated for its value.
- Assuming this is the wrong place to put this code, where should it actualy be?
Cheers
RockfordLhotka replied on Thursday, May 29, 2008
Your event hookup would only ever happen if you were using lazy loading to create the child object. FieldManager.FieldExists() returns false only if you haven't already set the field to some value.
My guess is that you are creating the child object in your DP_Fetch() method - probably calling LoadProperty()? You'd need to hook the event there, and in an OnDeserialized() override too (to handle cloning and data portal serialization).
triplea replied on Thursday, May 29, 2008
But in that case I will need to do the event hookup in 2 locations, 1 in the DP_Fetch() when calling LoadProperty() (as indeed is happening) for existing data and 1 in the location I have it for New objects. This seems like a bit of duplication to me unless I missed something out again?RockfordLhotka replied on Thursday, May 29, 2008
Three – don’t forget the OnDeserialized() override :)
Each time you create a new instance of the object you need to
hook the event from that instance. I don’t see how you can avoid that, it
is just the way events work.
Rocky
triplea replied on Thursday, May 29, 2008
OK then 3 times, no probs! I just wanted to make sure I wasn't double hooking events or doing anything silly.Copyright (c) Marimer LLC