1 February 2008

Controlling Pages

by mo

WebForms is an awkward marriage between a Page Controller and a Template View. In the Web Forms model the Template View (aspx page) inherits from the Page Controller (code behind).
Patterns of Enterprise Application Architecture defines the Page Controller as:

“An object that handles a request for a specific page or action on a web site.” - PoEAA

In this example I’ve separated the Page Controller from the Template View, because… mostly because I was bored and thought this would be a great way to better understand the patterns. So let’s get started…
I defined a layer super type for all page controllers defined as :

public interface IPageController : IHttpHandler {
  void Execute();

The IPageController could have very well been called and Page Command because in this implementation I’m not concerned about having separate behaviors for GET and POST method requests. If this example were to evolve I might choose to separate the Execute() method into ProcessGetRequest() and ProcessPostRequest().

The IPageController type inherits IHttpHandler in order to register this page controller with ASP.NET to receive all requests for a particular path. In this case this handler is registered in the web.config for all requests to the “DisplayAllCustomers.aspx” page.

      type="PlayingWithPageControllers.Web.Controllers.DisplayAllCustomersController, PlayingWithPageControllers"/>

If I wanted to get a little more nitty, gritty I could have specified only GET Http method’s are handled by this handler.

Moving on…. The PageController base type for all controllers looks like:

    public abstract class PageController : IPageController {
        public void ProcessRequest( HttpContext context ) {
            Execute( );

        public bool IsReusable {
            get { return true; }

        public abstract void Execute();

And finally our “DisplayAllCustomersController” component looks like:

    public class DisplayAllCustomersController : PageController, IDisplayAllCustomersController {
        public DisplayAllCustomersController( IDisplayAllCustomersView view, ICustomerTasks tasks ) {
            _view = view;
            _tasks = tasks;

        public override void Execute() {
            _view.AddToBag( _tasks.AllCustomers( ) );
            _view.Render( );

        private readonly IDisplayAllCustomersView _view;
        private readonly ICustomerTasks _tasks;

And voila all requests to “DisplayAllCustomers.aspx” are handled by the DisplayAllCustomersController which pulls information from the model and fires it off to the template view to be rendered.

The Template View Pattern is defined as:

“Renders information into HTML by embedding markers in an HTML page.” - PoEAA

Our template view for “AllCustomers.aspx” looks like this:

      <td>First Name:</td>
      <td>Last Name:</td>
    <% foreach ( DisplayCustomerDTO dto in ViewBagLocator.For( ViewBagKeys.DisplayCustomers ) ) { %>
      <td><%= dto.FirstName() %></td>
      <td><%= dto.LastName( ) %></td>
    <% } %>

This separates the Template view from having any knowledge of the Page Controller. The Page controller has the responsibility of pulling information from the model to pass along to the appropriate view.

All access to the current context and the ASP.NET pipeline has been isolated to the HttpGateway which abstracts the ASP.NET facilities available through a condensed client interface.

public interface IHttpGateway {
  void RedirectTo( IView view );
  void AddItemWith< T >( IViewBagKey< T > key, T itemToAddToBag );
  T FindItemFor< T >( IViewBagKey< T > key );

Now that I think about it, another step that I could have taken would have to shield the page controllers from having any knowledge of “IHttpHandler”, which would have further isolated the ASP.NET infrastructure from the rest of the web presentation layer.

Patterns of Enterprise Application Architecture defines a Gateway as:

“An object that encapsulates access to an external system or resource.” - PoEAA

    public class HttpGateway : IHttpGateway {
        public HttpGateway( IHttpContext context ) {
            _context = context;

        public void RedirectTo( IView view ) {
            _context.Server.Transfer( view.Path( ) );

        public void AddItemWith< T >( IViewBagKey< T > key, T itemToAddToBag ) {
            _context.Items.Add( key, itemToAddToBag );

        public T FindItemFor< T >( IViewBagKey< T > key ) {
            return ( T )_context.Items[ key ];

        private readonly IHttpContext _context;

If you haven’t already you should go buy and read, then re-read, then re-read “Patterns of Enterprise Application Architecture by Martin Fowler”

Patterns of Enterprise Application Architecture (The Addison-Wesley Signature Series)

by Martin Fowler Read more about this title…


csharp designpatterns