<?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>Sat, 18 Jul 2009 11:04:04 +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>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[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 various formats like Json, Xml or [...]]]></description>
			<content:encoded><![CDATA[<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://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>
		<item>
		<title>Hello Twitter World</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/WSrHsrGw8u8/</link>
		<comments>http://fatagnus.com/hello-twitter-world/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 16:46:35 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=314</guid>
		<description><![CDATA[A guy named Adam just wrote me and asked if I could make my blog available through Twitter and my first thought was “what a great idea”. So, my next thought was, “how do I do this” and it was actually pretty easy. I added my feed to Twitterfeed.com and of course created an account [...]]]></description>
			<content:encoded><![CDATA[<p>A guy named <a href="http://twitter.com/mindmodel">Adam</a> just wrote me and asked if I could make my blog available through <a href="http://www.Twitter.com">Twitter</a> and my first thought was “<em>what a great idea</em>”. So, my next thought was, “<em>how do I do this</em>” and it was actually pretty easy. I added my feed to <a href="http://www.twitterfeed.com">Twitterfeed.com</a> and of course created an account on <a href="http://www.Twitter.com">Twitter</a> and now I am ready to conquer the world, woohoo (<em>hope it works :p</em>).</p>
<p><a href="http://twitter.com/fatagnus">Find me here @fatagnus</a></p>
<p>So thank you Adam for the idea :) </p>
<p><a href="http://fatagnus.com/contact/">If anyone else has an idea or have a question, please contact me.</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/WSrHsrGw8u8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/hello-twitter-world/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/hello-twitter-world/</feedburner:origLink></item>
		<item>
		<title>Nate Koechley talks about Professional Frontend Engineering</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/t4yCzBaG5Gw/</link>
		<comments>http://fatagnus.com/nate-koechley-talks-about-professional-frontend-engineering/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 12:46:30 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Css]]></category>

		<category><![CDATA[Javascript]]></category>

		<category><![CDATA[Technology]]></category>

		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=294</guid>
		<description><![CDATA[Over the years I have been involved in doing both backend and frontend development, but for some reason there seems to be a lack of understanding and maybe even respect for what Frontend Engineers do.
Maybe it is because that some people think that doing Html, Css, Javascript and so forth is easy, but in reality [...]]]></description>
			<content:encoded><![CDATA[<p>Over the years I have been involved in doing both backend and frontend development, but for some reason there seems to be a lack of understanding and maybe even respect for what Frontend Engineers do.</p>
<p>Maybe it is because that some people think that doing Html, Css, Javascript and so forth is easy, but in reality it can be really difficult, at least if you want to have it done right.</p>
<blockquote><p>Browsers are the most hostile software development environment imaginable.<br />
<em>– <a href="http://en.wikipedia.org/wiki/Douglas_Crockford">Douglas Crockford</a></em></p></blockquote>
<p>Douglas said this when he was talking about browsers on desktop computers, but he later on changed his mind when he saw how much worse it was on mobile.</p>
<p>There are so many things to keep track of when doing frontend development and you don’t have the same benefit as the Backend people do, like compiling your stuff, so you cant really trust what is going on.</p>
<p>In Nate Koechleys’s speech he sums up how many things that a frontend engineer needs to balance out.</p>
<blockquote><p>A conservative number may be 672 different things we’re trying to balance as we built out these websites. – <a href="http://nate.koechley.com/blog/about/">Nate Koechley</a></p></blockquote>
<p>The number doesn’t include things like usability, performance and security.</p>
<p>In my mind, frontend engineers should get lots more credit for the work they do.</p>
<p><a href="http://video.yahoo.com/watch/4671445/12486762">Watch Nate Koechley speak</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/t4yCzBaG5Gw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/nate-koechley-talks-about-professional-frontend-engineering/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/nate-koechley-talks-about-professional-frontend-engineering/</feedburner:origLink></item>
		<item>
		<title>The Observer Pattern in C#</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/xQNeyeSkPCA/</link>
		<comments>http://fatagnus.com/the-observer-pattern-in-csharp/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 18:12:46 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Design Pattern]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=283</guid>
		<description><![CDATA[There are a few ways to implement this pattern and I will show you two different ways to do it, but first things first.
The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. - Head First, Design Patterns
When you are [...]]]></description>
			<content:encoded><![CDATA[<p>There are a few ways to implement this pattern and I will show you two different ways to do it, but first things first.</p>
<blockquote><p>The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. - <a href="http://www.amazon.com/gp/product/0596007124?ie=UTF8&#038;tag=fatagnus-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596007124">Head First, Design Patterns</a></p></blockquote>
<p>When you are working with this pattern you usually have something called a <em>Subject </em>(<em>sometimes referred to as Observerable</em>) and an <em>Observer</em>. The <em>Subject</em> is an object that messages the <em>Observers</em> when its state is changed, just like it says in the above definition.</p>
<p>To illustrate how the pattern works you should think of a newspaper subscription, there you (<em>the observer</em>) subscribe to a newspaper (<em>the subject</em>) which you will receive when ever they send out a new issue. </p>
<p>In the first example we will look at how this pattern can be applied with a few interfaces and in the second example we will use a delegate and an event.</p>
<p>When this pattern is implemented in the right way you will have a <a href="http://en.wikipedia.org/wiki/Coupling_(computer_science)">loosely coupled design</a>, which of course is something we want.</p>
<p><strong>Example 1 – Using interfaces</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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> ISubject
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">void</span> RegisterObserver<span style="color: #000000;">&#40;</span>IObserver o<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">void</span> RemoveObserver<span style="color: #000000;">&#40;</span>IObserver o<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">void</span> NotifyObservers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IObserver
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">void</span> Update<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> story<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NewsPublisher <span style="color: #008000;">:</span> ISubject
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span>IObserver<span style="color: #008000;">&gt;</span> observers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>IObserver<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> archive <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> RegisterObserver<span style="color: #000000;">&#40;</span>IObserver o<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		observers.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>o<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> RemoveObserver<span style="color: #000000;">&#40;</span>IObserver o<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		observers.<span style="color: #0000FF;">Remove</span><span style="color: #000000;">&#40;</span>o<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> NotifyObservers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span>IObserver o <span style="color: #0600FF;">in</span> observers<span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			o.<span style="color: #0000FF;">Update</span><span style="color: #000000;">&#40;</span>archive<span style="color: #000000;">&#91;</span>archive.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> 1<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;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> PublishBreakingNews<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> story<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		archive.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>story<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		ArchiveChanged<span style="color: #000000;">&#40;</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> <span style="color: #0600FF;">void</span> ArchiveChanged<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		NotifyObservers<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>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NewsSubscriber <span style="color: #008000;">:</span> IObserver
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Update<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> story<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;--------------------- Breaking News ---------------------&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>story<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">WriteLine</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>
&nbsp;
<span style="color: #FF0000;">class</span> Program
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		NewsSubscriber me <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NewsSubscriber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		NewsPublisher publisher <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NewsPublisher<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		publisher.<span style="color: #0000FF;">RegisterObserver</span><span style="color: #000000;">&#40;</span>me<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		publisher.<span style="color: #0000FF;">PublishBreakingNews</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;hello world&quot;</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>Example 2 – Using delegates and events</strong></p>
<p>This example look a lot like the <a href="http://msdn.microsoft.com/en-us/library/ms954621.aspx#observerpattern_topic3b">Event Pattern</a>, but there are a few conventions that differs like the name convention but also how the signature of the method looks like for the delegate, but since we aren’t using that pattern let’s move on.</p>
<p>I have added a few comments here and there to show the similarities between the previous example and this one.</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
</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;">/// Observer</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NewsSubscriber
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Update<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> story<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;--------------------- Breaking News ---------------------&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>story<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		Console.<span style="color: #0000FF;">WriteLine</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>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
<span style="color: #008080; font-style: italic;">/// Subject</span>
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NewsPublisher
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span> archive <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> NotifyObserversHandler<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> story<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> NotifyObserversHandler ArchiveChanged<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> PublishBreakingNews<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> story<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		archive.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>story<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Notify observers</span>
		ArchiveChanged<span style="color: #000000;">&#40;</span>archive<span style="color: #000000;">&#91;</span>archive.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> 1<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;
<span style="color: #FF0000;">class</span> Program
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		NewsSubscriber me <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NewsSubscriber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		NewsPublisher publisher <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> NewsPublisher<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">//add the delegate to the event (register observer)</span>
		publisher.<span style="color: #0000FF;">ArchiveChanged</span> <span style="color: #008000;">+=</span> me.<span style="color: #0000FF;">Update</span><span style="color: #008000;">;</span>
&nbsp;
		publisher.<span style="color: #0000FF;">PublishBreakingNews</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Hello world&quot;</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>Finale word</strong></p>
<p>As you can see the two examples work pretty much the same way, so it is really up to you to choose which one you prefer. </p>
<p><strong>Further reading</strong></p>
<p><a href="http://www.amazon.com/gp/product/0596007124?ie=UTF8&#038;tag=fatagnus-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596007124">Head First, Design Patterns</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/17sde2xt(VS.80).aspx">Events and Delegates</a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms954621.aspx">Exploring the Observer Design Pattern</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/xQNeyeSkPCA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/the-observer-pattern-in-csharp/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/the-observer-pattern-in-csharp/</feedburner:origLink></item>
		<item>
		<title>The Common Service Locator from Microsoft Patterns &amp; Practices</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/tZ08nzOXZcY/</link>
		<comments>http://fatagnus.com/the-common-service-locator-from-microsoft-patterns-practices/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 20:04:15 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=279</guid>
		<description><![CDATA[The Common Service Locator (CSL) is a helpful library and at first sight it might look like some new cool thing that you need, but it really depends on what you are trying to build. 
Do I need it?
Typically, the answer to this question is no. Once you&#8217;ve decided on a container that suits your [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.codeplex.com/CommonServiceLocator">Common Service Locator</a> (<em>CSL</em>) is a helpful library and at first sight it might look like some new cool thing that you need, but it really depends on what you are trying to build. </p>
<p><strong>Do I need it?</strong></p>
<blockquote><p>Typically, the answer to this question is no. Once you&#8217;ve decided on a container that suits your project, there&#8217;s not a whole lot of benefit from writing your whole application in a way that can switch containers. – The CSL Wiki</p></blockquote>
<p>The library will just provide you with an extra layer of abstraction and is only meant for people who are building frameworks where there is a need for allowing people to specify what kind of <a href="http://en.wikipedia.org/wiki/Dependency_injection">DI</a> (<em>dependency injection</em>) container they want to use, so that they aren’t tied down to using a specific implementation made by the authors.</p>
<p><strong>Using the library and creating the adaptor</strong></p>
<p>There are a few ways to have the adapter implement the interface. You can either use the IServiceLocator interface or you can use ServiceLocatorImplBase, which is used as a convenience for those who wants to implement the IServiceLocator interface.</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
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyServiceLocatorAdapter <span style="color: #008000;">:</span> ServiceLocatorImplBase
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">private</span> IMyContainer container<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> MyServiceLocatorAdapter<span style="color: #000000;">&#40;</span>IMyContainer container<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">container</span> <span style="color: #008000;">=</span> container<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">object</span> DoGetInstance<span style="color: #000000;">&#40;</span>Type serviceType, <span style="color: #FF0000;">string</span> key<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> container.<span style="color: #0000FF;">Resolve</span><span style="color: #000000;">&#40;</span>serviceType, key<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> DoGetAllInstances<span style="color: #000000;">&#40;</span>Type serviceType<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> container.<span style="color: #0000FF;">ResolveAll</span><span style="color: #000000;">&#40;</span>serviceType<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>You only need to implement the two protected methods and then you are good to go. Now you are ready create your own adapter, but you can also find a few that has already been written.</p>
<p><strong>Setting it up</strong></p>
<p>I have changed the above example a bit, so that it will take a UnityContainer.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">IUnityContainer container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UnityContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
container.<span style="color: #0000FF;">RegisterType</span><span style="color: #008000;">&lt;</span>ISomeRepository, SomeRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
MyServiceLocatorAdapter locator <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MyServiceLocatorAdapter<span style="color: #000000;">&#40;</span>container<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
ServiceLocator.<span style="color: #0000FF;">SetLocatorProvider</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> locator<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
ISomeRepository repository <span style="color: #008000;">=</span> ServiceLocator.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">GetInstance</span><span style="color: #008000;">&lt;</span>ISomeRepository<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;;</span></pre></td></tr></table></div>

<p><strong>Finale word</strong></p>
<p>There are of course already a few online, so you might as well take advantage of them instead of starting from scratch.</p>
<p>If you are going to use this, make sure you <a href="http://blogs.msdn.com/miah/archive/2009/05/12/servicelocator-and-unity-be-careful.aspx">check out this blog</a> first because there are a few pitfalls that you need to be aware of.</p>
<p><strong>Further reading</strong></p>
<p><a href="http://msdn.microsoft.com/en-us/library/dd140083.aspx">How to setup the Unity Container</a></p>
<p><a href="http://commonservicelocator.codeplex.com/Wiki/View.aspx?title=API%20Reference">Common Service Locator Wiki</a></p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/tZ08nzOXZcY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/the-common-service-locator-from-microsoft-patterns-practices/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/the-common-service-locator-from-microsoft-patterns-practices/</feedburner:origLink></item>
		<item>
		<title>How to prevent proxy servers from caching your content</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/4WAvSEcDwNs/</link>
		<comments>http://fatagnus.com/how-to-prevent-proxy-servers-from-caching-your-content/#comments</comments>
		<pubDate>Fri, 29 May 2009 20:28:33 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[Asp.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=265</guid>
		<description><![CDATA[A lot of people around the world are sitting behind proxy servers and it can be a good thing, because it reduces the bandwidth costs to a site. However, you don’t really have control over how the proxy server is setup, so you can’t really tell when it will refresh the content. To get around [...]]]></description>
			<content:encoded><![CDATA[<p>A lot of people around the world are sitting behind <a href="http://en.wikipedia.org/wiki/Web_proxy">proxy servers</a> and it can be a good thing, because it reduces the bandwidth costs to a site. However, you don’t really have control over how the proxy server is setup, so you can’t really tell when it will refresh the content. To get around this, you can apply “<em>Cache-Control: private</em>” to the header of the request that is made. Doing so will tell the proxy server (<em>shared cache</em>) that it shouldn’t cache it. </p>
<blockquote><p>Private - Indicates that all or part of the response message is intended for a single user and MUST NOT be cached by a shared cache. This allows an origin server to state that the specified parts of the response are intended for only one user and are not a valid response for requests by other users. A private (<em>non-shared</em>) cache MAY cache the response.<br />
- <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">RFC 2616</a></p></blockquote>
<p>Weather or not to use this really depends on your site and what you are trying to achieve.</p>
<blockquote><p>If you have a large, diverse audience, can afford higher bandwidth costs, and have a reputation for high quality, compress your content and use “<em>Cache-Control: private</em>”. This disables proxies but avoids edge case bugs.<br />
- <a href="http://www.amazon.com/gp/product/0596529309?ie=UTF8&#038;tag=fatagnus-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596529309">High Performance Web Sites, Steve Souders</a><img src="http://www.assoc-amazon.com/e/ir?t=fatagnus-20&#038;l=as2&#038;o=1&#038;a=0596529309" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important; display:none;" /></p></blockquote>
<p>If you can’t afford the bandwidth costs then you shouldn’t do it and furthermore there are no guarantees in life and this will only work if the proxy server honors “<em>private</em>”.</p>
<img src="http://feeds.feedburner.com/~r/FatAgnus/~4/4WAvSEcDwNs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/how-to-prevent-proxy-servers-from-caching-your-content/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/how-to-prevent-proxy-servers-from-caching-your-content/</feedburner:origLink></item>
		<item>
		<title>Using the Repository Pattern with ActiveRecord from Castle</title>
		<link>http://feedproxy.google.com/~r/FatAgnus/~3/7xC0n2G0wCY/</link>
		<comments>http://fatagnus.com/using-the-repository-pattern-with-activerecord-from-castle/#comments</comments>
		<pubDate>Mon, 25 May 2009 19:51:14 +0000</pubDate>
		<dc:creator>Mark Jensen</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[Design Pattern]]></category>

		<category><![CDATA[MonoRail]]></category>

		<guid isPermaLink="false">http://fatagnus.com/?p=237</guid>
		<description><![CDATA[I first learned about ActiveRecord (AR) when I was introduced to the MonoRail framework made by Castle. Then a few years later I started to learn about Domain-Driven Design (DDD) and found that is was an interesting way to design software. Just like so many other software design techniques, DDD also uses design patterns and [...]]]></description>
			<content:encoded><![CDATA[<p>I first learned about <a href="http://www.martinfowler.com/eaaCatalog/activeRecord.html">ActiveRecord</a> (AR) when I was introduced to the <a href="http://www.castleproject.org/monorail/index.html">MonoRail framework made by Castle</a>. Then a few years later I started to learn about <a href="http://en.wikipedia.org/wiki/Domain-driven_design">Domain-Driven Design</a> (DDD) and found that is was an interesting way to design software. Just like so many other software design techniques, DDD also uses design patterns and one of them is the <a href="http://www.martinfowler.com/eaaCatalog/repository.html">Repository Pattern</a>.</p>
<blockquote><p>Repository - A mechanism for encapsulating storage, retrieval, and search behaviour which emulates a collection of objects.<br />
– <a href="http://www.amazon.com/gp/product/0321125215?ie=UTF8&#038;tag=techbooks00-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321125215">Eric Evans, Domain-driven design</a></p></blockquote>
<p>Usually when you use <a href="http://www.castleproject.org/activerecord/index.html">ActiveRecord from Castle</a>, then your entities would have to inherit from a base class like <em>ActiveRecordBase</em>, but in this example we don’t need to do that anymore.</p>
<p>You need to have some basic understanding of <a href="http://www.castleproject.org/activerecord/index.html">ActiveRecords</a>, <a href="https://www.hibernate.org/343.html">NHibernate</a> and the <a href="http://www.martinfowler.com/eaaCatalog/repository.html">Repository Pattern</a> to implement the example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>
<span style="color: #000000;">&#123;</span>
	T FindBy<span style="color: #000000;">&#40;</span>Guid id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	IList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T item<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">void</span> Delete<span style="color: #000000;">&#40;</span>T item<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</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
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> RepositoryBase<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> IRepository<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> IAggregateRoot
<span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> T FindBy<span style="color: #000000;">&#40;</span>Guid id<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>ActiveRecordMediator.<span style="color: #0000FF;">FindByPrimaryKey</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>, id<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> FindAll<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>IList<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#41;</span>ActiveRecordMediator.<span style="color: #0000FF;">FindAll</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>T<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: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> Save<span style="color: #000000;">&#40;</span>T item<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		ActiveRecordMediator.<span style="color: #0000FF;">Save</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>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> Delete<span style="color: #000000;">&#40;</span>T item<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		ActiveRecordMediator.<span style="color: #0000FF;">Delete</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></pre></td></tr></table></div>

<p>The <em>IAggregateRoot</em> is just a <a href="http://en.wikipedia.org/wiki/Marker_interface_pattern">marker interface</a>, meaning that it doesn’t specify any functionality. I know that this might look like <a href="http://en.wikipedia.org/wiki/Code_smell">“code smell”</a> to some, but it really depends on what you are trying to achieve. </p>
<blockquote><p>Custom attributes are preferred when you can defer checking for the attribute until the code is executing. If your scenario requires compile-time checking, you cannot comply with this guideline.<br />
- <a href="http://msdn.microsoft.com/en-us/library/ms229022.aspx">MSDN, Guidelines for developing Class libraries</a>.</p></blockquote>
<p><strong>The problem with using AR with DDD</strong></p>
<blockquote><p>Well, PI means clean, ordinary classes where you focus on the business problem at hand without adding stuff for infrastructure-related reasons.<br />
– <a href="http://www.amazon.com/gp/product/0321268202?ie=UTF8&#038;tag=techbooks00-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321268202">Applying DDD and Patterns, Jimmy Nilsson</a></p></blockquote>
<p>Using AR will you get up and running pretty fast, <em>but you might want to remove the AR parts later on and then re-implement your repository to only use NHibernate</em>. The reason for this is that entities shouldn’t know about how they are persisted (<em>i.e. Persistence Ignorance</em>) and when you apply them with <a href="http://www.castleproject.org/activerecord/documentation/trunk/manual/attributedocs/index.html">AR attributes</a> they do.</p>
<p>If you want to achieve a higher Persistence Ignorance, then you could try out <a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/commons/Rhino.Commons.NHibernate/Repositories/">Ayende’s Rhino-Tools</a> (also know as rhino-commons). There you can find a better implementation of the <a href="http://www.martinfowler.com/eaaCatalog/repository.html">Repository Pattern</a>, which also uses <a href="https://www.hibernate.org/343.html">NHibernate</a>. Another solution would of course be to create one your self.</p>
<p><strong>Further reading</strong></p>
<p><a href="http://www.amazon.com/gp/product/0321125215?ie=UTF8&#038;tag=techbooks00-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321125215">Domain-Driven Design: Tackling Complexity in the Heart of Software</a></p>
<p><a href="http://www.amazon.com/gp/product/0321268202?ie=UTF8&#038;tag=techbooks00-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0321268202">Applying Domain-Driven Design and Patterns: With Examples in C# and .NET</a></p>
<p><img src="http://www.assoc-amazon.com/e/ir?t=techbooks00-20&#038;l=as2&#038;o=1&#038;a=0321125215" 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/7xC0n2G0wCY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://fatagnus.com/using-the-repository-pattern-with-activerecord-from-castle/feed/</wfw:commentRss>
		<feedburner:origLink>http://fatagnus.com/using-the-repository-pattern-with-activerecord-from-castle/</feedburner:origLink></item>
	</channel>
</rss>
