The COM+ Event System
- The COM+ Event System
- Event Subscribers
- Event Publishers
- About this Article
The COM+ event system introduces Visual Basic programmers to the concept of a loosely coupled event (LCE) system in which event consumersknown as subscribersdon't have to declare a variable against the event providerknown as a publisher. Instead, the subscriber and publisher both rely on an interface definition in the form of a COM+ event class.
Figure 1 shows a conceptual drawing of the COM+ event system.
COM+ events loosely couple publisher and subscriber.
All Visual Basic programmers are familiar with the concept of an event because events permeate every aspect of Visual Basic development. Not even a simple program is accomplished without using them.
Visual Basic events, such as Form Load, are said to be early bound or tightly bound because they require a direct knowledge of the available events and their function signature directly in code. For example, if you want to receive the MouseDown event, you must write the code for the event directly in the form where the event fires. No other part of your application is generally capable of receiving that event.
Beginning with Visual Basic 5.0, Microsoft extended the event system to include the ability to generate your own events from class modules. Therefore, you can define an event signature and then raise the event programmatically. Although this system improved Visual Basic's overall event capabilities, it was still a tightly bound system. To receive the events, an object variable had to be declared WithEvents, which then tightly bound the event provider and event consumer. Furthermore, Visual Basic events don't work well in a distributed system in which events must cross processes and possibly fire on several different machines.
Event Classes
Understanding the COM+ events system begins by understanding COM+ event classes. Event classes form the primary mechanism for decoupling the event as they define the interface that the publisher calls and the subscriber implements. Neither publisher nor subscriber is required to have any knowledge of the other because they operate through the event class.
You define an event class in Visual Basic as you would any other interfaceby simply defining the function signature for the events you want to fire. In fact, event classes are nothing more than interfaces built into an abstract class.
Suppose that you wanted to provide an email notification feature for an online store. In this scenario, you can imagine that products are ordered online and then processed offline with a post-shipping customer notification and a tracking number.
To define the event class, you start a new ActiveX DLL project in Visual Basic to create the new function signature inside an abstract class. For the sake of argument, say that the name of the class is ISendMail. The abstract class will define a single method named Process. The following shows the function signature for the interface:
Public Sub Process( _ ByVal strProductID As String, _ ByVal strDescription As String, _ ByVal strEMail As String, _ ByVal strTracking As String) End Sub
Although your interface is admittedly simple, you can include any further information you want. However, remember a few points concerning the interface definition:
-
Your interface can't have a return value. Because LCE systems decouple the publisher and the subscriber, there's no way to return data to the event's publisher, meaning that you must declare your interface as a Sub and not a Function.
-
You must declare all variables in the interface by value for the same reason that you can't use a Function. By-reference variables are returned to the caller after the routine is processed, which is impossible in a LCE system.
NOTE
Declare event class interfaces with the Sub keyword. Declare all interface arguments with the ByVal keyword.
When the interface is properly defined, you are ready to install it in a COM+ application. Because COM+ manages the event system for you, you must specifically install the interface as an event class. COM+ allows you to do this through the COM Component Install Wizard shown in Figure 2.
Use the COM Component Install Wizard to install the event class.