DirectoryModuleCatalog in Prism 4.0

Topics: Prism v4 - WPF 4
Mar 28, 2012 at 10:29 PM

I am upgrading from v1.2 and notice that IModuleCatalog has replaced IModuleEnumerator. The developer that originally did our implementation used something called a CompositeModuleEnumerator which effectively took a path to a directory, and then recursed through any subdirectories to find IModuleInfo implementations. This allowed us to organize our modules into their own folders.

Is there a way to configure the DirectoryModuleCatalog to do the same thing?

e.g. 

     directoryModuleEnumerator.ModulePath = "c:\Modules";

     directoryModuleEnumerator.Load(); // scans all subdirectories of Modules for implementations of IModuleInfo

 

assume a disk structure like:

c:\Modules\ModuleA

c:\Modules\ModuleB

c:\Modules\ModuleC

etc.

So at the end the catalog would contain: ModuleA.dll, ModuleB.dll, ModuleC.dll etc.

Thanks,

Developer
Mar 29, 2012 at 7:11 PM

Hi,

As far as I know this functionality is not provided by Prism from out of the box.

In my opinion you could try creating a custom implementation of the DirectoryModuleCatalog, which looks into all sub-directories of the ModulePath. For example you could obtain all sub-directories using the following line of code, as explained in this blog post:

string[] folders = System.IO.Directory.GetDirectories(ModulePath, "*", System.IO.SearchOption.AllDirectories);

Based on this, your CustomDirectoryModuleCatalog could be similar to this:

public class CustomDirectoryModuleCatalog : DirectoryModuleCatalog
    {
        protected override void InnerLoad()
        {
            base.InnerLoad();

            string[] folders = System.IO.Directory.GetDirectories(ModulePath, "*", System.IO.SearchOption.AllDirectories);

            foreach (string folder in folders)
            {
                ModulePath = folder;
                base.InnerLoad();
            }
        }
    }

Finally you will only have to override the CreateModuleCatalog method in your bootstrapper in order to create your new directory module catalog class,for example like in the following code snippet:

 protected override IModuleCatalog CreateModuleCatalog()
        {
            return new CustomDirectoryModuleCatalog() { ModulePath = @".\Modules" };  
        }
I hope you find this handy,

Agustin Adami
http://blogs.southworks.net/aadami


Mar 29, 2012 at 7:44 PM

Ok, thanks. I was thinking that I might have to do this, but I thought that I would investigate first.

Thanks,

HS