1
votes

I'm loading a set of modules using a DirectoryModuleCatalog in Prism for wpf. I want to load a module and display its default view on a button click in my shell application (all the modules are labelled with [Module(OnDemand = true)] attribute). I cache the module names when they are discovered:

var modulesCatalog = new DirectoryModuleCatalog {ModulePath = @"C:\WhereTheModulesLive"};
modulesCatalog.Initialize();             

foreach (var module in modulesCatalog.Modules)
{
    _availableModules.Add(module.ModuleName);
}

This enables me to map the name of the module I require to a button (the below is called as a result of a command, triggered by a button click, where the buttons are populated from _availableModules). At the moment I navigate to the default view by naming it by convention:

private void LoadAndDisplayModule(string moduleName)
{
    _moduleManager.LoadModule(_availableModules.GetNameOfModuleToLoad());
    _moduleManager.Run();
    _regionManager.RequestNavigate(RegionNames.ContentRegion, moduleName + "View");
}

This feels like an untidy and brittle implementation - and there's some more information which I'd ideally like to embed in the module - ideally to be accessed before it's loaded. I wonder if there's a way of doing this? I wondered about adding a custom attribute to the module, but I can't work out how I would read it during module discovery (without rewriting the DirectoryModuleCatalog). Alternatively, if there were a way to make a module support an interface, which it could be cast to (MyModule : IModule, ISupportsDefaultView), but I think that directly accessing IModule objects goes against the Prism design philosophy. Any ideas?

1

1 Answers

1
votes

The modules are short-lived and only used to do the initialization. You should create a registry where each module can put its extra information (like default view) when it's initialized:

public interface IModuleRegistry
{
    void RegisterDefaultView( string name );
    IReadOnlyCollection<string> GetDefaultViews();
}

The application would then, when all modules are loaded probably (or each time a new module adds data), look through the list of requested default views an add them. Production code should use two different interfaces, of course, and in different scopes, most likely, depending on your architectural requirements.