LoadModule from multiple points

Topics: Prism v4 - WPF 4
Jan 14, 2013 at 1:16 AM
Edited Jan 14, 2013 at 1:37 AM

Hi

I'm implementing a module on demand using IModuleManager and Unity. Suppose there are multiple points where functionality from the module is required, am I right to call 

moduleManager.LoadModule("MyModule")
 at each of these points even if the module might have already loaded at a previous point? It's not going to reload is it? And is this best practice?

 

Thankyou.

Developer
Jan 14, 2013 at 6:38 PM

Hi,

Based on my understanding in Prism modules are not intended to be loaded more than once. And when calling the LoadModule method for the second time this won't call the Initialize method again. However, after you have loaded a module, all the components exposed in it will be available to the main application, allowing you to consume them from any point that requires them. Therefore you may have to define some logic to react accordingly in case your module has already been loaded, and perform the necessary functionality as required on each of the different points that may require this components.

In order to achieve this I believe you could benefit of the IModuleCatalog (to obtain the loading process state of the registered modules) and IModuleManager implementations provided with Prism.

For example, a possible approach to detect if a module has been loaded and to react accordingly could be like this:

 

(...)

 var module = this.moduleCatalog.Modules.FirstOrDefault(m => m.ModuleName == "MyModule");
            if (module != null)
            {
                if (module.State != ModuleState.Initialized)
                {
                    moduleManager.LoadModuleCompleted += moduleManager_LoadModuleCompleted;
                    moduleManager.LoadModule("MyModule");
                }
                else
                {

                    //Initialization logic

                }
            }
        }

        void moduleManager_LoadModuleCompleted(object sender, LoadModuleCompletedEventArgs e)
        {
            moduleManager.LoadModuleCompleted -= moduleManager_LoadModuleCompleted;

            if (e.ModuleInfo.ModuleName == "MyModule")
            {
                //Initialization logic
            }
        }

(...)

 

Also, for more information regarding this topic, I believe you could check the Modular Application Development Chapter of the Prism documentation.

I hope you find this helpful,

Agustin Adami
http://blogs.southworks.net/aadami

Jan 14, 2013 at 10:44 PM

That is very helpful, I will use that. Thankyou Agustin.