Search code examples
moduleprisminstance

How to get a module instance in PRISM


I have a PRISM desktop app which loads modules from a directory with the help of the DirectoryModuleCatalog.

Everything is fine, except that I cannot find a way to get the instance of a loaded module. IModuleManager and IModuleCatalog don't have a method like getInstance(ModuleInfo) or similar.

See

moduleManager.LoadModule(moduleInfo.ModuleName);

This line loads the module properly(moduleManager is of type IModuleManager), but what do I have to do next to get the actual instance of this module?

The modules are loaded on demand of the user, so I cannot register all modules at startup within the bootstrapper.


Solution

  • If by Module instance you mean the class that implement IModule, then you must explicitly register the instance into the container to be able to get it.

    Although the aforementioned will work, you should not take that approach at all. The idea is that the module classes are specific to a particular module and should only be used for module initialization purposes.

    I would place each module's Start method in a separate component (IStartable), register each component in the container with a different Id and resolve/import an IEnumerable to get all instances that have the start method.

    Hope this helps