This is a super early release of the next version of the MVVM Light Toolkit. Please enjoy with care, be aware that this version is not fully tested and not feature complete by a long shot.
What’s new in the Messenger?
So far the changes are:
- Bug correction: A “collection was modified” error could occur if a recipient was registering for a message type in the same method it used to receive another message. A similar error had been fixed in V2, but unfortunately this one remained. A unit test was added to isolate this case, and a fix has been implemented.
- New built-in message types: NotificationMessageAction and NotificationMessageAction<TCallbackParameter>. These types can be used to send a message through the Messenger, and get a callback. A next post will give more information about this scenario.
- Renamed message types: To avoid some confusion, I renamed CommandMessage to NotificationMessage. These messages are used to convey a notification (of type string) from a sender to a recipient. The term “command” was too confusing because it is a loaded term in the MVVM pattern (ICommand, RoutedCommand, RelayCommand, etc…). The term NotificationMessage is clearer. The old types are still available but have been marked Obsolete and will be removed in a future version of the toolkit.
At this point it is good to remember that the Messenger class (from V2) can be used to send any kind of message, from simple values to complex objects. The built-in message types are merely a convenience for specific scenarios, but nothing forces you to use them!
- To make testing and mocking easier, it is now possible to override the Messenger.Default property and provide another implementation. In addition, all the public methods of the Messenger class have now been marked virtual, which means that they can be overriden. A next post will give more information about this scenario.
- In V2, you could send a message to a given type of recipients only by using the method overload Send<TMessage, TRecipient>. In V3, TRecipient can now be an interface. This enables the Messenger to send messages to all the objects implementing a given interface. Note that the Messenger can send to any type of recipient, and no special requirement is needed to receive a message. But if some of your objects happen to implement an interface (for example IViewModel), you can make sure that your messages reach only these recipients (of course, the recipients still need to register for a type of messages).
- When a recipient registers for a type of message, it can provide a token (any object). Then, when a sender sends a message, it can also provide the same token. The Messenger will only send the messages to those recipients who provided the same token when registering. This enables creating separate optimized communication channels between objects. The token can be any object, for example a string, a GUID, etc…
What’s new in ViewModelBase?
In V3 alpha 1, a small change has been made to ViewModelBase: The constructor ViewModelBase(Messenger) has been replaced by ViewModelBase(IMessenger). This makes it easier to test and mock ViewModels that use their own implementation of the IMessenger interface.
More changes will be implemented in V3 final.
if you have MVVM Light Toolkit V2 installed, you can easily deploy the V3 alpha1 DLLs manually by following the steps:
- In Windows, choose the Start menu then All Programs, MVVM Light Toolkit, Binaries (Assemblies). This starts the Windows Explorer in the Binaries folder.
- (optional but recommended) Rename GalaSoft.MvvmLight.dll (and the Silverlight version too) to GalaSoft.MvvmLight.bak.
- Download the V3 alpha binaries.
- Extract this file to a local folder.
- Copy the WPF version of the assembly in Binaries, and then copy the Silverlight version to Binaries\Silverlight.
After these few steps, you are all set to use the new features described here. Again, remember that this is a super early release, so use with care and revert to V2 if you are encountering issues. Also, please give me feedback and suggestions!
I will publish in the next few days a few blog posts about these new features, and also continue to work on new features. As soon as some features are ready, I will push an alpha2 and blog about it. Thanks a lot for the support!!