Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Tags

News

Unit Test Today! Get Typemock Isolator!

Projects

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

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.

Published Sun, Apr 1 2007 23:59 by Paulo Morgado

Comments

# re: Adding Presenters to MasterPages in the Web Client Software Factory@ Monday, April 02, 2007 1:22 AM

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

David

# re: Adding Presenters to MasterPages in the Web Client Software Factory@ Monday, April 02, 2007 3:28 AM

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.

Paulo Morgado

# re: Adding Presenters to MasterPages in the Web Client Software Factory@ Wednesday, April 04, 2007 5:07 AM

Nice work Paulo... I've voted for your issue!

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

http://www.dotnetblogs.co.uk/Default.aspx?tabid=137&mid=550&ctl=ViewEntry&EntryID=294

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!

Simon

Simon

# re: Adding Presenters to MasterPages in the Web Client Software Factory@ Friday, February 01, 2008 1:04 PM

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

Sheir

# re: Adding Presenters to MasterPages in the Web Client Software Factory@ Monday, February 04, 2008 6:06 PM

Hi Sheir.

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

www.codeplex.com/.../View.aspx

Paulo Morgado

# Web Client Software Factory Resources@ Friday, January 23, 2009 12:55 AM

Web Client Software Factory Resources

TipsOnLips.net

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: