Question

In an Event Aggregator pattern, the publishers publish events to the Aggregator and subscribers subscribe to a particular event via the Aggregator.

Suppose there are 3 subscribers, SubscriberA, SubscriberB and SubscriberC. They all want to subscribe to an event called OnPowerUpEvent. The OnPowerUpEvent can be fired by either PowerItemA or PowerItemB. There can also be multiple instances of PowerItemA and PowerItemB.

Based on this scenario, I don't understand how the Event Aggregator pattern would be able to allow the subscribers to subscribe to the event published by a specific publisher. It seems to me that the subscribers can only subscribe to the event OnPowerUpEvent in general, and respond to the event no matter who the publisher was.

For example, SubscriberA may want to subscribe to the event OnPowerUpEvent that is published by any PowerItemA instances. SubscriberB may want to subscribe to the event OnPowerUpEvent that is published by a particular instance of PowerItemB. And SubscriberC may want to subscribe to the event OnPowerUpEvent in general, regardless of who the publisher was.

In these 3 different cases, how does the Event Aggregator pattern manage this?

Was it helpful?

Solution

Popular Event Aggregator implementations provides not only simple Publish/Subscribe features but more comples subscription filtering infrastructure, for instance Prism EventAggregator allows providing a predicate along with a handler: (see MSDN)

FundAddedEvent fundAddedEvent = eventAggregator.GetEvent<FundAddedEvent>();

fundAddedEvent.Subscribe(FundAddedEventHandler, 
                         ThreadOption.UIThread, false,
                         fundOrder => fundOrder.CustomerId == _customerId);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top