An IModuleTypeLoader for deviant modules’ location; But WHY?

Tags

, ,

The best place to learn about IModuleTypeLoader is looking at the source code hosted on Github.  Looking at the name, we get a clear idea about its role.  It loads  the module type exported into the current AppDomain so that the ModuleInitializer in charge of instantiating the a module type can accomplish its task.  When implementing your custom IModuleTypeLoader, if you want it to interoperate nicely with existing moduleType loaders, and not circumvent their functions, pay close attention on how the interface method CanLoadModuleType is implemented.  The out-of-box implementation of IModuleTypeLoader return true or false depending on the ModuleInfo.Ref value (from PRISM doc: Reference to the location of the module assembly.), which is set to the CodeBase of the Assembly defined by the ModuleInfo.ModuleType property set by the ModuleCatalog in the case of a WPF application.

The IModuleTypeLoader loads found assemblies into the current AppDomain .  the default implementation rely on having the assembly containing the modules being exported either on disk or hosted in a remote location as is the case of Silverlight xap files.  When a PRISM app first load, the ModuleCatalog finds all modules being exported based on its internal logic ( DirectoryModuleCatalog searches a directory’s assemblies exporting IModule); Afterward, The ModuleManager queries its IModuleTypeLoaders that can load the ModuleInfo(found by the ModuleCatalog) for each ModuleInfo that needs to be loaded to make sure that the module is loadable; then relays the charge of instantiating and initializing the Modules to the ModuleInitializer; The ModuleInitializer instantiate a concrete instance of the newly discovered Module type and simply calls the new concrete instance Initialize() method . 

There are different ways to manually load your custom modules; but in the case of MEF based PRISM application, the easiest approach is to add your modules to the AggragateCatalog (<seealso MefBootstrapper /> Smile)  otherwise, implement IModuleTypeLoader if your moduleSleeping half-moon(s) reside anywhere but on an accessible Drive or Share to inform ModuleManager that yes indeed the Module being describe by IModuleInfo can be loaded and implement IModuleInfo.LoadModuleType(ModuleInfo moduleInfo) to load the module assembly into the AppDomain so its Types can be found and instantiated.

Follow

Get every new post delivered to your Inbox.