Module Loading with ModuleManager in Prism

Gus Cavalcanti picture Gus Cavalcanti · Dec 19, 2008 · Viewed 9.2k times · Source

This question was also posted on Prism's site on Codeplex.

I am a little lost on loading modules on demand, and I hope you guys can enlighten me. I am using v2 #7.

Here's what I want to accomplish:

1) I have 2 regions - one for buttons that instantiate the modules (MenuRegion), another to show the modules (MainRegion). Only one module is to be visible at a time in MenuRegion.
2) Modules are to be found in a directory
3) The MenuModule module is associated with MenuRegion and is to create buttons showing the module names. When one of the buttons is clicked, the corresponding module is to be loaded and displayed in MainRegion.
4) Behavior:
    a) ModuleA button is clicked and ModuleA is displayed. User enters data in ModuleA and data is left in an unsaved state.
    b) ModuleB button is clicked and ModuleB is displayed. User enters data in ModuleB and data is left in an unsaved state.
    c) ModuleA button is click and ModuleA is displayed with the information previously entered.

Please correct me if I am wrong in my assumptions, and please give me other suggestions:

1) MainRegion should be a ContentControl, so only one module is visible at a time.
2) I think I need to enumerate the "GrouplessModules" of my Catalog to get the ModuleInfo for the modules available in the directory
3) To load the module I think I need to call myModuleManager.LoadModule(moduleName)

Finally, my questions:

1) Should I have the moduleManager as a parameter in my MenuModuleView's constructor?
2) How do I get the catalog accessible from inside MenuModuleView, in order to enumerate the available modules?
3) From MenuModuleView, as I enumerate through the modules, I create the buttons, assigning the ModuleInfo to the button's Tag property. This way, I have only one click event to load the module. Is this correct? It smells a bit work-aroundish to me...

Thanks a lot for your help!!!

Answer

Kent Boogaart picture Kent Boogaart · Dec 19, 2008

1) Should I have the moduleManager as a parameter in my MenuModuleView's constructor?

Technically you could. Practically speaking though, if you do this you will still want to define a default constructor for your view. Otherwise, your view will not work in designers. Therefore, I recommend that properties be used to pass in dependencies to UI components.

2) How do I get the catalog accessible from inside MenuModuleView, in order to enumerate the available modules?

You would depend on Prism's IModuleEnumerator interface and dependency injection will take care of supplying the module enumerator to your view. Assuming you're using Prism with the Unity container, that would look something like this:

[Dependency]
public IModuleEnumerator ModuleEnumerator 
{
    get; set;
}

3) From MenuModuleView, as I enumerate through the modules, I create the buttons, assigning the ModuleInfo to the button's Tag property. This way, I have only one click event to load the module. Is this correct? It smells a bit work-aroundish to me..

I would suggest you have each button raise the same WPF command and instead assign the ModuleInfo to the Button's CommandParameter property. Again, Prism has infrastructure to help you do this cleanly in a composite scenario. See the DelegateCommand class.

As a final note, be aware that whilst you may be able to lazily load your modules, you won't be able to unload them. For that, you will require AppDomain isolation, which is a whole other kettle of fish.