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;


    #region Constructors

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


    #region Implementation of IModuleInitializer

    public void Initialize(ModuleInfo moduleInfo)
        if (this.moduleLoadCompleted)


        if (!ModuleLoaded()) return;
        foreach (var module in modules)
        modules = null;
        moduleLoadCompleted = true;

    private static bool ModuleLoaded()
        return _counter == _modulesCount;

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

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

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

And its sorted !


About Jinish Bhardwaj
Jinish works as a Contractor @ Microsoft, Hyderabad and has more than 8 years of experience in building Web, Windows and Smart Client application for clients across the globe. Apart from that Jinish is also a Microsoft Certified Professional and a Certified SCRUM Master

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: