List of loaded modules

Topics: Prism v4 - WPF 4
May 9, 2011 at 6:54 AM

This is a recurrent question, still not having a definitive answer (or I did not find it out): A module needs to get the list of all other modules.

The first idea is to get the module catalog using some IModuleCatalog object created (I am using MEF) and updated by Prism but this does not seem that easy to do and there is the risk of interfering with Prism mechanims when using methods I found.

Another way could be to create my own object starting from import metadata but I do not know how to import modules defined with [ModuleExport(typeof(SomeModuleA))].

Did somebody already achieve this ?

May 9, 2011 at 7:08 AM
Edited May 9, 2011 at 10:55 AM

posting a question makes you think differently: The definitive answer is to create my own list, I need some non-Prism information about modules and some of the modules must not be in the list.

Here is what I did:

Create a bean:

    public class InternalModuleInfo
    {
        string key;
        public string Key
        {
            get { return key; }
            set { this.key = value; }
        }      
       //Others properties to be inserted...
    }

Export the bean from each module:
    [ModuleExport(typeof(MyModule))]
    public class MyModule : IModule
    {

        [Export(typeof(InternalModuleInfo))]
        InternalModuleInfo internalModuleInfo = new InternalModuleInfo();


        public void Initialize()
        {
            internalModuleInfo.Key = "1";
        }

Import internalModuleInfo beans
    public class CommonViewModel : NotificationObject, IPartImportsSatisfiedNotification
    {
       ...

        [ImportMany(AllowRecomposition = true)]
        private Lazy<InternalModuleInfo>[] _internalModulesInfo;
        
        ...
        
        public void OnImportsSatisfied()
        {
            foreach (var lazyInternalModuleInfo in _internalModulesInfo)
            {
                InternalModuleInfo internalModuleInfo = lazyInternalModuleInfo.Value;
            }

        }

This works but the OnImportsSatisfied method in CommonViewModel is raised as initialization modules and setting keys values (internalModuleInfo.Key = "1";) is not yet done. If I wait those modules are initialized I get correct values in lazyInternalModuleInfo. In my particular case this is not an issue but I am not satisfied with this. Any idea to improve it?

And at last I am wondering why so many discussion about this as it is so simple to solve. Did I miss something?