<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bizcoder</title>
	<atom:link href="http://www.bizcoder.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bizcoder.com</link>
	<description>Developing business applications for small businesses</description>
	<lastBuildDate>Fri, 12 Feb 2010 14:54:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Convert XML to JSON using XSLT</title>
		<link>http://www.bizcoder.com/index.php/2010/02/12/convert-xml-to-json-using-xslt/</link>
		<comments>http://www.bizcoder.com/index.php/2010/02/12/convert-xml-to-json-using-xslt/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 14:54:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2010/02/12/convert-xml-to-json-using-xslt/</guid>
		<description><![CDATA[How about that for acronym soup?&#160; In the spirit of doing smaller posts but more often, here is a handy little XSLT.&#160; 
Assuming you have the following XML, 
&#160;

&#60;Customers&#62;
    &#60;Customer Id=&#34;99&#34;&#62;
        &#60;Name&#62;Bob&#60;/Name&#62;
        &#60;Age&#62;39&#60;/Age&#62;
      [...]]]></description>
			<content:encoded><![CDATA[<p>How about that for acronym soup?&#160; In the spirit of doing smaller posts but more often, here is a handy little XSLT.&#160; </p>
<p>Assuming you have the following XML, </p>
<p>&#160;</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Customers</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Customer</span> <span class="attr">Id</span><span class="kwrd">=&quot;99&quot;</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">Name</span><span class="kwrd">&gt;</span>Bob<span class="kwrd">&lt;/</span><span class="html">Name</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">Age</span><span class="kwrd">&gt;</span>39<span class="kwrd">&lt;/</span><span class="html">Age</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">Address</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">Street</span><span class="kwrd">&gt;</span>10 Idle Lane<span class="kwrd">&lt;/</span><span class="html">Street</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">City</span><span class="kwrd">&gt;</span>Yucksville<span class="kwrd">&lt;/</span><span class="html">City</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">PostalCode</span><span class="kwrd">&gt;</span>xxxyyy<span class="kwrd">&lt;/</span><span class="html">PostalCode</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">Address</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Customers</span><span class="kwrd">&gt;</span></pre>
</div>
<p>&#160;</p>
<p>Using this XSLT </p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">=&quot;1.0&quot;</span>?<span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">xsl:stylesheet</span> <span class="attr">version</span><span class="kwrd">=&quot;1.0&quot;</span> <span class="attr">xmlns:xsl</span><span class="kwrd">=&quot;http://www.w3.org/1999/XSL/Transform&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">xsl:output</span> <span class="attr">method</span><span class="kwrd">=&quot;text&quot;</span><span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">xsl:template</span> <span class="attr">match</span><span class="kwrd">=&quot;/&quot;</span><span class="kwrd">&gt;</span>{
        <span class="kwrd">&lt;</span><span class="html">xsl:apply-templates</span> <span class="attr">select</span><span class="kwrd">=&quot;*&quot;</span><span class="kwrd">/&gt;</span>}
    <span class="kwrd">&lt;/</span><span class="html">xsl:template</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">xsl:template</span> <span class="attr">match</span><span class="kwrd">=&quot;*&quot;</span><span class="kwrd">&gt;</span>
        &quot;<span class="kwrd">&lt;</span><span class="html">xsl:value-of</span> <span class="attr">select</span><span class="kwrd">=&quot;name()&quot;</span><span class="kwrd">/&gt;</span>&quot; :
        <span class="kwrd">&lt;</span><span class="html">xsl:choose</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">xsl:when</span> <span class="attr">test</span><span class="kwrd">=&quot;not(*|@*)&quot;</span><span class="kwrd">&gt;</span>&quot;<span class="kwrd">&lt;</span><span class="html">xsl:value-of</span> <span class="attr">select</span><span class="kwrd">=&quot;.&quot;</span><span class="kwrd">/&gt;</span>&quot;<span class="kwrd">&lt;/</span><span class="html">xsl:when</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">xsl:otherwise</span><span class="kwrd">&gt;</span>{
                <span class="kwrd">&lt;</span><span class="html">xsl:apply-templates</span> <span class="attr">select</span><span class="kwrd">=&quot;@*&quot;</span><span class="kwrd">/&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">xsl:apply-templates</span> <span class="attr">select</span><span class="kwrd">=&quot;*&quot;</span><span class="kwrd">/&gt;</span>
    }<span class="kwrd">&lt;/</span><span class="html">xsl:otherwise</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">xsl:choose</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">xsl:if</span> <span class="attr">test</span><span class="kwrd">=&quot;following-sibling::*&quot;</span><span class="kwrd">&gt;</span>,<span class="kwrd">&lt;/</span><span class="html">xsl:if</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">xsl:template</span><span class="kwrd">&gt;</span>

    <span class="kwrd">&lt;</span><span class="html">xsl:template</span> <span class="attr">match</span><span class="kwrd">=&quot;@*&quot;</span><span class="kwrd">&gt;</span>&quot;<span class="kwrd">&lt;</span><span class="html">xsl:value-of</span> <span class="attr">select</span><span class="kwrd">=&quot;name()&quot;</span><span class="kwrd">/&gt;</span>&quot; : &quot;<span class="kwrd">&lt;</span><span class="html">xsl:value-of</span> <span class="attr">select</span><span class="kwrd">=&quot;.&quot;</span><span class="kwrd">/&gt;</span>&quot;,
    <span class="kwrd">&lt;/</span><span class="html">xsl:template</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">xsl:stylesheet</span><span class="kwrd">&gt;</span></pre>
</div>
<p>You can generate this JSON</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">{
    <span class="str">&quot;Customers&quot;</span> : {
        <span class="str">&quot;Customer&quot;</span> : {
            <span class="str">&quot;Id&quot;</span> : <span class="str">&quot;99&quot;</span>,
            <span class="str">&quot;Name&quot;</span> : <span class="str">&quot;Bob&quot;</span>,
            <span class="str">&quot;Age&quot;</span> : <span class="str">&quot;39&quot;</span>,
            <span class="str">&quot;Address&quot;</span> : {
                <span class="str">&quot;Street&quot;</span> : <span class="str">&quot;10 Idle Lane&quot;</span>,
                <span class="str">&quot;City&quot;</span> : <span class="str">&quot;Yucksville&quot;</span>,
                <span class="str">&quot;PostalCode&quot;</span> : <span class="str">&quot;xxxyyy&quot;</span>
            }
        }
    }
}</pre>
</div>
<p>It would be really interesting to try and use <a href="http://johannburkard.de/software/xsltjs/">this</a> Javascript XSLT processor to do the transform directly in the browser.&#160; That way any web api that generates XML could be flipped into a JSON representation for direct access in Javascript.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2010/02/12/convert-xml-to-json-using-xslt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>But can you test it?</title>
		<link>http://www.bizcoder.com/index.php/2010/02/12/but-can-you-test-it/</link>
		<comments>http://www.bizcoder.com/index.php/2010/02/12/but-can-you-test-it/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 14:13:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTTPClient]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2010/02/12/but-can-you-test-it/</guid>
		<description><![CDATA[Testing code that uses HttpWebRequest directly, is a real pain.&#160; Usually what I have seen people do is create a service interface that hides the real http client behind the interface and then create fake service implementations to actually run their tests against.&#160; The annoying part about that solution is that Http client interfaces tend [...]]]></description>
			<content:encoded><![CDATA[<p>Testing code that uses HttpWebRequest directly, is a real pain.&#160; Usually what I have seen people do is create a service interface that hides the real http client behind the interface and then create fake service implementations to actually run their tests against.&#160; The annoying part about that solution is that Http client interfaces tend to be relatively large to fake when you consider all the properties and methods of the Http request objects and http response objects.&#160; It is not inconceivable that the interface ends up exposing 90% of the functionality of the http client objects.</p>
<p>The Microsoft.Http library provides an alternative solution, assuming you don’t mind taking a dependency on it.&#160; The HttpClient class supports the notion of a pipeline of stages with one of those stages being a transport stage that actually makes the real http request.&#160; If we introduce a new stage prior to the transport stage we can intercept the request and return a response object with pre-canned values.&#160;&#160; </p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="rem">//Arrange</span>
var client = <span class="kwrd">new</span> HttpClient();
client.Stages.Add(<span class="kwrd">new</span> FixedTransport((key) =&gt; <span class="kwrd">new</span> HttpResponseMessage() {
   Content = HttpContent.Create(<span class="str">&quot;Hello World&quot;</span>)
}));

<span class="rem">//Act</span>
var  response = client.Get(<span class="str">&quot;http://example.org&quot;</span>);

<span class="rem">//Assert</span>
Assert.AreEqual(<span class="str">&quot;Hello World&quot;</span>,response.Content.ReadAsString());</pre>
</div>
<p>&#160;</p>
<p>This unit test creates a instance of the real HttpClient and adds a new processing stage called FixedTransport.&#160; This class is available in Microsoft.Http.Test and unfortunately you cannot create your own version of this class because the method that needs to be overridden to create a custom response is marked as internal.&#160; The Test dll can do it because it is pointed to by the InternalsVisibleTo attribute.&#160; The only other option is to recompile the core library yourself. <img src='http://www.bizcoder.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>The FixedTransport stage is actually derived from a caching stage that can associate a URI to particular response.&#160; In the sample above, I simply ignore the key and return the same content for all URIs.&#160; However, this mechanism would allow you to preload the cache with a set of URI/response pairs and then you could run a test that performed multiple GET requests.</p>
<p>I’m not exactly sure why but from looking through the source it does appear that if you made a non-GET request the key value would be null.&#160; That would allow you to return a specific response to a POST or PUT request based on that null key value.</p>
<p>I hope it is fairly obvious even after this very simple example that you should be able to easily generate pre-canned responses to standard http requests and still have access to the full capabilities of the HttpClient library without having to fake any classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2010/02/12/but-can-you-test-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FOR XML PATH and CQRS</title>
		<link>http://www.bizcoder.com/index.php/2010/01/18/for-xml-path-and-cqrs/</link>
		<comments>http://www.bizcoder.com/index.php/2010/01/18/for-xml-path-and-cqrs/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 13:20:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/?p=107</guid>
		<description><![CDATA[In a recent twitter exchange with Colin Jack I claimed that SELECT {…} FROM […] FOR XML PATH() is pretty much all you need for providing the Reporting side of CQRS.&#160; Ok, so maybe I overstated it a tad, but it is a valuable technique.&#160; He suggested I blog about it, so I figured even [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent twitter exchange with <a href="http://twitter.com/colin_jack">Colin Jack</a> I claimed that SELECT {…} FROM […] FOR XML PATH() is pretty much all you need for providing the Reporting side of CQRS.&#160; Ok, so maybe I overstated it a tad, but it is a valuable technique.&#160; He suggested I blog about it, so I figured even if people don’t use it for CQRS, some of the neat things you can do with FOR XML PATH may be interesting to a few people.</p>
<p>Command Query Responsibility Separation(CQRS) is an interesting architecture that is suitable for highly scalable applications with complex business logic.&#160; Instead of me doing a horrible job of trying to explain it, here are links to people who know what they are talking about.</p>
<p>Greg Young &#8211; <a href="http://codebetter.com/blogs/gregyoung/archive/2009/07/15/unshackle-your-domain.aspx">http://codebetter.com/blogs/gregyoung/archive/2009/07/15/unshackle-your-domain.aspx</a></p>
<p>UDI Dahan &#8211; <a href="http://www.udidahan.com/2009/12/09/clarified-cqrs/">http://www.udidahan.com/2009/12/09/clarified-cqrs/</a></p>
<p>The reporting side of CQRS requires a simple mechanism for getting read-only data from a server down to the presentation tier with minimal effort.&#160; I am suggesting the use of XML as a wire format and I am going to make the assumption that we have chosen a relational database as the store for the reporting database. More specifically, we require Microsoft SQL Server because, from what I can <a href="http://stackoverflow.com/questions/664815/is-there-an-equivalent-to-ms-sql-for-xml-path-in-other-database-products">find</a>, MS SQL Server is the only database engine that supports this level of flexibility when generating XML from relational tables.&#160;&#160; </p>
<p>Ideally we should be able to create any desired structure of XML based on data in our relational database.&#160; We need the flexibility to generate data as attributes or elements, create nested structures, use namespaces or not, at a minimum. </p>
<p>I’m going to start showing some of the basics of the FOR XML PATH syntax and build on it until we are creating relatively complex xml documents in a single SQL query.</p>
<p>This is pretty much the <strong>simplest query</strong> that you can create:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">SELECT</span> Code <span class="kwrd">AS</span> <span class="str">'Code'</span>,
       Name <span class="kwrd">AS</span> <span class="str">'Name'</span>
<span class="kwrd">FROM</span> tblCustomer
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Customer'</span>)</pre>
</p></div>
<p>It produces XML that looks like this:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Code</span><span class="kwrd">&gt;</span>CUSTOMER-ONE<span class="kwrd">&lt;/</span><span class="html">Code</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Name</span><span class="kwrd">&gt;</span>Hypro Networks Inc.<span class="kwrd">&lt;/</span><span class="html">Name</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Code</span><span class="kwrd">&gt;</span>CUSTOMER-A<span class="kwrd">&lt;/</span><span class="html">Code</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Name</span><span class="kwrd">&gt;</span>Customer A<span class="kwrd">&lt;/</span><span class="html">Name</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Customer</span><span class="kwrd">&gt;</span></pre>
</p></div>
<p>The only problem with this is that it is not a valid XML document as there is more than one root element.&#160; To <strong>create a proper XML document</strong>, you need to do:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">SELECT</span> Code <span class="kwrd">AS</span> <span class="str">'Code'</span>,
        Name <span class="kwrd">AS</span> <span class="str">'Name'</span>
<span class="kwrd">FROM</span> tblCustomer
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Customer'</span>), ROOT(<span class="str">'Customers'</span>)</pre>
</div>
<p>which then gives you,</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Customers</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Code</span><span class="kwrd">&gt;</span>CUSTOMER-ONE<span class="kwrd">&lt;/</span><span class="html">Code</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Name</span><span class="kwrd">&gt;</span>Hypro Networks Inc.<span class="kwrd">&lt;/</span><span class="html">Name</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Code</span><span class="kwrd">&gt;</span>CUSTOMER-A<span class="kwrd">&lt;/</span><span class="html">Code</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Name</span><span class="kwrd">&gt;</span>Customer A<span class="kwrd">&lt;/</span><span class="html">Name</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Customers</span><span class="kwrd">&gt;</span></pre>
</div>
<p>&#160;</p>
<p>These examples showed how to create data as XML elements, but sometimes it is desirable to use attributes.&#160; Attributes take quite a bit less space and suitable for most content that is small and does not contain significant whitespace.</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">SELECT</span> Code <span class="kwrd">AS</span> <span class="str">'@Code'</span>,
        Name <span class="kwrd">AS</span> <span class="str">'@Name'</span>
<span class="kwrd">FROM</span> tblCustomer
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Customer'</span>), ROOT(<span class="str">'Customers'</span>)</pre>
</div>
<p>By adding an @ symbol to the front of the column name, the data will be <strong>output as attributes</strong>:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Customers</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Customer</span> <span class="attr">Code</span><span class="kwrd">=&quot;CUSTOMER-ONE&quot;</span> <span class="attr">Name</span><span class="kwrd">=&quot;Hypro Networks Inc.&quot;</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Customer</span> <span class="attr">Code</span><span class="kwrd">=&quot;CUSTOMER-A&quot;</span> <span class="attr">Name</span><span class="kwrd">=&quot;Customer A&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Customers</span><span class="kwrd">&gt;</span></pre>
</div>
<p>One of the things that I like about using XML is that it is easy to <strong>group related pieces of data into other elements</strong>.&#160; The following query demonstrates how to do this: </p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">SELECT</span> Code <span class="kwrd">AS</span> <span class="str">'@Code'</span>,
        Name <span class="kwrd">AS</span> <span class="str">'@Name'</span>,
        Tel1 <span class="kwrd">AS</span> <span class="str">'Contact/@Tel'</span>,
        Fax1 <span class="kwrd">AS</span> <span class="str">'Contact/@Fax1'</span>
<span class="kwrd">FROM</span> tblCustomer
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Customer'</span>), ROOT(<span class="str">'Customers'</span>)</pre>
</div>
<p>Adding the slash into the column name directs SQL Server to create a <strong>new element to contain other nodes</strong>. </p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Customers</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Customer</span> <span class="attr">Code</span><span class="kwrd">=&quot;CUSTOMER-ONE&quot;</span> <span class="attr">Name</span><span class="kwrd">=&quot;Hypro Networks Inc.&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Contact</span> <span class="attr">Tel</span><span class="kwrd">=&quot;1-993-345-4146&quot;</span> <span class="attr">Fax1</span><span class="kwrd">=&quot;1-993-345-4140&quot;</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Customer</span> <span class="attr">Code</span><span class="kwrd">=&quot;CUSTOMER-A&quot;</span> <span class="attr">Name</span><span class="kwrd">=&quot;Customer A&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Contact</span> <span class="attr">Tel</span><span class="kwrd">=&quot;872-494-2000&quot;</span> <span class="attr">Fax1</span><span class="kwrd">=&quot;872-494-3008&quot;</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Customer</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Customers</span><span class="kwrd">&gt;</span></pre>
</div>
<p>Just one small warning, columns must be adjacent in the SQL query to exist under the same child element and all attributes of the parent element must be specified in the SQL query before the columns that will be in a child element.</p>
<p>So far we have only considered data from a single table.&#160; Using a simple SQL join it is easy to pull in data from other tables, but we can also create <strong>parent/child documents</strong>.</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">SELECT</span>  Code <span class="kwrd">AS</span> <span class="str">'@Code'</span>,
        Invoice_Date <span class="kwrd">AS</span> <span class="str">'@Date'</span>,
        (<span class="kwrd">SELECT</span> ii.qty <span class="kwrd">AS</span> <span class="str">'Quantity'</span>,
                <span class="kwrd">LEFT</span>(ii.description,20) <span class="kwrd">AS</span> <span class="str">'Description'</span>
         <span class="kwrd">FROM</span> tblinvoitem ii <span class="kwrd">WHERE</span> ii.invoice_id = iv.id <span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Item'</span>),type)
<span class="kwrd">FROM</span> tblInvoice iv
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Invoice'</span>), ROOT(<span class="str">'Invoices'</span>)</pre>
</div>
<p>&#160;</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Invoices</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Invoice</span> <span class="attr">Code</span><span class="kwrd">=&quot;21201     &quot;</span> <span class="attr">Date</span><span class="kwrd">=&quot;2005-05-26T00:00:00&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Item</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>1.0000<span class="kwrd">&lt;/</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Description</span><span class="kwrd">&gt;</span>To supply material a<span class="kwrd">&lt;/</span><span class="html">Description</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Item</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Invoice</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Invoice</span> <span class="attr">Code</span><span class="kwrd">=&quot;21200     &quot;</span> <span class="attr">Date</span><span class="kwrd">=&quot;2005-05-18T00:00:00&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Item</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>25.0000<span class="kwrd">&lt;/</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Description</span><span class="kwrd">&gt;</span>S.S. Control Box Cov<span class="kwrd">&lt;/</span><span class="html">Description</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Item</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Item</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>0.0000<span class="kwrd">&lt;/</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Description</span><span class="kwrd">&gt;</span>S.S. Main Control Bo<span class="kwrd">&lt;/</span><span class="html">Description</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Item</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Invoice</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Invoices</span><span class="kwrd">&gt;</span></pre>
</div>
<p>The extra “type” parameter on the end of the subquery indicates to SQL that we want to embed the contents of the sub query as XML rather than just an escaped string.</p>
<p>One limitation that you will probably run into once you create this type of document is that there is no obvious way to <strong>insert attributes and elements into the root node</strong>.&#160; This can be overcome with a small trick:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">SELECT</span> <span class="str">'1.0'</span> <span class="kwrd">AS</span> <span class="str">'@Version'</span>,
        <span class="str">'List of Invoices'</span> <span class="kwrd">AS</span> <span class="str">'Summary'</span>,
( <span class="kwrd">SELECT</span>  Code <span class="kwrd">AS</span> <span class="str">'@Code'</span>,
        Invoice_Date <span class="kwrd">AS</span> <span class="str">'@Date'</span>,
        (<span class="kwrd">SELECT</span> ii.qty <span class="kwrd">AS</span> <span class="str">'Quantity'</span>,
                <span class="kwrd">LEFT</span>(ii.description,20) <span class="kwrd">AS</span> <span class="str">'Description'</span>
         <span class="kwrd">FROM</span> tblinvoitem ii <span class="kwrd">WHERE</span> ii.invoice_id = iv.id <span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Item'</span>),type)
<span class="kwrd">FROM</span> tblInvoice iv
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Invoice'</span>),type)
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Invoices'</span>)</pre>
</div>
<p>Using an outer query you can specify the attributes and elements that you want to appear in the root.&#160; Note that there is no alias for the sub query column.&#160;
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">Invoices</span> <span class="attr">Version</span><span class="kwrd">=&quot;1.0&quot;</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Summary</span><span class="kwrd">&gt;</span>List of Invoices<span class="kwrd">&lt;/</span><span class="html">Summary</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Invoice</span> <span class="attr">Code</span><span class="kwrd">=&quot;21201     &quot;</span> <span class="attr">Date</span><span class="kwrd">=&quot;2005-05-26T00:00:00&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Item</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>1.0000<span class="kwrd">&lt;/</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Description</span><span class="kwrd">&gt;</span>To supply material a<span class="kwrd">&lt;/</span><span class="html">Description</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Item</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Invoice</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">Invoice</span> <span class="attr">Code</span><span class="kwrd">=&quot;21200     &quot;</span> <span class="attr">Date</span><span class="kwrd">=&quot;2005-05-18T00:00:00&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Item</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>25.0000<span class="kwrd">&lt;/</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Description</span><span class="kwrd">&gt;</span>S.S. Control Box Cov<span class="kwrd">&lt;/</span><span class="html">Description</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Item</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">Item</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>0.0000<span class="kwrd">&lt;/</span><span class="html">Quantity</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">Description</span><span class="kwrd">&gt;</span>S.S. Main Control Bo<span class="kwrd">&lt;/</span><span class="html">Description</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">Item</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">Invoice</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">Invoices</span><span class="kwrd">&gt;</span></pre>
</div>
<p>And of course, no discussion on XML is complete without discussing namespaces.&#160; Here is how you can <strong>add namespaces to the XML result</strong>:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">WITH</span> XMLNAMESPACES(<span class="kwrd">DEFAULT</span> <span class="str">'http://example.org/Invoices'</span>)
<span class="kwrd">SELECT</span> <span class="str">'1.0'</span> <span class="kwrd">AS</span> <span class="str">'@Version'</span>,
        <span class="str">'List of Invoices'</span> <span class="kwrd">AS</span> <span class="str">'Summary'</span>,
( <span class="kwrd">SELECT</span>  Code <span class="kwrd">AS</span> <span class="str">'@Code'</span>,
        Invoice_Date <span class="kwrd">AS</span> <span class="str">'@Date'</span>,
        (<span class="kwrd">SELECT</span> ii.qty <span class="kwrd">AS</span> <span class="str">'Quantity'</span>,
                <span class="kwrd">LEFT</span>(ii.description,20) <span class="kwrd">AS</span> <span class="str">'Description'</span>
         <span class="kwrd">FROM</span> tblinvoitem ii <span class="kwrd">WHERE</span> ii.invoice_id = iv.id <span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Item'</span>),type)
<span class="kwrd">FROM</span> tblInvoice iv
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Invoice'</span>),type)
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'Invoices'</span>)</pre>
</div>
<p>I’m not going to show you the output of this, because it is ugly!&#160; Actually, the annoying part is that it puts the namespace on the root and then on each row of the sub query.&#160; </p>
<p>Hopefully these examples have shown some of the capability of FOR XML PATH but I find that sometimes technologies work well while you are working on arbitrary examples, but then you hit the real world and they seem to fall short.&#160; So, I decided to try and <strong>create an Atom feed using FOR XML PATH</strong>.&#160; Here is what I came up with.</p>
</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd">WITH</span> XMLNAMESPACES(<span class="kwrd">DEFAULT</span> <span class="str">'http://www.w3.org/2005/Atom'</span>)
<span class="kwrd">SELECT</span> <span class="str">'Customers'</span> <span class="kwrd">AS</span> <span class="str">'title'</span>,
        <span class="str">'List of customers'</span> <span class="kwrd">AS</span> <span class="str">'subtitle'</span>,
        <span class="str">'http://tavis.net/Customers'</span> <span class="kwrd">AS</span> <span class="str">'id'</span>,
        REPLACE(<span class="kwrd">CONVERT</span>(<span class="kwrd">varchar</span>,(<span class="kwrd">SELECT</span> <span class="kwrd">MAX</span>(<span class="kwrd">COALESCE</span>(ModifiedDate,CreatedDate)) <span class="kwrd">FROM</span> tblCustomer), 121),<span class="str">' '</span>,<span class="str">'T'</span>) + <span class="str">'Z'</span> <span class="kwrd">AS</span> <span class="str">'updated'</span>,
    (<span class="kwrd">SELECT</span> cu.Code <span class="kwrd">AS</span> <span class="str">'title'</span>,
            REPLACE(<span class="kwrd">CONVERT</span>(<span class="kwrd">varchar</span>,<span class="kwrd">COALESCE</span>(ModifiedDate,CreatedDate) , 121),<span class="str">' '</span>,<span class="str">'T'</span>) + <span class="str">'Z'</span> <span class="kwrd">AS</span> <span class="str">'updated'</span>,
            <span class="str">'Darrel Miller'</span> <span class="kwrd">AS</span> <span class="str">'author/name'</span>,
            <span class="str">'http://tavis.net/Customer/'</span> + LTRIM(STR(cu.ID)) <span class="kwrd">AS</span> <span class="str">'id'</span>,
            (<span class="kwrd">SELECT</span> *
                <span class="kwrd">FROM</span> (<span class="kwrd">SELECT</span> <span class="str">'alternate'</span> <span class="kwrd">AS</span> <span class="str">'@rel'</span>,
                    <span class="str">'http://tavis.net/Customer/1.html'</span> <span class="kwrd">AS</span> <span class="str">'@href'</span>
              <span class="kwrd">UNION</span>
              <span class="kwrd">SELECT</span> <span class="str">'edit'</span> <span class="kwrd">AS</span> <span class="str">'@rel'</span>,
                    <span class="str">'http://tavis.net/Customer/1.html'</span> <span class="kwrd">AS</span> <span class="str">'@href'</span>) l
             <span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'link'</span>),type ),
            cu.ModifiedDate <span class="kwrd">AS</span> <span class="str">'updated'</span>,
            cu.Name <span class="kwrd">AS</span> <span class="str">'summary'</span>
        <span class="kwrd">FROM</span> tblCustomer cu
        <span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'entry'</span>),type  )
<span class="kwrd">FOR</span> XML <span class="kwrd">PATH</span>(<span class="str">'feed'</span>)</pre>
</div>
<p>&#160;</p>
<p>Ok, so it is not pretty, but with a few T-SQL functions it could be cleaned up quite a bit.&#160; This query produces an atom feed that validates at validator.w3.org/feed and it does it in 8ms on my cheapo server with 20 entries .</p>
<p>The trickiest part of the above query was creating the two link elements.&#160; When you try and create two child elements with the same name, by default SQL Server will attempt to merge the two elements.&#160; By using the UNION and a sub query I was able to create the two separate elements.</p>
<p>So you really can create real world XML documents using FOR XML PATH. I believe this is a useful tool to have under your belt when you are trying to quickly get data out of a database down to a client tier.&#160; It would also be really nice if other database vendors picked up on this feature and implemented in their engines.&#160; One area that I didn’t cover but that is also very useful is that you can create T-SQL Functions that return XML and then call them recursively.&#160; This allows you to <strong>build hierarchical XML documents</strong>.&#160;&#160; My tests so far have also shown that building trees this way is very quick.</p>
<p>I’m hoping to do a follow up article that shows how you can use the XML from FOR XML PATH as input to XSLT to do all sorts of other interesting things like create audit triggers, data import scripts, create HTML pages, XAML&#160; and JSON documents.&#160; </p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2010/01/18/for-xml-path-and-cqrs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Woe is me, the WOA unmanifesto</title>
		<link>http://www.bizcoder.com/index.php/2009/12/14/woe-is-me-the-woa-unmanifesto/</link>
		<comments>http://www.bizcoder.com/index.php/2009/12/14/woe-is-me-the-woa-unmanifesto/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 21:53:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2009/12/14/woe-is-me-the-woa-unmanifesto/</guid>
		<description><![CDATA[This started as a comment on the blog post here, but it got too long.
I have two questions for Dion and a few comments.   1) Have you read (not just skimmed a few times) Roy&#8217;s dissertation on REST?    2) Have you written both a REST service and a REST client [...]]]></description>
			<content:encoded><![CDATA[<p>This started as a comment on the blog post <a href="http://hinchcliffe.org/archive/2009/12/14/18179.aspx">here</a>, but it got too long.</p>
<p>I have two questions for Dion and a few comments.   <br />1) Have you read (not just skimmed a few times) Roy&#8217;s dissertation on REST?    <br />2) Have you written both a REST service and a REST client that is in production today? </p>
<p>With all due respect, without actually creating REST applications, I don&#8217;t think anyone is qualified to attempt to define principles relating to REST and based on your Twitter bio: </p>
<blockquote><p>Internationally recognized business strategist, enterprise architect, keynote speaker, author, blogger, and consultant on Web 2.0, SOA, and next-gen business</p>
</blockquote>
<p>you sound like a talker, not a walker.&#160; Forgive me if I am wrong.</p>
<p>Here are my comments on the suggested principles, I’ve tried to be as constructive as I can muster:</p>
<p>Principle #1: I think it is a significant oversimplification to refer to a resource&#160; as data.&#160; </p>
<p>Principle #2: Can you clarify what you mean by &quot;favor granularity and depth in linkage&quot;? </p>
<p>Principle #3: I am pretty sure no-one has ever said that URIs should be self descriptive.&#160; In REST the request message should be self-descriptive, the URI being just one part of the message.&#160;&#160; The statement “URI should indicate what data format is being used and indicate nested elements with URL segmentation” is way too strong, maybe if you replace “should” with “can”.</p>
<p>Principle #4:&#160; This idea that all legacy databases should be exposed as data-oriented REST apis is crazy-talk.&#160; REST is an architectural style for building distributed APPLICATIONS.&#160; I.e. Exposing functionality over the web, not just data. Your example of cloud computing API’s is a perfect example.&#160; The Sun Cloud API is one of the best REST examples out there at the moment, but it exposes functionality for manipulating those cloud applications.&#160; Functions like turning the instances on and off.&#160; It’s far more than just exposing data.</p>
<p>Principle #5:&#160; You seem to be describing benefits rather than a principle.</p>
<p>#6:&#160; Ok</p>
<p>#7: I don’t get this concept at all.&#160; Does this mean we should all be using text/plain if we want the widest audience?</p>
<p>#8: Maybe I don’t want my resources to crawled by an SEO.&#160; Even if I do, conforming to the uniform interface and delivering standard format representations should be sufficient to meet any search engine requirements.</p>
<p>#9:&#160; “higher realized value”?&#160; Are you sure this isn’t another one of those spoof manifestos?</p>
<p>#10: I think you are referring to the idea that a resource should only have one canonical URI.&#160; I see this topic debated regularly and I do not yet see any consensus.</p>
<p>Ok, I give up for the moment, I’m a bit too depressed to continue.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2009/12/14/woe-is-me-the-woa-unmanifesto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HttpContent instead of streams</title>
		<link>http://www.bizcoder.com/index.php/2009/12/09/http-content-instead-of-streams/</link>
		<comments>http://www.bizcoder.com/index.php/2009/12/09/http-content-instead-of-streams/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 04:44:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTTPClient]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/?p=93</guid>
		<description><![CDATA[I think the HttpContent class is my favourite part of this library.&#160; This class acts as a container for the content that you received or are about to send. 
Handling returned content
When you do make an http request with this library, the body of the response is wrapped inside an HttpContent object. So, when you [...]]]></description>
			<content:encoded><![CDATA[<p>I think the HttpContent class is my favourite part of this library.&#160; This class acts as a container for the content that you received or are about to send. </p>
<h2>Handling returned content</h2>
<p>When you do make an http request with this library, the body of the response is wrapped inside an HttpContent object. So, when you do:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var content = HttpClient.Get(“http://www.google.com”);</pre>
</div>
<p>what you get back is an HttpContent object.&#160; How you convert that into something useful, depends on what type of data the content object contains.&#160; The HttpContent.ContentType property will tell you the <a href="http://en.wikipedia.org/wiki/Internet_media_type">Internet-media-type</a> type of the data you received.</p>
<p>If you get back <code>text/plain</code> then you can access it like this,</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var mytext = content.ReadAsString();</pre>
</div>
<p>if you get application and <code>application/octet-stream</code> and you know what to do with the bytes, you can simply do</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var mybytearray = content.ReadAsByteArray();</pre>
</div>
<p>However, these examples are pretty primitive.&#160; If you reference the DLL Microsoft.Http.Extensions you will find a variety of richer data extraction methods that have been implemented as extension methods on the HttpContent class.&#160; So you can then do:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var xmlElement = content.ReadAsXElement()
var xmlReader = content.ReadAsXmlReader()</pre>
</div>
<p>to access stuff that comes to you as <code>application/xml.</code></p>
<p>One of the more sophisticated methods is ReadAsSyndicationFeed.&#160; This method leverages the RSS/Atom wrappers that are provided by WCF’s System.ServiceModel.Web DLL.</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var client = <span class="kwrd">new</span> HttpClient();
var response = client.Get(<span class="str">&quot;http://www.stackoverflow.com/feeds&quot;</span>);
var feed = response.Content.ReadAsSyndicationFeed();
<span class="kwrd">foreach</span> (SyndicationItem item <span class="kwrd">in</span> feed.Items) {
    Console.WriteLine(item.Title.Text);
}</pre>
</div>
<p>This example pulls an Atom feed from the front page of Stackoverflow.com.&#160; This really shows the beauty of a standardized data format like Atom.&#160; Those few lines of code above will work in so many places across the web, and now with Microsoft pushing the new OData standard which is based on the Atom Publishing Protocol, many more of the Microsoft products will expose data in this way. e.g. Sharepoint 2010, Azure.</p>
<p>For those of you who feel the burning desire to pull custom objects across the wire, you don’t have to feel ignored as these following methods will take care of all the deserialization work and return you a nice static type.</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var customer = content.ReadAsXmlSerializable&lt;Customer&gt;()
var customer = content.ReadAsDataContract&lt;Customer&gt;()</pre>
</div>
<p>and finally if you want to pull objects across the wire, but are offended by angle brackets, you can use curly braces too.</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var customer = content.ReadAsJsonDataContract&lt;Customer&gt;()</pre>
</div>
<p>The use of extension methods here is really quite elegant because it makes it really easy to add your own “ReadAs” methods and use them in a completely consistent way.&#160; The fact that many of the provided extension methods are factored out into their own Dll means that you do not need to deploy that library, and you can use just your own extension methods.&#160; This has the additional benefit of allowing you to use this library without taking a dependency on WCF if you don’t want to. </p>
<p>&#160;</p>
<h2>Sending Content</h2>
<p>To send content you can use either PUT or POST and the way you prepare the content is identical.&#160; The basic process looks like this:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var client = <span class="kwrd">new</span> HttpClient()
var content = HttpContent.Create(&lt;whatever you want to send&gt;);
client.Post(content); // or client.Put(content);</pre>
<p>The only part that changes is how you create the HttpContent object.</p>
</div>
<p>Suppose you want to send some simple text</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var content = HttpContent.Create(“Here <span class="kwrd">is</span> some content”, <span class="str">&quot;text/plain&quot;</span>);</pre>
</div>
<p>or maybe just an array of bytes.</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var content = HttpContent.Create(<span class="kwrd">new</span> <span class="kwrd">byte</span>[] { 1, 2, 3 }, <span class="str">&quot;application/octet-stream&quot;</span>);</pre>
</div>
<p>How about sending a file?</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var c = HttpContent.Create(<span class="kwrd">new</span> FileInfo(“myfile.zip”), <span class="str">&quot;application/octet-stream&quot;</span>);</pre>
</div>
<p>If you are dealing with an existing API that expects a POST from an html form then you will need to post using the content-type <code>application/x-www-form-urlencoded</code>.&#160; That is as easy as,</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var c = HttpContent.Create(<span class="kwrd">new</span> HttpUrlEncodedForm() { { <span class="str">&quot;a&quot;</span>, <span class="str">&quot;1&quot;</span> }, { <span class="str">&quot;b&quot;</span>, <span class="str">&quot;2&quot;</span> } });</pre>
</div>
<p>and for a more concrete example, twitter always makes a good test subject.&#160; Here is how you do a status update,</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var client = <span class="kwrd">new</span> HttpClient();
client.DefaultHeaders.Authorization = Credential.CreateBasic(<span class="str">&quot;username&quot;</span>,<span class="str">&quot;password&quot;</span>);
var form = <span class="kwrd">new</span> HttpUrlEncodedForm();
form.Add(<span class="str">&quot;status&quot;</span>,<span class="str">&quot;Test tweet using Microsoft.Http.HttpClient&quot;</span>);
var content = HttpContent.Create(form);
var resp = client.Post(<span class="str">&quot;http://www.twitter.com/statuses/update.xml&quot;</span>, content);</pre>
</div>
<p>&#160;</p>
<p>It is pretty easy to continue this pattern and create your own factory methods for converting your favourite data type into an HttpContent object.&#160; There are are quite a few other nice features about the content object relating to how it manages the underlying stream, when it reads from the stream, if it buffers the stream and other such nitty gritty, but I’ll save those details for another day.&#160; To keep track of the other articles I am attempting to write on the subject, you can see the summary post <a href="http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2009/12/09/http-content-instead-of-streams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HttpClient – The basics</title>
		<link>http://www.bizcoder.com/index.php/2009/12/08/httpclient-the-basics/</link>
		<comments>http://www.bizcoder.com/index.php/2009/12/08/httpclient-the-basics/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 03:16:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTTPClient]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2009/12/08/httpclient-the-basics/</guid>
		<description><![CDATA[Before I go into any details I thought it would be valuable to give some basic examples of how to use the HTTPClient.
Retrieve some HTTP content from an URL

var client = new HttpClient();
var response = client.Get(&#34;http://example.org&#34;);

Post some content to an Url

var client = new HttpClient();
var content = HttpContent.Create(&#34;This is some string content&#34;);
var response = client.Post(&#34;http://example.com&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>Before I go into any details I thought it would be valuable to give some basic examples of how to use the HTTPClient.</p>
<p>Retrieve some HTTP content from an URL</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var client = <span class="kwrd">new</span> HttpClient();
var response = client.Get(<span class="str">&quot;http://example.org&quot;</span>);</pre>
</div>
<p>Post some content to an Url</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var client = <span class="kwrd">new</span> HttpClient();
var content = HttpContent.Create(<span class="str">&quot;This is some string content&quot;</span>);
var response = client.Post(<span class="str">&quot;http://example.com&quot;</span>, content);</pre>
</div>
<p>Authenticated get</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var client = <span class="kwrd">new</span> HttpClient();
client.DefaultHeaders.Authorization = Credential.CreateBasic(<span class="str">&quot;user&quot;</span>, <span class="str">&quot;password&quot;</span>);
var response = client.Get(<span class="str">&quot;http://example.org&quot;</span>);</pre>
</div>
<p>and if you need to provide some kind of custom authentication scheme you can simply create credential like this</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">client.DefaultHeaders.Authorization = <span class="kwrd">new</span> Credential(“My secure authentication header”);</pre>
<pre class="csharpcode">&#160;</pre>
</div>
<p>Interestingly, the methods Get and Post are not actually members of the HttpClient class.&#160; They are extension methods that simply call one of the Send methods on the HttpClient class.&#160; If it is more appropriate you can also call the Send methods directly. As you will see, there are plenty of overloads to suit your needs:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode"><span class="kwrd"></span>&#160;</pre>
<pre class="csharpcode"><span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, Uri uri)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, Uri uri, RequestHeaders headers)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, Uri uri, HttpContent content)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, <span class="kwrd">string</span> uri)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, <span class="kwrd">string</span> uri, RequestHeaders headers)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, <span class="kwrd">string</span> uri, HttpContent content)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, <span class="kwrd">string</span> uri, RequestHeaders headers, HttpContent content)
<span class="kwrd">public</span> HttpResponseMessage Send(HttpMethod method, Uri uri, RequestHeaders headers, HttpContent content)</pre>
<pre class="csharpcode">&#160;</pre>
</div>
<p>I like this approach because it provides a host of easy to call helper methods that are all routed through one of these Send methods.&#160; </p>
<p>Another major advantage of the HttpClient library over the HttpWebRequest class is related to the RequestHeaders class.&#160; All of the Http header values have been wrapped with some kind of helper class that significantly eases the process of setthing header values.</p>
<p>Here are a few random examples of setting header information that pulled from the unit tests:</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var req = <span class="kwrd">new</span> RequestHeaders();
req.Accept.Add(StringWithOptionalQuality.Parse(<span class="str">&quot;audio/*; q=0.2&quot;</span>));

req.Allow.Add(<span class="str">&quot;GET&quot;</span>);
req.Allow.Add(<span class="str">&quot;POST&quot;</span>); 

var cc = <span class="kwrd">new</span> CacheControl();
cc.MaxAge = TimeSpan.FromSeconds(1);
cc.MaxStale = <span class="kwrd">true</span>;
cc.MaxStaleLimit = TimeSpan.FromSeconds(2);
cc.MinFresh = TimeSpan.FromSeconds(3);
cc.MustRevalidate = <span class="kwrd">true</span>;
cc.NoCache = <span class="kwrd">true</span>;

req.CacheControl = cc;</pre>
</div>
<p>In addition to these headers that have strongly typed classes there is also the nifty Parse() function that is on both the request and response header collections.</p>
<div class="csharpcode-wrapper">
<pre class="csharpcode">var h = RequestHeaders.Parse(<span class="str">&quot;Pragma: LinkBW=2147483647, AccelBW=1048576, AccelDuration=5000&quot;</span>); 

var resp = ResponseHeaders.Parse(
                <span class="str">@&quot;Set-Cookie: mkt1=norm=US; domain=.live.com; path=/
                    Set-Cookie: AFORM=NOFORM; expires=Mon, 20-Jul-2015 23:59:59 GMT; path=/&quot;</span>.Trim());

var h = RequestHeaders.Parse(
               <span class="str">@&quot;Accept-Charset: iso-8859-5, unicode-1-1;q=0.8
           Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
           Accept-Language: da, en-gb;q=0.8, en;q=0.7&quot;</span>);

var h = ResponseHeaders.Parse(<span class="str">@&quot;WWW-Authenticate: Digest realm=&quot;</span><span class="str">&quot;testrealm@host.com&quot;</span><span class="str">&quot;,
                qop=&quot;</span><span class="str">&quot;auth,auth-int&quot;</span><span class="str">&quot;,
                nonce=&quot;</span><span class="str">&quot;dcd98b7102dd2f0e8b11d0f600bfb0c093&quot;</span><span class="str">&quot;,
                opaque=&quot;</span><span class="str">&quot;5ccc069c403ebaf9f0171e9517f40e41&quot;</span><span class="str">&quot;&quot;</span>);</pre>
</div>
<p>Who wants to write and test the code necessary to parse this stuff?&#160; Not me, that’s for sure. Hopefully this has provided a taste for what this library is capable of and over the next few weeks I plan on writing a number of other posts that cover other interesting areas of the Microsoft.Http namespace.&#160; To see where I am up to, see the summary page <a href="http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2009/12/08/httpclient-the-basics/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Why the Microsoft.Http library is awesome.</title>
		<link>http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/</link>
		<comments>http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 03:15:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTTPClient]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/</guid>
		<description><![CDATA[In various forums I a finding myself raving about this new library.&#160; I decided that I should write some blog posts on the subject for a few reasons.&#160; 1) to convince myself that there is actually some substance to my raving; 2) provide a place to point people to in order to substantiate my infatuation [...]]]></description>
			<content:encoded><![CDATA[<p>In various forums I a finding myself raving about this new library.&#160; I decided that I should write some blog posts on the subject for a few reasons.&#160; 1) to convince myself that there is actually some substance to my raving; 2) provide a place to point people to in order to substantiate my infatuation and 3) maybe encourage a few more people to use it before it gets relegated to the dustbin of Microsoft’s forgotten projects.</p>
<p>Here are the posts I am planning.&#160; I will create the links as the posts are completed.</p>
<ul>
<li><a href="http://www.bizcoder.com/index.php/2009/12/08/httpclient-the-basics/">The basics</a> </li>
<li><a href="http://www.bizcoder.com/index.php/2009/12/09/http-content-instead-of-streams/">HttpContent instead of streams</a> </li>
<li><a href="http://www.bizcoder.com/index.php/2010/02/12/but-can-you-test-it/">But can you test it</a>? </li>
<li>HttpClient on the server </li>
</ul>
<p>You can get the library <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24644">here</a> and the official support forum is <a href="http://forums.asp.net/1180.aspx">here</a>.&#160; From what I have been told, this library will not make it into the .Net 4 release but it is still being actively developed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2009/12/08/why-the-microsoft-http-library-is-awesome/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Oh Data</title>
		<link>http://www.bizcoder.com/index.php/2009/11/30/oh-data/</link>
		<comments>http://www.bizcoder.com/index.php/2009/11/30/oh-data/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 14:44:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2009/11/30/oh-data/</guid>
		<description><![CDATA[Since the recent PDC09 I have been obsessing over OData and I need to write this post just to get it out of my head.&#160; Microsoft has made it obvious that they are taking this protocol very seriously by integrating it into Sharepoint, Visual Studio, RIA Services, PowerPivot, and I expect to see it in [...]]]></description>
			<content:encoded><![CDATA[<p>Since the recent PDC09 I have been obsessing over OData and I need to write this post just to get it out of my head.&#160; Microsoft has made it obvious that they are taking this protocol very seriously by integrating it into Sharepoint, Visual Studio, RIA Services, PowerPivot, and I expect to see it in the next version of Office and in the Dynamics products.&#160; I think it is a great direction to be headed but I also have concerns.</p>
<p>Let me start by quoting the most important paragraph from the <a href="http://download.microsoft.com/download/B/0/B/B0B199DB-41E6-400F-90CD-C350D0C14A53/[MC-APDSU].pdf">document</a> that explains how OData extends AtomPub.</p>
<blockquote><p>AtomPub, as specified in [RFC5023], in combination with the extensions defined in this document, is appropriate for use in Web services which need a uniform, flexible, general purpose interface for exposing create retrieve update delete (CRUD) operations on a data model to clients. It is less suited to Web services that are primarily method-oriented or in which data operations are constrained to certain prescribed patterns.</p>
</blockquote>
<p>Let me paraphrase that.&#160; If all your service is going to do for your client is <strong>“CRUD” on generic data then OData is appropriate</strong>.&#160; As long as everyone keeps this in mind going forward we should not run into too much trouble.&#160; However, there is a problem with this statement.&#160; <strong>REST is not really appropriate for doing CRUD</strong>.&#160; Harry Pierson sums it up best <a href="http://devhawk.net/2007/05/24/REST+Is+Neither+CRUD+Nor+CRAP.aspx">here</a>.&#160; What is worse, is that I am seeing some of the people behind the OData spec equating OData with <a href="http://blogs.msdn.com/aconrad/archive/2009/11/18/odbc-for-the-web.aspx">ODBC</a>.&#160; </p>
<p>The problem is that <strong>ODBC allows clients to initiate transactions</strong> across multiple requests. REST does not allow this as it would violate the stateless constraint.&#160; <strong>REST does not need this</strong> because it is intended to address a completely different layer of the application architecture than ODBC.&#160; REST provides a way to deliver Domain services.&#160; I.e.&#160; If you maintain weather data, REST provides you a easy way to expose “Today’s Weather”, “Last Week’s weather for Detroit”, “Average Rainfall in Orlando for the month of June”.&#160; ODBC is aimed at the layer that exposes the data points for a specific place at a specific date and time.&#160; </p>
<p>ODBC exposes dumb data, REST exposes intelligently presented information.</p>
<p>In an ODBC application it is the client that does something intelligent with the data before presenting it to the user.&#160; In a REST application, usually the client simply makes the “intelligent information” pretty.&#160; REST and ODBC are not comparable.</p>
<p>So is OData useful?&#160; Absolutely it is useful to people who want to manipulate generic information, like for example Sharepoint lists, or data to feed into PowerPivot or Excel.&#160; If you have a need to expose a generic data store to a client that will do graphing, statistical analysis, or some kind of visualization like rendering Mars Rover data then it could be very useful.&#160; </p>
<p>However, if you want to provide a <strong>service that delivers intelligent information</strong> that is specific to a particular domain then I believe and apparently the authors of the spec believe that <strong>OData is not appropriate</strong>.</p>
<p>Beyond my fear of developers attempting to use OData for unintended purposes there are few other things that I think should be fixed in the OData spec.&#160; </p>
<p>The Atom Entry content element should <strong>not use application/xml as the media type</strong>.&#160; The content contains XML that is specifically related to the Entity Data Model and should be identified as such.&#160; A media type such as application/EDM-Instance+xml may be sufficient.&#160; What would be even better is if that content element contained a <strong>link to the CSDL file that defines the EntityType</strong> and that is currently accessed by constructing an URI with [Service]/$metadata.&#160; Oh yeah, and maybe a precise media type on the metadata would be good too!</p>
<p>Client side URI construction is really nasty habit to get into.&#160; I think for the most part, MS can get away with the construction of query parameters like $skip, $top, and $orderby, but to actually construct the path segments of a URI is just going lead to client-server coupling that will hurt in the future.</p>
<p>I haven’t read the entire OData spec in detail but it is interesting to see the <strong>complications that are introduced because they have not strictly followed the hypermedia constraint</strong>.&#160; For example, it has become necessary to create custom HTTP headers to manage versioning of the “protocol” due to the use of client constructed URIs.&#160; If those URIs were delivered as Links with rel attributes then the <strong>versioning would be limited to the media type of the content</strong>.&#160; Yeah, I realize that you can’t create links for every combination of query parameter/ sub resource, but hey, I’m not the one saying that creating a REST interface for generic data was a good idea in the first place <img src='http://www.bizcoder.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p>It is fascinating how difficult it is to beat the RPC mentality out of people.&#160; Even though the OData spec is built on top of AtomPub, the authors have gone to <strong>great lengths to document the OData protocol in RPC terms</strong> and then map the RPC call to an HTTP request.&#160; When you find yourself creating documentation titles like “RetreiveEntitySet Request”, “RetreiveEntity Request”, “RetreiveComplexType Request”, “RetreivePrimitiveProperty Request” and there is actually some valuable information that distinguishes one of those requests from the other then you are violating the uniform interface.&#160; The idea is that the documentation can read “Use HTTP GET to retrieve representation of the resource from the URI”. Look at how simple the AtomPub <a href="http://tools.ietf.org/html/rfc5023#section-5.1">spec</a> is in comparison.</p>
<p>I think it is <strong>great that Microsoft have recognized the value of a RESTful protocol like AtomPub</strong> and they have taken the steps to incorporate this type of interface into many of their products.&#160; I understand what they are trying to do with their URL construction techniques, I know exactly why they have introduced a MERGE verb and have created a batch request mechanism, because I too have been down this path before.&#160; However, while there some areas they are justified in straying from the REST constraints, there are others that are definitely not and the protocol is suffering from it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2009/11/30/oh-data/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>It is not that we don&#8217;t like the convenience of Windows Installer</title>
		<link>http://www.bizcoder.com/index.php/2009/08/28/it-is-not-that-we-dont-like-the-convenience-of-windows-installer/</link>
		<comments>http://www.bizcoder.com/index.php/2009/08/28/it-is-not-that-we-dont-like-the-convenience-of-windows-installer/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 13:28:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2009/08/28/it-is-not-that-we-dont-like-the-convenience-of-windows-installer/</guid>
		<description><![CDATA[I just had a fight with an installation of Sharepoint and it dawned on my what I don’t like about Windows Installer.&#160; It is a black box.&#160; You click on the file and press next, next, next and magic happens.&#160; Personally, I would like to know where files are being put, which registry hives are [...]]]></description>
			<content:encoded><![CDATA[<p>I just had a fight with an installation of Sharepoint and it dawned on my what I don’t like about Windows Installer.&#160; It is a black box.&#160; You click on the file and press next, next, next and magic happens.&#160; Personally, I would like to know where files are being put, which registry hives are being touched, are services being installed, are accounts being created.&#160; Did something go in the GAC, was a COM DLL registered.</p>
<p>It should be easy to add some kind of UI that summarizes what parts of the system were touched by the install.&#160; It would make me feel much more comfortable about installing stuff.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2009/08/28/it-is-not-that-we-dont-like-the-convenience-of-windows-installer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WCF REST Starter Kit Preview 2</title>
		<link>http://www.bizcoder.com/index.php/2009/03/14/wcf-rest-starter-kit-preview-2/</link>
		<comments>http://www.bizcoder.com/index.php/2009/03/14/wcf-rest-starter-kit-preview-2/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 00:30:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HTTPClient]]></category>
		<category><![CDATA[REST]]></category>

		<guid isPermaLink="false">http://www.bizcoder.com/index.php/2009/03/14/wcf-rest-starter-kit-preview-2/</guid>
		<description><![CDATA[I have been looking thought the latest release of the WCF REST Starter Kit and there are some interesting things there.&#160; Especially the new HttpClient class in the Microsoft.Http namespace.&#160; I will not talk about the server side of things because I really don&#8217;t have anything nice to say and until I have some constructive [...]]]></description>
			<content:encoded><![CDATA[<p>I have been looking thought the latest release of the WCF REST Starter Kit and there are some interesting things there.&nbsp; Especially the new HttpClient class in the Microsoft.Http namespace.&nbsp; I will not talk about the server side of things because I really don&#8217;t have anything nice to say and until I have some constructive criticism to give I&#8217;m going to keep my mouth shut.</p>
<p>However, the new client side library is designed to make it easier to talk to HTTP endpoints and it seems to be quite nice.&nbsp; It is 3500 lines of code, so there is a fair bit of stuff in there, layered on top of HttpWebRequest and I was curious to see what it could do.</p>
<p>I started by looking at the enclosed sample, unhelpfully named &#8220;WpfSample&#8221; and I got a little sidetracked from my exploration.&nbsp; This sample is actually a very neat little http request builder tool.&nbsp; I know this is a cool tool to have, because I have an almost identical one that we built to help us test REST services.&nbsp; Testing with real web browsers is really annoying because they do all sorts of magic stuff behind the scenes, they are very picky about what content-types they will render and they keep trying to find a favicon!&nbsp; </p>
<p>Anyway, I was curious to see how this tool made use of the HttpClient class but I was rather dismayed to see <strong>all</strong> of the code in the &#8220;code behind&#8221; of the Window1.xaml file.&nbsp; This makes it really difficult to distinguish between what code is relevant to the web request and what is just UI gunk.&nbsp; To add salt to the wound, it is a WPF app that uses no data binding and no Commands, just events manipulating the contents of UI elements.&nbsp; It was a nice looking, handy WPF app that demonstrated this new spiffy HttpClient library with the architecture of a college student&#8217;s VB4 project.</p>
<p>Ok, I know it is just a sample.&nbsp; However, everything that Microsoft puts out is viewed by people who are learning.&nbsp; If Microsoft create samples with crappy architecture, why are we surprised when developers create applications with crappy architectures.</p>
<p>So despite the ton of work that I was supposed to be doing today, I refactored it.&nbsp; I created an MVC type of structure with databinding and routed commands.&nbsp; If you get the <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24644">WCF REST Starter Kit</a> and extract the zip file that gets put in the &#8220;C:\Program Files (x86)\Microsoft WCF REST&#8221; folder you will find a solution named Product.sln.&nbsp; Just add my project which is in the zip file below and you will have, what I hope is a much easier to read version of the HttpClient sample.</p>
<p><a href="http://www.tavis.ca/files/httpClientGui.zip">http://www.tavis.ca/files/httpClientGui.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizcoder.com/index.php/2009/03/14/wcf-rest-starter-kit-preview-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
