Petzold & functional construction

Hi everyone,

Petzold expresses some discomfort with object creation via code (vs XAML) in WPF over at his blog (http://www.charlespetzold.com/blog/blog.xml, "XAML, Code, and TextBlock" and "XAML Rules (but Code Suffers)"). I was just reading the XML stuff the LINQ team is doing and am wondering whether what they call functional construction would solve that problem.

They are in the business of constructing XML element trees. Typically this looks like this (it might be slightly off, I am doing this off my head, have a look at XLinq overview document):


XElement contact =
    new XElement("contact",
       new XElement("name", "John Foo"),
       new XElement("phone", "9898989",
          new XAttribute("type", "home")
       )
    );


 

Any object that can take child objects has a constructor has a


params object[] contents
 


parameter.

If this pattern would be implemented consistently within WPF, wouldn't that solve Petzold's problem And make for an even nicer programing model than the one he proposes The grid thing could become (oh, and I am using object initialisation expressions as well here):



Grid grid = new Grid(
    new ColumnDefinition { Width = new GridLength(100, GridUnitType.Pixel) },
    new ColumnDefinition { Width = new GridLength(50, GridUnitType.Star) },
    new WhateverCanBePlacedInsideGridInXAML() );

 


The textblock could become:



TextBlock text = new TextBlock(
    "Click the ",
    new Italic("Button"),
    " to",
    new LineBreak(),
    "launch the ",
    new Bold("Rocket"));

 


Of course this does require constructors for the Inline descendents with the params parameter.

I generally feel that this might be a good way to structure the programming model from code. It would also be nice to align the XML and WPF programming models (ie the work the WPF team and the XLinq team do) so that the two APIs feel consistent.

This obviously doesn't solve his problem on how to write his book. But I feel the WPF team should take the path that ends up with being close to what other teams that build APIs do.

I don't know how to ping Mr Petzold. If anyone does I would be most interested in his opinion on this. Oh, and maybe Brad Abrams wants to chim in as well, since this seems to be a API consistency question

Cheers,
David


Answer this question

Petzold & functional construction