Adding Presenters to MasterPages in the Web Client Software Factory

Master pages, in the Web Client Software Factory, act as equivalents to the Shell in the Smart Client Software Factory but, unlike their smart client counterpart, they don't benefit from dependency injection.

I've just cooked up a quick way to add dependency injection to master pages in the Web Client Software Factory and, thus, add a Presenter (with the associated Controller).

This can be done just by adding a few lines of code to the Microsoft.Practices.CompositeWeb.WebClientApplication class:

protected void InnerPreRequestHandlerExecute(IHttpContext context)


    if (HttpRequestHelper.IsHandledByPageHandlerFactory(context.Request.Url.ToString()))


        ICompositionContainer moduleContainer = GetModuleContainer(context);


        // We need to use a non-singleton based builder object here, otherwise

        // every object created from the aspx page with the BuildNew attribute

        // will be created with hard references on the lifetime container

        // and will never be released, causing a memory leak.

        CompositionContainer.BuildItem(PageBuilder, moduleContainer.Locator, context.Handler);


        Page page = context.Handler as Page;


        if (page != null)


            page.PreInit += new EventHandler(OnPagePreInit);







private void OnPagePreInit(object sender, EventArgs e)


    Page page = sender as Page;


    page.PreInit -= new EventHandler(OnPagePreInit);


    if (page.Master != null)


        ICompositionContainer moduleContainer = GetModuleContainer(new CompositeWeb.Web.HttpContext(HttpContext.Current));


        CompositionContainer.BuildItem(PageBuilder, moduleContainer.Locator, page.Master);



With this little change, master pages can be built the same way pages are:

public partial class MasterPage : System.Web.UI.MasterPage, IMasterView


    private MasterPresenter _presenter;


    protected void Page_Load(object sender, EventArgs e)


        if (!this.IsPostBack)








    public MasterPresenter Presenter


        get { return this._presenter; }



            this._presenter = value;

            this._presenter.View = this;




    #region IMasterView Members


        // ...




If you want to see this in the Web Client Software Factory, vote on the corresponding work item.


Apparently I had missed a previous post from Simon Ince on something like this.

There's a discussion on this on the community site.

David Hayden also posted something.

5 Responses to Adding Presenters to MasterPages in the Web Client Software Factory

  • David says:

    Hi Paulo,

    This is great. Is this also possible for UserControls. This problem also occurs when attempting this pattern with a UserControl as your View (specifically the CreateNew Attribute fails to be executed).

  • paulo says:

    I guess so.

    Instead of calling CompositionContainer.BuildItem only for the master page, you could call it for all UserControls.

    For dynamically added UserControls, you should inject the builder service (or a UserControl specific builder service) into the page.

    I would go for dynamically adding all UserControls through a UserControl builder service.

    On the other hand, you could add a service dependency on UserControl and have a strategy to build UserControls.

  • Simon says:

    Nice work Paulo… I’ve voted for your issue!

    Regarding the question above, I posted something about user controls a while ago;

    It is a bit of a hack as I haven’t updated the Composite Web stuff – just added lines into the user control to invoke Object Builder. Seems to work nicely though!


  • Sheir says:


    I need help with WCSF.  

    Using VS2005 Developer edition + SP1 on an XP box with SP2 and using .NET 2.0 Framework.

    I downloaded the

     Guidance Automation Extension

     Guidance Automation Toolkit

     Enterprise Library 3.1

     Web Client Software Factory

    Installed it all but am having troubles.

    Trying to create a new guidance solution for Web Application Project using C#.

    It complain about could not find several DLL files – mostly Microsoft.Practices.* ones.

    Manage to locate them and also the other files it complained about.

    Now building the solution caused some error saying it does not know about a ICompositionContainer interface.

    Is the WCSF forcing me to goto .NET 3.5??

    I cannot as the mandate here is to develop using 2.0!

    Can someone help me out?



  • Hi Sheir.

    I can see that you already found help in the right place: