When NOT to choose composition: an ASP.NET forums example
Note: this entry has moved.
Most people starting to develop custom controls have problems figuring out when to go with a full-blown composite control and when to go with a rendering approach. It may take some time until you fully realize the pros & cons of each approach.
It happens that I found a composite control in the ASP.NET Forums application that is a great example on when NOT to code a composite control. Don’t take me wrong. The ASP.NET Forums application is a great application, with lots of useful features and more to come. It’s just the fact its source code is influencing so many beginners that lead me to believe that such control is not serving as a good example.
The culprit is the AlphaPicker control. In case you don’t know it already, it’s a very simple control whose purpose is to display A to Z letters plus an “All” entry as links to ease navigation. Let’s take a look at its CreateChildControls method:
protected override void CreateChildControls()
{
Label label;
// Add the series of linkbuttons
char chrStart = 'A';
char chrStop = 'Z';
// Loop through all the characters
for (int iLoop = chrStart; iLoop <= chrStop; iLoop++)
{
Controls.Add(CreateLetteredLinkButton(((char) iLoop).ToString()));
label = new Label();
label.CssClass = "normalTextSmallBold";
label.Text = " | ";
Controls.Add(label);
}
Controls.Add(CreateLetteredLinkButton("All"));
}
Notice how two child controls get created for every letter; a LinkButton (through CreateLetteredLinkButton) and a Label with the “ | “ text serving as a letter separator. Also one more LinkButton is created for the “All” entry. This causes the simple AlphaPicker control to end up having 53 child controls. No, it’s not a typo. Fifty-three full-blown controls, that must be created, initialized, consume memory, etc.
Due to AlphaPicker extremely simple nature, you could code exactly the same control without the need to create a single child control -- just using rendering.
I made a post about a year ago in the ASP.NET Forums regarding this same topic and received a response from the guy in charge of the source code (can’t provide a link to it as the search is not working right now), he accepted the comments and told me he will be considering rewriting the control. A year later, AlphaPicker remains untouched, but hey!... it’s never too late! J