How to: Create and Publish Events


The Composite Application Library provides an event mechanism that enables communications between loosely coupled components in the application. This mechanism, based on the event aggregator service, allows publishers and subscribers to communicate through events and still do not have a direct reference to each other.

This topic describes how to create and publish an event that can be consumed in a loosely coupled way. For more information about events, see Event Aggregator Technical Concept.


This topic assumes that you already have a solution based on the Composite Application Library with a module. For information about how to do this, see the following topics:


Events created with the Composite Application Library are typed events. This means you can take advantage of compile-time type checking to detect errors before you run the application. The following procedure describes how to create a typed event.

To create a typed event

  1. Add a new class file to your infrastructure project (if you want to publish the event to all the modules in your application) or to a module project (if you want to publish the event only within the same module). This class will hold your event's definition. The recommended name for this class file is EventNameEvent.cs, where EventName is your event's name.

  2. Add the following using statement at the top of the file.

    using Microsoft.Practices.Composite.Presentation.Events;
  3. Change the class signature to make it public and to inherit from the CompositePresentationEvent<TPayLoad> class, where TPayLoad is the type of the event's payload. The payload is the argument that will be passed to subscribers when the event is published. The CompositePresentationEvent<TPayLoad> class is a base class for typed events that manages publications and subscriptions. The following code shows an example of an event class definition named FundAddedEvent and the payload is of type FundOrder.

    public class FundAddedEvent : CompositePresentationEvent<FundOrder>


    The class Microsoft.Practices.Composite.Presentation.Events.CompositePresentationEvent<TPayLoad> does not use .NET Framework events to implement the publication and subscription behavior. Therefore, TPayLoad does not need to inherit from the System.EventArgs class.

The following procedure describes how to publish an event. When you publish an event, subscribers are notified.

To publish an event

  1. In the class where you want to publish the event, add the following using statement and, if required, a using statement for the typed event's namespace. Use the following using statement to refer to event-related classes in the Composite Application Library.

    using Microsoft.Practices.Composite.Events;
  2. Obtain a reference to the event aggregator service (the event aggregator service implements the Microsoft.Practices.Composite.Events.IEventAggregator interface and is responsible for creating and locating event instances). To do this, you can use dependency injection by adding a parameter of type IEventAggregator to the class constructor, as shown in the following code. If your class is instantiated by a container, it will have an instance of the event aggregator service injected upon construction.

    public class Class1
      private IEventAggregator eventAggregator;
      public Class1(IEventAggregator eventAggregator)
        this.eventAggregator = eventAggregator;
  3. Obtain a reference to the event you want to publish by invoking the GetEvent method on the event aggregator service instance. The following code shows how to obtain an event of type FundAddedEvent.

    FundAddedEvent fundAddedEvent = this.eventAggregator.GetEvent<FundAddedEvent>(); 
  4. Set up a payload instance and invoke the Publish method on the event instance passing the payload instance as the parameter. The following code shows how to publish the event obtained in the previous step with a payload of type FundOrder.

    FundOrder fundOrder = new FundOrder();
    fundOrder.CustomerId = "customer1";
    fundOrder.TickerSymbol = "Contoso";


    The payload instance can be used to provide filter criteria for subscribers. For example, subscribers might want to receive notifications only when particular conditions are met on the payload instance.


You will have an event published in your solution.

Next Steps

Typically, after you publish an event, you subscribe to the event. For information about how to do this, see How to: Subscribe and Unsubscribe to Events.

More Information

For a complete list of How-to topics included with the Composite Application Guidance, see Development Activities.

Home page on MSDN | Community site