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

Published Monday, May 19, 2003 12:52 AM by vga
Filed under:

Comments

# re: When NOT to choose composition: an ASP.NET forums example

Sunday, May 18, 2003 7:12 PM by Jesse Ezell
This isn't really a good reason not to use composite controls. If you are really concerned about creating 50 objects (which is actually not a lot if you really think about how many objects get initialized, created, and destroyed before your page has completed rendering even if it is just a vanilla web form), then you could just combine the output and use a LiteralControl. In this way, you still get the nice composite control features without the extra overhead. The problem with just using a custom render method is that if you have to augment the functionality of your control some time down the road and want to use a third party component or something, you are probably going to end up back with all your code in the CreateChildControls method anyway, since you would have to create all sorts of hacks to get it working otherwise.

# re: When NOT to choose composition: an ASP.NET forums example

Tuesday, May 20, 2003 8:47 AM by vga
Hi Jesse, as you can tell from my post I disagree :) Here I'm addressing specifically the AlphaPicker control and the fact that I dont need any "nice composite control features" for it. It is such a simple control that you can very easily rewrite it into a better control.
I know lots of objects get created by Page but that's not reason enough (at least not for me) to freely create 53 child controls that could be easily avoided.
Lastly, keep in mind that I was referring specifically to AlphaPicker, I know that the "rendering" approach could end up being a maintenance nightmare sooner or later for any complex control.
Thanks for your commments!!

# re: When NOT to choose composition: an ASP.NET forums example

Monday, May 26, 2003 10:56 AM by Andrew
I think the issue here is specific.
A control that presents the same content to everyone time after time should not create a whole bunch of child controls every time.

It should be one control that caches it's html output, eliminating the redudency and keeping the abstraction (of a control) allows you to make arbitrary changes later.

This control like a lot of MS's sample code illustrates what you can do and not neccesarily what you should do.

# re: When NOT to choose composition: an ASP.NET forums example

Friday, December 05, 2008 7:09 PM by Semil

<a href= spiritez.com ></a>

# re: When NOT to choose composition: an ASP.NET forums example

Saturday, February 28, 2009 10:54 PM by jyner_xq

<a href= http://adultromancefinder.com >dating</a>

# re: When NOT to choose composition: an ASP.NET forums example

Tuesday, March 10, 2009 3:20 PM by ...

Dies ist ein gro�er Ort. Ich m�chte hier noch einmal.

# re: When NOT to choose composition: an ASP.NET forums example

Wednesday, April 22, 2009 6:06 PM by Mery-kf

<a href= <div align=center><h3>Error. Page cannot be displayed. Please contact service provider for more details.</h3></div> ></a>

# re: When NOT to choose composition: an ASP.NET forums example

Wednesday, April 29, 2009 6:35 PM by Mery-bq

<a href= <div align=center><h3>Error. Page cannot be displayed. Please contact service provider for more details.</h3></div> ></a>

# re: When NOT to choose composition: an ASP.NET forums example

Friday, March 26, 2010 1:04 PM by Freda

Hey. When they discover the center of the universe, a lot of people will be disappointed to discover they are not it. Help me! Please help find sites for: Alavert package insert. I found only this - <a href="genericalavert.info/.../">alavert contain</a>. Alavert, the keyword tree philosophy stews the most therewe suffered self boomerangs and how they are versed now. We have further hampered in the running teeth with our natural discovery spouse notion, and get then to supplement the antidote which is resulted even, alavert. Best regards ;-), Freda from Grenada.

Leave a Comment

(required) 
(required) 
(optional)
(required)