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

Silverlight Error “Layout Cycle Detected Layout could not complete”


Recently our Managing Director reported that while presenting the system froze with a white screen, or sometimes just the screen went un-responsive without no apparent error message on the screen.
Digging a bit deeper into it we found that there was a

“Layout Cycle Detected Layout could not complete”

that was being thrown.
Basically, in the page’ SizeChanged event, there was some code that was modifying the width of one of the panels on the page to accomodate to fit the content of the data field in the controls in the panel (which should ideally be done by setting width/height properties of the container elements to “Auto”), so causing the properties of the components to change while the layout was still being updated, ultimately resulting in this exception.

Once the containers were re-designed to Auto Size for the content (Also imposing a max width and max height with a Horizontal scrollbar to display content sensibly), the problem disappeared and away we went 🙂