Handling Event when using Managed Child

Handling Event when using Managed Child

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


xAvailx posted on Friday, May 01, 2009

CSLA 3.6.1

Parent > ChildCollection > Child

I have a custom event that is raised on ChildCollection. I need to handle/subscribe to this event on the parent object. I am using managed property for the childcollection. Is there a way to do this? Or do I need to use a private backing field? If I need to use a managed property with a private backing field, are there any examples/gotchas? I see examples for managed properties with private backing fields, but not for private backing child properties.

xAvailx replied on Friday, May 01, 2009

Figured it out, instead of trying to get the type of the managed field (which would work).. I just reference my property inside the class then use the addhandler to register the event.

e.g.

Private Sub RegisterEvents()
AddHandler Me.Statuses.StatusChanged, AddressOf Statuses_OnStatusChanged
End Sub

Then call RegisterEvents on DataPortal Create & Fetch.

On a side note, I tried registering the event in the constructor, but my reference gets lost somewhere in the data portal process. Any ideas why that is? Or do I just register on both Data Portal Fetch & Create?

ajj3085 replied on Friday, May 01, 2009

You'll have to override OnDeserialized and rehook any event handlers.

xAvailx replied on Friday, May 01, 2009

Thx, I did do that. Is that in response to why using the constructor wasn't working? or just general advice. I was using the deserialize when testing the constructor.

Thx either way :)

ajj3085 replied on Monday, May 04, 2009

.Net deserialization does some magic where your constructor code isn't run on deserialization. If you think about it, that makes sense. Why run code when you are just as likely to throw away any of that codes results?

Copyright (c) Marimer LLC