Loosely Coupled Events


Within a COM+ application, it is possible to have one component throw an event and have another component listen for and respond to that event. The event itself is decoupled from the components themselves and managed through the COM+ infrastructure, allowing for some pretty powerful techniques to be employed within your application.

Creating a loosely coupled event system involves the following tasks:

  1. Create an event interface. This is an interface that will describe the features of the event.

  2. Create an event component that implements the interface. The event component itself must be derived from ServicedComponent and implement the event interface.

  3. Define an event sink. The event sink is essentially the listener component that will be passed to the event when the event is fired by the publisher.

  4. Create a publisher that fires the event.

The event interface is quite simple; it's just an interface that defines the method required by the event:

using System; using System.Collections.Generic; using System.Text; namespace LceDemoLibrary {    public interface IMessageEvent    {        void EventMessage(string message);    } } 


Next, create the event class that implements the event interface. This class is just a simple serviced component:

using System; using System.Collections.Generic; using System.Text; using System.EnterpriseServices; namespace LceDemoLibrary { [EventClass] public class MessageEvent : ServicedComponent, IMessageEvent {     #region IMessageEvent Members     public void EventMessage(string message)     {     }     #endregion } } 


Note that there is no real implementation for the EventMessage event. This is deliberate as the real work comes from the event sink (the subscriber).

To create the event sink or subscriber class, add the following class to the class library:

using System; using System.Collections.Generic; using System.Text; using System.EnterpriseServices; using System.Windows.Forms; namespace LceDemoLibrary { public class MessageEventSink : ServicedComponent, IMessageEvent {     #region IMessageEvent Members     public void EventMessage(string message)     {         MessageBox.Show(message, "Event Sink Message");     }     #endregion } } 


Note that the event sink component implements the same interface as the event component. This is done so that COM+ can tell which events can be subscribed to by which subscribers or sinks.

With this library built, register it manually using the regsvcs.exe tool. In order for it to work, you need to use the Component Services administration console to hook up the event subscription, and you can't do that until the COM+ application has been registered.

After the application has been registered, open the Component Services administration console and expand the newly registered application. Expand the component node for the LceDemoLibrary.MessageSink component, right-click the Subscriptions node, and choose New, then Subscription. Browse to select the MessageEvent class underneath the IMessageEvent interface and select to have the subscription enabled immediately.

Now create a test harness that looks something like this:

using System; using System.Collections.Generic; using System.Text; using LceDemoLibrary; namespace LceDemo { class Program {     static void Main(string[] args)     {         Console.WriteLine("Triggering Loosely Coupled Event.");         MessageEvent me = new MessageEvent();         me.EventMessage("Hello from the Console Application");         Console.ReadLine();     } } } 


When you run this application, the event is created and then COM+ will pass that event directly to the event sink you created by virtue of the subscription configured earlier. Without any delay, you should see the dialog box in Figure 40.4.

Figure 40.4. Dialog box as a result of loosely coupled event handling.


Loosely coupled events provide a wealth of power, flexibility, and reliability, which are the cornerstone features of COM+ Enterprise Services that no COM+ developer should forget about.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net