Event Aggregation QuickStart

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The Event Aggregation QuickStart demonstrates how to build a Windows Presentation Foundation application that uses the Event Aggregator service. This service enables you to establish loosely coupled communications between components in your application.

Business Scenario

The main window of the Event Aggregation QuickStart represents a subset of a fictitious financial system. This window allows the user to add funds to customers and to see the activity log for each customer.

Figure 1 illustrates the QuickStart main window.

Figure 1Event Aggregation QuickStart user interface

Building and Running the QuickStart

The QuickStart ships as source code, which means you must compile it before running it. This QuickStart does not have any prerequisites.

To build and run the Event Aggregation QuickStart

Run the file Open QS-EventAggregation Quickstart Solution.bat to open the solution in Visual Studio.

Right-click the EventAggregation project, and then click Set as StartUp Project.

On the Build menu, click Rebuild Solution.

Press F5 to run the QuickStart.

Walkthrough

Perform the following steps in the Event Aggregation QuickStart to explore the scenario.

To explore the scenario

Run the file Open QS-EventAggregation Quickstart Solution.bat to open the solution in Visual Studio.

On the Build menu, click Rebuild Solution.

Press F5 to run the application. The main window is composed of two views: one view for adding funds to a customer, and one view that shows the activity log for each customer account. The main window is illustrated in Figure 2.

Figure 2QuickStart main window

In the Customer drop-down list box, click a customer, and then click a fund in the Fund drop-down list box, as shown in Figure 3.

Figure 3Customer and fund selected

Click the Add button to add the chosen fund to the selected customer, as shown in Figure 4. Notice that the fund is added to the activity log view corresponding to the selected customer.

Figure 4When the Add button is clicked, the selected fund is added to the corresponding activity log view

Try adding different combinations of customer and fund pairs, as shown in Figure 5. Notice how the activity log views are updated to reflect the funds added.

Implementation Details

The QuickStart highlights the key elements that interact when using the Event Aggregator service. The key artifacts of the QuickStart are illustrated in Figure 6 and described in this section.

Figure 6Event Aggregation QuickStart Conceptual View

The FundAddedEvent Event

The FundAddedEvent event is raised when the user adds a fund for a customer. This event is used by the modules ModuleA and ModuleB to communicate in a loosely coupled way. The following code shows the event class signature; the class extends the CompositeWpfEvent<TPayload> class specifying FundOrder as the payload type. You can find this code at EventAggregation.Infrastructure\FundAddedEvent.cs.

The following code is the class definition for the FundOrder class; this class represents a fund order and specifies the ticker symbol and the customer's identifier. This code is located at EventAggregation.Infrastructure\FundOrder.cs.

Event Publishing

When the user adds a fund for a customer, the event FundAddedEvent is published by the AddFundPresenter class (located at ModuleA\AddFundPresenter.cs). The following code shows how the FundAddedEvent is published.

In the preceding code, first a FundOrder instance is created and set up. Then, the FundAddedEvent is retrieved from the Event Aggregator service and the Publish method is invoked on it, supplying the FundOrder instance recently created as the FundAddedEvent event's parameter.

Event Subscription

The ModuleB module contains a view named ActivityView. An instance of this view shows the activity log for a single customer. The ModuleB initializer class creates two instances of this view, one for Customer1 and one for Customer2, as shown in the following code (this code is located at ModuleB\ModuleB.cs).

When an instance of the ActivityView view is created, its presenter subscribes an event handler to the FundAddedEvent event using a filter expression. This filter expression defines a condition that the event's argument must meet for the event handler to be invoked. In this case, the condition is satisfied if the fund order corresponds to the customer associated to the view. The event handler contains code to display the new fund added to the customer in the user interface.

The following code shows the CustomerId property of the ActivityPresenter class. In the property setter, an event handler for the FundAddedEvent event is subscribed using the Event Aggregator service.

In the preceding line, the following parameters are passed to configure the subscription:

The FundAddedEventHandler action. This event handler will be executed when the Add button is clicked and the filter condition is satisfied.

The ThreadOption.UIThread option. This option specifies the event handler has to run in the user interface thread.

The KeepSubscriberReferenceAlive flag. This flag is false and indicates that the lifetime of the subscriber's reference is not managed by the event. This is set to false because the lifetime of the subscriber—the presenter class—is managed by its view, which contains a reference to it.

The filter predicate. This filter is a condition that specifies that the event handler will be invoked only when the fund is added to the view's corresponding customer.

Acceptance Tests

The Event Aggregation QuickStart includes a separate solution that includes acceptance tests. Acceptance tests describe how an application should perform when you follow a series of steps; you can use the acceptance tests to explore the functional behavior of the applications in a variety of scenarios.

The acceptance tests were developed using the testing framework White. To run these tests, you need to have White installed. For more information about White, including download information, see White on CodePlex.

Note:

The acceptance tests have been developed and verified with the White 0.1.5.0 release. Although other releases of White might also work, it is recommended to use this release to avoid any issues when running the tests.

To run the Event Aggregation QuickStart acceptance tests

Place the assemblies required by White in the folder Source\Lib\White. The files are the following:

Bricks.dll

Bricks.RuntimeFramework.dll

Castle.Core.dll

Castle.DynamicProxy2.dll

Core.dll

log4net.config

log4net.dll

nunit.framework.dll

White.NUnit.dll

Xstream.Core.dll

In Visual Studio, open the solution file QuickStarts\EventAggregation\EventAggregation_AcceptanceTests.sln.

Press F5.

Outcome

You should see the QuickStart window and the tests automatically interact with the user interface. At the end of the test pass, you should see that all tests have passed.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Show:
Inherited
Protected

Was this page helpful?

Your feedback about this content is important.Let us know what you think.