473,414 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

question about the "Page Controller" pattern

In reading some documents from the Patterns and Practices group, I had
a question about the example given for the Page Controller pattern.
(I have posted the code for the BasePage below)
In short, their example shows a "BasePage " class for other pages to
inherit from.

The subclasses are supposed to implement the PageLoadEvent method
(which I guess should really be abstract, but can't because asp.net
won't let you inherit from a base page that is an abstract class).

Now, my question is this. I have seen many applications where the base
page, and the inheriting pages BOTH subscribe to the Load event, w/ the
Base class's event handler firing first, and then the subclasses Load
event handler.

In execution, it would work the same as what is described here, with
the only diff being that Page Controller would be intiating the Load
event on the page, rather than ASP.NET initiating the event.
Can someone explain why this matters? I guess if you were passing some
kind of information from the Page Controller to the sub class on the
PageLoadEvent method, but if you are doing just to "kick off" an event
that is already going to be kicked off by ASP.NET, what is the point?
Thanks...
Here is the Page Controller class:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public class BasePage : Page
{
protected Label eMail;
protected Label siteName;

virtual protected void PageLoadEvent(object sender, System.EventArgs e)
{}

protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string name = Context.User.Identity.Name;
eMail.Text = DatabaseGateway.RetrieveAddress(name);
siteName.Text = "Micro-site";
PageLoadEvent(sender, e);
}
}

}

Nov 19 '05 #1
3 1753
cmay wrote:
In reading some documents from the Patterns and Practices group, I had
a question about the example given for the Page Controller pattern.
(I have posted the code for the BasePage below)
In short, their example shows a "BasePage " class for other pages to
inherit from.

The subclasses are supposed to implement the PageLoadEvent method
(which I guess should really be abstract, but can't because asp.net
won't let you inherit from a base page that is an abstract class).

Now, my question is this. I have seen many applications where the
base page, and the inheriting pages BOTH subscribe to the Load event,
w/ the Base class's event handler firing first, and then the
subclasses Load event handler.

In execution, it would work the same as what is described here, with
the only diff being that Page Controller would be intiating the Load
event on the page, rather than ASP.NET initiating the event.
Can someone explain why this matters? I guess if you were passing
some kind of information from the Page Controller to the sub class on
the PageLoadEvent method, but if you are doing just to "kick off" an
event that is already going to be kicked off by ASP.NET, what is the
point?


One reason why a base class and a derived class would subscribe to an
event is that both need to handle it without needing to know
a) that the other one handles it
and
b) how the other one handles it.
Thus there's never a requirement for a derived class to call the base
class implementation, which you must do for many framework event
handling methods (On<Event>).

But overriding methods is a dangerous thing -- you need to make sure to
fullfil the base class's expectations -- either it wants you to call
its own implementation, or it doesn't care, and in the first case you
must amke sure to call it at the correct point in time (start of
method, end of method, anywhere).

And there's no language construct to enforce either behaviour -- it's
purely driven by documentation.

This brings us to your code sample -- PageLoadEvent is a Template
Method, an extension point of a base class for modifying behaviour as a
child class sees fit.

In your BasePage example, the event handling aspect and the Template
Method server practically the same purpose, so I don't see any reason
to use both approaches at the same time.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #2
Thanks for the reply.

I understand that you would not use both methods, but guess I am
questioning the reason for presenting this pattern for asp.net
development. (Why MS Patterns and Practices like this pattern).

It seems like you don't "gain" anything over simply using a custom base
class that doesn't bother calling a PageLoadEvent method, letting the
ASP.NET process raise that event.

Also, if you were to use the Page Controller method how could you have
an inheritance tree of more than 2 items deep? If each Page Controller
assumes that the class inheriting from it will implement the
PageLoadEvent method, wouldn't you hit a road block if you tried to
have 3 classes?

