Search code examples
c#silverlightprismmefprism-4

Extending the MefModuleManager


I want to extend the MefModuleManager. Now I am searching for a way to do this. First I have created my own ModuleManager:

public class MyModuleManager : MefModuleManager {
    public MyModuleManager(IModuleInitializer moduleInitializer, IModuleCatalog moduleCatalog, ILoggerFacade loggerFacade) : base(moduleInitializer, moduleCatalog, loggerFacade) {
    }

    protected override void HandleModuleTypeLoadingError(ModuleInfo moduleInfo, Exception exception) {
        // ... my Code
    }
}

Now, In my Shell I have (like described in Prism with Mef):

// The shell imports IModuleManager once to load modules on-demand.
// Due to SilverLight/MEF restrictions this must be public.
[Import(AllowRecomposition = false)]
public IModuleManager ModuleManager

I don't know, how to tell Mef, that It should use MyModuleManager instead of MefModuleManager for IModuleManager.


Solution

  • In order to have the MefBootStrapper use different dependencies, you need to declare your custom classes with the [Export] attribute.

    Then, if you want Mef to use your custom ModuleManager for a specific type class, you may add a parameter on the attribute for specifying the dependency type which would be resolved as follows:

    [Export(typeof(IModuleManager))]
    public class MyModuleManager : MefModuleManager
    {
        [ImportingConstructor]
        public MyModuleManager(IModuleInitializer moduleInitializer, IModuleCatalog moduleCatalog, ILoggerFacade loggerFacade) : base(moduleInitializer, moduleCatalog, loggerFacade) {
        }
    
        protected override void HandleModuleTypeLoadingError(ModuleInfo moduleInfo, Exception exception) {
            // ... my Code
        }
    }
    

    Notice that I have added an [ImportingConstructor] attribute so the BootStrapper would be able to pass the requested arguments when initializing. That way, Mef would use your custom ModuleManager when a request is made for an IModuleManager instance.

    You can refer to the following MSDN Prism Guide chapter for more information:

    I hope this helped, regards.