views:

303

answers:

4

In my pre-ASP.NET development environment, there was a near-universal best practice:

* NEVER use the native controls!
* Instead, subclass ALL the controls, and ALWAYS use the subclassed version.

Why? Because that gave you a hook... one place to write code and have it applied throughout your application.

For example: Suppose you decide that you want a question mark icon to appear to the right of every TextBox in your webforms app. The icon is rendered, and hovering over it pops up bubble help -- iff there is text in the TextBox.ToolTip property.

How would you accomplish that, if you're using the MS-provided TextBox control?

If you consistently used a subclassed version of TextBox in your application, then you could go to that object, and add the method that renders the icon, stocked with your favorite bubblehelp javascript.

Presto! All of your app's TextBoxes sprout little question mark icons -- or they will, when you set their ToolTip text.

Over time, you can easily adapt and enhance ALL your TextBoxes, because they all have a base class that you can modify. You add a feature where ToolTips are set from a resource file. Next, you add a ShowOnLeft property that presents the icon on the left side of the TextBox. Do you like how the iPhone password control shows the last character you type, obscuring the earlier characters? Override your subclassed TextBox's default behavior for passwords with a method to implement that behavior.

I have never encountered advocates for this practice, in ASP.NET. Have I just missed it? An article describing two dozen ASP.NET design patterns doesn't have anything related. The posts about how to subclass server controls describe special-purpose one-offs, like a TextBox that only accepts digits -- but none of them recommend the pervasive "ALWAYS use subclassed controls!" policy that I subscribed to in olden days.

Does it make sense, to apply this ancient wisdom, when working in ASP.NET? To always use the subclassed equivalent of the native server controls?

If not -- why not? Are there other ways to skin this cat? A technique that provides you with just one place where you can augment ALL your application's instances of a given control?

I'd love to hear about that. I want my TextBoxQMark control. :-)

TIA - Hoytster

+2  A: 

In theory, it's a good idea. In practice, I'm too lazy to create the subclasses most of the time, and it's not often that I have to go back and add them.

Joel Coehoorn
+2  A: 

It's nice in theory, but how often do you need these subclasses and how hard is it to derive the class and search and replace the necessary variables. I'd guess that the "clutter" caused by all the unused subclasses would unnecessarily "complicate" the project.

Better to add a user control with a textbox and an image or what-ever-control-combination-you-need-than-to-clutter-up-your-project.

Do you also do this for your WinForms projects? (I've seen it done, rarely, and it doesn't seem to add the value that it claims.)

> how often do you need these subclassesYou'd be surprised. If you have a subclass - you find opportunities to use it. When a new release had the defect that center-justified date controls would GPF -- I made all the date controls left-justified in the subclass's constructor -- and perhaps 100 date controls started working again. 10 minutes work.> I'd guess that the "clutter" caused by all the unused subclasses would unnecessarily "complicate" the project.I would (will) replace TextBox with ZTextBox, and remove TextBox from the VS inventory. No more clutter; exactly the same amount.
hoytster
@hoytster - That's not the clutter I was referring to. The clutter being your ZTextBox ;) "Center Justified Date Control would GPF"... hrm have any proof that this actuall occurred? Point being that what you are doing is *making you appear superior to yourself* without adding value to the project. Must be the group think at your workplace.
Hey, Roygbw. I suppose you're skeptical because you think you would have heard of such a thing. It was a circa 2000 point release of PowerBuilder -- pretty obscure, if you're not in that technology. As for making myself appear superior: *I wish*. I'm a former PowerBuilder guru working hard to climb the .NET learning curve. I have a long way to go, and the subject seems to expand faster than I can learn. Not feeling very superior. :)
hoytster
A: 

I have used this concept extensively in my application and the only drawback i found is

1) sometimes it happens that you are having too many if/else condition in your base class which are required for only 5 % cases but they are affecting 100 % case

2) Code can become complex and difficult to understand for developer, so it is difficult to add new developer in your project

So, it is good idea but in practice it is difficult to implement in a good manner

Harryboy
Re 1: I cannot believe that a few if-then clauses would have any impact on performance. You'd have to do 1 billion iterations to get something measurable.Re 2: Valid problem. If ZTextBox has a ShowOnLeft property, then when the noob Googles -- only *this* page will hit. On the other hand, the source is readily available, unlike MS's server controls. Add the control project to the solution, and "Go To Implementation" takes you to the well-commented code.
hoytster
It is not only if then clauses but if our control works on configuration setting then in that if then clauses it requires costly operations like database connection or service calls which makes the processing slower
Harryboy
You might just want to cache configuration settings like that up front regardless of whether or not you're using this pattern. If it makes a marked increase in performance, it's worth it.
aehiilrs
A: 

Most of the time, subclasses aren't needed.

You can use tag mapping or control adapters instead to accomplish many types of customizations.

In case it helps, I discuss both approaches in my book, along with sample code: Ultra-Fast ASP.NET.

RickNZ
I checked out the book at Barnes and Noble this weekend. It was stunning to see how many performance-boosting things I'd never heard of. In my quick look, I didn't see the tag mapping part, and the bit about control adapters was light; I probably failed to find the right parts. Thanks for the leads.
hoytster
Glad to hear that you found the book. The tag mapping info (aka "tag transforms") is on page 247. I discuss control adapters in two places: once right after tag mapping, and also starting on page 216.
RickNZ