Loading Modules from non-referenced assemblies AFTER Shell is created

Topics: Prism v4 - WPF 4
Feb 10, 2011 at 2:46 PM

So I went through the Prism Training Kit's MEF examples (adjusting them to work with WPF). I got a WPF application to load the shell using an MEFBootstrapper, and I was able to load a view into a region.

I did this by overriding the Bootstrapper's ConfigureAggregateCatalog() method. The Training Kit's code was as follows:

protected override void ConfigureAggregateCatalog()
{
    // Modules are added / registered in the aggregate catalog
    // More info: http://msdn.microsoft.com/en-us/library/ff921163(PandP.40).aspx
    base.ConfigureAggregateCatalog();

    // Add neccesary classes to the catalog                        
    this.AggregateCatalog.Catalogs.Add(new TypeCatalog(new[] {
        typeof(Shell),  // Add the shell to the catalog so that it can be retrieved afterwards in the CreateShell method. 
        typeof(AutoPopulateExportedViewsBehavior), // Region behavior for finding viewExports
        typeof(ModuleAView) // View of Module A.
        }));
}

Which was fine for the training/demo. However, is it possible to add Views or Modules AFTER the Shell is created and initialized without referencing them? I guess I'm just curious how this would be done.

Feb 10, 2011 at 3:49 PM
Edited Feb 10, 2011 at 3:55 PM

yes,  DirectoryCatalog its part of MEF and it will get all modules that are in it and attributed correctly and add them to the AggregateCatalog once you....

Reference at http://msdn.microsoft.com/en-us/library/gg405479(v=PandP.40).aspx and look near the bottom of the page for "Discovering Modules in a Directory Using MEF"

As for add modules "AFTER" shell creation, you would need to look into Recomposition which is part of MEF and build it into your shell.  MEF docs.  Also look into module initialization.

 

What you had in your previous code snip for the AggregateCatalog was correct, you didn't need to mess with TypeCatalog.  Part of it was the InitilizationMode you set (don't need it unless you were going to manually load it). 

Feb 10, 2011 at 5:17 PM

Hi,

Prism provides you with a mechanism to load modules on demand, without reference them on your project.

As documentation shows: “…In WPF, it is possible to specify your modules information in the App.config file. The advantage of this approach is that this file is not compiled into the application. This makes it very easy to add or remove modules at run time without recompiling the application.

When your modules are registered in the ModuleCatalog, you can resolve an instance of the ModuleManager and finally you can load the modules on demand like this:

private void OnLoadModuleClick(object sender, EventArgs e)
{   
   moduleManager.LoadModule("MyModule");
}

Additionally, you can read more about modularity in Chapter 4: Modular Application Development and also you can check the Modularity QuickStart for WPF (using MEF).

I hope you find this information useful.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic