Friday, September 12, 2008

SCSF Events (One firer and multiple (Not wanted) subscribers)

So when working with SCSF you may face a problem like this.

You have built two views as part of a module (View1 and View2). View1 fires an event that View2 is  subscribed to. this all seems very legal until now. but what if View2 and View2 are loaded and displayed more than one time. now comes the illegal part. every time any of the View1 fires the event, all instances of View2 will receive it and react to it.

To illustrate it even further, assume you have built a hospital application that consists of two views. SearchView and DisplayView. the search view allows the user to search for a patient by specifying part of the patient name. but the SearchView does not display the search result, it is the DisplpayView that displays the results after receiving the Event showResults. now when the user types part of the patient name and click search, the event ShowResults will be raised and DisplayView will receive it. DisplayView will use the EventArgs to find the patient and then display him/her. the problem happens if you loaded two of the SearchView and two of the DisplayView. now when you type part of the paient name in any of the SearchView loaded and click Search, both of the loaded DisplayView will receive this event and will display this patient.

The solution to this problem is to make your module create a new work item every time it wants to add views. then add the views (SearchView and DisplayView) to this new work item.

when you fire the event you fire the event to the subscribers within the work item.
Here is how.
in your ModuleController.cs declare a new workitem as follows

WorkItem instancewi = new WorkItem();

Next, in the AddViews method add this new workitem to the WorkItems collection of the current item

instancewi = WorkItem.WorkItems.AddNew<WorkItem>();

now when adding views add then in this new workitem as follows

instancewi.SmartParts.AddNew<WhateverYourViewIs>();


now when firing an event, you should always be limiting the scope to the work item using PublicationScope.WorkItem enumeration and specifying the current workitem as follows

WorkItem.EventTopics[WhateverYourTopicIs].Fire(this,new EventArgs(WhaeteverWahtever), WorkItem, PublicationScope.WorkItem);

This way when you fire an event from one view, only the other views that were loaded with the same view will receive this event, since they are in the same workitem.
Hope that helps.

1 comment:

Anonymous said...

have a peek at this site f4s03r2k75 good quality replica bags replica bags forum m7c51r8a74 published here e9r96o5y55 louis vuitton fake replica bags forum u5t08a8a26 replica bags chicago investigate this site x3p96j2n28 replica bags china