Load modules on demand from DirectoryModuleCatalog - II

Jan 25, 2009 at 2:21 PM

I have a Shell that I reuse with different modules.  All the modules are in a subfolder "Modules".
When I call the Shell, I'm passing a parameter with the name(s) of the module I want.  This way, from all the modules that are in the subfolder - that have the attribute LoadOnDemand=true) - I just want to load this whos name equals the parameter.

I'm using the following bootstrapper:
 
protected override IModuleCatalog GetModuleCatalog()
        {
            return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
        }

        protected override void ConfigureContainer()
        {
            Container.RegisterType<IShell, Shell>(new ContainerControlledLifetimeManager());
            base.ConfigureContainer();
        }

        protected override DependencyObject CreateShell()
        {
            IModuleCatalog moduleCatalog = Container.Resolve<IModuleCatalog>();
            IModuleManager moduleManager = Container.Resolve<IModuleManager>();

            foreach (var module in moduleCatalog.Modules)
            {
                if (module.ModuleName == "MyModuleParameter")
                    moduleManager.LoadModule(module.ModuleName);
            }

            ShellPresenter presenter = Container.Resolve<ShellPresenter>();
            presenter.SetTitle(App.Title);
            IShell view = presenter.View;

            view.ShowView();

            return view as DependencyObject;

        }

But the module catalog is allways empty.  (but it's work if I change the attribute to LoadOnDemand=false). I can't found how to enumerate the modules and load on demand those I need.

Thanks for the help.

Marcos

Jan 26, 2009 at 2:23 PM

Hi Marcos,

 

The DirectoryModuleCatalog’s constructor does not initialize the catalog. Also, the CreateShell method is called before the InitializeModules method during the application startup, so the Directory Module Catalog and the Module Manager are not initialized (that is why the Modules collection is empty).

Try moving the code that loads your modules to the InitializeModules method in your Bootstrapper class:

 

protected override void InitializeModules()

{

base.InitializeModules();

 

IModuleCatalog moduleCatalog = Container.Resolve<IModuleCatalog>();

IModuleManager moduleManager = Container.Resolve<IModuleManager>();

 

foreach (var module in moduleCatalog.Modules)

{

if (module.ModuleName == "MyModuleParameter")

moduleManager.LoadModule(module.ModuleName);

}

}

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti

Jan 26, 2009 at 7:15 PM
Thanks a lot, Mariano, that made the trick.

I'm using Prism V2 a lot, and it's good to have your - and your colegues - support when we adopt a new technology.
Apr 23, 2009 at 9:51 PM
Hi Mariano/Marcos,

I am using Prism Modules for ondemand xap download. I able to LoadModule as per RemoteModuleLoading example. Now i want to access types of downloaded modules. as per document it is loaded into AppDomain. I am confused, how to call methods of downloaded modules?
e.g. I having SLApp1, SLApp2. SLApp2 having one class file with one sample method. Using Modules i have loaded SLApp2 in Memory. Now the question is, how should i call the sample method if SLApp2? here is the code inside SLApp1 page.xaml.cs.

 

private void LoadModule_ButtonClick(object sender, RoutedEventArgs e)

 

{

 

this.moduleManager.LoadModule("SLApp2");

 

 

 

}

After this i am stuck. How should i call method if SLApp2??

Thanks,
Chirag