The Illusion of Strong Typed Xml with Boo

One interesting feature of boo is the ability to do dynamic typing. To do dynamic typing in boo you simply have to declare a variable as type “duck”, which is a reference to Ruby’s duck typing concept (“if it walks like a duck and quacks like a duck… then it must be a duck!”). The fact that they named the type “duck” in boo reveals the light hearted and humorous style that went into the creation of boo. I think it makes programming feel a little more casual and fun, boo is not a very self important language.

So while messing around with dynamic typing a bit I decided to come up with a simple example of how to use dynamic typing to read XML with a strongly typed style. Here is the main code for the example application I have written:
namespace Boo1
 
import System
import System.Collections
import System.Xml
 
doc as XmlDocument = XmlDocument()
doc.LoadXml(“<root><child1 att=\”hello\” /><child2 att=\”world!\” /></root>”)
 
xml as duck = XmlDocumentFu(doc)
hello = xml.root.child1.att.Value
world = xml.root.child2.att.Value
 
print ${hello} ${world}
 
print “Press any key to continue . . . “
Console.ReadKey(true)
 
·         Download: Source Code [rar]
Below you can see I have passed the XmlDocument into a class called XmlDocumentFu. This is a custom class I have created that implements the interface IQuackFu. In boo if you make a call to a method or property on an object that implements IQuackFu that does not exist, it will allow you to intercept the failed call and handle it yourself. In this case our XmlDocumentFu object will intercept failed calls to the root property first and attempt to translate that call into a one that iterates through the ChildNodes and finds a node of the name of the property instead.
This allows us to access elements in our XML as if they were properties on a strongly typed object! Pretty sweet! You should note however that in order to do this I had to declare my xml object as type “duck”. This enables duck typing for that object and as a result you do not get any intellisense support at design time. So it’s a bit of a tradeoff, what is more important to you explicit intellisense supported code or brevity? With this style you can kiss goodbye all of the various looping you might have done otherwise and encapsulate it in a few simple classes.
namespace Boo1
 
import System
import System.Reflection
import Boo.Lang
import System.Xml
 
class XmlDocumentFu(IQuackFu):
      
       _document as XmlDocument
      
       def constructor(document as XmlDocument):
              _document = document
      
       def QuackInvoke(name as string, o as (object)) as object:
              return null
             
       def QuackGet(name as string, params as (object)) as object:
              node as XmlNode                                
              for child as XmlNode in _document.ChildNodes:
                     if child.Name == name:
                           node = child
                           break
             
              if node is not null:
                     return XmlNodeFu(node)
                    
              ret as duck #this is type duck so you can call indexers optionally below
              prop as PropertyInfo = _document.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.Public)
              if prop is not null:
                     ret = prop.GetValue(_document, null)
                    
              if params is not null:
                     for p in params:
                           ret = ret[p]
                          
              return ret
             
       def QuackSet(name as string, o as (object), value as object) as object:
              return null
             
class XmlNodeFu(IQuackFu):
      
       _node as XmlNode
      
       def constructor(node as XmlNode):
              _node = node
             
       def QuackInvoke(name as string, o as (object)) as object:
              return null
             
       def QuackGet(name as string, params as (object)) as object:
              node as XmlNode
              for child as XmlNode in _node.ChildNodes:
                     if child.Name == name:
                           node = child
                           break
                                        
              if node is not null:
                     return XmlNodeFu(node)
                    
              attribute as XmlAttribute
              for att as XmlAttribute in _node.Attributes:
                     if att.Name == name:
                           attribute = att
                           break
                          
              if attribute is not null:
                     return attribute
                    
              ret as duck
              prop as PropertyInfo = _node.GetType().GetProperty(name, BindingFlags.Instance | BindingFlags.Public)
              if prop is not null:
                     ret = prop.GetValue(_node, null)
                    
              if params is not null:
                     for p in params:
                           ret = ret[p]
                          
              return null
             
       def QuackSet(name as string, params as (object), value as object) as object:
              return null