CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Glenn Block


XML and Fluent Interfaces

I have posted several times in the past on Fluent Interfaces. I think about Fluent Interfaces much in the same way I think about recursive functions, that is they are not for everything, but when you find the situation that warrants them then they are a great fit. On the other hand when you try to use them for the wrong situations they are an awful fit.

In the past, I've found at various orgs that i am constantly creating an XmlUtils() class to handle the creation of documents in a more tighter syntax. Back in those days, I was unaware of Fluent Interfaces and how they could possibly tackle this problem

Well today I just came across Mark Resmussen's sweet FI for defining an XmlDocument, and it looks like a great fit.

XML we are trying to create

<?xml version="1.0" encoding="utf-8"?>
<root>
    <result type="boolean">true</result>
</root>

Using System.Xml

XmlDocument xd = new XmlDocument();
xd.AppendChild(xd.CreateXmlDeclaration("1.0", "utf-8", ""));

XmlNode root = xd.CreateElement("root");
xd.AppendChild(root);

XmlNode result = xd.CreateElement("result");
result.InnerText = "true";

XmlAttribute type = xd.CreateAttribute("type");
type.Value = "boolean";

result.Attributes.Append(type);
root.AppendChild(result);

New way using Mark's FI

XmlOutput xo = new XmlOutput()
    .XmlDeclaration()
    .Node("root").Within()
        .Node("result").Attribute("type", "boolean").InnerText("true");

ANY QUESTIONS?



Comments

Rob said:

Also, note that you can do something very similar to this with XDocument:

msdn2.microsoft.com/.../system.xml.linq.xdocument.aspx

# February 26, 2008 11:57 AM

Bil Simser said:

While this might be fluent in look, it's not fluent in feel to me. I still need to know things like InnerText and attributes and nodes. I think the approach is interesting, but needs work to make it really friction-free (without making it like crazy like batshit XML embedded inside VB that I really don't understand or ever want to write).

# February 26, 2008 1:02 PM

Sergio Pereira said:

I think in a not too distant future, we will look back at these things and be less than proud of them. There's something about fluent interfaces that just doesn't feel right to me, and it's not just lack of habit. Time will tell.

# February 26, 2008 1:03 PM

ehaskins said:

VB

Dim XDocument = <?xml version="1.0" encoding="utf-8"?>

<root>

   <result type="boolean">true</result>

</root>

ANY QUESTIONS?

# February 26, 2008 1:57 PM

Steve said:

I'm partial to LINQ to XML :)

var xml = new XDocument(

                  new XDeclaration("1.0", "utf-8", "yes"),

                  new XElement("root",

                    new XElement("result", new XAttribute("type", "boolean"), "true")

                  )

               );

# February 26, 2008 2:08 PM

ISerializable - Roy Osherove's Blog said:

Oh, next time I'm going to generate XML, remind me to use Mark Resmussen's ultra cool XML Fluent Interface

# February 26, 2008 2:31 PM

Glenn Block said:

@Rob, it's similar but different. Actually if you follow the post there's an example of using the XDocument syntax.

# February 26, 2008 3:38 PM

Glenn Block said:

@Bil, I see your point, though the interface can certainly be expanded. Also don't forget about all the extensions tricks we can do :)

# February 26, 2008 3:39 PM

Bil Simser said:

@Glenn: Totally, extension methods help us get a way better FI that makes the code much more readable. I'm looking at something like this for dealing with the SharePoint OM now and going to really push the readability of dealing with it for setting up tests (much like the Fluent Fixtures project does). Stay tuned for that as I'm in monster blog-mode lately.

# February 26, 2008 4:49 PM

Glenn Block said:

@Bil, my suggestion was a bit of tongue-and-cheek. I like extension methods, but it's a double-edge sword.

# February 26, 2008 5:54 PM

Eduardo Miranda said:

I'm 100% in favor of LINQ to Xml and XDocument API.  It express the developer intention in a clear, readable way. In VB.Net it's even better, I don't use VB for a while, but after diving into the new XML features, I'm almost like VB again :)

# February 26, 2008 6:40 PM

Marcos Silva Pereira said:

Nice, but Steve post a more readable solution. I alread have done something like that using Java (sorry :-P):

Document doc = document("root",

node("some",

node("inner1"),

node("inner2")

),

node("more"),

node("evenmore",

attribute("name", "John"),

attribute("age", "28")

));

# February 26, 2008 10:48 PM

Ken Egozi said:

I'd favor text templating (after all, XML is text):

www.kenegozi.com/.../generating-xml-do-we-really-another-api.aspx

# February 27, 2008 5:39 AM

Jon said:

Have you seen the XML thing Andres Noras did with VB? I think it is pretty cool: andersnoras.com/.../is-it-a-bird-is-it-a-plane-it-s-a-dsl.aspx

Scroll down untill you find the xml builder stuff...

# February 27, 2008 6:36 AM

Tobin Harris said:

I like it! The fluent interface approach certainly simplifies XML building tasks I think, much faster and more readable than working directly with the object model.

I think that c# adn VB.NET will continue evolve to allow even simpler and more understandable syntax. This RubyOnRails code looks pretty nice I think...

xml.customers {

 xml.customer( :id=>01) {

   xml.name("Fred")

   xml.age( 21 )

   xml.description("blah")

 }

}

<customers>

 <customer id="01">

    <name>Fred</name>

    <age>21</age>

    <description>Blah</description>

  </customer>

</customers>

# February 27, 2008 6:52 AM

Christopher Steen said:

Link Listing - February 26, 2008

# February 27, 2008 8:22 AM

Christopher Steen said:

Sharepoint Using NAnt to Build SharePoint Solutions [Via: Bil Simser ] WPF Drag drop library updated...

# February 27, 2008 8:22 AM

rascunho » Blog Archive » links for 2008-02-27 said:

Pingback from  rascunho  &raquo; Blog Archive   &raquo; links for 2008-02-27

# February 27, 2008 3:24 PM

Sergey Shishkin: Fluent XML Writer said:

Pingback

# February 28, 2008 5:44 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Glenn Block

Glenn Block is the Technical Product Planner for the Client UX program at patterns & practices. As Product Planner he is responsible for driving the vision and creation of p&p client deliverables including the Web Client and Smart Client software factories. Prior to joining Microsoft, Glenn has lived in various roles being "in the trenches" with developers, including being responsible for the overall architecture and technology direction. He has worked in both large and small organizations building enterprise systems for financial services, manufacturing, and print & mail on multiple platforms including .NET and Java. His technology passions are in software frameworks, architecture and systems integration. He resides with his wife and 3 year old daughter in Seattle (his other passion). Check out Devlicio.us!

This Blog

Syndication

News

View Glenn Block's profile on LinkedIn

Me