[Back to Table of Contents]
SCSF Events are coming to the picture when the announcer of the event, for example the “Refresh” button and the listener of the event – let’s say a grid control that needs to be refreshed with the latest data belong to separate loosely coupled components.
SCSF objects responsible for events
Workitems objects contain WorkItem.EventTopics collections of EventTopic objects. An EventTopic object contains information of a specific event and stores list of subscribers/publishers of this event.
Loosely coupled events communication within Smart Client applications is provided by the Event Broker service. It receives notification from the event publisher and fires it to the event subscribers.
Registering publications or subscriptions using attributes
A SCSF event publication method need to be declared with the [EventPublication] attribute with uniquely identified string. Subscriptions need to have a method with the same signature as the event publisher and need to be declared with the [EventSubscription] attribute.
Let’s look at the auto generated StatusUpdateHandler function in the ShellForm.cs. It updates the status strip of the Shell form. This function is declared as subscriber of the event named EventTopicNames.StatusUpdate and could be called from the user interface thread. EventTopicNames.StatusUpdate constant is stored in the EventTopicsName.cs file of the Constants folder of the Infrastructure.Interface project.
[EventSubscription(EventTopicNames.StatusUpdate, ThreadOption.UserInterface)]
public void StatusUpdateHandler(object sender, EventArgs<string>e)
{
_statusLabel.Text = e.Data;
}
To call this function we need to add a functionlity which will fire this event – the even publisher functionality.
In the sample below I put an event declaration in the business module’s ModuleController and the code which fires this event in the begining and in the end of the Run() to update the user when the module is ready for usage:
[EventPublication(EventTopicNames.StatusUpdate, PublicationScope.Global)]
public event EventHandler<EventArgs<string>> StatusUpdateTest;
public override void Run()
{
StatusUpdateTest(null, new EventArgs<string>("Module is loading..."));
AddServices();
ExtendMenu();
ExtendToolStrip();
AddViews();
StatusUpdateTest(null, new EventArgs<string>("Module is ready."));
}
Let us create an another subscriber of the same event since the first was auto generated. We may add the code below into ShellForm.cs:
[EventSubscription(EventTopicNames.StatusUpdate, ThreadOption.UserInterface)]
public void OnStatusUpdate(object sender, EventArgs<string>eventArgs)
{
MessageBox.Show("The module is ready!");
}
After loading the module which is firing the event both subscibers will receive the event’s notification.
If the publication scope is global like in our example, the publisher may notify multiple modules’ subscribers of the same event. And in another scenario a subscriber could be notified by multiple global publishers!
The code generation Wizard
The good news is SCSF provides a graphical interface enabling generate event publication and subscription codes.