Removing EventTopic from ManagedObject collection

Topics: CAB & Smart Client Software Factory
Dec 14, 2005 at 10:09 AM
originally posted by: StephenTurton

Hi

I’m curious as to why the removal of EventTopic(s) from the Managed object collection appears to not be permitted.

My scenario – I have an application like OneNote. I create a new document and drag a SmartPart from the taskpane onto the document. When the part is created on the document I create a new EventTopic, add a subscription and add the EventTopic to the Page WorkItem EventTopics collection.

User saves the document, I persist all the WorkItems to a file. User closes the document, all WorkItems in the document hierarchy are terminated - during the OnTerminating method of my Page WorkItem, I remove the EventTopic subscription added earlier and I attempted to remove the EventTopic from the Page WorkItem EventTopics collection. In the ManagedObjectCollection.Remove method an exception is thrown as the item is an EventTopic which is not allowed.

Therefore If I don't remove the EventTopic - When the user reopens the document just saved without closing the application I deserialize my file stream and reconstruct all the WorkItems. As I rebuild a page I attempt to add the EventTopic for the object I am recreating on the page but the EventTopic still exists in the ManagedObjectCollection, as I could not remove it earlier, yielding an exception (I’m using the same topicId, which was persisted.)

Yes I could just check if the EventTopic exists and just add a subscription. and I can think of other ways to avoid this situation. However – what I really want to accomplish here is that when a user closes a document I would like to clean up nicely behind me and remove from the ManagedObjectCollection any document specific objects that I created dynamically. Otherwise my application may be up and running for a while a user may be opening and closing many documents, consequently I may end up having unnecessary/unused objects specifically EventTopics hanging around.

Before I do a little redesign and take a slightly different approach to circumnavigate this I’d like first to verify if adding and in particular removing event topics dynamically is/should be supported in my kind of scenario. If an EventTopics subscription count is zero should I be able to remove it from the ManagedObject collection?

Thanks in advance
-Stephen

BTW: Kudos to the CAB team – nice framework.
Dec 15, 2005 at 6:56 AM
originally posted by: BradWilsonMSFT

There's no need to explicitly create event topics. Merely accessing them ensures that they exist.

There really shouldn't be any need to remove them.
Dec 15, 2005 at 12:01 PM
originally posted by: StephenTurton

Thanks Brad for getting back to me – I’ll mull this over, and dig into the event mechanism code a little deeper. I’m still building out my design so no claims that I am using the most appropriate methodology for this scenario just yet. However it has been useful to be able to add event topics dynamically. I am firing the event dynamically too using the this.EventTopicstopicId.Fire(…) method from a workitem at the top of a workitem hierarchy. Rather than have potentially hundreds of subscribers lower down the food chain I register a particular event/topicId when a workitem is dynamically created. At the time that the workitem at the top needs to fire the event it is able to determine the topicId to fire and the event goes directly to the interested party rather than wend its way past all of the descendants. I’ll play around with this some …

Thanks again.
-Stephen