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);

 

            PrePageExecute(page);

        }

    }

}

 

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)

        {

            this._presenter.OnViewInitialized();

        }

        this._presenter.OnViewLoaded();

    }

 

    [Microsoft.Practices.ObjectBuilder.CreateNew]

    public MasterPresenter Presenter

    {

        get { return this._presenter; }

        set

        {

            this._presenter = value;

            this._presenter.View = this;

        }

    }

 

    #region IMasterView Members

 

        // …

 

    #endregion

}

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

Update

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 thoughts on “Adding Presenters to MasterPages in the Web Client Software Factory”

  1. 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).

  2. 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.

  3. Hi,

    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?

    Thanks

    Sheir

Comments are closed.