views:

765

answers:

3

I've got a site with two master pages: one for one-column layout and one for two-column layout. These are nested within a master page that provides a common header and footer.

I have functionality that I want each of the content pages to have; should I:

  • create a page base class and inherit that inside my content pages, or
  • create a master page base class and inherit that inside one of my levels of nested master page?

Ultimately I want the content pages to have access to a connection object and to a configuration object that I want to be instantiated as each page loads.

+1  A: 

Ultimately I want the content pages to have access to a connection object and to a configuration object that I want to be instantiated as each page loads.

Base page is a prime target for this.

Chris
+2  A: 

Master pages should only be used for layouts in my opinion.

If you want to be doing work such as creating connections, do that in a base class.

However, it is important that you not create a database connection at the start of your page, and close it at the end. You should be opening and closing connections as you run individual queries. This allows connection pooling to work efficiently.

Additionally, I would not be putting a connection of any type in the page itself, as you want to separate your functionality as much as possible from the layout.

Jason Coyne
A: 

I typically use a base page that contains a number of features that ease the creation of content pages. See http://dotnetslackers.com/articles/aspnet/Four-Helpful-Features-to-Add-to-Your-Base-Page-Class.aspx for some examples of useful BasePage features.

Rob Decker
So you wouldn't put this code in the master pages at all, but instead derive the content pages from this type instead of System.Web.UI.Page?
Caveatrob
Exactly, and if your MasterPage has any User Controls and they need some of shared functionality as well, then you can also create a BaseUserControl as well.
Rob Decker