WCSF guidance package includes a nice set of recipes that automate creation of web forms, master pages and user controls. The boilerplate code that is autogenerated for code-behind classes defines them as derived from Microsoft.Practices.CompositeWeb.Web.UI.Page class instead of standard System.Web.UI.Page:
public partial class MySummaryView : Microsoft.Practices.CompositeWeb.Web.UI.Page, IMySummaryView
{
}
The Page class overrides the OnPreInit method, and that is where dependency injection "magic" happens:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
Microsoft.Practices.CompositeWeb.WebClientApplication.BuildItemWithCurrentContext(this);
}
We can use similar approach for mobile web forms. Of course, there is no class in WCSF that is derived from System.Web.UI.MobileControls.MobilePage, so we will have to create our own base class in App_Code (or in a shared class library):
namespace Microsoft.Practices.CompositeWeb.Web.UI
{
public class MobilePage : System.Web.UI.MobileControls.MobilePage
{
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
Microsoft.Practices.CompositeWeb.WebClientApplication.BuildItemWithCurrentContext(this);
}
}
}
Now, as long as we derive our mobile web form from this class, we can declare a Presenter property and let CWAB inject it at runtime. There is one drawback, though: we still need to manually create the presenter class and view interface, something that the guidance package recipe used to do automatically. Unfortunately, I don't know GAT well enough to create my own recipe for mobile web form, so the workaround I am using is this:
- Execute "Add page with presenter" recipe
- Modify .aspx file to register "mobile" tag prefix
- Modify code-behind file to change the base class