Abstracting from ASP.NET pages – level 300

Some of the common things we do in ASP.NET v1.1 is set configuration information explicitly to our aspx’s.  We set the title, the template we are using, the theme (maybe css), the navigation (links), the roles that are allowed access to our page, maybe some custom information for that page, and then think of some other information you “hard-code” in your page.  To abstract this away and make my pages more configurable without changing code, I’ve developed an interface that my base page class implements.  The definition of the interface is as follows:

 public interface IPageConfig
  string this[string sKey]{get;set;}
  string Title{get;set;}
  string VirtualPath{get;set;}
  string Template{get;set;}
  string Theme{get;set;}
  string[] Roles{get;set;}

  bool IsRoot{get;}
  IPageConfig Parent{get;}
  IPageConfig[] Siblings{get;}
  IPageConfig[] Children{get;}
  string[] ChildrenNames{get;set;}

  NameValueCollection Attributes{get;}
  NavigationUrlCollection Navigation{get;}


Then I implemented a custom object to read information from my web.config file.  In my properties that implement this interface, I can access this helper object to retrieve the configuration items from my web.config: title, template, theme, etc.  It is really powerful and allows me more control over my site through my configuration file instead of a lot of repetitive code changes.

You don’t have to use the web.config right off to implement this interface, just implement this interface in your base page and make the properties return the value you want to use for that page.  This allows for abstraction later on when you find you need it.  If you find you don’t need it, you have some standardized locations to set setting for that page.

I’ve learned something the hard way with .Net:  “Always code to interfaces.”