<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Emad Ibrahim</title>
	
	<link>http://www.emadibrahim.com</link>
	<description>web development, tech reviews, tips, tricks, blogging</description>
	<pubDate>Tue, 11 Nov 2008 03:45:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/EmadIbrahim" type="application/rss+xml" /><feedburner:emailServiceId>1084126</feedburner:emailServiceId><feedburner:feedburnerHostname>http://www.feedburner.com</feedburner:feedburnerHostname><item>
		<title>Yonkly Making Progress</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/449099625/</link>
		<comments>http://www.emadibrahim.com/2008/11/10/yonkly-making-progress/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 03:43:19 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[yonkly]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/11/10/yonkly-making-progress/</guid>
		<description>I have two reasons to celebrate.&amp;#160; Yonkly&amp;#8217;s team has doubled in size.&amp;#160; That is 200% growth.&amp;#160; Yes, we are two people now.&amp;#160; Scott Scheper has joined Yonkly and will be bringing a whole lot of experience, strategic planning, vision, direction and eventually a lot of money.
The second reason to celebrate: we finally popped the cherry [...]</description>
			<content:encoded><![CDATA[<p>I have two reasons to celebrate.&#160; Yonkly&#8217;s team has doubled in size.&#160; That is 200% growth.&#160; Yes, we are two people now.&#160; Scott Scheper has joined Yonkly and will be<a href="http://www.emadibrahim.com/wp-content/uploads/2008/11/champagne.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="191" alt="champagne" src="http://www.emadibrahim.com/wp-content/uploads/2008/11/champagne-thumb.jpg" width="244" align="right" border="0" /></a> bringing a whole lot of experience, strategic planning, vision, direction and eventually a lot of money.</p>
<p>The second reason to celebrate: we finally popped the cherry and sold our first enterprise copy of Yonkly (<a href="http://about.yonkly.com" onclick="javascript:pageTracker._trackPageview ('/outbound/about.yonkly.com');">http://about.yonkly.com</a>).&#160;&#160; Now, we just have to do it again a few thousand times.</p>
<p>Yonkly is now in private beta, but you can signup for an invitation at <a href="http://about.yonkly.com/" onclick="javascript:pageTracker._trackPageview ('/outbound/about.yonkly.com');">http://about.yonkly.com/</a>.&#160; It will be coming out shortly and it will be kicking some ass.&#160; If you can&#8217;t wait, you can always buy the latest version with source code for $399, go to <a href="http://about.yonkly.com/" onclick="javascript:pageTracker._trackPageview ('/outbound/about.yonkly.com');">http://about.yonkly.com/</a> enter your email and you will receive instructions.</p>
<p>You can follow Yonkly&#8217;s progress on our new blog at <a href="http://blog.yonkly.com" onclick="javascript:pageTracker._trackPageview ('/outbound/blog.yonkly.com');">http://blog.yonkly.com</a> (it&#8217;s a work in progress).&#160; You can also follow us on twitter at <a href="http://twitter.com/yonkly" onclick="javascript:pageTracker._trackPageview ('/outbound/twitter.com');">http://twitter.com/yonkly</a>&#160;</p>
<p>Back to work, we have got a lot to do and not enough time.</p>
<p>Also, don&#8217;t for get to check Scott&#8217;s blog is <a href="http://scottdig.com/" onclick="javascript:pageTracker._trackPageview ('/outbound/scottdig.com');">http://scottdig.com/</a></p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/29/yonkly-source-code/" rel="bookmark" title="Permanent Link: Yonkly Source Code" >Yonkly Source Code</a></span><div class="aizattos_related_posts_excerpt">[UPDATE] Source code is now available in codeplex at http://www.codeplex.com/yonkly  I apologize for...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/06/24/yonkly-groups-and-new-look/" rel="bookmark" title="Permanent Link: Yonkly Groups and new Look" >Yonkly Groups and new Look</a></span><div class="aizattos_related_posts_excerpt">We finally got around to releasing a new version of yonkly.&nbsp; We have cleaned up the look and fe...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/" rel="bookmark" title="Permanent Link: Themes and ASP.NET MVC" >Themes and ASP.NET MVC</a></span><div class="aizattos_related_posts_excerpt">I wanted to easily add theme support to yonkly, so that others can install it and modify its look an...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/09/yonkly-updated-05092008/" rel="bookmark" title="Permanent Link: Yonkly: Updated 05/09/2008" >Yonkly: Updated 05/09/2008</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/20/i-quit/" rel="bookmark" title="Permanent Link: I quit" >I quit</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/449099625" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/11/10/yonkly-making-progress/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/11/10/yonkly-making-progress/</feedburner:origLink></item>
		<item>
		<title>Marketing Your Services Using Affiliate Programs</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/415830218/</link>
		<comments>http://www.emadibrahim.com/2008/10/09/marketing-your-services-using-affiliate-programs/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 14:35:52 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[Startup]]></category>

		<category><![CDATA[yonkly]]></category>

		<category><![CDATA[affiliates]]></category>

		<category><![CDATA[marketing]]></category>

		<category><![CDATA[yonklyapp]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/10/09/marketing-your-services-using-affiliate-programs/</guid>
		<description>I just launched a hosted microblogging platform (aka twitter clone) at www.yonklyapp.com and right now I am looking into different ways to market it.
One approach to my marketing strategy is to use an affiliate program.&amp;#160; We have all seen affiliate programs for everything from selling t-shirts to selling domains.&amp;#160; The question I struggled with is [...]</description>
			<content:encoded><![CDATA[<p>I just launched a hosted microblogging platform (aka twitter clone) at <a href="http://www.yonklyapp.com" onclick="javascript:pageTracker._trackPageview ('/outbound/www.yonklyapp.com');">www.yonklyapp.com</a> and right now I am looking into different ways to market it.</p>
<p>One approach to my marketing strategy is to use an affiliate program.&#160; We have all seen affiliate programs for everything from selling t-shirts to selling domains.&#160; The question I struggled with is &quot;how much do I pay my affiliates?&quot;&#160; I want it to be fair, competitive and also motivational enough to encourage the affiliates to market it.</p>
<p>I found out that <a href="http://www.37signals.com/svn/posts/1063-launch-announcing-the-new-37signals-affiliate-program-with-recurring-income" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/www.37signals.com');">37 Signals</a> pays 5% to their affiliates that sounds way too low.&#160; What would be a good percentage?&#160; 15%? 25%?&#160; The plan I came up with is a tiered approach which gradually increases your percentage from 10% to 50% - you can read more about it at <a href="http://www.yonklyapp.com/affiliates.aspx" onclick="javascript:pageTracker._trackPageview ('/outbound/www.yonklyapp.com');">http://www.yonklyapp.com/affiliates.aspx</a> and maybe even <a href="http://www.yonklyapp.com/affiliates.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/www.yonklyapp.com');">signup</a> to be an affiliate.&#160; The best part is that it is recurring.&#160; </p>
<p>So if you bring in a 100 customers, you will earn:</p>
<p>$200/month on the first 20   <br />$400/month on the next 20    <br />$600/month on the next 20    <br />$800/month on the next 20    <br />$1000/month on the last 20</p>
<p>That&#8217;s a total of <strong>$3000 EVERY MONTH</strong>.&#160; Isn&#8217;t that pretty damn good?&#160; Or not?&#160; I would love to hear your opinions.</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/30/my-elevator-pitch/" rel="bookmark" title="Permanent Link: My Elevator Pitch" >My Elevator Pitch</a></span><div class="aizattos_related_posts_excerpt">I found this cool site that has a free wizard which helps you build your elevator (15 second) pitch....</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/09/24/free-icons-and-images/" rel="bookmark" title="Permanent Link: Free Icons and Images" >Free Icons and Images</a></span><div class="aizattos_related_posts_excerpt">If you are creating any type of software applications with a user interface, most likely you will ne...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/14/visual-studio-window-manager/" rel="bookmark" title="Permanent Link: Visual Studio Window Manager" >Visual Studio Window Manager</a></span><div class="aizattos_related_posts_excerpt">This is a great productivity plugin and time saver…  Have you ever wanted to have different window...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/09/20/blog-rush-attract-more-readers/" rel="bookmark" title="Permanent Link: Blog Rush - Attract More Readers" >Blog Rush - Attract More Readers</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/29/make-money-from-my-blog/" rel="bookmark" title="Permanent Link: Make money from my blog" >Make money from my blog</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/415830218" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/10/09/marketing-your-services-using-affiliate-programs/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/10/09/marketing-your-services-using-affiliate-programs/</feedburner:origLink></item>
		<item>
		<title>Themes and ASP.NET MVC</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/409686380/</link>
		<comments>http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 22:34:53 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Tips &amp; Tricks]]></category>

		<category><![CDATA[Web Design]]></category>

		<category><![CDATA[Web Dev]]></category>

		<category><![CDATA[asp.net mvc]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/</guid>
		<description>I wanted to easily add theme support to yonkly, so that others can install it and modify its look and feel as they please.&amp;#160; I also wanted it to be as easy as installing a theme in wordpress.
I created a themes folder under the content folder
 
Then I referenced my css file in the master [...]</description>
			<content:encoded><![CDATA[<p>I wanted to easily add theme support to <a href="http://yonkly.com" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/yonkly.com');">yonkly</a>, so that others can install it and modify its look and feel as they please.&#160; I also wanted it to be as easy as installing a theme in <a href="http://wordpress.org" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/wordpress.org');">wordpress</a>.</p>
<p>I created a themes folder under the content folder</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image8.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="204" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb8.png" width="173" border="0" /></a> </p>
<p>Then I referenced my css file in the master page using a helper method</p>
<blockquote><p>&lt;%= ThemeHelper.GetCss() %&gt;</p>
</blockquote>
<p>I also use a helper method for images</p>
<blockquote><p>&lt;img src=&#8217;&lt;%=ThemeHelper.GetImageUrl(&quot;reply.png&quot;)%&gt;&#8217; alt=&quot;reply&quot; class=&quot;icon&quot; /&gt;</p>
</blockquote>
<p>But most of my images are set in the stylesheet, which makes it easier to manipulate different skins</p>
<p>The helper methods above look at the defined theme in the config file (or database or wherever you store your settings) and then return the path to the correct resource.</p>
<p>Take a look at these live samples:</p>
<ul>
<li><a href="http://www.yonkly.com" onclick="javascript:pageTracker._trackPageview ('/outbound/www.yonkly.com');">www.yonkly.com</a></li>
<li><a href="http://www.isweat.com" onclick="javascript:pageTracker._trackPageview ('/outbound/www.isweat.com');">www.isweat.com</a></li>
<li><a href="http://www.hokietalk.com" onclick="javascript:pageTracker._trackPageview ('/outbound/www.hokietalk.com');">www.hokietalk.com</a></li>
</ul>
<p>They are all using the same codebase but have a different theme defined.&#160; The trick is in making your HTML css-friendly by naming elements and assigning them classes as well as using Divs and avoiding tables.&#160; This allows you to create a stylesheet that radically changes the look of the site.&#160; Think of the element ids and classes as an API to your view, that the css can manipulate.</p>
<p>I also added a feature that lets you upload a folder theme as a zip file and have the application unzip it into the themes folder.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image9.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="355" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb9.png" width="494" border="0" /></a> </p>
<p>It would be cool if we can define a &quot;virtual folder&quot; in our application, so I wouldn&#8217;t have to use helper methods.&#160; Imagine if you could just say /content/theme/logo.gif and it would just work.&#160; The theme folder doesn&#8217;t really exist but instead it would route to the correct folder based on a setting.&#160; I wonder if I can do that with current routing mechanism in asp.net mvc!!!&#160; Anyone?</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/" rel="bookmark" title="Permanent Link: Ajax with ASP.NET MVC Framework Preview 2" >Ajax with ASP.NET MVC Framework Preview 2</a></span><div class="aizattos_related_posts_excerpt">This is the updated version of the Nikhil's excellent example on how to use Ajax with ASP.net MVC.
...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/" rel="bookmark" title="Permanent Link: Deciding Between ASP.NET MVC and WebForms" >Deciding Between ASP.NET MVC and WebForms</a></span><div class="aizattos_related_posts_excerpt">I am trying to create a decision flowchart for my ASP.NET MVC book and wanted to get some community ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/05/resolving-url-in-aspnet-using-tilda/" rel="bookmark" title="Permanent Link: Resolving URL in ASP.net using Tilda (~)" >Resolving URL in ASP.net using Tilda (~)</a></span><div class="aizattos_related_posts_excerpt">I have been using .net since it has come out and I just found this out (so, I am kind of pissed).  W...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/04/07/wordpress-themes/" rel="bookmark" title="Permanent Link: Wordpress themes" >Wordpress themes</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/11/aspnet-20-error-title-is-not-a-member-of-asp%e2%80%a6/" rel="bookmark" title="Permanent Link: asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…" >asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/409686380" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/</feedburner:origLink></item>
		<item>
		<title>Major Update to Yonkly: Widgets, Ads &amp; More</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/408447654/</link>
		<comments>http://www.emadibrahim.com/2008/10/01/major-update-to-yonkly-widgets-ads-more/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 17:39:50 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[yonkly]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/10/01/major-update-to-yonkly-widgets-ads-more/</guid>
		<description>I just released a new version of Yonkly with a ton of new features.
Widgets
If you are logged in, you can add a widget to your sidebar even place Google ads if you wanted.&amp;#160; Take a look at the widget in my profile at http://yonkly.com/eibrahim.&amp;#160; To add your own widget, go to settings and click the [...]</description>
			<content:encoded><![CDATA[<p>I just released a new version of <a href="http://yonkly.com" onclick="javascript:pageTracker._trackPageview ('/outbound/yonkly.com');">Yonkly</a> with a ton of new features.</p>
<h2>Widgets</h2>
<p>If you are logged in, you can add a widget to your sidebar even place Google ads if you wanted.&#160; Take a look at the widget in my profile at <a href="http://yonkly.com/eibrahim" onclick="javascript:pageTracker._trackPageview ('/outbound/yonkly.com');">http://yonkly.com/eibrahim</a>.&#160; To add your own widget, go to settings and click the widgets link then simply add your widget code and hit save</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="463" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb.png" width="385" border="0" /></a> </p>
<p>If you want your widget to fit nicely with the rest of the sidebar, click the link &quot;insert default widget&quot; and add your script in the correct place.</p>
<h2>Control Panel</h2>
<p>If you are the owner of the site i.e. an admin, you will have access to a control panel where you can customize a ton of things</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image1.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="170" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb1.png" width="244" border="0" /></a> </p>
<p><strong>Site Settings</strong></p>
<p>You can modify all sort of site settings such as title, description, keywords, amazon s3 information and so on.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="337" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb2.png" width="170" border="0" /></a> </p>
<p><strong>Email Templates</strong></p>
<p>You can customize all the emails that get sent out e.g. &quot;someone is following&quot;, &quot;someone joined your group&quot;, &quot;thanks for signing up&quot;, etc.</p>
<p><strong>Ad Management</strong></p>
<p>Now, you can add advertisements to your site by adding the ads code in the appropriate location.&#160; You can add it as a banner in the sidebar or in the message stream.</p>
<p>Here is a sidebar ad, you can also skin to match the site</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image3.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="227" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb3.png" width="244" border="0" /></a> </p>
<p>Here is a banner ad</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image4.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="81" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb4.png" width="244" border="0" /></a></p>
<p>And here is an embedded ad in the message stream </p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image5.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="141" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb5.png" width="244" border="0" /></a> </p>
<p><strong>Theme Management</strong></p>
<p>You can change the theme of the site.&#160; For examples, check <a href="http://www.hokietalk.com" onclick="javascript:pageTracker._trackPageview ('/outbound/www.hokietalk.com');">www.hokietalk.com</a> and <a href="http://www.isweat.com" onclick="javascript:pageTracker._trackPageview ('/outbound/www.isweat.com');">www.isweat.com</a>.&#160; Here is the theme selection screen</p>
<p> <a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image6.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="368" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb6.png" width="512" border="0" /></a>
<p>&#160;</p>
<p><strong>Content Management</strong></p>
<p>See the TOS, privacy and contact us links in the footer, those are dynamically created and you can create other pages as well e.g. &quot;FAQ&quot;, &quot;Help&quot; and so on&#8230;&#160; You simply enter a page name and the content using a rich text editor</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/10/image7.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="314" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/10/image-thumb7.png" width="344" border="0" /></a> </p>
<p><strong>User Management</strong></p>
<p>This is just a way to assign/revoke admin privileges to users.</p>
<h2>The Future</h2>
<p>I am very excited about all the new features specially that the site owner and the user can display ads and <strong>make money</strong>.&#160;&#160; I can&#8217;t wait to release the new version.&#160; <a href="http://www.emadibrahim.com/contact/">Contact me</a> if you are interested in setting up your own microblogging platform.</p>
<p>Stay Tuned!!!</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/11/yonkly-open-source-twitter/" rel="bookmark" title="Permanent Link: Yonkly - Open Source Twitter" >Yonkly - Open Source Twitter</a></span><div class="aizattos_related_posts_excerpt">[update: Yonkly source code is now public]

[update 2: I just created www.isweat.com using the yon...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/29/yonkly-source-code/" rel="bookmark" title="Permanent Link: Yonkly Source Code" >Yonkly Source Code</a></span><div class="aizattos_related_posts_excerpt">[UPDATE] Source code is now available in codeplex at http://www.codeplex.com/yonkly  I apologize for...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/09/yonkly-updated-05092008/" rel="bookmark" title="Permanent Link: Yonkly: Updated 05/09/2008" >Yonkly: Updated 05/09/2008</a></span><div class="aizattos_related_posts_excerpt">[update: Yonkly source code is now public]

We just released a new updated version younkly that is...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/11/10/yonkly-making-progress/" rel="bookmark" title="Permanent Link: Yonkly Making Progress" >Yonkly Making Progress</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/14/yonkly-new-release-04-14-08/" rel="bookmark" title="Permanent Link: Yonkly: New Release 04-14-08" >Yonkly: New Release 04-14-08</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/408447654" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/10/01/major-update-to-yonkly-widgets-ads-more/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/10/01/major-update-to-yonkly-widgets-ads-more/</feedburner:origLink></item>
		<item>
		<title>A Good TDD Process Diagram?</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/399855216/</link>
		<comments>http://www.emadibrahim.com/2008/09/22/a-good-tdd-process-diagram/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 14:50:33 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[MVC Book]]></category>

		<category><![CDATA[Software Development]]></category>

		<category><![CDATA[Testing]]></category>

		<category><![CDATA[mvcbook]]></category>

		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/09/22/a-good-tdd-process-diagram/</guid>
		<description>I am trying to create a flow diagram that depicts the Test Driven Development process and I need your expertise/opinion/criticism in nailing it down.
Take a look below and let me know what is wrong, what is right and what can be done better.
 
I drew the above diagram based on my understanding of TDD and [...]</description>
			<content:encoded><![CDATA[<p>I am trying to create a flow diagram that depicts the Test Driven Development process and I need your expertise/opinion/criticism in nailing it down.</p>
<p>Take a look below and let me know what is wrong, what is right and what can be done better.</p>
<p align="center"><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image4.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="725" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb4.png" width="235" border="0" /></a> </p>
<p>I drew the above diagram based on my understanding of TDD and by referencing other diagrams that I found online.&#160; Specifically:</p>
<p align="center"><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image5.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="375" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb5.png" width="211" border="0" /></a> </p>
<p>from <a href="http://www.agiledata.org/essays/tdd.html" onclick="javascript:pageTracker._trackPageview ('/outbound/www.agiledata.org');">http://www.agiledata.org/essays/tdd.html</a></p>
<p align="center"><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image6.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="284" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb6.png" width="241" border="0" /></a> </p>
<p>from <a href="http://designindrive.com/?p=31" onclick="javascript:pageTracker._trackPageview ('/outbound/designindrive.com');">http://designindrive.com/?p=31</a></p>
<p align="center"><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image7.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="126" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb7.png" width="244" border="0" /></a> </p>
<p>from <a href="http://tinyurl.com/498pjg" title="http://tinyurl.com/498pjg" onclick="javascript:pageTracker._trackPageview ('/outbound/tinyurl.com');">http://tinyurl.com/498pjg</a></p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/" rel="bookmark" title="Permanent Link: Deciding Between ASP.NET MVC and WebForms" >Deciding Between ASP.NET MVC and WebForms</a></span><div class="aizattos_related_posts_excerpt">I am trying to create a decision flowchart for my ASP.NET MVC book and wanted to get some community ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/23/my-altnet-code/" rel="bookmark" title="Permanent Link: My ALT.NET Code" >My ALT.NET Code</a></span><div class="aizattos_related_posts_excerpt">Here is my alt.net code...  Click on the link to decode it.
IOC(SM):IOC(Nin):
MOC(MQ):
TDD(NU):T...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/" rel="bookmark" title="Permanent Link: The Best IoC Container?" >The Best IoC Container?</a></span><div class="aizattos_related_posts_excerpt">As I previously mentioned in my post "The Best JavaScript Library", I am in the process of developin...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/" rel="bookmark" title="Permanent Link: The Best JavaScript Library" >The Best JavaScript Library</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/06/08/failed-to-generate-a-user-instance-of-sql-server-due-to-a-failure-in-starting-the-process-for-the-user-instance-the-connection-will-be-closed/" rel="bookmark" title="Permanent Link: Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed." >Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/399855216" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/09/22/a-good-tdd-process-diagram/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/09/22/a-good-tdd-process-diagram/</feedburner:origLink></item>
		<item>
		<title>Client &amp; Server Side Validation in ASP.NET MVC</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/387919299/</link>
		<comments>http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 22:58:24 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[MVC Book]]></category>

		<category><![CDATA[asp.net mvc]]></category>

		<category><![CDATA[aspnetmvc]]></category>

		<category><![CDATA[castle]]></category>

		<category><![CDATA[jquery]]></category>

		<category><![CDATA[mvcbook]]></category>

		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/</guid>
		<description>[Update: The code is open-sourced at http://code.google.com/p/mvcvalidation/]
The asp.net mvc codeplex preview 5 version just came out a few days ago and it includes a lot nice enhancements.&amp;#160; For the best introduction read Scott&amp;#8217;s post.&amp;#160; 
Some of my favorite changes happened in validation.&amp;#160; Incidentally, I was already working on a post to talk about doing client [...]</description>
			<content:encoded><![CDATA[<p><font color="#ff0000">[<strong>Update</strong>: The code is open-sourced at </font><a href="http://code.google.com/p/mvcvalidation/" onclick="javascript:pageTracker._trackPageview ('/outbound/code.google.com');"><font color="#ff0000">http://code.google.com/p/mvcvalidation/</font></a><font color="#ff0000">]</font></p>
<p>The asp.net mvc <a href="http://codeplex.com/aspnet" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/codeplex.com');">codeplex preview 5</a> version just came out a few days ago and it includes a lot nice enhancements.&#160; For the best introduction read Scott&#8217;s <a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/weblogs.asp.net');">post</a>.&#160; </p>
<p>Some of my favorite changes happened in validation.&#160; Incidentally, I was already working on a post to talk about doing client and server side validation the easy way.&#160; The inspiration came after reading <a href="http://blog.codeville.net/2008/04/30/model-based-client-side-validation-for-aspnet-mvc/" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/blog.codeville.net');">Steve Sanderson&#8217;s post</a> and watching his screencast on how he created a model-based client-side validation - very cool.</p>
<p>I want a simple way to perform client and server side validation for the application I am creating for <a href="http://www.emadibrahim.com/tag/mvcbook/">my book</a> and my objectives are simple (yet there is no easy way to do it):</p>
<ol>
<li>Have a model-based validation mechanism </li>
<li>Have the least amount of repetition (<a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/en.wikipedia.org');">DRY</a>) </li>
<li>Client side validation using jQuery </li>
<li>Server side validation </li>
</ol>
<p>First, I will show you how it works then I will explain how I got there.&#160; Here is my model class for a user</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> User</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> UserName { get; set; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> Email { get; set; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> Password { get; set; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ProviderId { get; set; }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   7:</span> }</pre>
</p></div>
</div>
<p>To add validation to it, I simply decorate it with the appropriate validation attributes.&#160; It ends up looking like this:</p>
</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> User
{
    [ValidateNonEmpty(<span style="color: #006080">&quot;Username is required&quot;</span>)]
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> UserName { get; set; }

    [ValidateNonEmpty(<span style="color: #006080">&quot;Email is required&quot;</span>)]
    [ValidateEmail(<span style="color: #006080">&quot;Invalid email address&quot;</span>)]
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> Email { get; set; }

    [ValidateNonEmpty(<span style="color: #006080">&quot;Password is required&quot;</span>)]
    [ValidateLength(4,50,<span style="color: #006080">&quot;Password should be at least 4 characters long&quot;</span>)]
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> <span style="color: #0000ff">string</span> Password { get; set; }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> Guid ProviderId { get; set; }
}</pre>
</div>
<p>I then generate the client script by adding <strong>1 line of code</strong> to my view</p>
</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&lt;%=Html.ClientSideValidation(<span style="color: #006080">&quot;formCreateList&quot;</span>, ViewData.Model) %&gt;</pre>
</div>
<p>On the server side, I simply make a call to a validation method from my controller</p>
</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[Authorize]
[AcceptVerbs(<span style="color: #006080">&quot;POST&quot;</span>), ActionName(<span style="color: #006080">&quot;Create&quot;</span>)]
<span style="color: #0000ff">public</span> ActionResult Save(User newUser)
{
    AppHelper.ValidateModel(ViewData.ModelState, newUser);
    <span style="color: #008000">//the rest of the action code</span>
}</pre>
</div>
<p>If there are any validation errors on the server, I display the errors by using the framework helper method ValidationSummary.&#160; All I have to do is add this 1 line to the view</p>
</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&lt;%=Html.ValidationSummary() %&gt;</pre>
</div>
<h2>JavaScript</h2>
<p>That&#8217;s all there is to it.&#160; Now let&#8217;s talk about how it works.&#160; The first challenge was generating the JavaScript to validate on the client.&#160; </p>
<p>I decided to use the excellent <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/bassistance.de');">jQuery Validation plugin</a>.&#160; The plugin gives me 2 ways to validate my form.&#160; The first method uses classes on the the fields to be validated, for example a required email field would look like this</p>
</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">id</span><span class="kwrd">=&quot;email&quot;</span> <span class="attr">name</span><span class="kwrd">=&quot;email&quot;</span> <span class="attr">size</span><span class="kwrd">=&quot;25&quot;</span>  <strong><span class="attr">class</span><span class="kwrd">=&quot;required email&quot;</span></strong> <span class="kwrd">/&gt;</span></pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<p>The second method uses rules that are defined in JavaScript and look like this</p>
</p>
<pre class="csharpcode">&lt;script type=<span class="str">&quot;text/javascript&quot;</span>&gt;
$(<span class="str">&quot;#signupForm&quot;</span>).validate({
        rules: {
            email: {
                required: <span class="kwrd">true</span>,
                email: <span class="kwrd">true</span>
            }
        },
        messages: {
            email: <span class="str">&quot;Please enter a valid email address&quot;</span>
        }
    });
&lt;/script&gt;</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<p>Check out the plugin <a href="http://docs.jquery.com/Plugins/Validation" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/docs.jquery.com');">documenation</a> and <a href="http://jquery.bassistance.de/validate/demo/" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/jquery.bassistance.de');">demos</a> to understand how it all works. </p>
<p>Since I was generating the script on the server and I wanted to make only one call, I went with the second option.</p>
<p>The script generator simply loops through all the model properties and their attributes and generate the appropriate script.&#160; <em>Afterthought: Now that I think about it, I could have easily generated a script to add classes to the elements using jQuery&#8217;s addClass method&#8230;&#160; Oh well, maybe someone else will be kind enough to do it.</em></p>
<h2>Castle Validator</h2>
<p>I used the Castle Validator Component as my validation framework.&#160; That&#8217;s where all the validation attributes you see above come from, but you can also create your own validators.&#160; I then created a class called JQueryValidationProvider that implements IBrowserValidationGenerator interface in the Castle.Components.Validator namespace.&#160; <em>I haven&#8217;t implemented the entire interface and only have a couple of validations working (I will update the source code when it is all done).</em>&#160; Here is a couple of methods that I implemented.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetEmail(<span style="color: #0000ff">string</span> target, <span style="color: #0000ff">string</span> violationMessage)
{
     Rules.Add(<span style="color: #0000ff">new</span> ValidationRule(Validate.Email, violationMessage));
}
<span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetAsRequired(<span style="color: #0000ff">string</span> target, <span style="color: #0000ff">string</span> violationMessage)
{
    Rules.Add(<span style="color: #0000ff">new</span> ValidationRule(Validate.Required, violationMessage));
}</pre>
</div>
<p>As you can see, all I am really doing is just adding the validation rule to List of ValidationRules.&#160; ValidationRule is a straight forward class and looks like this:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 364px; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">internal</span> <span style="color: #0000ff">class</span> ValidationRule
{
    <span style="color: #0000ff">public</span> ValidationRule(Validate validate) : <span style="color: #0000ff">this</span>(validate, <span style="color: #0000ff">null</span>) { }
    <span style="color: #0000ff">public</span> ValidationRule(Validate validate, <span style="color: #0000ff">string</span> failureMessage)
        : <span style="color: #0000ff">this</span>(validate, failureMessage, <span style="color: #0000ff">null</span>) { }
    <span style="color: #0000ff">public</span> ValidationRule(Validate validate, <span style="color: #0000ff">string</span> failureMessage, <span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>)
    {
        Validate = validate;
        Value = <span style="color: #0000ff">value</span>;
        <span style="color: #0000ff">if</span> (failureMessage != <span style="color: #0000ff">null</span>)
        {
            ErrorMessage = failureMessage;
        }
        <span style="color: #0000ff">else</span>
        {
            <span style="color: #008000">//setup default error messages</span>
            <span style="color: #0000ff">switch</span> (Validate)
            {
                <span style="color: #0000ff">case</span> Validate.Required:
                    ErrorMessage = <span style="color: #006080">&quot;This field is required&quot;</span>;
                    <span style="color: #0000ff">break</span>;
                <span style="color: #0000ff">default</span>:
                    ErrorMessage = <span style="color: #006080">&quot;Field error&quot;</span>;
                    <span style="color: #0000ff">break</span>;
            }
        }

    }
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">readonly</span> <span style="color: #0000ff">object</span> Value;
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">readonly</span> Validate Validate;
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">readonly</span> <span style="color: #0000ff">string</span> ErrorMessage;
}</pre>
</div>
<p>The helper ClientSideValidation method simply loops through the collection of rules and generate the appropriate JavaScript.&#160; Here it is</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> ClientSideValidation(this HtmlHelper htmlHelper,
    <span style="color: #0000ff">string</span> formName,
    <span style="color: #0000ff">object</span> modelToValidate)
{
    var results = <span style="color: #0000ff">new</span> StringBuilder();
    var rules = <span style="color: #0000ff">new</span> StringBuilder();
    var messages = <span style="color: #0000ff">new</span> StringBuilder();
    results.AppendFormat(<span style="color: #006080">&quot;$(\&quot;#{0}\&quot;).validate({1}&quot;</span>, formName, <span style="color: #006080">&quot;{&quot;</span>);
    results.AppendLine();
    rules.AppendFormat(<span style="color: #006080">&quot;rules: {0}&quot;</span>, <span style="color: #006080">&quot;{&quot;</span>);
    messages.AppendFormat(<span style="color: #006080">&quot;messages: {0}&quot;</span>, <span style="color: #006080">&quot;{&quot;</span>);

    var props = TypeDescriptor.GetProperties(modelToValidate.GetType());

    var propCounter = 0;
    <span style="color: #0000ff">foreach</span> (PropertyDescriptor prop <span style="color: #0000ff">in</span> props)
    {
        var generator = <span style="color: #0000ff">new</span> JQueryValidationProvider();
        <span style="color: #0000ff">foreach</span> (var attrib <span style="color: #0000ff">in</span> prop.Attributes.OfType&lt;AbstractValidationAttribute&gt;())
        {
            var v = attrib.Build();
            v.ErrorMessage = v.ErrorMessage ?? <span style="color: #006080">&quot;*&quot;</span>;
            v.Initialize(<span style="color: #0000ff">new</span> CachedValidationRegistry(), <span style="color: #0000ff">null</span>);
            v.ApplyBrowserValidation(<span style="color: #0000ff">null</span>, InputElementType.Undefined,
                                     generator, <span style="color: #0000ff">null</span>, <span style="color: #0000ff">null</span>);
        }

        <span style="color: #0000ff">if</span> (generator.Rules.Count &gt; 0)
        {
            <span style="color: #0000ff">if</span> (propCounter &gt; 0)
            {
                rules.Append(<span style="color: #006080">&quot;,&quot;</span>);
                messages.Append(<span style="color: #006080">&quot;,&quot;</span>);
            }
            rules.AppendLine();
            messages.AppendLine();
            rules.AppendFormat(<span style="color: #006080">&quot;{0}: {1}&quot;</span>, prop.Name.ToLower(), <span style="color: #006080">&quot;{&quot;</span>);
            messages.AppendFormat(<span style="color: #006080">&quot;{0}: {1}&quot;</span>, prop.Name.ToLower(), <span style="color: #006080">&quot;{&quot;</span>);
            rules.AppendLine();
            messages.AppendLine();

            <span style="color: #0000ff">for</span> (var i = 0; i &lt; generator.Rules.Count; i++)
            {
                var rule = generator.Rules[i];
                rules.AppendFormat(<span style="color: #006080">&quot;{0}&quot;</span>, rule.GetRuleString());
                messages.AppendFormat(<span style="color: #006080">&quot;{0}&quot;</span>, rule.GetMessageString());

                <span style="color: #0000ff">if</span> (i &lt; generator.Rules.Count - 1)
                {
                    rules.Append(<span style="color: #006080">&quot;,&quot;</span>);
                    messages.Append(<span style="color: #006080">&quot;,&quot;</span>);
                }
                <span style="color: #0000ff">else</span>
                {
                    rules.Append(<span style="color: #006080">&quot;}&quot;</span>);
                    messages.Append(<span style="color: #006080">&quot;}&quot;</span>);
                }
                rules.AppendLine();
                messages.AppendLine();
            }

            propCounter++;
        }
    }

    rules.Append(<span style="color: #006080">&quot;},&quot;</span>);
    messages.Append(<span style="color: #006080">&quot;}&quot;</span>);
    rules.AppendLine();
    messages.AppendLine();
    results.Append(rules.ToString());
    results.Append(messages.ToString());
    results.Append(<span style="color: #006080">&quot;});&quot;</span>);

    <span style="color: #0000ff">return</span> <span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;&lt;script type='text/javascript'&gt;&lt;!--{1}{0}{1}--&gt;&lt;/script&gt;&quot;</span>,
                            results.ToString(), Environment.NewLine);
}</pre>
</div>
<h2>Changes</h2>
<p>Halfway through this post, I read Steve Sanderson&#8217;s <a href="http://blog.codeville.net/2008/09/08/thoughts-on-validation-in-aspnet-mvc-applications/" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/blog.codeville.net');">new post</a> and Stephen Walther&#8217;s <a href="http://weblogs.asp.net/stephenwalther/archive/2008/09/09/asp-net-mvc-application-building-forums-4-1-2-validation-revisited.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/weblogs.asp.net');">new post</a> on validation, I made some changes to the code.&#160; I agree with their point that the controller shouldn&#8217;t be responsible for initiating the validation and it should be done at a lower layer.&#160; I refactored the code and my controller now looks like this</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[AcceptVerbs(<span style="color: #006080">&quot;POST&quot;</span>)]
<span style="color: #0000ff">public</span> ActionResult Create(User user)
{
    <span style="color: #0000ff">try</span>
    {
        user = <span style="color: #0000ff">new</span> User();
        UpdateModel(user, <span style="color: #0000ff">new</span>[] { <span style="color: #006080">&quot;name&quot;</span>, <span style="color: #006080">&quot;email&quot;</span> });
        var service = <span style="color: #0000ff">new</span> UserService();
        service.Save(user);
    }
    <span style="color: #0000ff">catch</span> (RuleViolationException ex)
    {
        MvcValidator.PopulateModelStateWithErrors(user, ViewData.ModelState, ex);
    }
    <span style="color: #0000ff">return</span> View(user);
}</pre>
</div>
<p>&#160;</p>
<p>The validation gets initiated at the service (business logic) layer and if there is a rule violation an exception is thrown.&#160; I then take the exception and pass it to a helper method that populates the model state. Note: I could probably rewrite this so that the ModelState population happens before the exception throwing which will eliminate the call to the population method.</p>
<p>My view looks like this</p>
<pre class="csharpcode"><span class="asp">&lt;%</span>=Html.ValidationSummary() <span class="asp">%&gt;</span>
<span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">=&quot;formCreate&quot;</span> <span class="attr">name</span><span class="kwrd">=&quot;formCreate&quot;</span>
        <span class="attr">action</span><span class="kwrd">='&lt;%=Url.Action(&quot;Create&quot;)%&gt;'</span> <span class="attr">method</span><span class="kwrd">=&quot;post&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">label</span> <span class="attr">for</span><span class="kwrd">=&quot;name&quot;</span><span class="kwrd">&gt;</span>Name<span class="kwrd">&lt;/</span><span class="html">label</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">type</span><span class="kwrd">=&quot;text&quot;</span> <span class="attr">id</span><span class="kwrd">=&quot;name&quot;</span> <span class="attr">name</span><span class="kwrd">=&quot;name&quot;</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">br</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">label</span> <span class="attr">for</span><span class="kwrd">=&quot;email&quot;</span><span class="kwrd">&gt;</span>Email<span class="kwrd">&lt;/</span><span class="html">label</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">type</span><span class="kwrd">=&quot;text&quot;</span> <span class="attr">id</span><span class="kwrd">=&quot;email&quot;</span> <span class="attr">name</span><span class="kwrd">=&quot;email&quot;</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">br</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">type</span><span class="kwrd">=&quot;submit&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;Submit&quot;</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span>
<span class="asp">&lt;%</span>= Html.ClientSideValidation(<span class="str">&quot;formCreate&quot;</span>, ViewData.Model) <span class="asp">%&gt;</span></pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>&#160;</p>
<p>Now if I try to submit the form, the client script validation runs and I get these errors</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/clip-image001.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="100" alt="clip_image001" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/clip-image001-thumb.png" width="382" border="0" /></a></p>
<p>If you get rid of client validation and only use server validation </p>
<p>to submit the form, the form would look like this</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/clip-image0014.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="121" alt="clip_image001[4]" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/clip-image0014-thumb.png" width="513" border="0" /></a></p>
<p>Finally, here is the client JavaScript generated in the view:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">&lt;script type=<span style="color: #006080">'text/javascript'</span>&gt;&lt;!--
    $(<span style="color: #006080">&quot;#formCreate&quot;</span>).validate({
        rules: {
            name: {
                required: <span style="color: #0000ff">true</span>,
                minlength: <span style="color: #006080">&quot;3&quot;</span>,
                maxlength: <span style="color: #006080">&quot;25&quot;</span>
            },
            email: {
                email: <span style="color: #0000ff">true</span>,
                required: <span style="color: #0000ff">true</span>
            }
        },
        messages: {
            name: {
                required: <span style="color: #006080">&quot;Username is required&quot;</span>,
                minlength: <span style="color: #006080">&quot;Username should be between 3 and 25 characters long&quot;</span>,
                maxlength: <span style="color: #006080">&quot;Username should be between 3 and 25 characters long&quot;</span>
            },
            email: {
                email: <span style="color: #006080">&quot;Invalid email address&quot;</span>,
                required: <span style="color: #006080">&quot;Email is required&quot;</span>
            }
        }
    });
--&gt;&lt;/script&gt; </pre>
</div>
<p>&#160;</p>
<h2>Pros &amp; Cons</h2>
<p>Here is what I think the pros of this design are:</p>
<ol>
<li>I love using model attributes to define validation rules </li>
<li>I like that I only need 2 lines in my view </li>
<li>I like that I don&#8217;t have to do anything in the controller other than catch an exception and call a helper method </li>
</ol>
<p>The one thing I don&#8217;t like about this design is that it feels like there is a lot of dependencies.&#160; I might need to refactor the code and move things around.&#160; Here is my dependency diagram</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image3.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="505" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb3.png" width="417" border="0" /></a> </p>
<h2>Credit</h2>
<p>This code was possible because of other people&#8217;s work:</p>
<p>Stephen Walther - <a href="http://weblogs.asp.net/stephenwalther/archive/2008/09/09/asp-net-mvc-application-building-forums-4-1-2-validation-revisited.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/weblogs.asp.net');">link</a></p>
<p>Steve Sanderson - <a href="http://blog.codeville.net/?p=86" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/blog.codeville.net');">link</a> and <a href="http://blog.codeville.net/2008/09/08/thoughts-on-validation-in-aspnet-mvc-applications/" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/blog.codeville.net');">link 2</a></p>
<p>Hamilton Verissimo - <a href="http://hammett.castleproject.org/?p=114" onclick="javascript:pageTracker._trackPageview ('/outbound/hammett.castleproject.org');">link</a></p>
<p>Rick Strahl - <a href="http://www.west-wind.com/weblog/posts/114530.aspx" onclick="javascript:pageTracker._trackPageview ('/outbound/www.west-wind.com');">link</a></p>
<p>Scott Guthrie - <a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx" onclick="javascript:pageTracker._trackPageview ('/outbound/weblogs.asp.net');">link</a></p>
<h2>Source Code</h2>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/mvcvalidationexample.zip">Click here</a> to download the rough-quality source code.</p>
<p>Let me hear what you think of this design.&#160; What&#8217;s wrong with it?&#160; What&#8217;s right?&#160; <strong>Should I put this up on codeplex?</strong></p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/05/resolving-url-in-aspnet-using-tilda/" rel="bookmark" title="Permanent Link: Resolving URL in ASP.net using Tilda (~)" >Resolving URL in ASP.net using Tilda (~)</a></span><div class="aizattos_related_posts_excerpt">I have been using .net since it has come out and I just found this out (so, I am kind of pissed).  W...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/11/aspnet-20-error-title-is-not-a-member-of-asp%e2%80%a6/" rel="bookmark" title="Permanent Link: asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…" >asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…</a></span><div class="aizattos_related_posts_excerpt">I had this error come up on a few pages after I deployed my website and it was driving me crazy.  Af...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/22/is-microsoft-buying-the-community/" rel="bookmark" title="Permanent Link: Is Microsoft Buying the Community?" >Is Microsoft Buying the Community?</a></span><div class="aizattos_related_posts_excerpt">I love the blogosphere - specially for technical information and news.&nbsp; My guess is that probab...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/" rel="bookmark" title="Permanent Link: Ajax with ASP.NET MVC Framework Preview 2" >Ajax with ASP.NET MVC Framework Preview 2</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/" rel="bookmark" title="Permanent Link: The Best IoC Container?" >The Best IoC Container?</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/387919299" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/</feedburner:origLink></item>
		<item>
		<title>Deciding Between ASP.NET MVC and WebForms</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/386041779/</link>
		<comments>http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 20:21:26 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[MVC Book]]></category>

		<category><![CDATA[asp.net mvc]]></category>

		<category><![CDATA[aspnetmvc]]></category>

		<category><![CDATA[mvc]]></category>

		<category><![CDATA[mvcbook]]></category>

		<category><![CDATA[webdev]]></category>

		<category><![CDATA[webforms]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/</guid>
		<description>I am trying to create a decision flowchart for my ASP.NET MVC book and wanted to get some community feedback.&amp;#160; Does this diagram make sense?&amp;#160; What other factors should one consider that should make it into the chart?
 
Things to Note
I have tried to use the general consensus of the community for each decision.&amp;#160; For [...]</description>
			<content:encoded><![CDATA[<p>I am trying to create a decision flowchart for my <a href="http://www.emadibrahim.com/category/mvc-book/" target="_blank">ASP.NET MVC book</a> and wanted to get some community feedback.&#160; Does this diagram make sense?&#160; What other factors should one consider that should make it into the chart?</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image2.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="894" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb2.png" width="519" border="0" /></a> </p>
<h2>Things to Note</h2>
<p>I have tried to use the general consensus of the community for each decision.&#160; For example, some might argue that MVC is better for prototyping but I would say the majority would disagree.</p>
<p>Keep in mind that these choices are not set in stone and are not mutually exclusive but are more like a guide to which platform would be better suited to satisfy a particular requirement.&#160; For example you might want to use TDD but still end up choosing on WebForms.&#160; (I should probably assign a weight to each question)</p>
<p>The &quot;No&quot; branch doesn&#8217;t mean exactly NO.&#160; I mean, answering No to &quot;Want cleaner HTML?&quot; doesn&#8217;t mean you have something against clean HTML or you want crappy HTML - no one wants that.&#160; Think of the question in terms of &quot;is clean html important to you?&quot;&#160; </p>
<p>Let me hear <strong>your feedback</strong> below and I will incorporate it in version of 2 of the diagram and post it back on the blog.&#160; Hopefully, this will help someone choose between ASP.NET MVC and WebForms on a new project.</p>
<p>Also, I don&#8217;t think a flowchart diagram is the best type of diagram for this type of decision tree.&#160; Can you suggest a <strong>better format</strong>?</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/" rel="bookmark" title="Permanent Link: Ajax with ASP.NET MVC Framework Preview 2" >Ajax with ASP.NET MVC Framework Preview 2</a></span><div class="aizattos_related_posts_excerpt">This is the updated version of the Nikhil's excellent example on how to use Ajax with ASP.net MVC.
...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/05/resolving-url-in-aspnet-using-tilda/" rel="bookmark" title="Permanent Link: Resolving URL in ASP.net using Tilda (~)" >Resolving URL in ASP.net using Tilda (~)</a></span><div class="aizattos_related_posts_excerpt">I have been using .net since it has come out and I just found this out (so, I am kind of pissed).  W...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/11/aspnet-20-error-title-is-not-a-member-of-asp%e2%80%a6/" rel="bookmark" title="Permanent Link: asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…" >asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…</a></span><div class="aizattos_related_posts_excerpt">I had this error come up on a few pages after I deployed my website and it was driving me crazy.  Af...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/30/upgrading-yonkly-to-aspnet-mvc-preview-3/" rel="bookmark" title="Permanent Link: Upgrading Yonkly to ASP.NET MVC Preview 3" >Upgrading Yonkly to ASP.NET MVC Preview 3</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/22/is-microsoft-buying-the-community/" rel="bookmark" title="Permanent Link: Is Microsoft Buying the Community?" >Is Microsoft Buying the Community?</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/386041779" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/</feedburner:origLink></item>
		<item>
		<title>Use ModelBinder to Generically Bind Complex Types</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/382528437/</link>
		<comments>http://www.emadibrahim.com/2008/09/03/use-modelbinder-to-generically-bind-complex-types/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 17:53:28 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[asp.net mvc]]></category>

		<category><![CDATA[mvc]]></category>

		<category><![CDATA[mvcbook]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/09/03/use-modelbinder-to-generically-bind-complex-types/</guid>
		<description>[Update: Simone brought my attention to the fact that ComplexModelBinder which comes with the framework does exactly that.&amp;#160; You can find more info here]
ASP.NET MVC Preview 5 introduce the ModelBinder attribute that can be used to decorate a complex type in an Action.&amp;#160; This allows us to have actions that look like this

public ActionResult Create([ModelBinder(typeof(GenericBinder))] [...]</description>
			<content:encoded><![CDATA[<p>[<strong>Update</strong>: Simone brought my attention to the fact that ComplexModelBinder which comes with the framework does exactly that.&nbsp; You can find more info <a href="http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/weblogs.asp.net');">here</a>]</p>
<p>ASP.NET MVC Preview 5 introduce the ModelBinder attribute that can be used to decorate a complex type in an Action.&nbsp; This allows us to have actions that look like this</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> ActionResult Create([ModelBinder(<span style="color: #0000ff">typeof</span>(GenericBinder))] ContactList myList)</pre>
</div>
<p>&nbsp;</p>
<p>Instead of this:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> ActionResult Create(<span style="color: #0000ff">string</span> name, <span style="color: #0000ff">string</span> description)</pre>
</div>
<p>&nbsp;</p>
<p>The problem is that you have to build a Binder for every complex type you want to use as a parameter.&nbsp; For example, Maarten Balliauw created a model binder on his <a href="http://blog.maartenballiauw.be/post/2008/09/01/Using-the-ASPNET-MVC-ModelBinder-attribute.aspx" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/blog.maartenballiauw.be');">blog</a> and it looks like this:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ContactBinder : IModelBinder
{
    <span style="color: #cc6633">#region</span> IModelBinder Members
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">object</span> GetValue(ControllerContext controllerContext,
                 <span style="color: #0000ff">string</span> modelName, Type modelType,
                 ModelStateDictionary modelState)
    {
        <span style="color: #0000ff">if</span> (modelType == <span style="color: #0000ff">typeof</span>(Contact))
        {
            <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> Contact
            {
                Name = controllerContext.HttpContext.Request.Form[<span style="color: #006080">"name"</span>] ?? <span style="color: #006080">""</span>,
                Email = controllerContext.HttpContext.Request.Form[<span style="color: #006080">"email"</span>] ?? <span style="color: #006080">""</span>,
                Message = controllerContext.HttpContext.Request.Form[<span style="color: #006080">"message"</span>] ?? <span style="color: #006080">""</span>
            };
        }
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">null</span>;
    }
    <span style="color: #cc6633">#endregion</span>
}</pre>
</div>
<p>&nbsp;</p>
<p>Now that is a lot of typing and because I am lazy, I decided to create a generic binder that uses reflection and can work with all my complex types.&nbsp; </p>
<p><strong>Note</strong>: By generic I mean common - it has nothing to do with .net Generics</p>
<p>Also note that this will <strong>only</strong> work if you follow these conventions:</p>
<ol>
<li>The html field name must match the property name
<li>User lower case names for the html fields
<li>You don&#8217;t have to user lower case on your model properties</li>
</ol>
<p>Here is the very rough and untested Generic Binder:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">class</span> GenericBinder : IModelBinder
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">object</span> GetValue(ControllerContext controllerContext,
                            <span style="color: #0000ff">string</span> modelName, Type modelType,
                            ModelStateDictionary modelState)
    {
        var instance = Activator.CreateInstance(modelType);
        <span style="color: #0000ff">foreach</span> (var prop <span style="color: #0000ff">in</span> modelType.GetProperties())
        {
            prop.SetValue(instance,
                    controllerContext.HttpContext.Request
                                    .Form[prop.Name.ToLower()],
                    <span style="color: #0000ff">null</span>);
        }
        <span style="color: #0000ff">return</span> instance;
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>If you find any bugs or have a better implementation, please share.</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/" rel="bookmark" title="Permanent Link: Mocking and Dependency Injection in ASP.NET MVC" >Mocking and Dependency Injection in ASP.NET MVC</a></span><div class="aizattos_related_posts_excerpt">Here is the situation, my controller constructors take multiple interfaces as parameters.&nbsp; I do...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/" rel="bookmark" title="Permanent Link: Ninject: Killer IoC" >Ninject: Killer IoC</a></span><div class="aizattos_related_posts_excerpt">In my previous post, The Best IoC Container, I decided to go with StructureMap as the framework of c...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/04/26/getting-flash-video-flv-to-work-in-iis/" rel="bookmark" title="Permanent Link: Getting Flash Video (FLV) to work in IIS" >Getting Flash Video (FLV) to work in IIS</a></span><div class="aizattos_related_posts_excerpt">I added a macromedia flash animation (swf) to my asp.net website and it would work fine but when it ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/20/ultimate-file-synchronization/" rel="bookmark" title="Permanent Link: Ultimate file synchronization between multiple computers" >Ultimate file synchronization between multiple computers</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/12/dynamically-build-linq-queries-using-reflection/" rel="bookmark" title="Permanent Link: Dynamically Build LINQ Queries Using Reflection" >Dynamically Build LINQ Queries Using Reflection</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/382528437" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/09/03/use-modelbinder-to-generically-bind-complex-types/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/09/03/use-modelbinder-to-generically-bind-complex-types/</feedburner:origLink></item>
		<item>
		<title>Google Chrome JavaScript Speed Test</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/381637736/</link>
		<comments>http://www.emadibrahim.com/2008/09/02/google-chrome-javascript-speed-test/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 19:37:15 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[Random]]></category>

		<category><![CDATA[browsers]]></category>

		<category><![CDATA[chrome]]></category>

		<category><![CDATA[firefox]]></category>

		<category><![CDATA[ie]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/09/02/google-chrome-javascript-speed-test/</guid>
		<description>Google&amp;#8217;s new browser - Chrome - has a JavaScript engine called V8 that was built from scratch.&amp;#160; So, how fast is it?
I ran these tests http://mootools.net/slickspeed/ on IE, Firefox and Chrome.
Here are the results: 

It looks like Chrome is about 13% faster than Firefox and 82% faster than IE 7.
All browsers are running on the [...]</description>
			<content:encoded><![CDATA[<p>Google&#8217;s new browser - Chrome - has a JavaScript engine called V8 that was built from scratch.&nbsp; So, how fast is it?</p>
<p>I ran these tests <a href="http://mootools.net/slickspeed/" onclick="javascript:pageTracker._trackPageview ('/outbound/mootools.net');">http://mootools.net/slickspeed/</a> on IE, Firefox and Chrome.</p>
<p>Here are the results: </p>
<p><iframe style="width: 513px; height: 493px" src="http://spreadsheets.google.com/pub?key=pWDFUiSiQCCMIP-aay__NYQ&amp;hl=en"></iframe></p>
<p>It looks like Chrome is about 13% faster than Firefox and 82% faster than IE 7.</p>
<p>All browsers are running on the same computer, so the hardware/os settings are irrelevant.&nbsp; But if you are curious, it&#8217;s a Windows Vista 64-bit machine with 6 Gigs of RAM and a quad core chip.</p>
<p><strong>[Update] If you are reading this in Chrome, the embedded document is broken.&nbsp; I guess Google didn&#8217;t test there own sites.&nbsp; It should look like this:</strong></p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="429" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb.png" width="461" border="0"></a> </p>
<p>Instead it looks like this:</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/09/image1.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="444" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/09/image-thumb1.png" width="460" border="0"></a></p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/21/google-reader-hacks/" rel="bookmark" title="Permanent Link: Google Reader Hacks" >Google Reader Hacks</a></span><div class="aizattos_related_posts_excerpt"> Here is a pretty comprehensive list of Google Reader hacks...
For those who want more out of Google...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/02/10/help-debugging-ajax-and-javascript/" rel="bookmark" title="Permanent Link: Help debugging AJAX and JavaScript" >Help debugging AJAX and JavaScript</a></span><div class="aizattos_related_posts_excerpt">As you all know, there is really no easy way to debug AJAX and JavaScript applications, but I found ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/25/sitemeter-and-wordpress/" rel="bookmark" title="Permanent Link: Sitemeter and wordpress" >Sitemeter and wordpress</a></span><div class="aizattos_related_posts_excerpt">I just added sitemeter to BlogsToGo (WordPres Mu).  I have a plugins file in my mu-plugins directory...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/02/10/validating-the-page-using-javascript/" rel="bookmark" title="Permanent Link: Validating the page using JavaScript" >Validating the page using JavaScript</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/09/11/microsoft-money-plus-2008-review/" rel="bookmark" title="Permanent Link: Microsoft Money Plus (2008) Review (Part 1)" >Microsoft Money Plus (2008) Review (Part 1)</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/381637736" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/09/02/google-chrome-javascript-speed-test/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/09/02/google-chrome-javascript-speed-test/</feedburner:origLink></item>
		<item>
		<title>Is this Better than Constructor Injection?</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/378527774/</link>
		<comments>http://www.emadibrahim.com/2008/08/29/is-this-better-than-constructor-injection/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 22:02:21 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[MVC Book]]></category>

		<category><![CDATA[asp.net mvc]]></category>

		<category><![CDATA[di]]></category>

		<category><![CDATA[ioc]]></category>

		<category><![CDATA[mvc]]></category>

		<category><![CDATA[mvcbook]]></category>

		<category><![CDATA[ninject]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/08/28/is-this-better-than-constructor-injection/</guid>
		<description>So after I finished my post on property injection I thought of something, maybe I shouldn&amp;#8217;t use constructor injection for my classes.&amp;#160; Maybe, I can just use lazy properties that would call my IoC (Ninject) Kernel to instantiate the object.&amp;#160; So instead of defining a constructor like this:

public IAccountService Service { get; set; }

[Inject]
public AccountController(IAccountService [...]</description>
			<content:encoded><![CDATA[<p>So after I finished <a href="http://www.emadibrahim.com/2008/09/02/property-injection-in-aspnet-mvc-with-ninject/" target="_blank">my post on property injection</a> I thought of something, maybe I shouldn&#8217;t use constructor injection for my classes.&nbsp; Maybe, I can just use lazy properties that would call my IoC (Ninject) Kernel to instantiate the object.&nbsp; So instead of defining a constructor like this:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas,'Courier New',courier,monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> IAccountService Service { get; set; }

[Inject]
<span style="color: #0000ff">public</span> AccountController(IAccountService service)
{
    Service = service;
}</pre>
</div>
<p>I could just have a property Service do the work like this:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas,'Courier New',courier,monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">public</span> IAccountService Service
{
    get
    {
        <span style="color: #0000ff">return</span> (IAccountService) Kernel.Get(<span style="color: #0000ff">typeof</span> (IAccountService));
    }
}</pre>
</div>
<p>If I get rid of all my constructor parameters then I won&#8217;t need to change my ControllerFactory to use the Ninject Factory.&nbsp; This keeps looking better and better.</p>
<p>What do you think?&nbsp; I keep reading everywhere that construction injection is the way to go.&nbsp; But why complicate my constructor when I can have the properties do the heavy lifting?&nbsp; I would love to hear what you think.</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/" rel="bookmark" title="Permanent Link: Ajax with ASP.NET MVC Framework Preview 2" >Ajax with ASP.NET MVC Framework Preview 2</a></span><div class="aizattos_related_posts_excerpt">This is the updated version of the Nikhil's excellent example on how to use Ajax with ASP.net MVC.
...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/29/property-injection-in-aspnet-mvc-with-ninject/" rel="bookmark" title="Permanent Link: Property Injection in ASP.NET MVC with Ninject" >Property Injection in ASP.NET MVC with Ninject</a></span><div class="aizattos_related_posts_excerpt">I got a design challenge with asp.net mvc.  I want to keep track of the currently logged in user in...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/" rel="bookmark" title="Permanent Link: The Best IoC Container?" >The Best IoC Container?</a></span><div class="aizattos_related_posts_excerpt">As I previously mentioned in my post "The Best JavaScript Library", I am in the process of developin...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/" rel="bookmark" title="Permanent Link: Mocking and Dependency Injection in ASP.NET MVC" >Mocking and Dependency Injection in ASP.NET MVC</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/378527774" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/08/29/is-this-better-than-constructor-injection/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/08/29/is-this-better-than-constructor-injection/</feedburner:origLink></item>
		<item>
		<title>Property Injection in ASP.NET MVC with Ninject</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/378527775/</link>
		<comments>http://www.emadibrahim.com/2008/08/29/property-injection-in-aspnet-mvc-with-ninject/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 12:01:41 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[MVC Book]]></category>

		<category><![CDATA[asp.net mvc]]></category>

		<category><![CDATA[di]]></category>

		<category><![CDATA[ioc]]></category>

		<category><![CDATA[mvc]]></category>

		<category><![CDATA[mvcbook]]></category>

		<category><![CDATA[ninject]]></category>

		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/09/02/property-injection-in-aspnet-mvc-with-ninject/</guid>
		<description>I got a design challenge with asp.net mvc.  I want to keep track of the currently logged in user in the session because I don&amp;#8217;t want to hit the database every time I need to get the username or id for a query.  And I have all my controllers inherit from a base controller named [...]</description>
			<content:encoded><![CDATA[<p>I got a design challenge with asp.net mvc.  I want to keep track of the currently logged in user in the session because I don&#8217;t want to hit the database every time I need to get the username or id for a query.  And I have all my controllers inherit from a base controller named BaseController.  So, I added a CurrentUser property to the BaseController and I want it to automagically work without the derived controllers having to do anything.  Here is a class diagram to help clarify:</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/08/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://www.emadibrahim.com/wp-content/uploads/2008/08/image-thumb2.png" border="0" alt="image" width="487" height="314" /></a></p>
<p>The CurrentUser Property needs to look something like this:</p>
<div>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #0000ff;">public</span> User CurrentUser
{
    get
    {
        var key = <span style="color: #006080;">"currentuser"</span>;
        <span style="color: #0000ff;">if</span> (Session[key] == <span style="color: #0000ff;">null</span>)
        {
            Session[key] = <span style="color: #008000;">/*get user from database some how*/</span>;
        }
        <span style="color: #0000ff;">return</span> (User) Session[key] ;
    }
}</pre>
</div>
<p>This looks simple enough but it is not.  The reason it is not simple is because to get the current user I have to call Membership.Provider.GetUser from the BaseController.  The problem with that is that it creates a dependency on the MembershipProvider class which I don&#8217;t want to have, because it will make testing very hard.</p>
<p>One obvious solution was to add the MembershipProvider (which is an abstract class) to the BaseController&#8217;s construct and then pass a mocked instance during testing&#8230;  The problem with this design is that now my BaseController will be forced to have a parameterized constructor which means that I have to change the code in all the derived controllers to handle that and pass the appropriate instance of MembershipProvider.  Sounds like a code smell.</p>
<p>My solution was to create the MembershipProvider class using my IoC container - in this case, my Ninject Kernel.  This allows me to inject a SqlMembershipProvider in development and runtime and inject a mocked provider in testing.  So the final CurrentUser property looks like this:</p>
<div>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;"><span style="color: #0000ff;">public</span> User CurrentUser
{
    get
    {
        var key = <span style="color: #006080;">"currentuser"</span>;
        <span style="color: #0000ff;">if</span> (Session[key] == <span style="color: #0000ff;">null</span>)
        {
            var Provider
                = (MembershipProvider)
                    Kernel.Get(<span style="color: #0000ff;">typeof</span>(MembershipProvider));

            Session[key] = AppHelper.CreateUserFromMembershipUser
                            (Provider.GetUser(User.Identity.Name, <span style="color: #0000ff;">true</span>));
        }
        <span style="color: #0000ff;">return</span> (User) Session[key] ;
    }
}</pre>
</div>
<p>If you have been paying attention, you are probably wondering  what is this &#8220;Kernel&#8221; thing.  Well Kernel is an instance of the Ninject Kernel which itself was injected into the BaseController class like this:</p>
<div>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">[Inject]
<span style="color: #0000ff;">public</span> IKernel Kernel { get; set; }</pre>
</div>
<p>I could have done this differently.  For example, I could have injected the provider itself using property injection like this:</p>
<div>
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;">[Inject]
<span style="color: #0000ff;">public</span> MembershipProvider Provider { get; set; }</pre>
</div>
<p>The only problem is that the provider isn&#8217;t needed by all the derived classed and I didn&#8217;t want to have a public property in the base class that would hardly be used anywhere else.   On the other hand Kernel could be globally used to instantiate an object.</p>
<p>What do you think?  Is this the way to do it?  Is there a better way?</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/29/is-this-better-than-constructor-injection/" rel="bookmark" title="Permanent Link: Is this Better than Constructor Injection?" >Is this Better than Constructor Injection?</a></span><div class="aizattos_related_posts_excerpt">So after I finished my post on property injection I thought of something, maybe I shouldn't use cons...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/30/upgrading-yonkly-to-aspnet-mvc-preview-3/" rel="bookmark" title="Permanent Link: Upgrading Yonkly to ASP.NET MVC Preview 3" >Upgrading Yonkly to ASP.NET MVC Preview 3</a></span><div class="aizattos_related_posts_excerpt">I just spent this morning upgrading the Yonkly code to work with the newly released ASP.NET MVC Fram...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/" rel="bookmark" title="Permanent Link: The Best IoC Container?" >The Best IoC Container?</a></span><div class="aizattos_related_posts_excerpt">As I previously mentioned in my post "The Best JavaScript Library", I am in the process of developin...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/" rel="bookmark" title="Permanent Link: Ajax with ASP.NET MVC Framework Preview 2" >Ajax with ASP.NET MVC Framework Preview 2</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/" rel="bookmark" title="Permanent Link: Deciding Between ASP.NET MVC and WebForms" >Deciding Between ASP.NET MVC and WebForms</a></span></li></ul></div><img src="http://feeds.feedburner.com/~r/EmadIbrahim/~4/378527775" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/08/29/property-injection-in-aspnet-mvc-with-ninject/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.emadibrahim.com/2008/08/29/property-injection-in-aspnet-mvc-with-ninject/</feedburner:origLink></item>
		<item>
		<title>Mocking and Dependency Injection in ASP.NET MVC</title>
		<link>http://feeds.feedburner.com/~r/EmadIbrahim/~3/376153277/</link>
		<comments>http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 12:41:16 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
		
		<category><![CDATA[MVC Book]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Testing]]></category>

		<category><![CDATA[asp.net mvc]]></category>

		<category><![CDATA[di]]></category>

		<category><![CDATA[ioc]]></category>

		<category><![CDATA[mbunit]]></category>

		<category><![CDATA[moq]]></category>

		<category><![CDATA[ninject]]></category>

		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/</guid>
		<description>Here is the situation, my controller constructors take multiple interfaces as parameters.&amp;#160; I do this in order to use constructor injection which allows me to inject the controllers with mocked objects in my unit tests.
For example, my AccountController takes IEmailService, IFormsAuthentication and MembershipProvider (abstract class) as parameters.
During my testing, I want to mock the email, [...]</description>
			<content:encoded><![CDATA[<p>Here is the situation, my controller constructors take multiple interfaces as parameters.&nbsp; I do this in order to use constructor injection which allows me to inject the controllers with mocked objects in my unit tests.</p>
<p>For example, my AccountController takes IEmailService, IFormsAuthentication and MembershipProvider (abstract class) as parameters.</p>
<p>During my testing, I want to mock the email, authentication and membership calls.&nbsp; For example when the user calls FormsAuthentication.Login, I don&#8217;t really care if actual call succeeded but rather that my login action works appropriately in the case FormstAuthentication.Login succeeds (or fails).&nbsp; I just want to mock that call.</p>
<p>I started off creating a few tests and slowly they have grown to several.&nbsp; There was a lot of repeated code in my unit tests and to be a good citizen of the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" onclick="javascript:pageTracker._trackPageview ('/outbound/en.wikipedia.org');">DRY</a> universe, I needed to refactor the code.</p>
<p>For IoC, I initially started with <a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/">StructureMap</a> but now I am using <a href="http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/">Ninject</a>.&nbsp; </p>
<p>I created this module to bind my interfaces to mocked instances.&nbsp; It looks like this:</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">internal</span> <span style="color: #0000ff">class</span> TestModule : StandardModule
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Load()
    {
        Bind&lt;IEmailService&gt;()
            .ToConstant(MyMocks.MockEmailService.Object);

        Bind&lt;IFormsAuthentication&gt;()
            .ToConstant(MyMocks.MockFormsAuthentication.Object);

        Bind&lt;MembershipProvider&gt;()
            .ToConstant(MyMocks.MockMembershipProvider.Object);

        Bind&lt;IContactListService&gt;()
            .ToConstant(MyMocks.MockContactListService.Object);
    }
}</pre>
</div>
<p>Notice that I bind the interfaces to actual instances and not classes.&nbsp; These instances are declared in a global static class that will be accessed from my unit tests.&nbsp; As you can tell from the name, they are all mocked objects (I am using <a href="http://code.google.com/p/moq/" onclick="javascript:pageTracker._trackPageview ('/outbound/code.google.com');">Moq</a>).&nbsp; Here is how the MockEmailService looks (all the others are declared the same way):</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">internal</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> MyMocks
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> Mock&lt;IEmailService&gt; _mockEmailService;
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> Mock&lt;IEmailService&gt; MockEmailService
    {
        get
        {
            _mockEmailService = _mockEmailService ?? <span style="color: #0000ff">new</span> Mock&lt;IEmailService&gt;();
            <span style="color: #0000ff">return</span> _mockEmailService;
        }
    }</pre>
</div>
<p>&nbsp;</p>
<p>So all this is good to setup Ninject and create my mocks.&nbsp; Now I want to easily and generically create a controller, so I can quickly create unit tests.&nbsp; In order to do that, I created a TestControllerFactory class that basically creates a controller with all the appropriate dependencies injected.</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">internal</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> TestControllerFactory</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   3:</span>     <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> IKernel _kernel;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IKernel Kernel</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   5:</span>     {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>         get</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   7:</span>         {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>             <span style="color: #0000ff">if</span> (_kernel == <span style="color: #0000ff">null</span>)</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   9:</span>             {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>                 var modules = <span style="color: #0000ff">new</span> IModule[] { <span style="color: #0000ff">new</span> TestModule() };</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  11:</span>                 _kernel = <span style="color: #0000ff">new</span> StandardKernel(modules);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  12:</span>             }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  13:</span>             <span style="color: #0000ff">return</span> _kernel;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  14:</span>         }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  15:</span>         <span style="color: #0000ff">private</span> set</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  16:</span>         {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  17:</span>             _kernel = <span style="color: #0000ff">value</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  18:</span>         }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  19:</span>     }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  20:</span>&nbsp; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> T GetControllerWithFakeContext&lt;T&gt;(<span style="color: #0000ff">string</span> httpMethod) </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  22:</span>         <span style="color: #0000ff">where</span> T : Controller</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  23:</span>     {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  24:</span>         var con = Kernel.Get&lt;T&gt;();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  25:</span>         con.SetFakeControllerContext();</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  26:</span>         <span style="color: #0000ff">if</span> (con != <span style="color: #0000ff">null</span>) con.Request.SetHttpMethodResult(httpMethod);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  27:</span>         <span style="color: #0000ff">return</span> con;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  28:</span>     }</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  29:</span>&nbsp; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  30:</span> }</pre>
</div>
</div>
<p>In line #10, I use the TestModule class mentioned above to setup the <a href="http://ninject.org/" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/ninject.org');">Ninject</a> Kernel.&nbsp; In lines #21 to #28, I create an instance of T which must be of type Controller from the Kernel which will automatically create the Controller with all the mocked objects.&nbsp; In line #25 and #26, I just set a fake/mocked context and the Http Method for the request (<a href="http://www.emadibrahim.com/2008/04/04/unit-test-linq-to-sql-in-aspnet-mvc-with-moq/">more info here</a>).</p>
<p>Now my unit tests are very clean and easy to setup.&nbsp;&nbsp;&nbsp; Using <a href="http://www.mbunit.com/" target="_blank" onclick="javascript:pageTracker._trackPageview ('/outbound/www.mbunit.com');">MbUnit</a> as my unit test framework, here is a unit tests that tests the reset password functionality.</p>
<div>
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> ResetPasswordQuestion_Should_Send_Email_On_Success()</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   3:</span> {</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>     var newpassword = <span style="color: #006080">"newpassword"</span>;</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   5:</span>     MyMocks.MockMembershipProvider</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   6:</span>          .Expect(p =&gt; p.ResetPassword(username, answer))</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   7:</span>          .Returns(newpassword);</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   8:</span>     MyMocks.MockEmailService</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   9:</span>          .Expect(m =&gt; m.SendPasswordReset(username, newpassword));</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  10:</span>&nbsp; </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">  11:</span>     var ac = TestControllerFactory</pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">