e.g. If you just build rely on ASP.NET to fire the PageLoad and catch
that at each level, then you could have:
BasePage (PageLoad does generic stuff)
SpecificFolderBasePage (PageLoad does stuff specific for its section)
EndPage (PageLoad Sets up the page)

but if you tried to do this same thing with the Page Controller, I
don't see how you could do it. In the SpecificFolderBasePage, you
would need a method PageLoadEvent to contain logic for when it is
called by BasePage, and also expect EndPage's PageLoadEvent to be fired
later, which wouldn't happen.

What I am trying to say is "SpecificFolderBasePage" and "EndPage" can't
both declare a method called PageLoadEvent have have the execution
propagate down the tree like you would need it to.

Nov 19 '05 #3
cmay wrote:
Thanks for the reply.

I understand that you would not use both methods, but guess I am
questioning the reason for presenting this pattern for asp.net
development. (Why MS Patterns and Practices like this pattern).

It seems like you don't "gain" anything over simply using a custom
base class that doesn't bother calling a PageLoadEvent method,
letting the ASP.NET process raise that event.
The sample code is somewhat contrived. They implement a base class
version of Page_Load to provide the common page header -- that's why
Template Method is used, to provide a hook for derived classes to add
their own logic without accidentally removing the page header logic.
Also, if you were to use the Page Controller method how could you have
an inheritance tree of more than 2 items deep? If each Page
Controller assumes that the class inheriting from it will implement
the PageLoadEvent method, wouldn't you hit a road block if you tried
to have 3 classes?
Yep, this may bring trouble -- you need to thoroughly document who does
what with which side-effects, or provide yet another template method...

The point here though is that deep inheritance trees are evil anyway ;-)
e.g. If you just build rely on ASP.NET to fire the PageLoad and catch
that at each level, then you could have:
BasePage (PageLoad does generic stuff)
SpecificFolderBasePage (PageLoad does stuff specific for its section)
EndPage (PageLoad Sets up the page)

but if you tried to do this same thing with the Page Controller, I
don't see how you could do it. In the SpecificFolderBasePage, you
would need a method PageLoadEvent to contain logic for when it is
called by BasePage, and also expect EndPage's PageLoadEvent to be
fired later, which wouldn't happen.

What I am trying to say is "SpecificFolderBasePage" and "EndPage"
can't both declare a method called PageLoadEvent have have the
execution propagate down the tree like you would need it to.


They can, but this leaves with you with the original problem: Do you
need to call the base class's implementation? In this situation,
Strategy is a more useful Design Pattern.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: The Bicycling Guitarist | last post by:
I've used and loved the webmaster tools at Delorie.com for some years now. I just added some more text content to my home page, but in the web page "purifier" the text doesn't render as I thought...
12
by: Lee David | last post by:
How do you track down this to where the error actually is? TIA, Lee
5
by: Diffident | last post by:
Hello All, I have written a webform which is by default derived from "Page" class. I have coded another utility class with few methods and an object of this class is instantiated from the webfom...
3
by: nan | last post by:
Hi All, I am trying to connect the Database which is installed in AS400 using DB2 Client Version 8 in Windows box. First i created the Catalog, then when i selected the connection type...
2
by: Paul Wake | last post by:
Another CSS newbie question: What would be the best / most-correct-according-to-standards way to get the two photos on http://www.xmission.com/~wake/test.html to set just at the left and right...
33
by: STILL LEARNING | last post by:
I'm not sure if this can even be done, but what prompts the question is my desire to be able to create an "Uber Link" script/code of some sort, such that even if the html page contains nothing but...
4
by: lander | last post by:
I've read the page life cycle thing in msdn, still, i'm getting a bit confused of thinking how all the things are going under the hood... I know that when page loading, that the controls'...
6
by: James Yang | last post by:
"bad page", especially in table, is a risk for database maintenance. The particular access attempt for corrupt blocks may not occur often, and corrupt DB2 blocks are not recognized during a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.