Manually load module

Topics: Prism v2 - WPF 3.5
Jul 14, 2009 at 7:30 PM

I need to load the same module multiples times - for instance customer A while working with customer B-.  I'm using a TabControl to add module instances in different tabs.

The first time the module loads ok, but if I need to load the same module in a different tab I'm trying:

Assembly assembly = Assembly.ReflectionOnlyLoadFrom(module.Ref.Substring(8));
Type type = assembly.GetType("W2.Affaires.AffairesModule", true);
IModule aModule = (IModule)container.Resolve(type);

When executing the third line I get the following error:

{"Resolution of the dependency failed, type = \"W2.Affaires.AffairesModule\", name = \"\". Exception message is: The current build operation (build key Build Key[W2.Affaires.AffairesModule, null]) failed: Type must be of type RuntimeType. (Strategy type BuildPlanStrategy, index 3)"}

How can I solve the problem?

Thanks in advance,


Jul 15, 2009 at 3:17 AM

Hi Marcos,

The way I handled this was to have my modules implement a Show method (in addition to the Initialize). That way the automated module loading does all the background work to get a module initialized, and it only does it once (very efficient). Then the show method simply resolves a new instances of the main view I will be showing and places that in the required region.

Hope this helps.



Jul 15, 2009 at 11:10 PM

Hi Justin,

Thanks a lot for the answer and for the idea, it's a good way.  But the problem I have is previous to the method call.

I have a menu, and when the user select a module I have to create a variable before calling your Show method.  Thas what I'm trying to do with

Assembly assembly = Assembly.ReflectionOnlyLoadFrom(module.Ref.Substring(8));
Type type = assembly.GetType("W2.Affaires.AffairesModule", true);
IModule aModule = (IModule)container.Resolve(type);

How are you doing to get a variable (and then call your Show method) from the menuItem?


Jul 16, 2009 at 1:14 PM

Hi Marcos,

Modules are usually not loaded multiple times. You will probably add View into regions multiple times.  The following threads deal with related issues to you might find them helpful:

Please let me know if this helps.

Damian Schenkelman

Jul 16, 2009 at 11:04 PM

Hi Damian,

Thanks for your answer, I understand and agree.  Anyway, I have the same problem: 

How I get the a variable containing the module from a string (given by a menu button) to load the presenter. 


Jul 17, 2009 at 7:15 PM
Edited Jul 17, 2009 at 7:17 PM

Hi Marcos,

To load a particular module on demand after clicking a menu item, you can write code similar to this if you have the module’s name string:


moduleManager.LoadModule("MyModule"); //moduleManager is an instance of the IModuleManager service, which you can get through DI


When a module is loaded it can perform its particular logic, like showing a particular view after constructing its presenter. If you try to get an instance of the class that implements the IModule interface in the other module you would be breaking the decoupling between modules, so the approach you are taking is not recommended in Composite applications.

You can check the Modularity Quickstarts solutions to check an example of On Demand loading.

The following topic explains how to load modules on demand:

Please let me know if this helps.

Damian Schenkelman

Jul 20, 2009 at 4:24 PM

Hi Damian,

Thanks for the answer.  But when I recall


nothing happens, because the module is already loaded.

What I need is to load another instance of the module in another tab. How can I get this behavieour?


Jul 20, 2009 at 7:50 PM
Edited Jul 20, 2009 at 7:50 PM

Hi Marcos,

Taken from the Composite Application Guidance Documentation:

  • "A module in the Composite Application Library is a logical unit in your application"
  • "Views are the composite portions of the user interface".

In other words, modules are not part of the user interface. Modules might contain Views which will compose the UI. As we discussed above, you load different view instances in regions, not modules. Having said that, I assume you want to load different instances of a view in a TabRegion. To inject and show a new instance of a view to a TabRegion you can use the following code:


Where regionManager keeps a reference to an implementation of the IRegionManager interface (you can get through Unity), and viewInstance is an instance of the view you want to show. This should be done somewhere in the module that has the view (since no other module has a reference to the view). So if you create a new instance of the view’s Presenter/ViewModel you could do something like:


If you want to execute that code as soon as the module is initialized you should place it in the Initialize method of the class that implements the IModule interface. If this code responds to a particular events in your application, other than your module loading, it could be executed in an event handler of an EventAggregator event.

The following topics from the documentation provide more information about adding views to regions:

I hope this explanation was useful for you.

Damian Schenkelman

Jul 20, 2009 at 9:59 PM

Hi Damian,

Very clear explanation, thanks a lot.


Jul 23, 2009 at 1:02 AM

Hi Damian,

In the View Injection QuickStart example, the 'Employee' module has a project reference to the 'Project' module, which seems to lead to some coupling between modules.

Is this a recommended approach if the controller needs to inject views found from multiple modules?

Jul 23, 2009 at 5:54 PM


No, this is not a recommended approach. When two modules reference each other, they are very tight coupled which goes against the modularity guidance. Think of it as if you were to completely remove a module from your application, it should still work. It would not have full functionality, but no changes would be required (except modules to be loaded, of course).

Take into account that the Quickstart application’s objective is showing a particular feature or capability of the guidance, in the simplest way possible. A good approach for this situation could be firing an event using EventAggregator (to communicate between modules), so when the new view has to be injected the module that has it can be notified by the module where the action is initiated. As you can see, this approach would have mixed EventAggregation with UIComposition, thus missing the important point of the Quickstart.

For a sample application on how things would really be done you can check the Reference Implementation solution that comes with the guidance.

Please let me know if this helps.

Damian Schenkelman

Jul 23, 2009 at 10:28 PM

EventAggregator is fine, but we need more control over the order of the views injected. For views that may be injected in views form other modules, our approach is to defined their interfaces in the infrastructure. Our controller uses these interfaces to the view to do the view injection. Seems to work fine for us.

Jul 24, 2009 at 11:41 PM
Edited Jul 24, 2009 at 11:43 PM


I agree that event agregator is the way to go for me.  But I create a "MenuButtonClicked" event in the module, and it's not fired .  May be is not the good place?

My code:

In the shell I have the     menuButtonClicked.Publish(MenuName);    correctly fired.

In the XXXModule the code is:

In the Initialize() function I put

     menuButtonClicked = eventAgregator.GetEvent<MenuButtonClicked>();


who is correctly executed in the module initializer.  But the event is never fired. 


public void CheckMenuClick(string MenuName)


      if (MenuName == "xxxModule"



 I'm using a lot EventAgregator without problems.  There is something special in the Module itself?

Thanks in advance,



Jul 27, 2009 at 6:21 PM

Hi Marcos,

I can think of two possible reasons for this happening.

  1. The event is being implemented with weak references and the handlers is being GC. You can read more about that in this thread.
  2. You are using different instances of the EventAggregator. I cannot be certain of this because I do not know how you are getting the EventAggregator’s instance in your module and shell. This thread is related.

Please let me know if this helps.

Damian Schenkelman