Hi
This must be quite a common question with CAB's Event Broker - is
there any way to Raise Events dynamically at runtime ?
We would like to remove the hard coding from our "Event Dispatching"
pump along the lines of the following pseudo code:
foreach (message in queue)
{
// e.g. Message.Topic might be "topic://SomeTopic"
EventBroker.FireEvent(message.Topic, new
[Data]EventArgs<MessageClass>(message));
}
The message is a standard format for all "Events"
(We might need to determine whether there are any subscribers to the
"SomeTopic" event - EventBroker again might be able to assist here.
And then in the respective modules, the EventSubscriptions remain "as
they are" (i.e. Design Time)
[EventSubscription("topic://SomeTopic")]
public void ActionSomeTopic(object sender,
[Data]EventArgs<MessageClass> e)
What we want to avoid is having to manually add new publications to
the dispatcher, i.e. NOT the current :
[EventPublication(EventTopicNames.SomeTopic1)]
public event EventHandler<EventArgs<MessageClass>> SomeTopic1Event;
[EventPublication(EventTopicNames.SomeTopic2)]
public event EventHandler<EventArgs<MessageClass>> SomeTopic2Event;
...
[EventPublication(EventTopicNames.SomeTopicN)]
public event EventHandler<EventArgs<MessageClass>> SomeTopic3Event;
void Dispatcher()
{
foreach (message in queue)
{
switch (message.Topic)
{
case "SomeTopic1":
if (SomeTopic1Event != null)
{
SomeTopic1Event(this, new
EventArgs<MessageClass>(message))
}
break;
case "SomeTopic2":
if (SomeTopic2Event != null)
{
SomeTopic2Event(this, new
EventArgs<MessageClass>(message))
}
break;
etc
}
}
A dynamic event raising mechanism would be far more elegant
Thanks in Advance!
Stuart
NonNB - 07 Feb 2008 07:48 GMT
Hi
FYI below links show how this can be done.
Rich Newman is right - there is no need to add the Publication or even
the Event topic "constant" before "Firing it".
http://richnewman.wordpress.com/2007/09/23/events-in-the-cab-introduction-to-cab
scsf-part-12/
http://safari.oreilly.com/9780735624146/ch06lev1sec4
http://forums.lhotka.net/forums/ShowThread.aspx?PostID=10154#10154
Cheers.