Yet another way to do publish & subscribe . . .

About 2,5 years ago I first came across the idea of combining dependency injection with loosly coupled publish & subscribe. This was when I inspected the source code of the Composite Application Block (CAB) with its underlying InversionOfControl-container ObjectBuilder from Microsoft.

What I liked about publish & subscribe in CAB:

  • The automatic thread marshalling. (Basically you’re able to specify whether the subscription-callback will be handled on the UI-thread or the same thread as the publisher)
  • The wiring was done during the build process in the ObjectBuilder. Publishers and Subscribers are completely decoupled. Wow, this was pretty amazing for me when I discovered that late 2005 :-) .

What I didn’t like about it:

  • It is very tightly coupled to a CAB internal data structure called WorkItem. (Not reusable outside the scope of CAB)
  • It uses standard .NET events / delegates. (The publisher needed to define an event and the subscriber needed to have a public method with a matching “object-sender-eventargs-e” – signature.)
  • It uses attributes and especially a string based topic identifier to correlate publishers for a topic and corresponding subscribers. (Maybe I’m narrow-minded on this, but I think the simplest way to specify a subscription is the best, which imho is expressing a subscription by implementing an interface.)

Many frameworks or libraries I looked into over time mostly followed a comparable approach using either events or some other delegate-based solution (the EventBroker in CAB, the EventBrokerFacility in Castle Windsor, the EventAggregator in PRISM, ….) (Please correct me if I overlooked or misunderstood something :-) ). The only framework I came across with something similar to what I had in mind is Caliburn.

Unfortunately Caliburn is a WPF/ .NET 3.0 based framework, but most of the products of my current employer are limited to .NET 2.0 due to a minimum system requirement of Windows 2k. Because of that I decided to implement something similar for our purposes which can be easily integrated into the InversionOfControl – container of our choice (which is Castle Windsor at the moment, by the way :-) ).

I’ll be describing the implementation and publish the source code for anyone who is interested in one or more follow-up posts . . . .

Kick it on dotnet-kicks.de Kick it on DotNetKicks.com
BjRo posted at 2008-6-30 Category: Uncategorized | Tags: ,

2 Responses Leave a comment

  1. #1Rainer Schuster @ 2008-8-15 15:58

    Did you have a look at the Publish/Subscriber Pattern Sample at the Unity Source

  2. #2BjRo @ 2008-8-15 21:00

    Yes I did (in case you mean the SimpleEventBroker sample). It’s conceptually very close to the original CAB-implementation (Based on .NET-events which are marked with attributes containing string-based topic identifiers.) So most of the things I didn’t like about Publish & Subscribe in CAB apply to this sample, too.

    Or is there something newer?

Leave a Reply

(Ctrl + Enter)