MVVM Fabric: Using MessageBus

While working with MVVM and trying to keep clean separation of view models, it becomes clear that there is a need for some sort of communication mechanism to allow view models to interact with each other without knowing about each other.  A message bus works perfectly to fit this need.

Message Bus Overview

The message bus acts as a communication switchboard for your application.  All messages route through the message bus and the message bus gets them to any interested parties.

The message bus is an example of the publisher/subscriber pattern, similar to events in .NET.  However, events require at least one object to know about the other in order to subscribe to the event.  With the message bus, objects only need to know about the message bus to subscribe to or publish messages.  All messages go through a single instance of the message bus and that single instance must be available throughout the application or the pattern quickly breaks down.

With the message bus, there is no longer the need to bubble events from one object to the next, eventually getting them to an object that cares.  Objects subscribe to specific message types.  When that type of message is published through the message bus, the message bus passes the message along to any subscribers.  If there are no subscribers, the message quietly goes away.

A message is simply a class.  It can be an empty class, which acts more as a signal, or a sort of data transfer object, which contains relevant information.  The message bus doesn’t care what the message is, but it does care about the message’s type.  The message’s type is how the message bus knows who is subscribed.

A while back I wrote a post describing how to develop a simple message bus.  The message bus I wrote for MVVM Fabric uses the same interface I described in that post, but has a more robust implementation.  More specifically, I added thread-safety and the use of WeakReferences so that it doesn’t promote memory leaks.  The message bus provides a clean interface which can be used to send and receive messages throughout the application.  Let’s take a look at an example.

Message Bus In Action

First of all, here is a simple example of a message.

Notice that the message doesn’t derive from anything (though it very well could).  In this case, I am using the message to pass keywords used for a search.  Next up is the class which will publish the message.

I only included the important parts for brevity. Notice that we will be using the IMessageBus interface, so that this can be mocked out in testing.  The Search method creates a new instance of the SearchMessage and calls Publish on the message bus passing the message as a parameter and using the message’s type as the generic argument.

To make sure that the message doesn’t fall on deaf ears, we need a subscriber.

Notice the method called HandleSearch, which takes a SearchMessage as a parameter.  The constructor subscribes the HandleSearch method to messages of type SearchMessage and is all ready to search.

Conclusion

The message bus is something that I have used frequently in my travels with WPF and MVVM to keep my view models decoupled.  It provides a simple way to provide fire and forget functionality in a testable manner.  Using the message bus in your application will allow you to keep your view models nicely de-coupled.

Share