<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Raj Aththanayake&#039;s Blog</title>
	<atom:link href="http://rajsoftware.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rajsoftware.wordpress.com</link>
	<description>Articles on ASP.NET and Software Development</description>
	<lastBuildDate>Mon, 17 Oct 2011 21:38:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rajsoftware.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Raj Aththanayake&#039;s Blog</title>
		<link>http://rajsoftware.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rajsoftware.wordpress.com/osd.xml" title="Raj Aththanayake&#039;s Blog" />
	<atom:link rel='hub' href='http://rajsoftware.wordpress.com/?pushpress=hub'/>
		<item>
		<title>ASP.NET MVC Unit Tests with UnityAutoMoq Container</title>
		<link>http://rajsoftware.wordpress.com/2011/10/18/asp-net-mvc-unit-tests-with-unityautomoq-container/</link>
		<comments>http://rajsoftware.wordpress.com/2011/10/18/asp-net-mvc-unit-tests-with-unityautomoq-container/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 21:38:48 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">https://rajsoftware.wordpress.com/2011/10/18/asp-net-mvc-unit-tests-with-unityautomoq-container/</guid>
		<description><![CDATA[&#160; UnityAutoMoq container is a great way to write maintainable Unit Tests. Since the container simplifies faking of dependencies, we can now focus on the actual test itself and spend less time on faking unwanted dependencies For more info, please refer to the article below. http://www.dotnetcurry.com/ShowArticle.aspx?ID=767 Filed under: ASP.NET<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=113&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>UnityAutoMoq container is a great way to write maintainable Unit Tests. Since the container simplifies faking of dependencies, we can now focus on the actual test itself and spend less time on faking unwanted dependencies</p>
<p>For more info, please refer to the article below.</p>
<p><a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=767">http://www.dotnetcurry.com/ShowArticle.aspx?ID=767</a></p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/113/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/113/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/113/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=113&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2011/10/18/asp-net-mvc-unit-tests-with-unityautomoq-container/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>
	</item>
		<item>
		<title>Keeping your C# code clean with Regionerate</title>
		<link>http://rajsoftware.wordpress.com/2011/10/16/keeping-you-c-code-clean-with-regionerate/</link>
		<comments>http://rajsoftware.wordpress.com/2011/10/16/keeping-you-c-code-clean-with-regionerate/#comments</comments>
		<pubDate>Sun, 16 Oct 2011 11:34:23 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">https://rajsoftware.wordpress.com/2011/10/16/keeping-you-c-code-clean-with-regionerate/</guid>
		<description><![CDATA[I came across with this really nice VS add-in called Regionerate, which allows you to group various member types into regions. I’m a fan of keeping code tidy and this add-in really helps me to organise the code. It works on Visual Studio 2010 and only works with C#. You can download the Add-In from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=111&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I came across with this really nice VS add-in called Regionerate, which allows you to group various member types into regions. I’m a fan of keeping code tidy and this add-in really helps me to organise the code.</p>
<p>It works on Visual Studio 2010 and only works with C#.</p>
<p>You can download the Add-In from <a href="http://www.rauchy.net/regionerate/">here</a>.</p>
<p>Once you install this tool, you are ready to use the Regionerate.</p>
<p>You can also change the settings of Regionerate, go to Tools and click Regionerate Settings.</p>
<p>To use the Regionerate, simply use the Ctrl +R. You should see a window with few options as below. <i>Alternatively just right click on Text Editor and select ‘Regionerate this’ from the context menu.</i></p>
<p><em></em></p>
<p><a href="http://rajsoftware.files.wordpress.com/2011/10/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://rajsoftware.files.wordpress.com/2011/10/image_thumb.png?w=509&#038;h=292" width="509" height="292" /></a></p>
<p>&#160;</p>
<p>As per the above options, you can create regions based on the member types (I.e <i>public, private, and internal), </i>regionalise by the member name, regionalise by the member type and the member name, order the members without and regions, and remove existing regions etc..</p>
<p>Below is a code sample on selecting the ‘Primary Code Layout’.</p>
<p><a href="http://rajsoftware.files.wordpress.com/2011/10/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://rajsoftware.files.wordpress.com/2011/10/image_thumb1.png?w=252&#038;h=554" width="252" height="554" /></a></p>
<p>&#160;</p>
<p>I strongly encourage you to have a look at this Add-In as it is very useful.</p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=111&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2011/10/16/keeping-you-c-code-clean-with-regionerate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2011/10/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2011/10/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Returning Null from a generic method</title>
		<link>http://rajsoftware.wordpress.com/2011/09/30/returning-null-from-a-generic-method/</link>
		<comments>http://rajsoftware.wordpress.com/2011/09/30/returning-null-from-a-generic-method/#comments</comments>
		<pubDate>Fri, 30 Sep 2011 11:22:17 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">https://rajsoftware.wordpress.com/2011/09/30/returning-null-from-a-generic-method/</guid>
		<description><![CDATA[&#160; Consider this generic method. 1: public T Get&#60;T&#62;() { 2: //return null 3: } 4:&#160; If you want to return null from a C# generic method, which contains the Type(T) parameter, there are 2 options. a. Restrict the type parameter T, to be class constraint 1: public T Get&#60;T&#62;() where T : class; 2: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=106&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p><font size="3" face="Calibri">Consider this generic method.</font></p>
<div id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">public</span> T Get&lt;T&gt;() { </pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> <span style="color:#008000;">//return null</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span> }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span>&#160; </pre>
<p><!--CRLF--></div>
</div>
<p><font size="3" face="Calibri">If you want to return null from a C# generic method, which contains the Type(T) parameter, there are 2 options.</font></p>
<p><font size="3" face="Calibri">a. Restrict the type parameter T, to be class constraint</font></p>
<p><font size="3" face="Calibri"></p>
<p id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">public</span> T Get&lt;T&gt;() <span style="color:#0000ff;">where</span> T : <span style="color:#0000ff;">class</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>    <span style="color:#008000;">//return null</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span> }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>&#160; </pre>
<p><!--CRLF--></div>
</p>
<p><font size="3" face="Calibri">Value types or Nullable Values types are not allowed. Reference types only.</font></p>
<p><font size="3" face="Calibri">b. Return default(T) which return null if T is a reference type or a Nullable value type</font></p>
<div id="codeSnippetWrapper">
<div style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;padding:0;" id="codeSnippet">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum1">   1:</span> <span style="color:#0000ff;">public</span> T Get&lt;T&gt;()</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum3">   3:</span>   <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">default</span>(T);</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum4">   4:</span> }</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;font-family:&#039;direction:ltr;color:black;font-size:8pt;overflow:visible;border-style:none;margin:0;padding:0;"><span style="color:#606060;" id="lnum5">   5:</span>&#160; </pre>
<p><!--CRLF--></div>
</p></div>
<p>  <font size="3" face="Calibri">Anything else will simply return their default values.&#160; Here is summary on both reference and value types….</font></font></p>
<p><font size="3" face="Calibri">//If T is a reference type </font></p>
<p><font size="3" face="Calibri">var retVal = f.Get&lt;String&gt;(); //return null</font></p>
<p><font size="3" face="Calibri">……….</font></p>
<p><font size="3" face="Calibri">//If T is a value type &#8211; int</font></p>
<p><font size="3" face="Calibri">var retValInt = f.Get&lt;int&gt;(); //return 0</font></p>
<p><font size="3" face="Calibri">……….</font></p>
<p><font size="3" face="Calibri">//If T is a value type &#8211; char</font></p>
<p><font size="3" face="Calibri">var retValChar = f.Get&lt;char&gt;(); //return &#8221;</font></p>
<p><font size="3" face="Calibri">……….</font></p>
<p><font size="3" face="Calibri">//If T is a value type &#8211; bool</font></p>
<p><font size="3" face="Calibri">var retValBool = f.Get&lt;Boolean&gt;(); //return false</font></p>
<p><font size="3" face="Calibri">……….</font></p>
<p><font size="3" face="Calibri">//If T is a value type &#8211; enum</font></p>
<p><font size="3" face="Calibri">var retValEnum = f.Get&lt;EnumType&gt;(); </font><font size="3" face="Calibri">// For enum types, if there are no enum values specified, then the return value is 0. If there are values, then the first enumeration returns as the default.</font></p>
<p><font size="3" face="Calibri">……….</font></p>
<p><font size="3" face="Calibri">//If T is a value type &#8211; struct</font></p>
<p><font size="3" face="Calibri">var retValStruct = f.Get&lt;Bar&gt;(); //Structs, returns the value of the struct</font></p>
<p><font size="3" face="Calibri">……….</font></p>
<p><font size="3" face="Calibri">//If T is a Nullable&lt;Int32&gt;</font></p>
<p><font size="3" face="Calibri">var retNullableInt = f.Get&lt;Int32?&gt;(); //return null</font></p>
<p><font size="3" face="Calibri">……….</font></p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/106/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/106/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/106/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=106&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2011/09/30/returning-null-from-a-generic-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>
	</item>
		<item>
		<title>Moq 4 and &#8220;LINQ to Mocks&#8221;</title>
		<link>http://rajsoftware.wordpress.com/2011/06/07/moq-4-and-e2809clinq-to-mockse2809d/</link>
		<comments>http://rajsoftware.wordpress.com/2011/06/07/moq-4-and-e2809clinq-to-mockse2809d/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 22:22:23 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">https://rajsoftware.wordpress.com/2011/06/07/moq-4-and-e2809clinq-to-mockse2809d/</guid>
		<description><![CDATA[&#8220;If the code samples are harder to follow please visit my blog http://blog.rajsoftware.com/post/2011/06/07/Moq-4-and-e2809cLINQ-to-Mockse2809d.aspx&#8220; While searching for Moq code samples, I came across that Moq 4 was released few months ago. The key improvement was the new “LINQ to Mocks”. This post demonstrates few examples of the usage of Mocks and LINQ queries. For each example, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=103&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>&#8220;If the code samples are harder to follow please visit my blog <a href="http://blog.rajsoftware.com/post/2011/06/07/Moq-4-and-e2809cLINQ-to-Mockse2809d.aspx">http://blog.rajsoftware.com/post/2011/06/07/Moq-4-and-e2809cLINQ-to-Mockse2809d.aspx</a>&#8220;</strong></p></blockquote>
<p>While searching for Moq code samples, I came across that <a href="http://code.google.com/p/moq/downloads/detail?name=Moq.4.0.10827.Final.zip">Moq 4</a> was released few months ago. The key improvement was the new “LINQ to Mocks”. <strong></strong></p>
<p>This post demonstrates few examples of the usage of Mocks and LINQ queries. For each example, we stub out dependencies using lambda expressions first, and then see the corresponding LINQ version. At the end we see the performance of each Unit Tests.</p>
<p><em>Note: I’m using the <a href="http://xunit.codeplex.com/">xUnit Testing Framework</a>. However you can apply the same principles to other Unit testing frameworks.</em></p>
<p>&nbsp;</p>
<h4>Single Stub</h4>
<p><em>Lambdas:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> UpdateCustomer_WhenCustomerIsNotUpdated_ReturnsFalse()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     var dbServiceStub = <span style="color:#0000ff;">new</span> Mock&lt;IDbService&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     dbServiceStub.Setup(d =&gt; d.Update(It.IsAny&lt;Customer&gt;())).Returns(<span style="color:#0000ff;">false</span>);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>     var customerService = <span style="color:#0000ff;">new</span> CustomerService();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>     customerService.DbService = dbServiceStub.Object;</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>     <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span>     <span style="color:#0000ff;">bool</span> isUpdated = customerService.Update(It.IsAny&lt;Customer&gt;());</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>     <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>     Assert.False(isUpdated);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span> }</pre>
<p>&nbsp;</p>
</div>
</div>
<p><em>LINQ:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span>  <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> UpdateCustomer_WhenCustomerIsNotUpdated_ReturnsFalse()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span>  {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>      <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>      IDbService dbServiceStub = (from dbService <span style="color:#0000ff;">in</span> Mocks.Of&lt;IDbService&gt;() <span style="color:#0000ff;">where</span> dbService.Update(It.IsAny&lt;Customer&gt;()) == <span style="color:#0000ff;">false</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>                                  select dbService).First();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>      var customerService = <span style="color:#0000ff;">new</span> CustomerService { DbService = dbServiceStub };</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span>      <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>      <span style="color:#0000ff;">bool</span> isUpdated = customerService.Update(It.IsAny&lt;Customer&gt;());</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>      <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>      Assert.False(isUpdated);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>  }</pre>
<p>&nbsp;</p>
</div>
</div>
<p>&nbsp;</p>
<h4>Multiple Stubs</h4>
<p><em>Lambdas:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> UpdateCustomer_WhenCustomerIsUpdated_ReturnsTrue()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     var dbServiceStub = <span style="color:#0000ff;">new</span> Mock&lt;IDbService&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     dbServiceStub.Setup(d =&gt; d.Update(It.IsAny&lt;Customer&gt;())).Returns(<span style="color:#0000ff;">true</span>);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>     var loggerServiceStub = <span style="color:#0000ff;">new</span> Mock&lt;ILoggerService&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span>     var customerService = <span style="color:#0000ff;">new</span> CustomerService { DbService = dbServiceStub.Object, LogService = loggerServiceStub.Object };</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span>     <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>     <span style="color:#0000ff;">bool</span> isUpdated = customerService.Update(It.IsAny&lt;Customer&gt;());</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>     <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span>     Assert.True(isUpdated);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span> }</pre>
<p>&nbsp;</p>
</div>
</div>
<p>&nbsp;</p>
<p><em>LINQ:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> UpdateCustomer_WhenCustomerIsUpdated_ReturnsTrue()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     var fakes = (from dbService <span style="color:#0000ff;">in</span> Mocks.Of&lt;IDbService&gt;()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>                  from loggerService <span style="color:#0000ff;">in</span> Mocks.Of&lt;ILoggerService&gt;()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>                  <span style="color:#0000ff;">where</span> dbService.Update(It.IsAny&lt;Customer&gt;()) == <span style="color:#0000ff;">true</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>                  select <span style="color:#0000ff;">new</span> { dbServiceStub = dbService, loggerServiceStub = loggerService }).First();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>     var customerService = <span style="color:#0000ff;">new</span> CustomerService { DbService = fakes.dbServiceStub, LogService = fakes.loggerServiceStub };</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>     <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>     <span style="color:#0000ff;">bool</span> isUpdated = customerService.Update(It.IsAny&lt;Customer&gt;());</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span>     <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span>     Assert.True(isUpdated);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;"> 17:</span> }</pre>
<p>&nbsp;</p>
</div>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h4>Mocks and Stubs</h4>
<p>&nbsp;</p>
<p><em>Lambdas:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> UpdateCustomer_WhenCustomerIsUpdated_VerifyLoggerServiceLogIsCalled()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     var dbServiceStub = <span style="color:#0000ff;">new</span> Mock&lt;IDbService&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     dbServiceStub.Setup(d =&gt; d.Update(It.IsAny&lt;Customer&gt;())).Returns(<span style="color:#0000ff;">true</span>);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>     var loggerServiceMock = <span style="color:#0000ff;">new</span> Mock&lt;ILoggerService&gt;(); <span style="color:#008000;">//We verify against the Mock</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>     loggerServiceMock.Setup(l =&gt; l.Log(It.IsAny&lt;<span style="color:#0000ff;">string</span>&gt;())).Verifiable();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>     var customerService = <span style="color:#0000ff;">new</span> CustomerService()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span>     {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>         DbService = dbServiceStub.Object,</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>         LogService = loggerServiceMock.Object</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>     };</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span>     <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;"> 17:</span>     customerService.Update(It.IsAny&lt;Customer&gt;());</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum18" style="color:#606060;"> 18:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum19" style="color:#606060;"> 19:</span>     <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum20" style="color:#606060;"> 20:</span>     Mock.Get(loggerServiceMock.Object).Verify(c =&gt; c.Log(It.IsAny&lt;<span style="color:#0000ff;">string</span>&gt;()));</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum21" style="color:#606060;"> 21:</span> }</pre>
<p>&nbsp;</p>
</div>
</div>
<p>&nbsp;</p>
<p><em>LINQ:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> UpdateCustomer_WhenCustomerIsUpdated_VerifyLoggerServiceLogIsCalled()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     var fakes = (from dbService <span style="color:#0000ff;">in</span> Mocks.Of&lt;IDbService&gt;()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>                  from loggerService <span style="color:#0000ff;">in</span> Mocks.Of&lt;ILoggerService&gt;()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>                  <span style="color:#0000ff;">where</span> dbService.Update(It.IsAny&lt;Customer&gt;()) == <span style="color:#0000ff;">true</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>                  select <span style="color:#0000ff;">new</span> { dbServiceStub = dbService, loggerServiceMock = loggerService }).First();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>     var customerService = <span style="color:#0000ff;">new</span> CustomerService { DbService = fakes.dbServiceStub, LogService = fakes.loggerServiceMock };</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>     <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>     <span style="color:#0000ff;">bool</span> isSend = customerService.Update(It.IsAny&lt;Customer&gt;());</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span>     <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span>     Mock.Get(fakes.loggerServiceMock).Verify(c =&gt; c.Log(It.IsAny&lt;<span style="color:#0000ff;">string</span>&gt;()));</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;"> 17:</span> }</pre>
<p>&nbsp;</p>
</div>
</div>
<p>&nbsp;</p>
<h4>Testing an MVC Action Filter</h4>
<p>&nbsp;</p>
<p><em>Lambdas:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> OnActionExecuted_WhenEmployeeSalaryIsEqualToZero_ EnsureActionExecutedContextResultEqualToFilterResult()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     <span style="color:#0000ff;">decimal</span> employeeUnpaidAmount = 0.0M;</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     var employeeServiceStub = <span style="color:#0000ff;">new</span> Mock&lt;IEmployeeService&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>     employeeServiceStub.Setup(s =&gt; s.GetSalary()).Returns(employeeUnpaidAmount);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span>     var filter = <span style="color:#0000ff;">new</span> EmployeeSalaryNotificationAttribute</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>     {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span>         EmployeeService = employeeServiceStub.Object</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>     };</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>     var actionExecutedContextMock = <span style="color:#0000ff;">new</span> Mock&lt;ActionExecutedContext&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span>     var routeData = <span style="color:#0000ff;">new</span> RouteData();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span>     routeData.Values.Add(<span style="color:#006080;">"fakeIdKey"</span>, <span style="color:#006080;">"fakeIdValue"</span>);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;"> 17:</span>     actionExecutedContextMock.SetupGet(r =&gt; r.RouteData).Returns(routeData);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum18" style="color:#606060;"> 18:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum19" style="color:#606060;"> 19:</span>     var request = <span style="color:#0000ff;">new</span> Mock&lt;HttpRequestBase&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum20" style="color:#606060;"> 20:</span>     var httpContext = <span style="color:#0000ff;">new</span> Mock&lt;HttpContextBase&gt;();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum21" style="color:#606060;"> 21:</span>     httpContext.SetupGet(c =&gt; c.Request).Returns(request.Object);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum22" style="color:#606060;"> 22:</span>     actionExecutedContextMock.SetupGet(h =&gt; h.HttpContext).Returns(httpContext.Object);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum23" style="color:#606060;"> 23:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum24" style="color:#606060;"> 24:</span>     <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum25" style="color:#606060;"> 25:</span>     filter.OnActionExecuted(actionExecutedContextMock.Object);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum26" style="color:#606060;"> 26:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum27" style="color:#606060;"> 27:</span>     <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum28" style="color:#606060;"> 28:</span>     Assert.Equal((actionExecutedContextMock.Object.Result <span style="color:#0000ff;">as</span> ContentResult).Content, filter.SalaryResult);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum29" style="color:#606060;"> 29:</span> }</pre>
<p>&nbsp;</p>
</div>
</div>
<p>&nbsp;</p>
<p><em>LINQ:</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [Fact]</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> OnActionExecuted_WhenEmployeeSalaryIsEqualToZero_EnsureActionExecutedContextResultEqualToFilterResult()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#008000;">//Arrange</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     RouteData routeData = <span style="color:#0000ff;">new</span> RouteData();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     routeData.Values.Add(<span style="color:#006080;">"fakeIdKey"</span>, <span style="color:#006080;">"fakeIdValue"</span>);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>     var fakes = (from employeeService <span style="color:#0000ff;">in</span> Mocks.Of&lt;IEmployeeService&gt;()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span>                  from actionExecutedContext <span style="color:#0000ff;">in</span> Mocks.Of&lt;ActionExecutedContext&gt;()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>                  <span style="color:#0000ff;">where</span> employeeService.GetSalary() == 0.00M &amp;&amp;</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span>                  actionExecutedContext.HttpContext == Mock.Of&lt;HttpContextBase&gt;() &amp;&amp;</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>                  actionExecutedContext.HttpContext.Request == Mock.Of&lt;HttpRequestBase&gt;() &amp;&amp;</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>                  actionExecutedContext.RouteData == <span style="color:#0000ff;">new</span> RouteData()</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>                  select <span style="color:#0000ff;">new</span> { EmployeeServiceStub = employeeService, ActionExecutedContextMock = actionExecutedContext })</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span>                  .First();</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;"> 17:</span>     var filter = <span style="color:#0000ff;">new</span> EmployeeSalaryNotificationAttribute</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum18" style="color:#606060;"> 18:</span>     {</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum19" style="color:#606060;"> 19:</span>         EmployeeService = fakes.EmployeeServiceStub</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum20" style="color:#606060;"> 20:</span>     };</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum21" style="color:#606060;"> 21:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum22" style="color:#606060;"> 22:</span>     <span style="color:#008000;">//Act</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum23" style="color:#606060;"> 23:</span>     filter.OnActionExecuted(fakes.ActionExecutedContextMock);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum24" style="color:#606060;"> 24:</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum25" style="color:#606060;"> 25:</span>     <span style="color:#008000;">//Assert</span></pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum26" style="color:#606060;"> 26:</span>     Assert.Equal((fakes.ActionExecutedContextMock.Result <span style="color:#0000ff;">as</span> ContentResult).Content, filter.SalaryResult);</pre>
<p>&nbsp;</p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum27" style="color:#606060;"> 27:</span> }</pre>
<p>&nbsp;</p>
</div>
</div>
<h4> </h4>
<h4><strong>Key things to notice:</strong></h4>
<p>- The tests with LINQ queries reflect the test scenarios/conditions in a more of a specification style. This improves the tests readability.</p>
<p>&nbsp;</p>
<p>- The query returns an infinite number of mocks. Typically we access the first element.</p>
<p>   <span style="color:#4f81bd;">var fakes = (LINQ query).First();</span></p>
<p>&nbsp;</p>
<p>- LINQ query result returns real object(s) and not the type Mock&lt;T&gt;. So we do not need to use the EmployeeServiceStub.Object. Instead we can just use the returned element.</p>
<p>   <span style="color:#4f81bd;">EmployeeService = fakes.EmployeeServiceStub;</span></p>
<p>&nbsp;</p>
<p>- When you have multiple stubs or mocks, you can return all of them using an anonymous type.</p>
<p><span style="color:#4f81bd;">   select new { dbServiceStub = dbService, loggerServiceStub = loggerService }));</span></p>
<p>&nbsp;</p>
<p>- Not used in these code samples, but if you are using the MockFactory class, it is now absolute in v4. MockFactory will be retired in v5.</p>
<p>&nbsp;</p>
<h4>Performances:</h4>
<p>Here is a very quick benchmark results on each Unit Test.</p>
<table width="400" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="10">Unit Test</td>
<td valign="top" width="258">Lambda version</td>
<td valign="top" width="133">LINQ version</td>
</tr>
<tr>
<td valign="top" width="10">UpdateCustomer_WhenCustomerIsNotUpdated_ReturnsFalse</td>
<td valign="top" width="258">0.95s</td>
<td valign="top" width="133">1.01s</td>
</tr>
<tr>
<td valign="top" width="10">UpdateCustomer_WhenCustomerIsUpdated_ReturnsTrue</td>
<td valign="top" width="258">0.97s</td>
<td valign="top" width="133">1.06s</td>
</tr>
<tr>
<td valign="top" width="10">UpdateCustomer_WhenCustomerIsUpdated</p>
<p>_VerifyLoggerServiceLogIsCalled</td>
<td valign="top" width="258">0.94s</td>
<td valign="top" width="133">1.07s</td>
</tr>
<tr>
<td valign="top" width="10">OnActionExecuted_WhenEmployeeSalaryIsEqualToZero</p>
<p>_EnsureFilterResultContainsTheStringNotPaid</td>
<td valign="top" width="258">1.08s</td>
<td valign="top" width="133">1.29s</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Unit Tests with LINQ queries take “tiny” bit longer than the Lambda counterparts. However, I would personally still use LINQ version as it improves the readability of Unit Tests.</p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/103/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=103&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2011/06/07/moq-4-and-e2809clinq-to-mockse2809d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>
	</item>
		<item>
		<title>MVC 3 Filter Ordering</title>
		<link>http://rajsoftware.wordpress.com/2011/05/14/mvc3-filter-ordering/</link>
		<comments>http://rajsoftware.wordpress.com/2011/05/14/mvc3-filter-ordering/#comments</comments>
		<pubDate>Sat, 14 May 2011 00:09:00 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">https://rajsoftware.wordpress.com/2011/05/14/mvc3-filter-ordering/</guid>
		<description><![CDATA[MVC 3 has introduced couple of new approaches related to the run order of filters. In this article, we will look at the order of filters execution. There are different types of filters in MVC. Depending on the type of the Filter, and the scope of the Filter, the order of the execution also changes. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=95&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>MVC 3 has introduced couple of new approaches related to the run order of filters. In this article, we will look at the order of filters execution.</p>
<p><a href="http://rajsoftware.files.wordpress.com/2011/05/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;margin:0;" title="image" src="http://rajsoftware.files.wordpress.com/2011/05/image_thumb.png?w=244&#038;h=184" alt="image" width="244" height="184" border="0" /></a></p>
<p>There are different types of filters in MVC. Depending on the type of the Filter, and the scope of the Filter, the order of the execution also changes.</p>
<p>You may already aware that there are different types of filters within MVC framework. They are listed below.</p>
<p>1. Authorization filters</p>
<p>2. Action filters</p>
<p>3. Response/Result filters</p>
<p>4. Exception filters</p>
<p>The above list is ordered as exactly the order they executes within MVC framework. The Authorization filters always run first, and the Exception filters run at the end.</p>
<p><strong>Specifying the Order property for Filters</strong></p>
<p>Within each filter, you may specify the <strong>Order</strong> property. (All filters are derived from the <em>abstract</em> class <em>FilterAttribute, </em>and this class has an Order property). This property will ensure the filter runs in a specific Order. For an example, let’s say we have 2 Authorization filters, <em>AuthorizationFilterA</em> and <em>AuthorizationFilterB </em>and they are implemented as below.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AuthorizationFilterA : AuthorizeAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnAuthorization(AuthorizationContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>         Debug.WriteLine(<span style="color:#006080;">"OnAuthorization : AuthorizationFilterA"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AuthorizationFilterB : AuthorizeAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnAuthorization(AuthorizationContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>         Debug.WriteLine(<span style="color:#006080;">"OnAuthorization : AuthorizationFilterB"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span> }</pre>
</div>
</div>
<p>Now, if I want these two filters to run in a specific order &#8211; <em>AuthorizationFilterB</em> to run first, and <em>AuthorizationFilterA</em> to run second, I can simply specify the execution order as below.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [AuthorizationFilterA(Order=2)]</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> [AuthorizationFilterB(Order=1)]</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> <span style="color:#0000ff;">public</span> ActionResult Index()</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>     <span style="color:#0000ff;">return</span> View();</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span> }</pre>
</div>
</div>
<p>When you run the code, the Output window should display that <em>AuthorizationFilterB</em> runs first, <em>AuthorizationFilterA</em> runs second.</p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterB</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterA</strong></span></p>
<p><strong>Filter Scope</strong></p>
<p>Within each filter type, you can define a scope. For an example, I could scope all my Authorization Filters to run within the <strong>Controller Scope</strong>, and all Action Filters to run in <strong>Global scope</strong> (Every Action within the MVC application).</p>
<p><strong>FilterScope.Global </strong>is a new scope which was introduced in MVC 3. The existing scopes are <strong>First</strong>, <strong>Controller</strong>, <strong>Action</strong>, and <strong>Last</strong>. The complete enumeration is defined as below.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">namespace</span> System.Web.Mvc {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">enum</span> FilterScope {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span>         First = 0,</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>         Global = 10,</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>         Controller = 20,</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>         Action = 30,</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>         Last = 100,</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span> }</pre>
</div>
</div>
<p>By default, the filters with the lowest scope runs first. For an example, scope Global (10) executes first and scope Controller (20) executes second.</p>
<p><strong>Filter Scope with explicit Ordering</strong></p>
<p>Below example shows, how explicit filter ordering (using Order property) and scope of the filters determine their correct run order.</p>
<p>Let’s add the scope to our existing Authorization Filters – <em>AuthorisationFilterA</em> and <em>AuthorisationFilterB</em>. Please note that we haven’t changed the usage of <strong>Order </strong>property.</p>
<p>AuthorizationFilterA(Order 2) has the <strong>scope Global</strong>.</p>
<p>AuthorizationFilterB(Order 1) has the <strong>scope Action</strong>.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AuthorizationFilterA : AuthorizeAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnAuthorization(AuthorizationContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>         Debug.WriteLine(<span style="color:#006080;">"OnAuthorization : AuthorizationFilterA (Order: 2) in scope : Global"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AuthorizationFilterB : AuthorizeAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnAuthorization(AuthorizationContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>         Debug.WriteLine(<span style="color:#006080;">"OnAuthorization : AuthorizationFilterB (Order: 1) in scope : Action"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;"> 17:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum18" style="color:#606060;"> 18:</span> [AuthorizationFilterB(Order = 1)]</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum19" style="color:#606060;"> 19:</span> <span style="color:#0000ff;">public</span> ActionResult Index()</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum20" style="color:#606060;"> 20:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum21" style="color:#606060;"> 21:</span>     <span style="color:#0000ff;">return</span> View();</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum22" style="color:#606060;"> 22:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum23" style="color:#606060;"> 23:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum24" style="color:#606060;"> 24:</span> GobalFilters.Filters.Add(<span style="color:#0000ff;">new</span> AuthorizationFilterA() { Order = 2});</pre>
</div>
</div>
<p>According to the above Filter Scope enumeration, if we were to specify only the scope but not the <em>Order </em>property, then we would expect <em>AuthorizationFilterA</em> (in Global scope) to run first, and <em>AuthorizationFilterB</em> (in Action scope) to run second. Since we are explicitly specifying the <strong><em>Order</em></strong> property for <strong>both filters</strong>, <em>AuthorizationFilterB</em> (in Action scope – Order 1) runs first and <em>AuthorizationFilterA</em> (in Global scope – Order 2) runs second.</p>
<p>Output:</p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterB (Order: 1) in scope : Action</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterA (Order: 2) in scope : Global</strong></span></p>
<p>Now let’s add another Authorization filter (<em>AuthorizationFilterC) </em>to Global scope. Also note that we have not specified the <em>Order </em>property.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AuthorizationFilterC : AuthorizeAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnAuthorization(AuthorizationContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>         {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>             Debug.WriteLine(<span style="color:#006080;">"OnAuthorization : AuthorizationFilterC (no Order defined) in scope : Global"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>         }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span> GlobalFilters.Filters.Add(<span style="color:#0000ff;">new</span> AuthorizationFilterC());</pre>
</div>
</div>
<p>After running all three filters, you can see the output as below.</p>
<p>Output</p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterC (no Order defined) in scope : Global</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterB (Order: 1) in scope : Action</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterA (Order: 2) in scope : Global</strong></span></p>
<p>For same <em>Filter</em> type, the filters without the explicit ordering (i.e <em>AuthorizationFilterC</em>) take precedence over the filters with explicit ordering (<em>AuthorizationFilterB</em>, <em>AuthorizationFilterA</em>).</p>
<p>Now let’s remove the Order property from both <em>AuthorizationFilterB</em> and <em>AuthorizationFilterA</em></p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> [AuthorizationFilterB]</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> <span style="color:#0000ff;">public</span> ActionResult Index()</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     <span style="color:#0000ff;">return</span> View();</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span> GlobalFilters.Filters.Add(<span style="color:#0000ff;">new</span> AuthorizationFilterA());</pre>
</div>
</div>
<p>Output:</p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterA (no order defined) in scope : Global</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterC (no order defined) in scope : Global</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>OnAuthorization : AuthorizationFilterB (no order defined) in scope : Action</strong></span></p>
<p>As we would expect, the filters with scope <strong>Global</strong> (<em>AuthorizationFilterA</em> and <em>AuthorizationFilterC</em>) run first and second in order, the scope <strong>Action</strong> filter (<em>AuthorizationFilterC</em>) runs at the end. 2 Global filters (<em>AuthorizationFilterA</em> and <em>AuthorizationFilterC</em>) run order relies on the order which they were added to the Global Filter collection. In this example <em>AuthorizationFilterA</em> which was added prior to AuthorizationFilterC ,<em> </em>runs first.</p>
<p>For the same filters, when determine the run order, first it sorts them by their order (lowest numbers first), and then by their scope (also lowest numbers first).</p>
<p><strong>Controller as a Filter</strong></p>
<p>In MVC the <strong>Controller</strong> itself is also a Filter. Controller implements all filters, so Controller can subscribe to all filter events.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">abstract</span> <span style="color:#0000ff;">class</span> Controller : ControllerBase,</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> IActionFilter, IAuthorizationFilter, IDisposable,</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span> IExceptionFilter, IResultFilter</pre>
</div>
</div>
<p>MVC framework ensures, the Controller always runs first, before any other filters get executed. It has been accomplished by specifying the scope as the <strong>First</strong> and the order property as <strong>Int32.MinValue</strong>. By default, an MVC filter has the value for the Order <strong>-1</strong>. Therefore <em>Int32.MinValue</em> along with the Scope <em>First</em> together ensures the Controller always runs first.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> ControllerInstanceFilterProvider : IFilterProvider {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span>         <span style="color:#0000ff;">public</span> IEnumerable&lt;Filter&gt; GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor) {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span>             <span style="color:#0000ff;">if</span> (controllerContext.Controller != <span style="color:#0000ff;">null</span>) {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>                 <span style="color:#008000;">// Use FilterScope.First and Order of Int32.MinValue to ensure controller instance methods always run first</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>                 <span style="color:#0000ff;">yield</span> <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> Filter(controllerContext.Controller, FilterScope.First, Int32.MinValue);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>             }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span>         }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>     }</pre>
</div>
</div>
<p><strong>Forward Filter Order and Reverse Filter Order</strong></p>
<p>So far we have discussed the order of filters which get executed based on the explicit ordering (using Order property) and the scope which they can be belong to. However, MVC filters run order, do not only rely only by these two factors. Sometimes filters run in forward order and sometimes filters run in reverse order. Let’s see the below example.</p>
<p>Let’s say I have three <em>Action</em> filters (<em>ActionFilter1</em>, <em>ActionFilter2, ActionFilter3)</em>, one <em>Authorization</em> filter (<em>AuthorizationFilter)</em> and two <em>Exception</em> filters (<em>HandleErrorA, HandleErrorB</em>). They are implemented as below.</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;"> 1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> ActionFilter1 : ActionFilterAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;"> 2:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;"> 3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnActionExecuting(ActionExecutingContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;"> 4:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;"> 5:</span>         Debug.WriteLine(<span style="color:#006080;">"Forward Order - OnActionExecuting : ActionFilter1 (Scope Global)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;"> 6:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;"> 7:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;"> 8:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnActionExecuted(ActionExecutedContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;"> 9:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;"> 10:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnActionExecuted : ActionFilter1 (Scope Global)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;"> 11:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;"> 12:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;"> 13:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnResultExecuting(ResultExecutingContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;"> 14:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;"> 15:</span>         Debug.WriteLine(<span style="color:#006080;">"Forward Order - OnResultExecuting : ActionFilter1 (Scope Global)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;"> 16:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;"> 17:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum18" style="color:#606060;"> 18:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnResultExecuted(ResultExecutedContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum19" style="color:#606060;"> 19:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum20" style="color:#606060;"> 20:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnResultExecuted : ActionFilter1 (Scope Global)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum21" style="color:#606060;"> 21:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum22" style="color:#606060;"> 22:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum23" style="color:#606060;"> 23:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum24" style="color:#606060;"> 24:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum25" style="color:#606060;"> 25:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> ActionFilter2 : ActionFilterAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum26" style="color:#606060;"> 26:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum27" style="color:#606060;"> 27:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnActionExecuting(ActionExecutingContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum28" style="color:#606060;"> 28:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum29" style="color:#606060;"> 29:</span>         Debug.WriteLine(<span style="color:#006080;">"Forward Order - OnActionExecuting : ActionFilter2 (Scope Controller)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum30" style="color:#606060;"> 30:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum31" style="color:#606060;"> 31:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum32" style="color:#606060;"> 32:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnActionExecuted(ActionExecutedContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum33" style="color:#606060;"> 33:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum34" style="color:#606060;"> 34:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnActionExecuted : ActionFilter2 (Scope Controller)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum35" style="color:#606060;"> 35:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum36" style="color:#606060;"> 36:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum37" style="color:#606060;"> 37:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnResultExecuting(ResultExecutingContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum38" style="color:#606060;"> 38:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum39" style="color:#606060;"> 39:</span>         Debug.WriteLine(<span style="color:#006080;">"Forward Order - OnResultExecuting : ActionFilter2 (Scope Controller)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum40" style="color:#606060;"> 40:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum41" style="color:#606060;"> 41:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum42" style="color:#606060;"> 42:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnResultExecuted(ResultExecutedContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum43" style="color:#606060;"> 43:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum44" style="color:#606060;"> 44:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnResultExecuted : ActionFilter2 (Scope Controller)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum45" style="color:#606060;"> 45:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum46" style="color:#606060;"> 46:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum47" style="color:#606060;"> 47:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum48" style="color:#606060;"> 48:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> ActionFilter3 : ActionFilterAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum49" style="color:#606060;"> 49:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum50" style="color:#606060;"> 50:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnActionExecuting(ActionExecutingContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum51" style="color:#606060;"> 51:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum52" style="color:#606060;"> 52:</span>         Debug.WriteLine(<span style="color:#006080;">"Forward Order - OnActionExecuting : ActionFilter3 (Scope Action)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum53" style="color:#606060;"> 53:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum54" style="color:#606060;"> 54:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum55" style="color:#606060;"> 55:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnActionExecuted(ActionExecutedContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum56" style="color:#606060;"> 56:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum57" style="color:#606060;"> 57:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnActionExecuted : ActionFilter3 (Scope Action)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum58" style="color:#606060;"> 58:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum59" style="color:#606060;"> 59:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum60" style="color:#606060;"> 60:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnResultExecuting(ResultExecutingContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum61" style="color:#606060;"> 61:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum62" style="color:#606060;"> 62:</span>         Debug.WriteLine(<span style="color:#006080;">"Forward Order - OnResultExecuting : ActionFilter3 (Scope Action)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum63" style="color:#606060;"> 63:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum64" style="color:#606060;"> 64:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum65" style="color:#606060;"> 65:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnResultExecuted(ResultExecutedContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum66" style="color:#606060;"> 66:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum67" style="color:#606060;"> 67:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnResultExecuted : ActionFilter3 (Scope Action)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum68" style="color:#606060;"> 68:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum69" style="color:#606060;"> 69:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum70" style="color:#606060;"> 70:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum71" style="color:#606060;"> 71:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum72" style="color:#606060;"> 72:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> AuthorizationFilter : AuthorizeAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum73" style="color:#606060;"> 73:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum74" style="color:#606060;"> 74:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnAuthorization(AuthorizationContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum75" style="color:#606060;"> 75:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum76" style="color:#606060;"> 76:</span>         Debug.WriteLine(<span style="color:#006080;">"Forward Order - OnAuthorization : AuthorizationFilter (Scope Controller)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum77" style="color:#606060;"> 77:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum78" style="color:#606060;"> 78:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum79" style="color:#606060;"> 79:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum80" style="color:#606060;"> 80:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum81" style="color:#606060;"> 81:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> HandleErrorA : HandleErrorAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum82" style="color:#606060;"> 82:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum83" style="color:#606060;"> 83:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnException(ExceptionContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum84" style="color:#606060;"> 84:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum85" style="color:#606060;"> 85:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnException : HandleErrorA (Scope Action)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum86" style="color:#606060;"> 86:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum87" style="color:#606060;"> 87:</span> }</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum88" style="color:#606060;"> 88:</span></pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum89" style="color:#606060;"> 89:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> HandleErrorB : HandleErrorAttribute</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum90" style="color:#606060;"> 90:</span> {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum91" style="color:#606060;"> 91:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> OnException(ExceptionContext filterContext)</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum92" style="color:#606060;"> 92:</span>     {</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum93" style="color:#606060;"> 93:</span>         Debug.WriteLine(<span style="color:#006080;">"Reverse Order - OnException : HandleErrorB (Scope Action)"</span>);</pre>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum94" style="color:#606060;"> 94:</span>     }</pre>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum95" style="color:#606060;"> 95:</span> }</pre>
</div>
</div>
<p>We will register the above filters as below…</p>
<p><strong><em>ActionFilter1</em></strong> has the scope <em>Global</em>, <strong><em>ActionFIlter2</em></strong> has the scope <em>Controller</em>, <strong><em>ActionFIlter3</em></strong> has the Scope <em>Action</em>, <strong><em>AuthorizationFilter</em></strong> has the scope <em>Controller</em>, <strong><em>HandleErrorA</em></strong> has the scope <em>Global</em>, and <strong><em>HandleErrorB</em></strong> has the scope <em>Action</em>.</p>
<p>At the beginning of this article I mentioned that the run order of different types of filters. First Authorization filters, then Action Filters, followed by Result filters and at the end the Exception filters. This is true, however within each type of filters, some executes in reverse order and some executes in forward order.</p>
<p>The best way to explain is to examine the generated output.</p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Forward Order &#8211; OnAuthorization : AuthorizationFilter (Scope Controller)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Forward Order &#8211; OnActionExecuting : ActionFilter1 (Scope Global)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Forward Order &#8211; OnActionExecuting : ActionFilter2 (Scope Controller)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Forward Order &#8211; OnActionExecuting : ActionFilter3 (Scope Action)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Home Controller, Index Action</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnActionExecuted : ActionFilter3 (Scope Action)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnActionExecuted : ActionFilter2 (Scope Controller)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnActionExecuted : ActionFilter1 (Scope Global)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Forward Order &#8211; OnResultExecuting : ActionFilter1 (Scope Global)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Forward Order &#8211; OnResultExecuting : ActionFilter2 (Scope Controller)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Forward Order &#8211; OnResultExecuting : ActionFilter3 (Scope Action)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnResultExecuted : ActionFilter3 (Scope Action)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnResultExecuted : ActionFilter2 (Scope Controller)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnResultExecuted : ActionFilter1 (Scope Global)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnException : HandleErrorB (Scope Action)</strong></span></p>
<p><span style="color:#000000;font-family:'Courier New';"><strong>Reverse Order &#8211; OnException : HandleErrorA (Scope Global)</strong></span></p>
<ul>
<li>There is only one Authorization Filter in Controller scope and it runs first as expected.</li>
</ul>
<ul>
<li>The Action Filters run at second. Notice that <em>OnActionExecuting </em>event runs in an order where the order of filter execution is determined by their scope (lowest number first in the enumeration). As you see, Action Filters <em>OnActionExecuting </em>event runs in a forward order.</li>
</ul>
<ul>
<li>Home Controller’s Index Action executes.</li>
</ul>
<ul>
<li>Action Filters do not always run in forward order. As you see during <em>OnActionExecuted </em>event, they run in a reverse order.</li>
</ul>
<ul>
<li>Very similar to <em>OnActionExecuted </em>event, <em>OnResultExecuted </em>event runs in reverse order.</li>
</ul>
<ul>
<li>Error Handler filters run at last. For each Error Handler filter, <em>OnException </em>event runs in  reverse order. Prior to MVC 3, ErrorHandler’s <em>OnException</em> event ran in forward order. This approach makes MVC exception filters more aligned with .NET exception handlers.</li>
</ul>
<p><strong>Summary</strong></p>
<p>There are multiple factors involved when deciding the run order of MVC filters. This includes the explicit ordering, the scope they belong to, and the type of the filter. Controller itself is a filter and always run first. By design, MVC executes certain filters in specific order. Some are run in forward order and some are run in reverse order.</p>
<p>For your favourite posts, subscribe to my blog: <a href="http://blog.rajsoftware.com">http://blog.rajsoftware.com</a></p>
<p><a href="http://blog.rajsoftware.com/post/2011/05/14/MVC3-Filter-Ordering.aspx">http://blog.rajsoftware.com/post/2011/05/14/MVC3-Filter-Ordering.aspx</a></p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>, <a href='http://rajsoftware.wordpress.com/category/asp-net-mvc/'>ASP.NET MVC</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/95/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/95/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/95/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=95&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2011/05/14/mvc3-filter-ordering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2011/05/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>My recent posts&#8230;</title>
		<link>http://rajsoftware.wordpress.com/2010/05/30/my-recent-posts/</link>
		<comments>http://rajsoftware.wordpress.com/2010/05/30/my-recent-posts/#comments</comments>
		<pubDate>Sun, 30 May 2010 00:28:30 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[.NET Framework Core]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Methodologies]]></category>

		<guid isPermaLink="false">http://rajsoftware.wordpress.com/?p=89</guid>
		<description><![CDATA[I know some of you using this blog to read my articles, but I decided to use my BlogEngine.NET  blog as my primary blog.  However I still add my RSS feeds to this blog and the links of my primary blog whenever I write a new post. So you can still refer to them from this blog. Below [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=89&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I know some of you using this blog to read my articles, but I decided to use my BlogEngine.NET  blog as my <a href="http://blog.rajsoftware.com/" target="_blank">primary blog</a>.  However I still add my RSS feeds to this blog and the links of my primary blog whenever I write a new post. So you can still refer to them from this blog. Below are some of the posts I recently added to my <a href="http://blog.rajsoftware.com/" target="_blank">primary blog</a>.</p>
<p><a href="http://blog.rajsoftware.com/post/2010/05/30/Working-with-Strings-in-CShapNET.aspx" target="_blank">Working with Strings in C#.NET</a></p>
<p><a href="http://blog.rajsoftware.com/post/2010/05/19/VS2010-Undo-Close-Documents.aspx" target="_blank">VS2010 Undo Close Documents</a></p>
<p><a href="http://blog.rajsoftware.com/post/2010/05/13/Tooling-support-for-Unit-Tests-and-TDD-(Test-Driven-Development).aspx" target="_blank">Tooling support for Unit Tests and TDD (Test Driven Development)</a></p>
<p><a href="http://blog.rajsoftware.com/post/2010/04/30/Debugging-Visualizing-and-Querying-data-using-LINQ.aspx" target="_blank">Debugging, Visualizing and Querying data using LINQ</a></p>
<p><a href="http://blog.rajsoftware.com/post/2010/05/06/MVC2-Validation-step-by-step.aspx" target="_blank">MVC2 Validation step by step</a></p>
<p><a href="http://blog.rajsoftware.com/post/2010/05/15/Useful-Unit-Testing-Research-Tools.aspx" target="_blank">Useful Unit Testing Research Tools</a></p>
<p><a href="http://blog.rajsoftware.com/post/2010/05/09/Practical-approach-to-Unit-Testing.aspx" target="_blank">Practical approach to Unit Testing</a></p>
<p>Regards,</p>
<p>Raj</p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/net-framework-core/'>.NET Framework Core</a>, <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>, <a href='http://rajsoftware.wordpress.com/category/methodologies/'>Methodologies</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/89/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/89/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/89/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=89&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2010/05/30/my-recent-posts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>
	</item>
		<item>
		<title>Practical approach to Unit Testing</title>
		<link>http://rajsoftware.wordpress.com/2010/05/09/practical-approach-to-unit-testing/</link>
		<comments>http://rajsoftware.wordpress.com/2010/05/09/practical-approach-to-unit-testing/#comments</comments>
		<pubDate>Sun, 09 May 2010 00:56:19 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Methodologies]]></category>

		<guid isPermaLink="false">http://rajsoftware.wordpress.com/?p=81</guid>
		<description><![CDATA[This article explains some key approaches that you would consider when writing Unit Tests.  As developers most of us write Unit Tests and we all know what is a Unit Test. Some may have slightly different opinions, but it is actually a programmatic verification of a single method/unit to ensure it behaves as expected. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=81&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This article explains some key approaches that you would consider when writing Unit Tests. </p>
<p>As developers most of us write Unit Tests and we all know what is a <strong><em>Unit Test</em></strong>. Some may have slightly different opinions, but it is actually a programmatic verification of a single method/unit to ensure it behaves as expected. The reason I explain this because some developers still refers to a <em>Unit</em> is a component, assembly, or a module. When to comes to the term Unit Testing, you really testing a sensible testable unit of your software, and anything bigger than that would considered to be more of a integration testing. </p>
<h2>Benefits</h2>
<p>Lets get real and discuss some of the benefits. I want to be more practical, and leave the MSDN Unit Testing benefits behind.</p>
<p>As a developer when you decide to write Unit Tests, do you ask the question from yourself, <em>why I’m really writing these Unit Tests</em>? Some of you answer <em>Yes</em>, and some of you say <em>No.</em></p>
<p>Answer <em>No</em>, is simply because not knowing what you do, or because everyone else write Unit Tests. If your answer is <em>Yes, </em>that’s great, and you really consider the pros and cons of having Unit Tests.</p>
<p>All these benefits can be categorized into 3 main areas.</p>
<p><strong>1.       </strong><strong>Quality.</strong></p>
<p><strong>2.       </strong><strong>Design.</strong></p>
<p><strong>3.       </strong><strong>Cost benefits.</strong></p>
<p><strong> </strong></p>
<p>Lets talk about these areas briefly. </p>
<p>The quality that provides by the Unit Tests can be many ways. It assists developing testable code. With TDD, the method/function that you write against a Unit Test, is well defined, and has a single responsibility. Unit Tests it self provide a valuable documentation to the project.  Less bugs will be introduced, also fixing bugs will be lot easier. As a developer you will have a great confidence on your system. </p>
<p>If you practice TDD, you know Unit Test allows you to design a specific behaviour within your method. For example with TDD, you write your test first, make it fails, and then based on your test you write the method to ensure that your test passes. The result of this is basically designing a specific behaviour within your method. The other aspect of the <em>Design</em> is, when you design your system, you will also consider your functional requirements. Because one of the things that you need to ensure when you design your system is that your functional requirements have been met. Your Unit Tests are based on these functional requirements. I will discuss this in more detail in “<strong><em>Selective Unit Testing</em></strong>” section. </p>
<p>We would love to develop a perfect system. But in reality, that is not always the case. Worse and the painful thing is some time projects are over the budget. This could be due to number of factors. One of the most common issues associated with the project cost is unexpected work at the end of the development cycle. This could be due to the number of bugs that have been raised during system testing. The number of iterations between the development and system testing cycles seems never ending due to the amount of bugs been raised. This can be due to poor system design, and or un-testable code. </p>
<p><a href="http://rajsoftware.files.wordpress.com/2010/05/costdevcycle.png"></a></p>
<p>As you can see here, the number of defects you find is high during the development and the cost to fix those defects is relatively low when comparing to further stage of project life cycle. This is why the Unit Tests are important to ensure that we introduce fewer bugs during the development so the cost to fix those bugs are negligible at the later stage of the project cycle.</p>
<h2>Selective Unit Testing</h2>
<p>Writing “Unit Tests” is a good thing. Writing bad Unit Tests and/or unnecessary Unit Tests can lead to considerably high maintenance cost to your overall project. Do you believe on statements such as</p>
<p>“<em>We should aim for 100% code coverage</em>”, “<em>Any Unit Test is better than none</em>”, or “<em>Our main priority is the Unit Testing</em>”?. All these statements do not add much value at all. These are some examples of expressing the way of the excitement, which causes by the <em>Unit Testing</em>. </p>
<p>Few years ago we hired a developer to one of our projects. He is very keen on Unit Testing and he spent lot of time writing and managing Unit Tests. At the end our project was over the budget, and not what we really expected. If he decided what is really important, and write only the necessary Unit Tests, it would have reduced our cost. </p>
<p>This is where it is really important that we need to ensure we write the absolutely necessary Unit Tests. This is also applicable if you practice TDD. You just don’t need to write Unit Tests for every method you write with in your application. Lets see some of these characteristics, and then how you decide what Unit Tests to write. </p>
<p>If you are aiming to write Unit Tests for every method to make your manager happy with higher code coverage, then it is fantastic to write these types of tests. But did you know that most of your tests did not add much value to the quality of your system at all. You see your entire tests pass with green ticks. There are so many green ticks make you feel good. Someone could argue that “<em>All the tests are there to ensure that every method behave as expected when it runs”</em>.  Well not necessarily correct. Did you consider if you have to make a change to a component how much re-factoring need to be carried out? </p>
<p>Let’s explain this with an example. When you have an obvious method, which takes a <em>String</em> parameter and returns a <em>Boolean</em>, and there are 100 methods calling this simple method, change to this method to return a <em>String</em> would require changing all those 100 methods. How about the repetition? Your Unit Test probably written to ensure that calling method takes a <em>String</em> value and returns a <em>Boolean. </em>Isn’t this repeating the same thing? How about the DRY? The time you have spent to write this Unit Test, you should have spent for some important task such as analysing some business requirements. </p>
<p>Consider the future maintenance cost for both production code and all these Unit Tests? The amount of time you need to write Unit Tests for every single method, the amount of time you have to spend to write tests for complicated methods, which add no obvious benefits to the design of the system. They do not add any value at all. </p>
<p>You may also reluctant to make changes to your code because of the amount to Unit Tests and the fear of breaking them.</p>
<h3><strong>Decisions<em> </em></strong></h3>
<p>So how you decide what Unit Tests to write. In order to decide this, you need to understand the types of methods you would normally write within your application. </p>
<p>1.       <strong>Simple methods </strong>– These methods are very obvious and simple. Just by having a look at code you can clearly see what it does.  The code it self explains the design of it.</p>
<p>        /// &lt;summary&gt;</p>
<p>        /// Gets the first name from fulllname.</p>
<p>        /// &lt;/summary&gt;</p>
<p>        /// &lt;param&gt;The full name.&lt;/param&gt;</p>
<p>        /// &lt;returns&gt;First Name&lt;/returns&gt;</p>
<p>        public string GetFirstNameFromFulllname(string fullName)</p>
<p>        {</p>
<p>            if (string.IsNullOrEmpty(fullName))</p>
<p>            {</p>
<p>                return string.Empty;</p>
<p>            }</p>
<p>            return fullName.Substring(0, fullName.IndexOf(&#8221; &#8220;));</p>
<p>        }</p>
<p>If you perform additional design and verification through Unit Testing, this method will give you a minimal benefit.</p>
<p>So if the method is simple and the design is obvious, you don’t need a Unit Test. If you are a TDD partitioner, the concept is the same. You do not need to write tests for every method you write within your application. </p>
<p>2.       <strong>Components that are already tested</strong> – I see some developers trying test components that have already been tested by the framework.            </p>
<p>        /// &lt;summary&gt;</p>
<p>        /// Adds the companies to cache.</p>
<p>        /// &lt;/summary&gt;</p>
<p>        /// &lt;param&gt;The company list.&lt;/param&gt;</p>
<p>        public void AddCompaniesToCache(IList&lt;Company&gt; companyList)</p>
<p>        {</p>
<p>            if (companyList != null &amp;&amp; companyList.Count() &gt; 0)</p>
<p>            {</p>
<p>HttpContext.Cache.Add(&#8220;companyCahchkey&#8221;, companyList, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 15, 0), CacheItemPriority.Normal, null);</p>
<p>            }</p>
<p>        } </p>
<p>The real purpose of this method is to add a list of companies to cache. A test would be to ensure the list is added to the cache. Why would do you need to write Unit Tests against this method when the ASP.NET caching – <em>Cache </em> is already tested for its capabilities. You can also see this method is very simple and hence Unit Tests are not required. </p>
<p>3.       <strong>Complex methods and have no value to the design at all</strong> –</p>
<p>For example you may have a method that writes to a registry using a complex pattern, and then using a complex algorithm to generate some custom strings, then serializes the data before calling a web service.</p>
<p>It will be very exhaustive exercise to write dependencies for some of these complex methods. There are so many things and variations to consider. These methods are not related to your main design or requirements but to assist or help other methods to get the job done.  Most of these methods are self-contained algorithms. </p>
<p>4.        <strong>High Level methods</strong> – There are methods within your application that integrated or calls other Units of methods. </p>
<p>public void ManageResourceData(string resources)</p>
<p>        {</p>
<p>            if (string.IsNullOrEmpty(resources))</p>
<p>                throw new ArgumentNullException(&#8220;resources&#8221;);</p>
<p>            string retResourceValues = MergeResources(resources);</p>
<p>            SaveResourcesToAFile(resources);</p>
<p>            EmailReaources(resources);</p>
<p>            AddResourcesToDatabase(resources);</p>
<p>            SetUserPermission();</p>
<p>        } </p>
<p>As you see here there are few units of methods being called. Writing Unit Tests for these types of methods, often waist of time. </p>
<p>5.       <strong>Complex methods with so many dependencies</strong>.</p>
<p>Some refer to this as GOD methods. Most TDD practitioners not experience these type of methods as they would to like to perform one assert per single method. Also methods are smaller and always written for a single responsibility. You might come across with a situation where complex method with so many dependencies. The method itself performs so many other things and need to be re-factored in order to make it more testable. You can see these methods really hard to Unit Tests. You have two options</p>
<p> a.       Re-factor your method and consider using interfaces, so you can mock all your dependencies during Unit Test.</p>
<p>b.       Simply do not waist your time to write the Unit Test. You may not have enough time to re-factor, and you can spend this time for something more useful.</p>
<p><a href="http://rajsoftware.files.wordpress.com/2010/05/codeunittestj.jpg"><img class="alignleft size-medium wp-image-83" title="codeUnitTestJ" src="http://rajsoftware.files.wordpress.com/2010/05/codeunittestj.jpg?w=300&#038;h=150" alt="Unit Tests" width="300" height="150" /></a></p>
<p>So the next important question is “<em>What types of methods that you write Unit Tests against?”</em>. The answer is not simple, which why you need to think carefully, before you write Unit Tests.</p>
<p> If you an application author, you would consider writing Unit Tests against the method that actually define your requirements. The focus should be to write methods with less number of dependencies by providing a single responsibility, while focusing on a specific requirement. Your dependencies should be easily mockable. The same consideration is applied if you are practicing TDD. When you implement your method with, then to pass your Unit Test, you should consider it as defining a specific requirement. If not, the test it self does not add value to your app.</p>
<p><a href="http://rajsoftware.files.wordpress.com/2010/05/selectiveutcostbenifitj.jpg"><img class="alignleft size-medium wp-image-84" title="selectiveUTCostBenifitJ" src="http://rajsoftware.files.wordpress.com/2010/05/selectiveutcostbenifitj.jpg?w=300&#038;h=209" alt="Requirements" width="300" height="209" /></a></p>
<p>The above graph displays the number of Unit Tests increases with the requirements. The more requirements you have the more Unit Tests you should have. </p>
<p>For example you may have a high level requirement to write a Banking application where customer performs banking operations. When you design your application, you will consider more detail requirements, such as customers should be able to withdraw, deposit, transfer, print statements within your application. Regardless of wether you are TDD practitioner or not, you should consider writing Unit Tests for withdraw, deposit, transfer and print statements. By doing this you add the <em>real </em>quality to your application. You will have the confidence that your application performs as expected from the Unit Level.</p>
<p>You should not consider writing Unit Tests for any algorithms that may have to calculate your balances, encryptions, serializations, locked/isolated, or wether the database get initialised. These are some of the examples that you should avoid as they do not add much values your Unit Tests.</p>
<p>If you framework author you would consider writing Unit Tests ensure that the methods you have written confirms to the key design principles of your framework. Framework author should target the requirements, which provided by the design of the framework. For example, you are developing a resource management framework, and you need to ensure the developer who uses the framework could successfully retrieve images from your resource management system. So you would write a Unit Test to ensure that developers can retrieve images as expected.</p>
<p>I do not say that we should avoid Unit Tests and should not practice TDD. They both provide some real benefits to the application. However having unnecessary code / Uni Tests leads to more maintenance, and extra cost to projects.<span id="_marker"> </span></p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>, <a href='http://rajsoftware.wordpress.com/category/methodologies/'>Methodologies</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=81&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2010/05/09/practical-approach-to-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2010/05/codeunittestj.jpg?w=300" medium="image">
			<media:title type="html">codeUnitTestJ</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2010/05/selectiveutcostbenifitj.jpg?w=300" medium="image">
			<media:title type="html">selectiveUTCostBenifitJ</media:title>
		</media:content>
	</item>
		<item>
		<title>MVC2 Validation step by step</title>
		<link>http://rajsoftware.wordpress.com/2010/05/09/mvc2-validation-step-by-step/</link>
		<comments>http://rajsoftware.wordpress.com/2010/05/09/mvc2-validation-step-by-step/#comments</comments>
		<pubDate>Sun, 09 May 2010 00:47:53 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://rajsoftware.wordpress.com/?p=76</guid>
		<description><![CDATA[Out of the box, MVC 2 framework supports both server side and client side validation.  Server Side validation is based on System.ComponentModel.DataAnnotations, and Client Side validation is based on ASP.NET Ajax 4.0. Both Server and Client Side validation are loosely coupled, and they are joined using JSON.  You can also have multiple validator providers plugged [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=76&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Out of the box, MVC 2 framework supports both server side and client side validation. </p>
<p>Server Side validation is based on <strong>System.ComponentModel.DataAnnotations</strong>, and Client Side validation is based on <strong>ASP.NET Ajax 4.0</strong>.</p>
<p>Both Server and Client Side validation are loosely coupled, and they are joined using <strong>JSON</strong>. </p>
<p>You can also have multiple validator providers plugged in and run at the same time. You can remove the built-in validation providers and you can create your own once. </p>
<p>Prior to MVC 2, MVC 1 used third party frameworks and plug-ins such as <strong>xVal</strong> to enable both client side and server validation. MVC 2 allows you to enable both server side and client side validation without having to use any third party frameworks or plug-ins. </p>
<p>This article explains how to enable both server side and client side validation step by step.</p>
<p> <strong>Enabling server side validation.</strong></p>
<p>Lets start with the View Model. Please refer to the <strong>DemoModel</strong> code sample below.</p>
<p>There is a <strong><em>DisplayName</em></strong><em> </em>attribute for each<em> </em>property. This attribute produces the name, which you should display within the View. Form validation point of view, it doesn’t add any value at all. What is important here is the “<strong><em>Required</em></strong><em>” </em>attribute and “<strong><em>StringLength</em></strong><em>” </em>attribute. These attributes belong to the <strong>System.ComponentModel.DataAnnotations</strong> namespace.</p>
<p> There four built-in attributes that you can for validation. They are</p>
<ul>
<li>Required</li>
<li>StringLength</li>
<li>Range</li>
<li>RegularExpression.</li>
</ul>
<p>If any of above attributes doesn’t fit for your requirements, then you can easily create your own validation attribute if needed. For more info please refer to</p>
<p><a href="http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx">http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx</a></p>
<p> public class DemoModel</p>
<p>    {</p>
<p>        [Required]</p>
<p>        [DisplayName("Product Code")]</p>
<p>        public string ProductCode { get; set; }</p>
<p>        [StringLength(10)]       </p>
<p>        [DisplayName("Product Description")]</p>
<p>        public string ProductDescription { get; set; }</p>
<p>        [Required]       </p>
<p>        [DisplayName("Product Date")]</p>
<p>        public string ProductDate { get; set; }</p>
<p>    }</p>
<p>1.       This is how the View looks like.</p>
<p>(<em>Please not that greyed code samples are not relevant to MVC validation.)</em> </p>
<p>&lt;%@ Page Title=&#8221;" MasterPageFile=&#8221;~/Views/Shared/Site.Master&#8221; <strong>Inherits=&#8221;System.Web.Mvc.ViewPage&lt;ValidationDemo.Models.DemoModel&gt;</strong>&#8221; %&gt; </p>
<p>&lt;% using (Html.BeginForm()) { %&gt;</p>
<p>        &lt;div&gt;</p>
<p>            &lt;fieldset&gt;</p>
<p>                &lt;legend&gt;Product Information&lt;/legend&gt;</p>
<p>                &lt;div&gt;</p>
<p>                    &lt;%= Html.LabelFor(m =&gt; m.ProductCode)%&gt;</p>
<p>                &lt;/div&gt;</p>
<p>                &lt;div&gt;</p>
<p>                    &lt;%= Html.TextBoxFor(m =&gt; m.ProductCode) %&gt;</p>
<p>                    &lt;%= Html.ValidationMessageFor(m =&gt; m.ProductCode) %&gt;</p>
<p>                &lt;/div&gt;               </p>
<p>                &lt;div&gt;</p>
<p>                    &lt;%= Html.LabelFor(m =&gt; m.ProductDescription) %&gt;</p>
<p>                &lt;/div&gt;</p>
<p>                &lt;div&gt;</p>
<p>                    &lt;%= Html.TextBoxFor(m =&gt; m.ProductDescription) %&gt;</p>
<p>                    &lt;%= Html.ValidationMessageFor(m =&gt; m.ProductDescription) %&gt;</p>
<p>                &lt;/div&gt;</p>
<p>                &lt;p&gt;</p>
<p>                    &lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221; /&gt;</p>
<p>                &lt;/p&gt;</p>
<p>            &lt;/fieldset&gt;</p>
<p>        &lt;/div&gt;</p>
<p>    &lt;% } %&gt;</p>
<p> You can see that I’m deriving from <strong>ValidationDemo.Models.DemoModel. </strong>So my View will be strongly typed and I can bind to the Model properties using m =&gt; m.ProductCode etc.</p>
<p>The value that enter to the ProductCode text box is bound to the value of the Model using &lt;%= Html.TextBoxFor(m =&gt; m.ProductCode) %&gt;</p>
<p> The validation message is displayed using the html helper extension.</p>
<p>&lt;%= Html.ValidationMessageFor(m =&gt; m.ProductCode) %&gt; </p>
<p>If you remove this line, your validation will still work, however no messages will be displayed. </p>
<p>Importantly you <span style="text-decoration:underline;">must</span> ensure that your form which require validation is placed in between &lt;% using (Html.BeginForm()) {  } tags. (See above)</p>
<p>2.       One last thing to remember is that you need an action method within your Controller to be able to execute your form POST. You must also pass the model to your action method, as a parameter. </p>
<p>    public class ValidationController : Controller</p>
<p>    {</p>
<p>        public ActionResult Demo()</p>
<p>        {</p>
<p>            return View();</p>
<p>        }</p>
<p>        <strong>[HttpPost]</strong></p>
<p><strong>        public ActionResult Demo(DemoModel model)</strong></p>
<p><strong>        {</strong></p>
<p><strong>            return View();</strong></p>
<p><strong>        }</strong>    </p>
<p>    }</p>
<p>3.       Now when you click on Submit button, server side validation will trigger and the error messages are displayed.</p>
<p><a href="http://rajsoftware.files.wordpress.com/2010/05/a.jpg"><img class="alignleft size-medium wp-image-77" title="a" src="http://rajsoftware.files.wordpress.com/2010/05/a.jpg?w=300&#038;h=108" alt="a" width="300" height="108" /></a></p>
<p>4.    Using <strong>ValidationSummaryControl</strong></p>
<p>       If you need the validation summary control (most of you do), you have to add the following helper extension method to the View. You can add this helper method anywhere you like (within the View). </p>
<p>       &lt;%= Html.ValidationSummary(false, &#8220;Product creation was unsuccessful. Please correct the errors and try again.&#8221;) %&gt;   </p>
<p> The first argument (<em>excludePropertyErrors: </em>true/false) specifies whether you want display list of error underneath the ValidationSummaryControl.</p>
<p><a href="http://rajsoftware.files.wordpress.com/2010/05/b.jpg"><img class="alignleft size-medium wp-image-78" title="b" src="http://rajsoftware.files.wordpress.com/2010/05/b.jpg?w=300&#038;h=185" alt="b" width="300" height="185" /></a></p>
<p><strong>Enabling client side validation.</strong> </p>
<p>MVC 2, uses Microsoft AJAX 4.0 as the default client side library. You can also use the JQuery version of the validation library. It is in MVC 2 Futures project. You can download it from <a href="http://aspnet.codeplex.com/releases/view/41742#DownloadId=110348">MVC2 Future library</a>.</p>
<p>1.       Ensure you reference three JS files within your application.</p>
<h2> </h2>
<p><strong>If you are using MS Ajax:</strong></p>
<p>&lt;script src=&#8221;/Scripts/MicrosoftAjax.js&#8221;&gt;&lt;/script&gt;</p>
<p>&lt;script src=&#8221;/Scripts/MicrosoftMvcAjax.js&#8221;&gt;&lt;/script&gt;</p>
<p>&lt;script src=&#8221;/Scripts/MicrosoftMvcValidation.js&#8221;&gt;&lt;/script&gt;  </p>
<p><strong>If you are using Jquery</strong></p>
<p>&lt;script src=&#8221;/Scripts/jquery-1.3.2.min.js&#8221;&gt;&lt;/script&gt;</p>
<p>&lt;script src=&#8221;/Scripts/jquery.validate.min.js&#8221;&gt;&lt;/script&gt;</p>
<p>&lt;script src=&#8221;/Scripts/MicrosoftMvcJQueryValidation.js&#8221;&gt;&lt;/script&gt; </p>
<p>Within your View, before the &lt;% using (Html.BeginForm()) { %&gt; statement, you need to add the code: &lt;% Html.EnableClientValidation(); %&gt; </p>
<ol>
<li>That’s it. Now run the app. You can see the client side validation in action.</li>
</ol>
<p><a href="http://rajsoftware.files.wordpress.com/2010/05/c.jpg"><img class="alignleft size-medium wp-image-79" title="c" src="http://rajsoftware.files.wordpress.com/2010/05/c.jpg?w=300&#038;h=74" alt="c" width="300" height="74" /></a></p>
<p>If it is still not working, ensure the browser does not disable your JavaScript by default. For example in FireFox you need to ensure <strong><span style="text-decoration:underline;">both</span> </strong><em>Java</em> and <em>Javascript</em> is not disabled. To be honest I don’t know why <em>Java</em> needed to be disabled. Please let me know if anyone finds out.</p>
<p><strong> Tweaks</strong></p>
<p>Enabling Validation Summary Control for client side. </p>
<p>Out of the box Validation Summary Control does not work for client side validation. For example you cannot display validation summary error massagers, within the Validation Summary Control during client side user interactions. To enable this you can implement a solution, which is described in the post <a href="http://geekswithblogs.net/stun/archive/2010/02/27/asp.net-mvc-client-side-validation-summary-with-jquery-validation-plugin.aspx">here</a>.</p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/76/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/76/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/76/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=76&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2010/05/09/mvc2-validation-step-by-step/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2010/05/a.jpg?w=300" medium="image">
			<media:title type="html">a</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2010/05/b.jpg?w=300" medium="image">
			<media:title type="html">b</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2010/05/c.jpg?w=300" medium="image">
			<media:title type="html">c</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging, Visualizing and Querying data using LINQ</title>
		<link>http://rajsoftware.wordpress.com/2010/04/29/debugging-visualizing-and-querying-data-using-linq/</link>
		<comments>http://rajsoftware.wordpress.com/2010/04/29/debugging-visualizing-and-querying-data-using-linq/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 10:11:44 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://rajsoftware.wordpress.com/?p=69</guid>
		<description><![CDATA[One of the challenging things that I have confronted in my day-to-day work is writing complex LINQ queries. I’m sure there are lot of developers out there who have experienced the same when writing complex LINQ queries. This is a simple article to assist for those developers to debug, visualize and query using LINQ to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=69&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the challenging things that I have confronted in my day-to-day work is writing complex LINQ queries. I’m sure there are lot of developers out there who have experienced the same when writing complex LINQ queries. This is a simple article to assist for those developers to debug, visualize and query using LINQ to SQL.   </p>
<p>Those who new to LINQ to SQL, it is a database development model in the context of an Object Oriented Programming Model. LINQ is a broad topic. If you need to know more about LINQ, there lots of great articles in the web including some excellent sources from MSDN.   </p>
<p>.NET Framework 4.0 has also made significant enhancements to its LINQ model. This includes improvements to areas such as performances, query stabality, usability, LINQ to SQL class designer and much more. You can find some of those improvement in <a href="http://www.linq-to-sql.com/linq-to-sql/what-is-linq/" target="_blank">here</a>. </p>
<p>LINQ to SQL translates the queries you write into parameterized SQL queries (in text form) and send them to the SQL server for processing. Sometimes debugging LINQ queries can be problematic. If you want to know more about debugging LINQ queries please refer to <a href="http://blogs.msdn.com/ericwhite/archive/2008/11/07/debugging-linq-queries.aspx" target="_blank">this article</a>.  </p>
<p>There are other ways to output LINQ to SQL queries so you can see the entire query that get executed. For example you can output to a debug window, file, or to a memory. Some examples are described in the below articles.   </p>
<p><a href="http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers" target="_blank">LINQ to SQL log to debug window, file, memory or multiple writers  </a></p>
<p><a href="http://www.codesprouts.com/post/View-LINQ-To-SQL-Statements-Using-A-Debug-TextWriter.aspx" target="_blank">View LINQ To SQL Statements Using A Debug TextWriter</a> </p>
<p> VS2010 has a <em>Historical Debugger  </em>which can also be used to view and debug LINQ queries. More info is <a href="http://blogs.msdn.com/habibh/archive/2009/07/14/debugging-linq-to-sql-queries-using-the-historical-debugger.aspx">here</a>. </p>
<p><strong>Debug Visualisers</strong> </p>
<p>LINQ to SQL Debug Visualiser enables you to view and execute the LINQ queries on the fly. It was a part of beta releases of VS2008, but for some reason MS has dropped it from the RTM release. I cannot see this is integrated into VS2010 either. It is a separate download. You can download it from <a href="http://www.scottgu.com/blogposts/linqquery/SqlServerQueryVisualizer.zip" target="_blank">here</a>. </p>
<p>Take a look at this <a href="http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx" target="_blank">article</a> on LINQ to SQL Debug Visualizer, which explains how it is integrated and used within Visual Studio.</p>
<p>VS LINQ to SQL Debug Visualizer has its own limitations to it. For example it does not diplay the complete query. It is hard to edit and execute while debugging. This <a href="http://davidhayden.com/blog/dave/archive/2007/08/17/LINQToSQLDebuggerVisualizerVisualStudio2008HasLimitedValueLINQToSQLTutorials.aspx" target="_blank">article</a> discribe some of the limitations.</p>
<p>There is another similar tool that you can download from VS Gallary. This tool should work with any database as well as MS SQL. You can find more information <a href="http://visualstudiogallery.msdn.microsoft.com/en-us/99468ece-689b-481c-868c-19e00e0a4e69" target="_blank">here</a>. </p>
<p class="MsoNormal" style="margin:0;"><strong><span style="font-size:10pt;font-family:Arial;">LINQPad</span></strong> </p>
<p>I think this is an excellent tool to write and test your LINQ queries. It is a great tool to help you build any type of queries with LINQ. Standard edition is free and I recon every developer should have it. You save lot of time by having these tools which allows you to be more productive when writng queries. </p>
<p><a href="http://rajsoftware.files.wordpress.com/2010/04/linqp2.png"><img class="alignleft size-medium wp-image-70" title="LINQP" src="http://rajsoftware.files.wordpress.com/2010/04/linqp2.png?w=300&#038;h=249" alt="LINQP" width="300" height="249" /></a> </p>
<p>You can also connect to a SQL Server DB (Express or Compact Edition) and with a connection to your LINQ Data component (if you have built one) you can perform queries right against your data with LINQ – that’s excellent!  </p>
<p>The standard edition is free to download and it supports .NET 4.0 as well. Standard edition does not have the <em>Autocompletion</em> feature.  For more information on Autocompletion please click <a href="http://www.linqpad.net/Purchase.aspx" target="_blank">here</a>. </p>
<p>There is also great webcast that explains the new features of LINQPad with respect to the Entity framework. </p>
<p>More importantly it is a great tool to learn and improve your skills on LINQ to SQL. </p>
<p>You can download this tool from <a href="http://www.linqpad.net/Purchase.aspx" target="_blank">here</a>.</p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/asp-net/'>ASP.NET</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=69&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2010/04/29/debugging-visualizing-and-querying-data-using-linq/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2010/04/linqp2.png?w=300" medium="image">
			<media:title type="html">LINQP</media:title>
		</media:content>
	</item>
		<item>
		<title>Few tips to make you a better Developer/Programmer</title>
		<link>http://rajsoftware.wordpress.com/2010/04/12/few-tips-to-make-you-a-better-developerprogrammer/</link>
		<comments>http://rajsoftware.wordpress.com/2010/04/12/few-tips-to-make-you-a-better-developerprogrammer/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 11:34:31 +0000</pubDate>
		<dc:creator>Raj Aththanayake</dc:creator>
				<category><![CDATA[Methodologies]]></category>

		<guid isPermaLink="false">http://rajsoftware.wordpress.com/?p=60</guid>
		<description><![CDATA[I will try to keep this article as short as I can, and at the same time sounds interesting. There are so many developers out there. They all write code to develop/support applications. Some are good at it, some are average, and some are not good at it. It doesn’t really matter which category you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=60&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://rajsoftware.files.wordpress.com/2010/04/programmer.jpg"><img class="alignleft size-medium wp-image-61" title="programmer" src="http://rajsoftware.files.wordpress.com/2010/04/programmer.jpg?w=300&#038;h=204" alt="programmer" width="300" height="204" /></a></strong></p>
<p>I will try to keep this article as short as I can, and at the same time sounds interesting. There are so many developers out there. They all write code to develop/support applications. Some are good at it, some are average, and some are not good at it. It doesn’t really matter which category you are in, you always have room to improve yourself.</p>
<p>Here are some tips for you to become better.</p>
<p>1.       Practice make perfect. Write code whenever you can. The best way to learn newer technology is to use them, and work on them. Reading makes you knowledgeable but using them makes you even better.</p>
<p>2.       Work on open source libraries, read code and try to understand.</p>
<p>3.       Teach others, when they need, show them the right way.</p>
<p>4.       Learn from others as well. It is impossible to know everything, so you can always learn from others.</p>
<p>5.       Follow best practices when coding. This includes following best coding standards and writing readable code. Remember to review your own code before you commit.</p>
<p>6.       Why not make sure the quality code that you written are well maintained. Nowadays many developers write Unit Tests. So I see good developers write Unit Tests to ensure the quality of the code and the product it self.</p>
<p>7.       Developers make mistakes. Even the best developers make mistakes. So do not afraid to make mistakes, and do not afraid to try new things. We all learn from mistakes.</p>
<p>8.       It is always rewarding when you have good code written in an application. So always try your best.</p>
<p>9.       Once you written a piece of code, get someone to check. Do a code review. There is always room for improvement. You see things more with another pare of eyes.</p>
<p>10.   If you learn something new and share with fellow developers. It is a great thing that you share your knowledge.</p>
<p> Happy programming.</p>
<br />Filed under: <a href='http://rajsoftware.wordpress.com/category/methodologies/'>Methodologies</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rajsoftware.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rajsoftware.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rajsoftware.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rajsoftware.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rajsoftware.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rajsoftware.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rajsoftware.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rajsoftware.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rajsoftware.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rajsoftware.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rajsoftware.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rajsoftware.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rajsoftware.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rajsoftware.wordpress.com/60/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rajsoftware.wordpress.com&amp;blog=12332628&amp;post=60&amp;subd=rajsoftware&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rajsoftware.wordpress.com/2010/04/12/few-tips-to-make-you-a-better-developerprogrammer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8d5445ef1aa954739b9d0719d09cf612?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rajithakba</media:title>
		</media:content>

		<media:content url="http://rajsoftware.files.wordpress.com/2010/04/programmer.jpg?w=300" medium="image">
			<media:title type="html">programmer</media:title>
		</media:content>
	</item>
	</channel>
</rss>
