Glenn Block

Sponsors

The Lounge

News

  • View Glenn Block's profile on LinkedIn

    Me

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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?


Posted Tue, Feb 26 2008 7:41 AM by Glenn Block

[Advertisement]

Comments

Rob wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 11:57 AM

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

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

Bil Simser wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 1:02 PM

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

Sergio Pereira wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 1:03 PM

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.

ehaskins wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 1:57 PM

VB

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

<root>

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

</root>

ANY QUESTIONS?

Steve wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 2:08 PM

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")

                  )

               );

ISerializable - Roy Osherove's Blog wrote Fluent XML Library - Ultra cool, short and weet
on Tue, Feb 26 2008 2:31 PM

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

Glenn Block wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 3:38 PM

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

Glenn Block wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 3:39 PM

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

Bil Simser wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 4:49 PM

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

Glenn Block wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 5:54 PM

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

Eduardo Miranda wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 6:40 PM

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 :)

Marcos Silva Pereira wrote re: XML and Fluent Interfaces
on Tue, Feb 26 2008 10:48 PM

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")

));

Ken Egozi wrote re: XML and Fluent Interfaces
on Wed, Feb 27 2008 5:39 AM

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

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

Jon wrote re: XML and Fluent Interfaces
on Wed, Feb 27 2008 6:36 AM

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

Tobin Harris wrote re: XML and Fluent Interfaces
on Wed, Feb 27 2008 6:52 AM

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>

Christopher Steen wrote Link Listing - February 26, 2008
on Wed, Feb 27 2008 8:22 AM

Link Listing - February 26, 2008

Christopher Steen wrote Link Listing - February 26, 2008
on Wed, Feb 27 2008 8:22 AM

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

rascunho » Blog Archive » links for 2008-02-27 wrote rascunho &raquo; Blog Archive &raquo; links for 2008-02-27
on Wed, Feb 27 2008 3:24 PM

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

Sergey Shishkin: Fluent XML Writer wrote re: XML and Fluent Interfaces
on Thu, Feb 28 2008 5:44 PM

Pingback

Add a Comment

(required)  
(optional)
(required)  
Remember Me?