left-icon

Prism 4 Succinctly®
by Eric Stitt

Previous
Chapter

of
A
A
A

CHAPTER 11

Prism 4 Event Aggregation

Prism 4 Event Aggregation


Prism 4 event aggregation is another Prism 4 communications mechanism. Rather than being built on the GoF Command design pattern the event aggregator uses the Observer design pattern. The observer design pattern is a publish / subscribe pattern that allows for multiple publishers and subscribers. 

Note: The GoF definition of the Observer design pattern is as a one to many construct that has a single publisher to many subscribers. The Prism 4 event aggregator uses a many to many construct that allows for as many subscribers and publishers as needed.

Event aggregation is an excellent method of communication between Prism 4 modules and it's versatile functionally makes it suitable for a number of different situations.

The Event Aggregation Class:

The event aggregation class is used to identify and create Prism 4 event aggregation objects. As a rule I place these classes in the infrastructure project of the solution so that the other classes have access the object. Listing 50 is an example of a Prism 4 event aggregation class.

I usually place an Event Aggregation folder in the Infrastructure project for these classes.

Listing 50: A Prism 4 Event Aggregation Class:

public class ShowMessage : CompositeWpfEvent<string>

{

}

These classes inherit from the CompositeWpfEvent generic class which takes a payload of the type that is passed to the class. In this case a string is passed.

Event Aggregation Subscribers:

Once the event aggregation class is created, subscribers can subscribe to the class to monitor for events being raised. There a number of subscriber overloads, let's take a look at them.

Subscriber Overloads:

The Prism 4 event aggregator subscribe method has 4 overloads.

  1. The default subscription
  2. The UI Thread subscription
  3. Subscription Filtering
  4. Strong Reference subscription

The Default Subscription:

The default subscription first uses the GetEvent method to get the event aggregator that is to be used. This class assigns the event aggregator to a callback method that is passed to the subscribe method, this method is where the code for the event aggregator is executed. Listing 51 shows an example of the event aggregator based GetEvent method.

Listing 51: A Prism 4 Event Aggregation GetEvent Class:

this.eventAggregator.GetEvent<MessageBoxEvent>().Subscribe(ShowMessage)

The payload in Listing 51, "MessageBoxEvent" is the event that this subscriber will use. This is the event that was added to the infrastructure project. The callback method is the ShowMessage argument that is passed to the Subscribe method. Listing 52 shows an example of the ShowMessage method.

Listing 52: A Prism 4 Event Aggregation ShowMessage Method:

Private void ShowMessage(string Message)

{

   //Show the message here.

}

Note that the string type payload that was passed to the event aggregator class is resolved as the message argument in the ShowMessage method.

The UI Thread Subscription:

The subscriber method UI Thread overload allows the publisher of the event to update the user interface. As a default when an event is published the publisher thread is used. If this thread is a background thread, the UI won't be updated. Using the subscriber method's UI Thread over load addresses this issue. Listing 53 shows an example that uses this overload.

Listing 53: The Subscribe UI Thread Overload:

this.eventAggregator.GetEvent<MessageBoxEvent>().Subscribe(ShowMessage, ThreadOption.UIThread)

The ThreadOption can use one of three selections.

  1. PublisherThread
  2. BackgroundThread
  3. UIThread

The PublisherThread:

This default setting allows the event to be received on the thread of the publisher.

The BackgroundThread:

This type of thread is an asynchronous thread, that works with .NET framework thread pool threads.

The UIThread:

The UI thread allows for the update of the UI by receiving a thread that is capable of UI updates.

Subscription Filtering:

The subscription filtering overload allows the subscriber to determine if its associated callback method is executed when an event is published. Filtering can be used in cases where single or multiple subscribers are used. Listing 54 shows an example of subscription filtering.

 Listing 54: The Subscribe Filter Overload:

this.eventAggregator.GetEvent<MessageBoxEvent>().Subscribe(ShowMessage, ThreadOption.UIThread, false, DisplayMessage => ShowMessage == "Hello Prism 4!")

This filter will only show the message if the ShowMessage argument is equal to "Hello Prism 4!". The false setting after the ThreadOption is the KeepSubscriberReferenceAlive argument. We'll talk more about this setting in the next section.

Strong Delegate Reference Subscription:

Sometimes it's necessary to publish a number of events in a short period of time. This can cause performance issues in your solution. To address this issue, strong delegate references can be used.

As a rule, weak delegate references are used by default. Using this kind of reference allows the garbage collector to remove objects without any action on your part. The issue with this kind of cleanup is that it may take a good deal of  time before objects are destroyed.

Using strong delegate references on the other hand, stops the automatic destruction of objects. This keeps the  resources from being garbage collected. When strong delegate references are used it is important that the code manually unsubscribe from the event.

 Listing 55 shows an example of using strong delegate references with event aggregator subscriptions.

 Listing 55: The Subscribe Strong Delegate Reference Overload:

Bool KeepSubscriberReferenceAlive = true;

this.eventAggregator.GetEvent<MessageBoxEvent>().Subscribe(ShowMessage, ThreadOption.UIThread, KeepSubscriberReferenceAlive , DisplayMessage => ShowMessage == "Hello Prism 4!")

Setting the KeepSubscriberReferenceAlive member to true, stops the garbage collector from destroying the object.

Event Aggregation Publishers:

Our last step when working with event aggregation is to publish an event. Publishing an event starts in the same manner as subscribing to an event. GetEvent is called from the event aggregator, but in this case, the Publish method is called. The method takes the payload that was created when the event Aggregation class was added to the infrastructure project. Listing 56 shows an example of an event aggregator publisher.

 Listing 56: The Event Aggregator Publish Method:

this.eventAggregator.GetEvent<MessageBoxEvent>().Publish("Hello Prism 4!")

Event Aggregation And The Virtual Calculator Solution:

The Virtual Calculator solution does not use event aggregation. So there is no code in the solution that shows this code.

So, What's Next?

Event aggregation is one of a number of methods that can be used to communicate between loosely coupled components in Prism 4 solutions. In this chapter we talked about how to create event aggregators, how to use GetEvent to identify and use the event class and how to subscribe to and publish events. We also took a close look at the subscribe method and its overloads.

 In the next chapter we'll look at Prism 4 navigation.


Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.