Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
35 lines (23 sloc) 7.74 KB
---
layout: post
title: What a difference a framework can make…
date: '2009-08-15T13:47:00.001+01:00'
tags: [rest, asp.net-mvc, openrasta]
modified_time: '2009-08-15T13:47:13.051+01:00'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-4335672925993169976
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2009/08/what-difference-framework-can-make.html
---
<p>Rick Strahl, C# MVP extraordinaire, has released a very cool app at codepaste.net. Go and check it out, it’s quite cool.</p> <p>One of the things Rick is doing is publishing a POD API that uses xml and json. And he posted on an unrelated entry <a href="http://www.west-wind.com/weblog/posts/847436.aspx">an implementation of POD using MVC</a>. For reference, here’s his snippet.</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]<br /><span style="color: #0000ff">public</span> ActionResult PostNewCodeSnippetObject()<br />{<br /> StreamReader sr = <span style="color: #0000ff">new</span> StreamReader(Request.InputStream);<br /> <span style="color: #0000ff">string</span> data = sr.ReadToEnd();<br /> sr.Close();<br /><br /> Snippet inputSnippet = <span style="color: #0000ff">null</span>;<br /><br /> <span style="color: #0000ff">if</span> (Request.ContentType == <span style="color: #006080">&quot;text/javascript&quot;</span> || Request.ContentType == <span style="color: #006080">&quot;application/json&quot;</span>)<br /> {<br /> JSONSerializer ser = <span style="color: #0000ff">new</span> JSONSerializer(SupportedJsonParserTypes.WestWindJsonSerializer);<br /> inputSnippet = ser.Deserialize(data, <span style="color: #0000ff">typeof</span>(Snippet)) <span style="color: #0000ff">as</span> Snippet;<br /> }<br /> <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (Request.ContentType == <span style="color: #006080">&quot;text/xml&quot;</span>)<br /> {<br /> inputSnippet = SerializationUtils.DeSerializeObject(data, <span style="color: #0000ff">typeof</span>(Snippet)) <span style="color: #0000ff">as</span> Snippet;<br /> }<br /> <span style="color: #0000ff">else</span><br /> <span style="color: #0000ff">return</span> ExceptionResult(<span style="color: #006080">&quot;Unsuppported data input format. Please provide input content-type as text/javascript, application/json or text/xml.&quot;</span>);<br /><br /><br /> busCodeSnippet codeSnippet = <span style="color: #0000ff">new</span> busCodeSnippet();<br /> codeSnippet.NewEntity();<br /><br /> DataUtils.CopyObjectData(inputSnippet, codeSnippet.Entity, <span style="color: #006080">&quot;Id&quot;</span>, BindingFlags.Public | BindingFlags.Instance);<br /><br /> <span style="color: #0000ff">if</span> (!codeSnippet.Validate())<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.ExceptionResult(<span style="color: #006080">&quot;Code snippet validation failed: &quot;</span> + codeSnippet.ValidationErrors.ToString());<br /><br /> <span style="color: #0000ff">if</span> (!codeSnippet.Save())<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.ExceptionResult(<span style="color: #006080">&quot;Couldn't save new code snippet: &quot;</span> + codeSnippet.ValidationErrors.ToString());<br /><br /> <span style="color: #0000ff">return</span> ApiResult(codeSnippet.Entity);<br />}</pre>
<br /></div>
<p></p>
<p>Ouch. Lot of fluff if I may say so. Let’s see what the equivalent implementation would be using <a href="http://www.openrasta.com">OpenRasta</a>.</p>
<p>The configuration:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">using</span> (OpenRastaConfiguration.Manual)<br />{<br /> ResourceSpace.Has.ResourcesOfType&lt;Snippet&gt;().AtUri(<span style="color: #006080">&quot;/snippets&quot;</span>)<br /> .HandledBy&lt;SnippetHandler&gt;()<br /> .AsJsonDataContract().And.AsXmlDataContract();<br />}</pre>
<br /></div>
<p>The handler:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> OperationResult Post(Snippet newSnippet)<br />{<br /> var codeSnippet = <span style="color: #0000ff">new</span> busCodeSnippet();<br /> codeSnippet.NewEntity();<br /><br /> DataUtils.CopyObjectData(inputSnippet, codeSnippet.Entity, <span style="color: #006080">&quot;Id&quot;</span>, BindingFlags.Public | BindingFlags.Instance);<br /><br /> <span style="color: #0000ff">if</span> (!codeSnippet.Validate())<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> OperationResult.BadRequest(<span style="color: #006080">&quot;Code snippet validation failed&quot;</span>, codeSnippet.ValidationErrors.ToString());<br /><br /> <span style="color: #0000ff">if</span> (!codeSnippet.Save())<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> OperationResult.InternalServerError(<span style="color: #006080">&quot;Couldn't save new code snippet&quot;</span>, codeSnippet.ValidationErrors.ToString());<br /><br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> OperationResult.OK(codeSnippet.Entity);<br />}</pre>
<br /></div>
<p>I much prefer the latter. Don’t you?</p>