<?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>House of Metnetsky &#187; json</title>
	<atom:link href="http://cowarthill.com/blog/index.php/tag/json/feed/" rel="self" type="application/rss+xml" />
	<link>http://cowarthill.com/blog</link>
	<description>Run Tortoise Run</description>
	<lastBuildDate>Fri, 26 Feb 2010 17:15:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>RESTful Web Services in .NET (Part 2)</title>
		<link>http://cowarthill.com/blog/index.php/2009/03/16/restful-web-services-in-net-part-2/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/03/16/restful-web-services-in-net-part-2/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 01:05:10 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[kmbs]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=193</guid>
		<description><![CDATA[Now that I&#8217;ve finally got a bit of time I figured I should explain how we updated our old RESTful framework to be nice and slick. Instead of one method per class which handles all sub method variations we now have something much prettier and easier to maintain. The UserService found in the previous post [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;ve finally got a bit of time I figured I should explain how we updated our old RESTful framework to be nice and slick. Instead of one method per class which handles all sub method variations we now have something much prettier and easier to maintain. The UserService found in the previous post can now look like:</p>
<pre class="brush: csharp; title: ; notranslate">
public class UserService : RestService
{
	[RestMethod(&quot;GET&quot;, &quot;^$&quot;)]
	public IList RetrieveAll()
	{
	}

	[RestMethod(&quot;GET&quot;, @&quot;^(?&lt;id&gt;[0-9]+)$&quot;)]
	public User Retrieve(int id)
	{
	}

	[RestMethod(&quot;HEAD&quot;, @&quot;^(?&lt;name&gt;\w+)$&quot;)]
	public User Exists(string name)
	{
	}

	[RestMethod(&quot;PUT&quot;, &quot;^$&quot;, typeof(User))]
	public User Create(User user)
	{
	}

	[RestMethod(&quot;POST&quot;, &quot;^$&quot;, typeof(User))]
	public bool Update(User user)
	{
	}

	[RestMethod(&quot;DELETE&quot;, @&quot;^(?&lt;id&gt;[0-9]*)$&quot;)]
	public bool Delete(int id)
	{
	}
}
</pre>
<p>As you can see there are a lot of differences:</p>
<ul>
<li>Heavy influence from newer APIs</li>
<li>Methods can return any Type</li>
<li>Methods can take 0 or more arguments of any Type</li>
<li>RestMethodAttribute has been added to match URLs against methods</li>
</ul>
<p>Most of the code above should be self-explanatory, but in case it&#8217;s not here&#8217;s some help.</p>
<p>The method decorator <b>[RestMethod("GET", "^$")]</b> tells the framework that any HTTP GET requests against the <b>UserService</b> class without any trailing arguments (<b>http://localhost/user.r</b>) should be handled by <b>RetrieveAll()</b>.</p>
<p>The method decorator <b>[RestMethod("GET", @"^(?&lt;id&gt;[0-9]+)$&#8221;)]</b> tells the framework that any HTTP GET request against the <b>UserService</b> class which ends with a number only (<b>http://localhost/user.r/1</b>, <b>http://localhost/user.r/5000</b>, etc) should be handled by <b>Retrieve(int id)</b>. The cool thing here is that the regex group <id> will provide a match to the argument name so our function will be called with whatever number the URL ends with. So calling <b>http://localhost/user.r/25</b> where the following is the executed method will result in?</p>
<pre class="brush: csharp; title: ; notranslate">
[RestMethod(&quot;GET&quot;, @&quot;^(?&lt;id&gt;[0-9]+)$&quot;)]
public bool Retrieve(int id)
{
	return id == 25;
}
</pre>
<p><b>TRUE</b>!! Pretty simple, and very cool. Another nice feature is when the HTTP request contains a body like a POST or PUT would. So POSTing to <b>http://localhost/user.r</b> with a body of:</p>
<pre class="brush: jscript; title: ; notranslate">
{'user': {
	'first_name': 'Matthew',
	'last_name': 'Metnetsky'
}}
</pre>
<p>would call the method decorated with <b>[RestMethod("POST", "^$", typeof(User))]</b> and it&#8217;s <b>user</b> argument would actually be set assuming the class implemented a JSON serializer. All of the URL/body mappers are definable by implementing <a href="http://msdn.microsoft.com/en-us/library/system.reflection.binder.aspx" target="_blank">Binder</a> and listing them in the web.config against a Content-Type. By default there are Binders for form variables, JSON, XML, raw streams, and a few more.</p>
<p>So why might I be discussing the external API of a closed library? To PUSH innovation in the current public ones. By taking these ideas and creating an &#8220;open&#8221; version I&#8217;m sure I&#8217;d be breaking my contract so I&#8217;ve got two options:</p>
<ol>
<li>Expose our public API to entice and PUSH others to innovate</li>
<li>Get a lot of feedback which might enable me to get our library opened</li>
</ol>
<p>So&#8230;. let me know&#8230;. <img src='http://cowarthill.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/03/16/restful-web-services-in-net-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RESTful Web Services in .NET (Part 1)</title>
		<link>http://cowarthill.com/blog/index.php/2009/02/26/restful-web-services-in-net-part-1/</link>
		<comments>http://cowarthill.com/blog/index.php/2009/02/26/restful-web-services-in-net-part-1/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 22:51:22 +0000</pubDate>
		<dc:creator>MET</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[kmbs]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://cowarthill.com/blog/?p=178</guid>
		<description><![CDATA[A couple of years ago I tossed together a RESTful IHttpHandlerFactory for .NET. It wasn&#8217;t amazing, but it was being used long before Microsoft et all decided to create there own. Instead of extending Page you extended RestService and implemented whatever methods you wanted. As you can probably guess the request&#8217;s HTTP Method was transformed [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago I tossed together a <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">RESTful</a> <a href="http://msdn.microsoft.com/en-us/library/system.web.ihttphandlerfactory.aspx" target="_blank">IHttpHandlerFactory</a> for <a href="http://msdn.microsoft.com/en-us/netframework/default.aspx" target="_blank">.NET</a>. It wasn&#8217;t amazing, but it was being used long before Microsoft et all decided to create there own. Instead of extending <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx" target="_blank">Page</a> you extended RestService and implemented whatever methods you wanted.</p>
<pre class="brush: csharp; title: ; notranslate">
public interface IRestService
{
	void Head(RestEventArgs args);
	object Get(RestEventArgs args);
	object Put(RestEventArgs args);
	object Post(RestEventArgs args);
}
</pre>
<p>As you can probably guess the request&#8217;s <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html" target="_blank">HTTP Method</a> was transformed into the function name and executed accordingly. If the method wasn&#8217;t &#8220;known&#8221; we would then use reflection to find the method for execution. Not to fansy as you can see, but it worked.</p>
<p>If a request was made for <b>http://localhost/app/users.r</b> the <a href="http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspx" target="_blank">IHttpHandler</a> <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" target="_blank">factory</a> would load users.r and it would tell us what <a href="http://msdn.microsoft.com/en-us/library/0b0thckt.aspx" target="_blank">class</a> you assigned to it (much like <a href="http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx" target="_blank">@Page</a> CodeBehind/Inherits in .aspx). From there we would instantiate the <a href="http://msdn.microsoft.com/en-us/library/0b0thckt.aspx" target="_blank">class</a> and call the appropriate <a href="http://msdn.microsoft.com/en-us/library/ms173114.aspx" target="_blank">method</a>. Whatever you returned would be marshalled for wire transfer based on it&#8217;s type.</p>
<pre class="brush: csharp; title: ; notranslate">
	class User : IXmlSerializable
	{
		/* ... */
	}

	public class UserService : RestService
	{
		public object Get(RestEventArgs args);
		{
			return new User(5, &quot;Matthew&quot;, &quot;Metnetsky&quot;);
		}
	}
}
</pre>
<p>Calling <b>http://localhost/app/users.r</b> would execute UserService::Get(..) which would return a User instance. The results are then inspected for a few different interfaces like <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx" target="_blank">IXmlSerializable</a>, IJsonSerializable (one of ours), etc. Based on what is supported by the <a href="http://msdn.microsoft.com/en-us/library/ms173104.aspx" target="_blank">Type</a>, we then check if the requester supports it by interogating their <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html" target="_blank">&#8220;Accepts&#8221; header</a>. This way each requester can receive what they understand how to handle (for instance &#8211; don&#8217;t send <a href="http://www.json.org/" target="_blank">JSON</a> to <a href="http://www.adobe.com/devnet/actionscript/articles/actionscript3_overview.html" target="_blank">AS3</a>). The system really worked well, and it&#8217;s simplicity made it damn fast.</p>
<p>Like all frameworks however, once it was actually in use we were able to see the pitfalls and issues. Take a look at the following URLs:</p>
<ul>
<li><b>http://localhost/app/users.r</b></li>
<li><b>http://localhost/app/users.r/1</b></li>
<li><b>http://localhost/app/users.r/matthew</b></li>
</ul>
<p>In order to handle all of the urls above the Get(RestEventArgs) method turned into one big switch/if/else conditional. Some people made it prettier than others, but the result was the same. So about 14 months ago we upgraded our framework &#8211; check back soon to see how we did it, and why I&#8217;m bringing it up now.</p>
]]></content:encoded>
			<wfw:commentRss>http://cowarthill.com/blog/index.php/2009/02/26/restful-web-services-in-net-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

