This project is read-only.

Thoughts on my Messenger implementation

Dec 21, 2009 at 9:09 PM

Hi,

I have come up with an implementation of Messenger that might be interesting to you. I notice your messages are all strings, and the callback limited to a single parameter.

Here's how mine looks:

    public class Messenger
    {
        public void Broadcast(object message); // aka NotifyColleagues
        public void Subscribe(Action handler); // aka Register
    }

Here, a message can be any object, and clients register with the message type. The callback accepts a single parameter of the type of the message.

An example message class:

    public class ShowOpenFileDialogMessage
    {
        public ShowOpenFileDialogMessage(string filter);
        public ShowOpenFileDialogMessage(string filter, bool multiSelect);

        public string Filter { get; private set; }
        public bool MultiSelect { get; private set; }
        public string[] Result { get; set; }
    }

It is subscribed to like this:

    messenger.Subscribe<ShowOpenFileDialogMessage>(ShowOpenFileDialog);

With an implementation something like this:

 

        void ShowOpenFileDialog(ShowOpenFileDialogMessage message)
        {
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Filter = message.Filter;
            dialog.Multiselect = message.MultiSelect;

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                message.Result = dialog.FileNames;
            }
            else
            {
                message.Result = null;
            }
        }

As you can see, the message can also return a value through the message parameter.

Thoughts?

 

 

 

Dec 21, 2009 at 9:13 PM
Edited Dec 21, 2009 at 9:17 PM

Oh! Almost forgot...

Another thing I do to make it easier to use the messenger is to create extension methods (bodies of all but one method omitted):

    public static class MessengerExtensions
    {
        public static MessageBoxResult ShowError(this MessengerService messenger, string message);
        public static MessageBoxResult ShowMessageBox(this MessengerService messenger, string message, string caption, MessageBoxImage image, MessageBoxButton button);

        public static string[] ShowOpenFileDialog(this MessengerService messenger, string filter)
        {
            ShowOpenFileDialogMessage m = new ShowOpenFileDialogMessage(filter);
            messenger.Broadcast(m);
            return m.Result;
        }

        public static string[] ShowOpenFileDialog(this MessengerService messenger, string filter, bool multiSelect);
        public static PrintDialog ShowPrintDialog(this MessengerService messenger);
        public static string ShowSaveFileDialog(this MessengerService messenger, string filter);
        public static string ShowSaveFileDialog(this MessengerService messenger, string filter, string defaultFileName);
    }

This makes, e.g. showing an error message box as simple as:

 

    messenger.ShowError("Error message...")

Easier than WinForms! ;)

 

Jan 5, 2010 at 4:32 PM

Very interesting!  Thanks for the food for thought. :)