Using an interface vs. MVVM light Messenger for data requests
I've got a plugin that needs access to certain information in order to populate its GUI elements properly. However, this plugin should not know about all other plugins, so I want it to request this information from the application.
In situations like this, I always create an interface for data exchange, and then pass this interface to plugins so that they can request the data when it's needed. However, I recently started to use the MVVM light toolkit because it's got some great features like RelayCommand
and Messenger
. In this case, I can totally see using Messenger -- plugins don't need the interface, because they can simply use Messenger.Default.Send<MyDataRequestMessage>(...)
. As long as they register the开发者_如何学Go Receive handler, it's all good... or is it?
Which method would you favor, and why?
In case of plugins, an aggregator like MVVM Light's messenger is quite alright; alternatively, you could look at MEF (now part of .Net 4), which also enables auto-discovery and other such nice features, and you could use interfaces with that. So the answer is it depends :) Personally I'd favor Messenger for its simplicity, unless it's for a very large enterprise-y project maybe.
As Alex said, MEF was created just for this purpose. If you need to manage plug-ins, you're probably going to end up duplicating a lot of work that MEF provides for you if you don't use it.
There's no reason you can't use both MEF and MVVM Light. Your idea of communicating from your plug-in to your app using MVVM light's messenger is intriguing, and I hope it works for you. However, keep in mind that any plug-in could register to receive these same messages and you could end up with one plug-in receiving another plug-in's messages. This might not be an issue for you, but if you don't control who writes these extensions you definitely have a security hole there.
Good luck!
精彩评论