CAB, in terms of Composite Application block, published by Microsoft Pattern & Design team. today we try to learn the event aggregator design pattern included in CAB, which could help understand how it's designed and implemented.
As unusal, module would interact with other module or itself, so it needs to take a reference other module, couple-loosed. is there decouple-loose? The anser is true,Event Aggregator,which could resolve this problem, there three important parts, Publisher, Subscriber and EventAggregator.
- Publisher, it just needs to publish its event and doesn't know the specific subscriber, and how many subscribers there are.
- Subscriber, justs subscribe its care-about event, and doesn't know who's the publisher.
- EventAggregator, just a transfer station, used to store all the subscribers for specific event. when publisher triggers its event, it would find all matched event to execute, within CAB, there are three different execution style.
- Background Thread
- UIThread
- Executing Thread
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
public
interface
IEvent
<
TPayload
>
2 {
3 SubscriptionToken Subscribe(Action < TPayload > action);
4 SubscriptionToken Subscribe(Action < TPayload > action, ThreadOption threadOption);
5 SubscriptionToken Subscribe(Action < TPayload > action, bool keepSubscriberReferenceAlive);
6 SubscriptionToken Subscribe(Action < TPayload > action, ThreadOption threadOption, bool keepSubscriberReferenceAlive);
7 SubscriptionToken Subscribe(Action < TPayload > action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate < TPayload > filter);
8 void Publish(TPayload payload);
9 void Unsubscribe(SubscriptionToken token);
10 void Unsubscribe(Action < TPayload > subscriber);
11 bool Contains(SubscriptionToken token);
12 bool Contains(Action < TPayload > subscriber);
13 }
2 {
3 SubscriptionToken Subscribe(Action < TPayload > action);
4 SubscriptionToken Subscribe(Action < TPayload > action, ThreadOption threadOption);
5 SubscriptionToken Subscribe(Action < TPayload > action, bool keepSubscriberReferenceAlive);
6 SubscriptionToken Subscribe(Action < TPayload > action, ThreadOption threadOption, bool keepSubscriberReferenceAlive);
7 SubscriptionToken Subscribe(Action < TPayload > action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate < TPayload > filter);
8 void Publish(TPayload payload);
9 void Unsubscribe(SubscriptionToken token);
10 void Unsubscribe(Action < TPayload > subscriber);
11 bool Contains(SubscriptionToken token);
12 bool Contains(Action < TPayload > subscriber);
13 }