I need in a simple project the ability to communicate various type of messages through different View Model in a WPF application, some VM raise some message about something that is happened in the system, and other VM can listen for messages and doing something with them.
I know that there are a lot of framework out of there, but sometimes you need a quick implementation that you can share with the team, without the need to tell to others Hey you need to master xxx framework for understanding what is happening. The result is a really 30 minutes implementation of a primitive broker. I want a central component where every View Model can register/unregister for a specific kind of a message, and send messages.
I decided to discriminate messages based on the type of data contained in the message, the message itself is a simple class with a message string and a payload.
View Models register for a specific kind of payload, as an example I want to be able of issue this code to register whenever someone send a message with a payload of type List<ActionLogViewModel>.
The result is this really simple class, that is based on delegate
It maintains internally a dictionary of registered action based on type of the payload
The RegisterForMessage<T> is really simple, because I simply need to save an Action<Message<T>> into the dictionary.
The send message is a very simple function too because it look for registered action in the internal dictionary, and executes them one after the other.
I know that this is really a too simple implementation for a serious brocker system, but for a simple program, where my only need is to dispatch some messages between windows, it is enough. The main advantage of this approach, is that it is really simple to understand, and I can avoid to force other members of the team to learn new framework.