Prism: Controlling module loading and initialization


Lately, we’ve been working on a project for a client. The project is built ont he silveright platform and uses the Prism architecture and has several modules.
One of the issues that our client had was that everytime they started the application they found that the modules we shuffled and placed differently. The underlying problem is that all the modules in prism are loaded asynchronously and by default, gives the user very little control on loading and initialization. But, that dosent mean it doesnt support excersing some control over this process.

Below is a simple class that sorts the modules alphabetically once they have been loaded and then initializes them in that order:

public class CustomModuleInitializer : IModuleInitializer
{
    #region Class members

    private bool moduleLoadCompleted;
    private readonly IModuleInitializer defaultInitializer = null;
    private List modules = new List();
    private readonly IModuleCatalog moduleCatalog;
    private static int _modulesCount;
    private static int _counter;

    #endregion

    #region Constructors

    public CustomModuleInitializer(IUnityContainer container, IModuleCatalog moduleCatalog)
    {
        this.defaultInitializer = container.Resolve("moduleInitializer");
        this.moduleCatalog = moduleCatalog;
        _modulesCount = moduleCatalog.Modules.ToList().Count;
    }

    #endregion

    #region Implementation of IModuleInitializer

    public void Initialize(ModuleInfo moduleInfo)
    {
        if (this.moduleLoadCompleted)
        {
            defaultInitializer.Initialize(moduleInfo);
            return;
        }

        modules.Add(moduleInfo);

        if (!ModuleLoaded()) return;
            this.SortModules();
        foreach (var module in modules)
        {
            defaultInitializer.Initialize(module);
        }
        modules = null;
        moduleLoadCompleted = true;
        }
    }

    private static bool ModuleLoaded()
    {
        _counter++;
        return _counter == _modulesCount;
    }

    private void SortModules()
    {
        if (modules.Count > 0)
        {
            modules.Sort((x, y) => string.Compare(x.ModuleName, y.ModuleName));
        }
    }
    #endregion
}

And then in the Bootstrapper just override the Configure Container method like this:

protected override void ConfigureContainer()
{
    base.ConfigureContainer();
    var defaultContainer = Container.Resolve();
    Container.RegisterInstance("moduleInitializer", defaultContainer);
    Container.RegisterType(new ContainerControlledLifetimeManager());
}

And its sorted !

Advertisements