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

3 Comments

  • 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.

  • 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 &quot;nice composite control features&quot; 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 &quot;rendering&quot; approach could end up being a maintenance nightmare sooner or later for any complex control.


    Thanks for your commments!!

  • 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.

Comments have been disabled for this content.