How to supply a MEF DirectoryCatalog exports to CM MefBootstrapper?

Topics: Bootstrappers & IoC, Extensibility
Jun 5, 2011 at 11:11 AM

Hi, I have a MEF-based plugin-oriented application, where VM's are loaded among a set of available ones (all exported via MEF by different plugins), following some application logic. The VM's (and their views, which are UserControl's) are exported by MEF and implemented with CM, using a MEF bootstrapper. So once I find out which VM's I need for a specific session, I want to instantiate them, locate, bind and instantiate the corresponding view, and add the view programmatically to a tabcontrol. Once the UI is built this way it will no more need to change for that session: these are not panes which can be activated, deactivated, closed or the like.

First, I asked myself how to instantiate the VM's programmatically, importing their dependencies and keeping their general structure equal (all derive from a base class, with a default ctor). I suppose I can put their dependencies (essentially event aggregators or window managers) in [Import] properties. This should allow to programmatically select a number of VM's and instantiate them, letting MEF satisfy their import's.

Next step is instantiating their views: here I'd like to use CM. Starting from the VM, I have this method for letting CM instantiate and bind them; I'll just have to pass to it all the VM's I instantiated in the previous step:

static private object LocateViewFor(object viewmodel)
{
  UIElement view = ViewLocator.LocateForModel(viewmodel, null, null);
  ViewModelBinder.Bind(viewmodel, view, null);
  return view;
}

My question is: is there a way of letting CM know about the available MEF exports it should use for locating the view? I was looking at the bootstrapper's SelectAssemblies, but it requires a number of Assembly objects. This way, I should look at my plugins directory and load all the DLL's into assemblies to be returned by this override. I fear this will loose the advantages offered by MEF DirectoryCatalog, which does not require to load *everything*. What would be a better way of "integrating" MEF directory-based catalogs with CM bootstrappers? I was thinking of instantiating the views myself via MEF, but I would then require to do something like bootstrapper's BuildUp to bind them to their VM's and satisfy their imports. But I'm a newbie to CM and probably I still miss the general view, so it might well be that there are other ways. Could anyone give a hint?