Partilhar via


How to: Create and Publish Events

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.

Overview

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.

Prerequisites

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:

Steps

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 running 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.Events;
    
  3. Change the class signature to make it public and to inherit from the CompositeWpfEvent<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 CompositeWpfEvent<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 : CompositeWpfEvent<FundOrder>
    {
    }
    

    Note

    The class Microsoft.Practices.Composite.Wpf.Events.CompositeWpfEvent<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.Wpf.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";
    fundAddedEvent.Publish(fundOrder);
    

    Note

    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.

Outcome

You will have an event published in your solution.

Next Steps

The following is a typical task that you perform after you publish an event:

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.