Discover and load modules dynamically at runtime

Topics: Prism v2 - Silverlight 3
Mar 4, 2010 at 7:49 PM

I am writing a sort of plugin framework. My first tought was to have each plugin implemented as a prism module and load them on demand. The problem is that when the application starts I don't know which plugins will be loaded because it will be possible to load plugins after the application has started. At the time the Bootstrapper's GetModuleCatalog method is called I don't know which plugins the user will load, so it is impossible to add the modules at this time. Can this be done in Prism? I know MEF maybe would be the correct framework to use here, but I was hoping to be able to use PRISM, and don't have to learn another new framework.

I have tried to ask a similar question at the silverlight.net forum (http://forums.silverlight.net/forums/p/165664/373649.aspx#373649), but hasn't got a solution for this yet.

Mar 5, 2010 at 3:32 PM

Hi,

If I understand correctly, when the application begins you don’t know what the available plug-ins will be. Thus, your specific concern is not how to load them on demand, but how to discover them instead.

If this is your scenario, discovering modules at runtime is not something Prism supports out of the box. That is, Prism (out-of-the-box) allows you to specify through configuration which modules will be loaded, and this is only done during the application’s bootstrapping process. This is useful to avoid compile time dependencies, but does not provide the capability of discovering new modules while the application is running.

While it is possible to build this kind of functionality into Prism, you might should still consider checking MEF out as it was created to address dynamic composition scenarios like this. It is possible to integrate Prism and MEF together, so that might also help you achieve what you want.

A possible way to load the modules on demand if you already know them (not at compile time but before running the application), could be writing each plug-in as a module as you said and use on demand module loading. You would have no need to rebuild the application, just updating the ModulesCatalog.xaml before loading your app. Thus, the user can choose only those plug-ins he wishes to load. The flow of your application would be something like:

  1. Load a module that provides the user with a view that gives the option of which plug-ins to load. This thread might be useful for that.
  2. Load the different modules on demand based on the user’s choice.

While this is a suitable approach and a scenario Prism is intended to target, you might also find it useful

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman