MemBus: Using an IoC Container to provide subscribers
Once we’re down to providing instances to handle available messages, it makes a lot of sense to bring IoC containers to the stage. Indeed, it is relatively easy to put your Container of choice into action by providing an implementation of “ISubscriptionResolver”.
MemBus comes with an implementation that uses the Common service locator dll to allow plugging your container of choice into the functionality of letting “IHandles” implementations handle messages. Numerous IoC container on the .NET platform come with a bridge to plug into the common service locator. It is also quite easy to provide your own bridge. Here’s an example for StructureMap:
ServiceLocatorImplBase is a class from the common service locator dll.
Moving along with the StructureMap example, the Container enters MemBus in the following way:
ServiceLocatorSupport is a MemBus class which accepts an implementation of IServiceLocator, the main interface of the common service locator library.
In StructureMap I can now make all IHandles implementations available e.g. inside a registry:
That way, any implementations that close this interface will be picked up and resolved when requested by MemBus.
Another nice way to bring in message notification is by defining how an observable is constructed. MemBus brings along a generic implementation of IObservable<T> that is IoC friendly. The class MessageObservable, which is also returned by a call to IBus.Observe<M> can also be constructed by the IoC container, provided you let it know how to provide an IBus implementation:
Hence any instance that expresses a dependency to an IObservable<T>
will obtain a valid instance that will pick up notifications published via MemBus.
By the way, all code samples here are taken from the Sample Application which is part of MemBus.