<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Fat Agnus</title>
	
	<link>http://fatagnus.com</link>
	<description>One is glad to be of service</description>
	<pubDate>Fri, 30 Oct 2009 15:42:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/FatAgnus" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Test Driven Development: By Example – By Kent Beck</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/5vwHvjKtZto/</link>
		<comments>http://fatagnus.com/test-driven-development-by-example-%e2%80%93-by-kent-beck/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 15:42:12 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Book reviews]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=400</guid>
		<description><![CDATA[


I have had this book laying in my stack of books that I want to read for quite some time, so when I finally got around to reading it, I was really excited. 
However, after reading it I felt a little bit disappointed, because I somehow had expected more. 
If you wanted to, then you [...]]]></description>
			<content:encoded><![CDATA[<div style="border: 0px none ; padding: 5px; float: right;">
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=fatagnus-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=0321146530" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<p>I have had this book laying in my stack of books that I want to read for quite some time, so when I finally got around to reading it, I was really excited. </p>
<p>However, after reading it I felt a little bit disappointed, because I somehow had expected more. </p>
<p>If you wanted to, then you could probably sum up the content of book to a few lines (<em>which some critiques seems to do</em>).</p>
<ol>
<li>Add a test.</li>
<li>Run test and see it fail.</li>
<li>Make some changes.</li>
<li>Run again and see it succeed.</li>
<li>Refactor and start over.</li>
</ol>
<p>The book doesn’t build on real world examples but instead shows how to get your mindset tuned in to start using TDD. It would have been nice with a set of best practices on naming conventions, how to write tests effectively, and where to put your test in your project. Every time I look at a new project, I often see it written in a completely different style than the last one.</p>
<p>If you are looking for a good and simple introduction to test-driven development, then this is the book. If you want something that explores the subject a little bit deeper, then should probably try to find another book.</p>
<p>However, I would recommend every developer to explorer the benefits that come with TDD, like higher quality in your code.</p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/5vwHvjKtZto" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/test-driven-development-by-example-%e2%80%93-by-kent-beck/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/test-driven-development-by-example-%e2%80%93-by-kent-beck/</feedburner:origLink></item>
		<item>
		<title>Guidelines for writing efficient CSS selectors</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/_6MyMwORuH0/</link>
		<comments>http://fatagnus.com/guidelines-for-writing-efficient-css-selectors/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 15:13:04 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Css]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=391</guid>
		<description><![CDATA[First, let’s see how the style system works.

“The style system matches a rule by starting with the rightmost selector and moving to the left through the rule&#8217;s selectors. As long as your little subtree continues to check out, the style system will continue moving to the left until it either matches the rule or bails [...]]]></description>
			<content:encoded><![CDATA[<p>First, let’s see how the style system works.</p>
<blockquote><p>
“The style system matches a rule by starting with the rightmost selector and moving to the left through the rule&#8217;s selectors. As long as your little subtree continues to check out, the style system will continue moving to the left until it either matches the rule or bails out because of a mismatch.” – <a href="https://developer.mozilla.org/en/Writing_Efficient_CSS">David Hyatt</a></p></blockquote>
<p>When I first read this, I got one of those Aha! Moments, because I always thought that CSS selectors was interpreted left to right.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#foobar</span> a <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">black</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>If we take our example from above, this would mean that instead of just checking for anchor elements inside foobar, the browser will lookup all anchors in the page.  Instead of just checking each anchor’s parent, the browser will crawl the document tree, trying to find an ancestor with the ID foobar and it will continue up the tree of ancestors until it reaches the document root.</p>
<p>These guidelines was put forward by <a href="http://en.wikipedia.org/wiki/Dave_Hyatt">David Hyatt, a Safari and WebKit architect.</a></p>
<p><strong>Avoid universal rules</strong></p>
<p>It is recommended that you use ID, class and tag selectors.</p>
<p><strong>Don’t qualify ID-categorized rules with tag names or classes</strong></p>
<p>Since there can only be one element in the page with a given ID, there is no need to add extra qualifiers.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">NO – div<span style="color: #cc00cc;">#foobar</span>
YES  - <span style="color: #cc00cc;">#foobar</span></pre></td></tr></table></div>

<p><strong>Don’t qualify class –categorized rules with tag names</strong></p>
<p>Instead of qualifying class selectors for specific tags, extend the class name to be specific to the use case.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">NO – div<span style="color: #6666ff;">.foobar</span>
YES - .foobar</pre></td></tr></table></div>

<p><strong>Try to put rules into the most specific category you can</strong></p>
<p>Don&#8217;t be tempted to build long selectors, because it can be the biggest slowdown in the system. It is better to be specific and add a class to the appropriate elements.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">NO – ul li a
YES – .list-anchor</pre></td></tr></table></div>

<p><strong>Avoid descendant selectors</strong></p>
<p>Watch out for this one, this is really an expensive operation and should be avoided. This can often be replaced with a child selector.</p>
<p><strong>Avoid tag-child selectors</strong></p>
<p>Don’t use a child selector that is based on a tag, it is better to use a class that is associated with the tag; this can really improve the matching time.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">NO - <span style="color: #cc00cc;">#foobar</span> <span style="color: #00AA00;">&gt;</span> li <span style="color: #00AA00;">&gt;</span> a
YES - .foobar-anchor</pre></td></tr></table></div>

<p><strong>Question all usages of the child selector</strong></p>
<p>In general you should be careful about the child selector and it would be a good idea to avoid them.</p>
<p><strong>Rely on inheritance</strong></p>
<p>Learn which properties are inherited, and avoid rules that specify these inherited styles.</p>
<p><strong>Should I use them?</strong></p>
<p>Well, the answer is; it depends. First you need to be sure that you are focusing on the right problem. Not all pages will be affected, it basically comes down too how much stuff you put into it. The more complex, the greater the chance is that your page will be slowed down significantly (<em>a couple of seconds</em>).</p>
<p>Second, not all complex selectors affect performance, because it comes down to the key selector, the rightmost argument.</p>
<p>Consider this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">Div div div a<span style="color: #6666ff;">.myclass</span> <span style="color: #00AA00;">&#123;</span>…<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>This can be ok, because our key selector is <em>a.myclass</em>, which might only match one or few elements.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #6666ff;">.myclass</span> <span style="color: #00AA00;">*</span> <span style="color: #00AA00;">&#123;</span>…<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>However, this is really bad because it will check every element to see if it is descendant of an anchor with the given class.</p>
<p><strong>Final Word</strong></p>
<p>You might be thinking what about the CSS quires, which I use with my favourite javascript library.  Those libraries read left to right because that is the way the DOM APIs work.  So the guidelines mentioned above is only how the browser will render CSS, so when you are working with stylesheets, focus on right to left.</p>
<p>And be sure to focus on the once that are the most expensive once.</p>
<p><strong>Further reading</strong></p>
<p><a href="http://www.youtube.com/watch?v=a2_6bGNZ7bA">Faster HTML and CSS: Layout Engine Internals for Web Developers</a></p>
<p><a href="http://www.stevesouders.com/blog/2009/06/18/simplifying-css-selectors/">Simplifying CSS Selectors</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/_6MyMwORuH0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/guidelines-for-writing-efficient-css-selectors/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/guidelines-for-writing-efficient-css-selectors/</feedburner:origLink></item>
		<item>
		<title>Removing the Page directive from you Views in Asp.Net Mvc</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/asKSlAtgfIQ/</link>
		<comments>http://fatagnus.com/removing-the-page-directive-from-you-views-in-aspnet-mvc/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 15:38:52 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Asp.NET]]></category>

		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=387</guid>
		<description><![CDATA[Phil Haack shows how you can remove the Page directive from the view, more or less. I have known for a long time that you could register your user controls in your web.config but never thought that this could be possible. It is quite nice.
]]></description>
			<content:encoded><![CDATA[<p>Phil Haack shows <a href="http://haacked.com/archive/2009/08/04/views-on-a-diet.aspx">how you can remove the Page directive from the view</a>, more or less. I have known for a long time that you could <a href="http://fatagnus.com/register-your-user-and-custom-controls-in-the-web-config/">register your user controls in your web.config</a> but never thought that this could be possible. It is quite nice.</p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/asKSlAtgfIQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/removing-the-page-directive-from-you-views-in-aspnet-mvc/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/removing-the-page-directive-from-you-views-in-aspnet-mvc/</feedburner:origLink></item>
		<item>
		<title>Quiz – Byte iteration in C#</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/2PeeN35N8go/</link>
		<comments>http://fatagnus.com/quiz-%e2%80%93-byte-iteration-in-csharp/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 10:21:39 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Quiz]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=374</guid>
		<description><![CDATA[At some job interviews you will be asked to explain a small piece of code, so I thought it could be fun to post some on my blog. If anyone has other examples, please let me know.
Question
How many times will Console.Write() be called ?

1
2
3
4
for &#40;byte i = byte.MinValue; i &#60;= byte.MaxValue; i++&#41;  
&#123;
	Console.Write&#40;i&#41;;
&#125;

A little [...]]]></description>
			<content:encoded><![CDATA[<p>At some job interviews you will be asked to explain a small piece of code, so I thought it could be fun to post some on my blog. If anyone has other examples, please let me know.</p>
<p><strong>Question</strong></p>
<p>How many times will Console.Write() be called ?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">byte</span>.<span style="color: #0000FF;">MinValue</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">byte</span>.<span style="color: #0000FF;">MaxValue</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>  
<span style="color: #000000;">&#123;</span>
	Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>A little tip</strong><br />
<a href="http://msdn.microsoft.com/en-us/library/system.byte.aspx">Information about Byte Structure</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/2PeeN35N8go" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/quiz-%e2%80%93-byte-iteration-in-csharp/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/quiz-%e2%80%93-byte-iteration-in-csharp/</feedburner:origLink></item>
		<item>
		<title>The Vendor Client relationship</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/bzpFYIae9Cc/</link>
		<comments>http://fatagnus.com/the-vendor-client-relationship/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 09:11:00 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Business]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=366</guid>
		<description><![CDATA[Have you sat though a meeting with a client where you are trying to figure out the requirements for a project and experienced the following situation, where they want all the bells and whistles but don’t really want to pay for it ?
This is something I have heard a lot in some form of variation. [...]]]></description>
			<content:encoded><![CDATA[<p>Have you sat though a meeting with a client where you are trying to figure out the requirements for a project and experienced the following situation, where they want all the bells and whistles but don’t really want to pay for it ?</p>
<p>This is something I have heard a lot in some form of variation. For some reason, it doesn’t seem to happen any other place. Why is that?</p>
<p><a href="http://www.youtube.com/watch?v=R2a8TRSgzZY">But what if it did. </a></p>
<p><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/R2a8TRSgzZY&#038;hl=en&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/R2a8TRSgzZY&#038;hl=en&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/bzpFYIae9Cc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/the-vendor-client-relationship/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/the-vendor-client-relationship/</feedburner:origLink></item>
		<item>
		<title>Implementing some useful Enumerable Extension methods from Ruby</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/wbslRm_DmdM/</link>
		<comments>http://fatagnus.com/implementing-some-useful-enumerable-extension-methods-from-ruby/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 20:00:05 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=355</guid>
		<description><![CDATA[I don&#8217;t know much about Ruby, but I am huge fan of PrototypeJS and therefore I decided to implement some of the Enumerable methods that are found within that framework (which actually comes from Ruby). 
I have made two extension classes; one is called EnumerableExtension and the other ArrayExtension. I have submitted the enum extension [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t know much about Ruby, but I am huge fan of <a href="http://www.prototypejs.org/">PrototypeJS</a> and therefore I decided to implement some of the Enumerable methods that are found within that framework (<em>which actually comes from Ruby</em>). </p>
<p>I have made two extension classes; one is called EnumerableExtension and the other ArrayExtension. I have submitted the enum extension to the <a href="http://www.codeplex.com/MVCContrib">MvcContrib Project</a>, but for some reason it seems like I have forgotten the array extension, so it might get submitted later on.</p>
<p>The PrototypeJS Api has some <a href=" http://www.prototypejs.org/api/enumerable">great examples on how these methods work</a>, so if you want to learn more about them, then you should check that out.</p>
<p>I hope that you will find them useful.</p>
<p><strong>The Enumerable Extensions</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> EnumerableExtension
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Returns all the elements that match the filter.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TEnumerable&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumerable&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;filter&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> GRep<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> enumerable, <span style="color: #FF0000;">string</span> filter<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var regex <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Regex<span style="color: #000000;">&#40;</span>filter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>TEnumerable item <span style="color: #0600FF;">in</span> enumerable<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>regex.<span style="color: #0000FF;">IsMatch</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                yield <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Returns the results of applying the iterator to each element.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TEnumerable&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T2&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumerable&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;func&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T2<span style="color: #008000;">&gt;</span> Map<span style="color: #008000;">&lt;</span>TEnumerable, T2<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> enumerable, Func<span style="color: #008000;">&lt;</span>TEnumerable, T2<span style="color: #008000;">&gt;</span> func<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>TEnumerable item <span style="color: #0600FF;">in</span> enumerable<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            yield <span style="color: #0600FF;">return</span> func<span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Returns all the elements for which the iterator returned false.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumerable&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;match&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> Reject<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> enumerable, Predicate<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> match<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>T item <span style="color: #0600FF;">in</span> enumerable<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>match<span style="color: #000000;">&#40;</span>item<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                yield <span style="color: #0600FF;">return</span> item<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Incrementally builds a result value based on the successive results of the iterator. This can be used for array construction, numerical sums/averages, etc.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TEnumerable&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TMemo&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumerable&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;accumulator&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;func&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TMemo Inject<span style="color: #008000;">&lt;</span>TEnumerable, TMemo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> enumerable, TMemo accumulator, Func<span style="color: #008000;">&lt;</span>TMemo, TEnumerable, TMemo<span style="color: #008000;">&gt;</span> func<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        enumerable.<span style="color: #0000FF;">Each</span><span style="color: #000000;">&#40;</span>item <span style="color: #008000;">=&gt;</span> accumulator <span style="color: #008000;">=</span> func<span style="color: #000000;">&#40;</span>accumulator, item<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> accumulator<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Incrementally builds a result value based on the successive results of the iterator. This can be used for array construction, numerical sums/averages, etc.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TEnumerable&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TMemo&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumerable&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;accumulator&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;func&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> TMemo Inject<span style="color: #008000;">&lt;</span>TEnumerable, TMemo<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> enumerable, TMemo accumulator, Func<span style="color: #008000;">&lt;</span>TMemo, TEnumerable, <span style="color: #FF0000;">int</span>, TMemo<span style="color: #008000;">&gt;</span> func<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        enumerable.<span style="color: #0000FF;">Each</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>item, index<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> accumulator <span style="color: #008000;">=</span> func<span style="color: #000000;">&#40;</span>accumulator, item, index<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> accumulator<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// It lets you iterate over all the elements in a generic fashion, then returns the Enumerable, thereby allowing chain-calling.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TEnumerable&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumerable&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;iterator&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Each<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> enumerable, Action<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> iterator<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> enumerable.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            iterator<span style="color: #000000;">&#40;</span>enumerable.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// It lets you iterate over all the elements in a generic fashion, then returns the Enumerable, thereby allowing chain-calling.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;TEnumerable&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;enumerable&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;iterator&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Each<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>TEnumerable<span style="color: #008000;">&gt;</span> enumerable, Action<span style="color: #008000;">&lt;</span>TEnumerable, <span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> iterator<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var index <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> index <span style="color: #008000;">&lt;</span> enumerable.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> index<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            iterator<span style="color: #000000;">&#40;</span>enumerable.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span>index<span style="color: #000000;">&#93;</span>, index<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ToJSON<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> enumerable<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var serializer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> JavaScriptSerializer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">return</span> serializer.<span style="color: #0000FF;">Serialize</span><span style="color: #000000;">&#40;</span>enumerable<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>The Array Extension</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> ArrayExtension
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Produces a new version of the array that does not contain any of the specified values.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;typeparam name=&quot;T&quot;&gt;&lt;/typeparam&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;array&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;values&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> T<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> Without<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> T<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> array, <span style="color: #0600FF;">params</span> T<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> values<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> array.<span style="color: #0000FF;">Reject</span><span style="color: #000000;">&#40;</span>t <span style="color: #008000;">=&gt;</span> values.<span style="color: #0000FF;">Contains</span><span style="color: #000000;">&#40;</span>t<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Further Reading</strong></p>
<p><a href="http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx">More about the keyword Yield</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">Extension Methods</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/wbslRm_DmdM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/implementing-some-useful-enumerable-extension-methods-from-ruby/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/implementing-some-useful-enumerable-extension-methods-from-ruby/</feedburner:origLink></item>
		<item>
		<title>Solskogen 2009 – My first demo party in years</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/Iw2fpf-MJHg/</link>
		<comments>http://fatagnus.com/solskogen-2009-%e2%80%93-my-first-demo-party-in-years/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 12:22:16 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=348</guid>
		<description><![CDATA[Last time I went to a demoparty was back in the days, when the Amiga was the King. Back then I had an Amiga 500, the best computer I have ever owned. It is nice to be back on the Scene again with a few of the members from my demo group (Fnuque).
The party is [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I went to a <a href="http://en.wikipedia.org/wiki/Demoparty">demoparty</a> was back in the days, when the <a href="http://en.wikipedia.org/wiki/Amiga">Amiga</a> was the King. Back then I had an <a href="http://en.wikipedia.org/wiki/Amiga_500">Amiga 500</a>, the best computer I have ever owned. It is nice to be back on the <a href="http://en.wikipedia.org/wiki/Demoscene">Scene</a> again with a few of the members from my demo group (<a href="http://www.fnuque.dk">Fnuque</a>).</p>
<p>The party is in Norway, so it took awhile to get here (<em>I live in Denmark</em>), but it is worth it. We wont compete in any of the demo compos, since we haven’t got anything prepared, but one of our musicians is going to compete in the 16k executable compo.</p>
<p>Here are a few cool 4k intros</p>
<p><a href="http://www.demoscene.tv/prod.php?id_prod=13519">Receptor by tbc</a></p>
<p><a href="http://www.demoscene.tv/prod.php?id_prod=13718">Evelated by rgba and tbc</a></p>
<p><a href="http://www.demoscene.tv/prod.php?id_prod=13706">Luminagia by Loonies</a></p>
<p>It is amazing to see what people can get out of just 4kb</p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/Iw2fpf-MJHg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/solskogen-2009-%e2%80%93-my-first-demo-party-in-years/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/solskogen-2009-%e2%80%93-my-first-demo-party-in-years/</feedburner:origLink></item>
		<item>
		<title>How to serve the same data in Json, Xml or Html with Asp.Net Mvc revised</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/jXN91Xlvf4o/</link>
		<comments>http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc-revised/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 16:50:51 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=341</guid>
		<description><![CDATA[After I had blogged about my first example I talked with Eric Hexter (from the mvccontrib team) and Christian Dalager. Christian pointed out that there is an issue with the paths that doesn&#8217;t act in the same way as it does in Rails.
FormatController did something like this, when handling parameters, “/home/view.xml/4” when it should have [...]]]></description>
			<content:encoded><![CDATA[<p>After I had blogged about <a href="http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc/">my first example</a> I talked with Eric Hexter (<em>from the mvccontrib team</em>) and Christian Dalager. Christian pointed out that there is an issue with the paths that doesn&#8217;t act in the same way as it does in Rails.</p>
<p>FormatController did something like this, when handling parameters, <em>“/home/view.xml/4”</em> when it should have done <em>“/home/view/4.xml”</em>.</p>
<p>Another thing that also needed to change was the requirement to inherit from the base class before it would work.</p>
<p>Now you can decorate your controller or action with an attribute that will handle how your data will be rendered. It is also possible to disallow one or more of the formats.</p>
<p>I have of course submitted it to <a href="http://mvccontrib.org">MvcContrib</a>, but I don’t know if it will or when it will make it into the project. So, I hope that someone can benefit from my post in the meantime.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>AttributeUsage<span style="color: #000000;">&#40;</span>AttributeTargets.<span style="color: #FF0000;">Class</span> <span style="color: #008000;">|</span> AttributeTargets.<span style="color: #0000FF;">Method</span>, Inherited <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span>, AllowMultiple <span style="color: #008000;">=</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> FormatFilterAttribute <span style="color: #008000;">:</span> FilterAttribute, IActionFilter
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// The type of files we can server the requested content in</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">enum</span> FileFormat <span style="color: #000000;">&#123;</span> Html, Json, Xml <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> FormatFilterAttribute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        Disallow <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
        RequestedFormat <span style="color: #008000;">=</span> FileFormat.<span style="color: #0000FF;">Html</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Formats to disallow can be either Html, Json or Xml. Use comma to seperate multiple formats.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Disallow <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// The format that has been requested</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF;">private</span> FileFormat RequestedFormat <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Occurs before an action is executed</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;filterContext&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> OnActionExecuting<span style="color: #000000;">&#40;</span>ActionExecutingContext filterContext<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        FileFormat format <span style="color: #008000;">=</span> GetFileFormat<span style="color: #000000;">&#40;</span>filterContext.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Path</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>IsDisallowed<span style="color: #000000;">&#40;</span>format<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ArgumentException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Requested format has been disallowed&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        RequestedFormat <span style="color: #008000;">=</span> format<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Occurs after an action is executed</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;filterContext&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> OnActionExecuted<span style="color: #000000;">&#40;</span>ActionExecutedContext filterContext<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #000000;">&#40;</span>filterContext.<span style="color: #0000FF;">Result</span> <span style="color: #008000;">is</span> ViewResult<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;You need to call the View method, when the FormatFilter attribute is applied&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        var view <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>ViewResult<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>filterContext.<span style="color: #0000FF;">Result</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>            
        filterContext.<span style="color: #0000FF;">Result</span> <span style="color: #008000;">=</span> FormatViewResult<span style="color: #000000;">&#40;</span>view<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Verifies if the format has been disallowed</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;format&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> IsDisallowed<span style="color: #000000;">&#40;</span>FileFormat format<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> Disallow.<span style="color: #0000FF;">Split</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">','</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Any</span><span style="color: #000000;">&#40;</span>s <span style="color: #008000;">=&gt;</span> s.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> format.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Verifies that the requested format is one that can be servered</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;ext&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> IsValidFileExtension<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> ext<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">GetNames</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>FileFormat<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Any</span><span style="color: #000000;">&#40;</span>format <span style="color: #008000;">=&gt;</span> format.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> ext.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>1<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// </span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;path&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
    <span style="color: #0600FF;">private</span> FileFormat GetFileFormat<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> path<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">string</span> ext <span style="color: #008000;">=</span> Path.<span style="color: #0000FF;">GetExtension</span><span style="color: #000000;">&#40;</span>path<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>ext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> FileFormat.<span style="color: #0000FF;">Html</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>IsValidFileExtension<span style="color: #000000;">&#40;</span>ext<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> FormatException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Requested format is not available&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>FileFormat<span style="color: #000000;">&#41;</span><span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>FileFormat<span style="color: #000000;">&#41;</span>, ext.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>1<span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">private</span> ActionResult FormatViewResult<span style="color: #000000;">&#40;</span>ViewResultBase view<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>RequestedFormat<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">case</span> FileFormat.<span style="color: #0000FF;">Html</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF;">return</span> view<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">case</span> FileFormat.<span style="color: #0000FF;">Json</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> JsonResult <span style="color: #000000;">&#123;</span> Data <span style="color: #008000;">=</span> view.<span style="color: #0000FF;">ViewData</span>.<span style="color: #0000FF;">Model</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">case</span> FileFormat.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> XmlResult<span style="color: #000000;">&#40;</span>view.<span style="color: #0000FF;">ViewData</span>.<span style="color: #0000FF;">Model</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">default</span><span style="color: #008000;">:</span>
                <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> FormatException<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Cannot server the content in the request format: &quot;</span>, RequestedFormat<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>FormatFilter<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController <span style="color: #008000;">:</span> Controller
<span style="color: #000000;">&#123;</span>      
    <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var pizzas <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
                         <span style="color: #000000;">&#123;</span>
                             <span style="color: #008000;">new</span> Pizza <span style="color: #000000;">&#123;</span>Number <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>, Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Pizza 1&quot;</span>, Price <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;$10&quot;</span><span style="color: #000000;">&#125;</span>,
                             <span style="color: #008000;">new</span> Pizza <span style="color: #000000;">&#123;</span>Number <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>, Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Pizza 2&quot;</span>, Price <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;$13&quot;</span><span style="color: #000000;">&#125;</span>,
                             <span style="color: #008000;">new</span> Pizza <span style="color: #000000;">&#123;</span>Number <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>, Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Pizza 3&quot;</span>, Price <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;$20&quot;</span><span style="color: #000000;">&#125;</span>
                         <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>pizzas<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Working with parameters</strong></p>
<p>If you are going send data, like an ID, to your Action then you need to add an extra route or else it might not get parse correctly.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Add this to Global.asax</span>
routes.<span style="color: #0000FF;">MapRoute</span><span style="color: #000000;">&#40;</span>
	<span style="color: #666666;">&quot;Format&quot;</span>,
	<span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#123;</span>controller<span style="color: #000000;">&#125;</span><span style="color: #008000;">/</span><span style="color: #000000;">&#123;</span>action<span style="color: #000000;">&#125;</span><span style="color: #008000;">/</span><span style="color: #000000;">&#123;</span>id<span style="color: #000000;">&#125;</span>.<span style="color: #000000;">&#123;</span>format<span style="color: #000000;">&#125;</span><span style="color: #666666;">&quot;&quot;</span>,
	<span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span>id <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span>, format <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/jXN91Xlvf4o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc-revised/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc-revised/</feedburner:origLink></item>
		<item>
		<title>Unrolling your loop for better performance in Javascript</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/imISRbvwJ9w/</link>
		<comments>http://fatagnus.com/unrolling-your-loop-for-better-performance-in-javascript/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 19:40:37 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Design Pattern]]></category>

		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=330</guid>
		<description><![CDATA[Loop unrolling, is a loop transformation technique that attempts to optimize a program&#8217;s execution speed at the expense of its size. – Wikipedia
What this means is that we are trying to limit the number of iterations to mitigate the overhead in a loop.
Consider this.

1
2
3
4
var i=values.length;
while&#40;i--&#41;&#123;
	processData&#40;values&#91;i&#93;&#41;;
&#125;

If we say that there are only 5 items in the [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Loop unrolling, is a loop transformation technique that attempts to optimize a program&#8217;s execution speed at the expense of its size. – <a href=" http://en.wikipedia.org/wiki/Loop_unwinding">Wikipedia</a></p></blockquote>
<p>What this means is that we are trying to limit the number of iterations to mitigate the overhead in a loop.</p>
<p>Consider this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span>values.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	processData<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If we say that there are only 5 items in the array that will make it easier for us to unroll it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">processData<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
processData<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>1<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
processData<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>2<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
processData<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>3<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
processData<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>4<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It isn’t pretty and it would probably be annoying to maintain such an approach if the size of the array grow or shrinks. The performance gain for this example wont make the downside worth it, but if the array was quite large then it would be useful, but still even more annoying to maintain.</p>
<p><strong>Tom Duff to the rescue</strong></p>
<p><a href="http://en.wikipedia.org/wiki/Tom_Duff">Tom</a> came up with a brilliant solution/pattern for unrolling loops in C, which got the name <a href="http://en.wikipedia.org/wiki/Duff%27s_device">Duff’s Device</a> and was later on <a href="http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html#straightasapretzel">converted to Javascript by Jeff Greenberg</a>.</p>
<p>Here is the javascript version</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iterations <span style="color: #339933;">=</span> Math.<span style="color: #660066;">ceil</span><span style="color: #009900;">&#40;</span>values.<span style="color: #660066;">length</span> <span style="color: #339933;">/</span> 8<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> startAt <span style="color: #339933;">=</span> values.<span style="color: #660066;">length</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #000066; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>     
	<span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>startAt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 0<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 7<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 6<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 5<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 4<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 3<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 2<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         
		<span style="color: #000066; font-weight: bold;">case</span> 1<span style="color: #339933;">:</span> process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	<span style="color: #009900;">&#125;</span>     
	startAt <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>iterations <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The idea with this pattern is that each trip through the loop does the work of 1-8 iterations of a normal loop and Tom also figured out that 8 was the optimal number to use. A very important aspect of this pattern is the use of modulus, because not all arrays will be divisible by 8.</p>
<p>This technique might look a bit strange but it will actually run faster then a normal loop. How ever, we can make it run even faster, if we remove the switch statement from the loop, because conditionals have a performance overhead.</p>
<p>This example was introduced in <a href="http://www.amazon.com/gp/product/0735713243?ie=UTF8&#038;tag=fatagnus-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0735713243">Speed Up Your Site by Andrew B. King</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> iterations <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>values.<span style="color: #660066;">length</span> <span style="color: #339933;">/</span> 8<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> leftover <span style="color: #339933;">=</span> values.<span style="color: #660066;">length</span> <span style="color: #339933;">%</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span> 
<span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>  
&nbsp;
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>leftover <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>     
	<span style="color: #000066; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>         
		process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>leftover <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>  
&nbsp;
<span style="color: #000066; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     
	process<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">--</span>iterations <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>This optimized Duff&#8217;s Device is 39 percent faster than the original and 67 percent faster than a normal for loop. - <a href="http://www.amazon.com/gp/product/0735713243?ie=UTF8&#038;tag=fatagnus-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0735713243">Speed Up Your Site, Andrew B. King</a></p></blockquote>
<p><strong>Finale word</strong></p>
<p>Before you go and change all of your loops, you should consider if you really need to use this pattern. For small arrays the performance gain is to small to have any greater impact, so you should only applied this technique when you find a bottleneck that is related to a loop.</p>
<p><strong>Further Reading</strong></p>
<p><a href="http://www.amazon.com/gp/product/0596522304?ie=UTF8&#038;tag=fatagnus-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596522304">Even Faster Web Sites: Performance Best Practices for Web Developers</a><img src="http://www.assoc-amazon.com/e/ir?t=fatagnus-20&#038;l=as2&#038;o=1&#038;a=0596522304" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important; display: none;" /></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/imISRbvwJ9w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/unrolling-your-loop-for-better-performance-in-javascript/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/unrolling-your-loop-for-better-performance-in-javascript/</feedburner:origLink></item>
		<item>
		<title>How to serve the same data in Json, Xml or Html with Asp.Net MVC</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/-QY--TURI44/</link>
		<comments>http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 20:45:34 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Asp.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=301</guid>
		<description><![CDATA[A revised version can be found here
About four weeks ago I went to a small event for developers where one of the speakers (christian dalager) compared Ruby on Rails to Asp.Net Mvc. One of the things that really caught my attention in Ruby on Rails was the ability to have an Action serve content in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc-revised/">A revised version can be found here</a></p>
<p>About four weeks ago I went to a <a href="http://communityday.in/copenhagen">small event for developers</a> where <a href="http://dalager.com/blog/">one of the speakers (christian dalager)</a> compared Ruby on Rails to Asp.Net Mvc. One of the things that really caught my attention in Ruby on Rails was the ability to have an Action serve content in various formats like Json, Xml or Html just by changing the file extension of the requested resource.</p>
<p>I thought it would be cool if you could do something similar in Asp.net MVC and found it to be quite easy, however if you want to serve your content in Xml, you need to <a href="http://mvccontrib.codeplex.com">download MvcContrib</a>.</p>
<p>To do this, we need to create a new controller that we can inherit from.</p>
<p><strong>The Controller</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// A base controller that will enable you to serve your data in either Json, Xml, Html</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> FormatController <span style="color: #008000;">:</span> Controller
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// The Key to extract the format from Route Data</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">string</span> FORMAT_KEY <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Format&quot;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// The type of files we can server the requested content in</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> FileFormat <span style="color: #000000;">&#123;</span> Html, Json, Xml <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">protected</span> FormatController<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// Our Default format</span>
		RequestedFormat <span style="color: #008000;">=</span> FileFormat.<span style="color: #0000FF;">Html</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// The format that has been requested</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #0600FF;">protected</span> FileFormat RequestedFormat <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Occurs before an action is executed</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;filterContext&quot;&gt;&lt;/param&gt;</span>
	<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnActionExecuting<span style="color: #000000;">&#40;</span>ActionExecutingContext filterContext<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnActionExecuting</span><span style="color: #000000;">&#40;</span>filterContext<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		var routeValues <span style="color: #008000;">=</span> filterContext.<span style="color: #0000FF;">RouteData</span>.<span style="color: #0000FF;">Values</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>routeValues.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span>FORMAT_KEY<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			var requestedFormat <span style="color: #008000;">=</span> routeValues<span style="color: #000000;">&#91;</span>FORMAT_KEY<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsValidFormat<span style="color: #000000;">&#40;</span>requestedFormat<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				RequestedFormat <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>FileFormat<span style="color: #000000;">&#41;</span><span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>FileFormat<span style="color: #000000;">&#41;</span>, requestedFormat, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Verifies that the requested format is one that can be servered</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;requestedFormat&quot;&gt;&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;&lt;/returns&gt;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> IsValidFormat<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> requestedFormat<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #FF0000;">Enum</span>.<span style="color: #0000FF;">GetNames</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>FileFormat<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Any</span><span style="color: #000000;">&#40;</span>format <span style="color: #008000;">=&gt;</span> format.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> requestedFormat.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// Returns the content in the requested format</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;viewModel&quot;&gt;Viewmodel&lt;/param&gt;</span>
	<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;ActionResult&lt;/returns&gt;</span>
	<span style="color: #0600FF;">protected</span> ActionResult FormatView<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> viewModel<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>RequestedFormat<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">case</span> FileFormat.<span style="color: #0000FF;">Html</span><span style="color: #008000;">:</span>
				<span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>viewModel<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">case</span> FileFormat.<span style="color: #0000FF;">Json</span><span style="color: #008000;">:</span>
				<span style="color: #0600FF;">return</span> Json<span style="color: #000000;">&#40;</span>viewModel<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">case</span> FileFormat.<span style="color: #0000FF;">Xml</span><span style="color: #008000;">:</span>
				<span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> XmlResult<span style="color: #000000;">&#40;</span>viewModel<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">default</span><span style="color: #008000;">:</span>
				<span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> FormatException<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Concat</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Cannot server the content in the request format: &quot;</span>, RequestedFormat<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Setting it up</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Pizza
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Number <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Price <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController <span style="color: #008000;">:</span> FormatController
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		var pizzas <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #008000;">new</span> Pizza <span style="color: #000000;">&#123;</span>Number <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>, Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Pizza 1&quot;</span>, Price <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;$10&quot;</span><span style="color: #000000;">&#125;</span>,
				<span style="color: #008000;">new</span> Pizza <span style="color: #000000;">&#123;</span>Number <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>, Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Pizza 2&quot;</span>, Price <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;$13&quot;</span><span style="color: #000000;">&#125;</span>,
				<span style="color: #008000;">new</span> Pizza <span style="color: #000000;">&#123;</span>Number <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>, Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Pizza 3&quot;</span>, Price <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;$20&quot;</span><span style="color: #000000;">&#125;</span>
			<span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> FormatView<span style="color: #000000;">&#40;</span>pizzas<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Add this to Global.asax</span>
routes.<span style="color: #0000FF;">MapRoute</span><span style="color: #000000;">&#40;</span>
	<span style="color: #666666;">&quot;Format&quot;</span>,
	<span style="color: #666666;">&quot;{controller}/{action}.{format}/{id}&quot;</span>,
	<span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span>id <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Fire up your application and then type something like “<em>home/index.xml</em>” or “<em>home/index.json</em>” in the browser.</p>
<p><strong>Finale word</strong></p>
<p>Imaging that on your website you have different categories that contain products, by applying this technique you will be able to create a <em><a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST</a> like webservice</em> for your users in no time, everyone will be able to integrate your website into their own.</p>
<p>Sharing data has never been easier, this is so awesome.</p>
<p>I have submittet this piece of code (<em>and some other stuff</em>) to MvcContrib, I hope that it makes into the project.</p>
<p><strong>Further reading</strong><br />
<a href="http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc-revised/">A revised version can be found here</a><br />
<a href="http://msdn.microsoft.com/en-us/library/dd410269.aspx">Controllers and Action Methods in MVC Applications</a></p>
<p><a href="http://www.json.org/">JSON (JavaScript Object Notation)</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/-QY--TURI44" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/how-to-serve-the-same-data-in-json-xml-or-html-with-aspnet-mvc/</feedburner:origLink></item>
	</channel>
</rss>
