Skip to content
flq edited this page Aug 13, 2011 · 8 revisions

The provided way to start a MemBus instance is going through the BusSetup.

var _bus = BusSetup.StartWith<Conservative>().Construct();

Conservative is one of the already available Configurators contained in the "Configurators" namespace.

As you can see from such a setup, what MemBus needs as a minimum is a default publish pipeline, a default subscriptionresolver, and a default strategy what to do with new subscriptions.

The _bus that is now set up, is of type IBus. IBus derives from IPublisher and ISubscriber. This is due to the "I" in SOLID, so you can use the interface you really need (some of your code may only publish, other parts may need only subscribing features)

Usage:

Now you can do something like that:
using (_bus.Subscribe((Foo x) => Console.Writeline(x.Text))) {
  _bus.Publish(new Foo("hello world"));
}

Any subscription operation you undertake returns something of type IDisposable. So far MemBus does not do funny WeakDelegate stuff, however, this may come at some point. For now, I am happy about disposing of resources explicitly.

Publish/Subscribe message matching is by default done on the type of the message. Please note that message subscription is contravariant. I.e. a Method accepting a message of type object, will receive all messages published on MemBus.

There are no other rules apart from the following:

  • Subscriptions match the Action<T> delegate
  • There are no other rules apart from the previous

Subscription overloads

If you look at the Subscribe signature, there are a couple of overloads that help you in your messaging needs:

Providing Subscription shapes

Using Message reception via IObservable interface

It can be quite useful to obtain an IObservable<T> from your bus. There are 2 ways to do so:

var observable = _bus.Observe<FooMessage>();

or if you want to delegate observable construction to your container of choice, you could do what that method does for you:

var observable = new MessageObservable<T>(_bus);

This allows you to bring the Rx tastiness on top of the IObservable interface.

Clone this wiki locally