<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Emad Ibrahim &#187; Programming</title>
	<atom:link href="http://www.emadibrahim.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emadibrahim.com</link>
	<description>web development, tech reviews, tips, tricks, blogging</description>
	<lastBuildDate>Sat, 24 Dec 2011 18:22:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>30 Days of Rails</title>
		<link>http://www.emadibrahim.com/2011/12/24/30-days-of-rails/</link>
		<comments>http://www.emadibrahim.com/2011/12/24/30-days-of-rails/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 18:22:00 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[30 Days]]></category>
		<category><![CDATA[Interesting]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[Web Dev]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2011/12/24/30-days-of-rails/</guid>
		<description><![CDATA[If you haven’t noticed, I am a .net developer but I was inspired today by Matt Cutt’s TED talk to start a 30-day project.&#160; I highly recommend watching it; it might inspire you too. &#160; So I have decided to spend 30 days with Ruby on Rails and blog it right here. Stay tuned for [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven’t noticed, I am a .net developer but I was inspired today by <a href="http://www.ted.com/talks/matt_cutts_try_something_new_for_30_days.html" target="_blank">Matt Cutt’s TED talk</a> to start a 30-day project.&#160; I highly recommend watching it; it might inspire you too.</p>
<p><object width="526" height="374"><param name="movie" value="http://video.ted.com/assets/player/swf/EmbedPlayer.swf"></param><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="wmode" value="transparent"></param><param name="bgColor" value="#ffffff"></param><param name="flashvars" value="vu=http://video.ted.com/talk/stream/2011U/Blank/MattCutts_2011U-320k.mp4&amp;su=http://images.ted.com/images/ted/tedindex/embed-posters/MattCutts-2011U.embed_thumbnail.jpg&amp;vw=512&amp;vh=288&amp;ap=0&amp;ti=1183&lang;=&amp;introDuration=15330&amp;adDuration=4000&amp;postAdDuration=830&amp;adKeys=talk=matt_cutts_try_something_new_for_30_days;year=2011;theme=how_we_learn;event=TED2011;tag=Culture;tag=success;&amp;preAdTag=tconf.ted/embed;tile=1;sz=512x288;" /><embed src="http://video.ted.com/assets/player/swf/EmbedPlayer.swf" pluginspace="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent" bgColor="#ffffff" width="526" height="374" allowFullScreen="true" allowScriptAccess="always" flashvars="vu=http://video.ted.com/talk/stream/2011U/Blank/MattCutts_2011U-320k.mp4&#038;su=http://images.ted.com/images/ted/tedindex/embed-posters/MattCutts-2011U.embed_thumbnail.jpg&#038;vw=512&#038;vh=288&#038;ap=0&#038;ti=1183&#038;lang=&#038;introDuration=15330&#038;adDuration=4000&#038;postAdDuration=830&#038;adKeys=talk=matt_cutts_try_something_new_for_30_days;year=2011;theme=how_we_learn;event=TED2011;tag=Culture;tag=success;&#038;preAdTag=tconf.ted/embed;tile=1;sz=512x288;"></embed></object></p>
<p>&#160;</p>
<p>So I have decided to spend 30 days with Ruby on Rails and blog it right here. Stay tuned for a daily post on rails, it will be fun.&#160; I am starting my journey with TekPub’s Rails 3 production at <a href="http://tekpub.com/view/rails3/1">http://tekpub.com/view/rails3/1</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2011/12/24/30-days-of-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Nodejs on Windows</title>
		<link>http://www.emadibrahim.com/2011/11/07/nodejs-on-windows/</link>
		<comments>http://www.emadibrahim.com/2011/11/07/nodejs-on-windows/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 03:09:02 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[npm]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/?p=804</guid>
		<description><![CDATA[This turned out to be easier than expected. Step 1: Download node.exe Get it from http://nodejs.org/#download and put it somewhere in your path e.g. c:\node Step 2: Install git Get it from here and install into your windows console if you want to use the windows command prompt instead of bash Step 3: Install NPM [...]]]></description>
			<content:encoded><![CDATA[<p>This turned out to be easier than expected.</p>
<h1>Step 1: Download node.exe</h1>
<p>Get it from <a href="http://nodejs.org/#download">http://nodejs.org/#download</a> and put it somewhere in your path e.g. c:\node</p>
<h1>Step 2: Install git</h1>
<p>Get it from <a href="http://code.google.com/p/msysgit/downloads/list?can=3&amp;q=official+Git">here</a> and install into your windows console if you want to use the windows command prompt instead of bash</p>
<h1>Step 3: Install NPM (node package manager)</h1>
<p>This one is a little more involved.&#160; Run the following commands (ref: <a href="http://npmjs.org/doc/README.html#Installing-on-Windows-Experimental">http://npmjs.org/doc/README.html#Installing-on-Windows-Experimental</a>)</p>
<blockquote><p>git config &#8211;system http.sslcainfo /bin/curl-ca-bundle.crt      <br />git clone &#8211;recursive git://github.com/isaacs/npm.git       <br />cd npm       <br />node cli.js install npm –gf</p>
</blockquote>
<h1>Step 4: Install express</h1>
<p>Express is a really cool <a href="http://expressjs.com">web framework</a> for nodejs and should simplify your web development considerably.&#160; This is of course optional, but here are the steps anyway.</p>
<p>create a folder for your app e.g c:\projects\myapp</p>
<blockquote><p>cd c:\projects\myapp</p>
<p>npm install –g express</p>
<p>node app.js</p>
</blockquote>
<p>Now, if you visit http:/127.0.0.1:3000 you will see a 500 error page because you are missing a dependency.&#160; npm install –d should fix it but that threw all kinds of errors in windows “couldn’t unpack blah blah”, “unknown error blah blah”, “out of memory blah blah”…&#160; Fortunately, running this command downloaded the missing dependency (jade) into the temp folder, on my machine it was at C:\Users\Emad\AppData\Local\Temp\npm-1320719310093\1320719310093-0.5456247869879007\contents\___package.npm\package (if it is not there, you can get it from <a href="https://github.com/visionmedia/jade">https://github.com/visionmedia/jade</a>)</p>
<p>I copied the content of that folder into C:\node\node_modules\jade and that took care of the problem.&#160; Now visiting <a href="http://127.0.0.1:3000/">http://127.0.0.1:3000/</a>, I see</p>
<p><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" border="0" src="http://files.dotnetfactory.com/2011-11-07_2159.png" /></p>
<h1>Step 5: Install linux in a Virtual machine</h1>
<p>Yes, you read that right.&#160; It is just much simpler to work with node stuff on linux.&#160; Everything just works.&#160; It seems like on windows, you are constantly swimming upstream.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2011/11/07/nodejs-on-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AppHarbor + ASP.NET MVC + Orchard = Awesome</title>
		<link>http://www.emadibrahim.com/2011/05/03/appharbor-asp-net-mvc-orchard-awesome/</link>
		<comments>http://www.emadibrahim.com/2011/05/03/appharbor-asp-net-mvc-orchard-awesome/#comments</comments>
		<pubDate>Tue, 03 May 2011 23:34:10 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[aspnetmvc]]></category>
		<category><![CDATA[cloud computing]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2011/05/03/appharbor-asp-net-mvc-orchard-awesome/</guid>
		<description><![CDATA[ASP.NET development has come a long way.&#160; It’s now easier to practice Test Driven Development (TDD) thanks to ASP.NET MVC and all the open source tools out there from Ninject to Moq to Entity Framework Code First.&#160; But one thing remains real sucky in the .net world and that is deployment.&#160; Have you tried deploying [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET development has come a long way.&#160; It’s now easier to practice Test Driven Development (TDD) thanks to ASP.NET MVC and all the open source tools out there from <a href="http://ninject.org/" target="_blank">Ninject</a> to <a href="http://code.google.com/p/moq/" target="_blank">Moq</a> to <a href="http://msdn.microsoft.com/en-us/data/aa937723" target="_blank">Entity Framework Code First</a>.&#160; But one thing remains real sucky in the .net world and that is deployment.&#160; Have you tried deploying an Azure application?&#160; It sucks and I hate Azure for it.&#160; I created a web service using Python and deployed it on <a href="http://code.google.com/appengine/" target="_blank">Google AppEngine</a> and it was much faster to create, easier to maintain and deploy even though I knew zero Python.&#160; Deploying a site to IIS is a pain in the butt.</p>
<p>I have always been envious of how easy it was/is to deploy a rails app.&#160; It takes seconds and usually one command line.&#160; It is just awesome.&#160; So, I was really excited when a few weeks ago I ran across <a href="http://appharbor.com/" target="_blank">AppHarbor</a> and it looked too good to be true.&#160; I thought I would see how easy it would be to create a site and deploy it.&#160; Instead of creating an app from scratch though, I am going to create an Orchard website.&#160; If you haven’t seen Orchard, go <a href="http://www.orchardproject.net/" target="_blank">check it out</a>.&#160; It’s really cool.&#160; It’s a CMS/Blog engine written in ASP.NET MVC and is open source and very extensible.</p>
<p>Prerequisites: Setup git on your computer, see this <a href="http://help.github.com/win-set-up-git/" target="_blank">link</a> for instructions.</p>
<p>So, here is how to get a website up and running in seconds…</p>
<p>1 – Login to <a href="http://appharbor.com/" target="_blank">AppHarbor</a> and create a new application</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1545.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="2011-05-03_1545" border="0" alt="2011-05-03_1545" src="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1545_thumb.png" width="358" height="224" /></a></p>
<p>2 – Change your new application settings to enable write-access to the file system</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1607.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="2011-05-03_1607" border="0" alt="2011-05-03_1607" src="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1607_thumb.png" width="239" height="244" /></a></p>
<p>3 – Start WebMatrix and create a new site from Web Gallery</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1548.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="2011-05-03_1548" border="0" alt="2011-05-03_1548" src="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1548_thumb.png" width="486" height="227" /></a></p>
<p>4 – Choose Orchard and name your website</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1550.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="2011-05-03_1550" border="0" alt="2011-05-03_1550" src="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1550_thumb.png" width="402" height="298" /></a></p>
<p>5 – Open up a command prompt (I use <a href="http://sourceforge.net/projects/console/" target="_blank">Console2</a>)</p>
<p>6 – Change to the path of your new website e.g. cd c:\websites\mywebsite</p>
<p>7 – run the following commands</p>
<ul>
<li>git init</li>
<li>git add .</li>
<li>git commit –m “initial commit”</li>
<li>git remote add appharbor <a href="https://eibrahim@appharbor.com/MyWebsite.git">https://eibrahim@appharbor.com/MyWebsite.git</a> (the URL will be unique to your app)</li>
<li>git push appharbor master (enter your AppHarbor password)</li>
</ul>
<p>8 – Visit your new site on appharbor, in this example the site is located at <a href="http://mywebsite.apphb.com/">http://mywebsite.apphb.com/</a>&#160;</p>
<p>9 – DONE!!!&#160; Just follow the prompts to setup Orchard</p>
<p>&#160;</p>
<p>You have just deployed a web app and have it hosted for free.&#160; You can easily scale it using AppHarbor’s cloud infrastructure.&#160; It is simply awesome.&#160; </p>
<p>Another cool feature is that every time you want to deploy a new version, you simply commit your changes and run the git push command above.&#160; AppHarbor maintains different versions of your app and you can easily switch between versions.&#160; So if you deployed a bad version, you simply click a link and deploy the last working version.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_19411.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="2011-05-03_1941" border="0" alt="2011-05-03_1941" src="http://www.emadibrahim.com/wp-content/uploads/2011/05/2011-05-03_1941_thumb1.png" width="500" height="134" /></a></p>
<p>I personally haven’t deployed a scalable app on AppHarbor, so I would love to hear your feedback on how well the scaling works and if it is cost-effective or not.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2011/05/03/appharbor-asp-net-mvc-orchard-awesome/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Dude, Where Is My Update?</title>
		<link>http://www.emadibrahim.com/2011/03/30/dude-where-is-my-update/</link>
		<comments>http://www.emadibrahim.com/2011/03/30/dude-where-is-my-update/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 16:17:57 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Interesting]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/?p=748</guid>
		<description><![CDATA[Update: Version 1.1 Available – more info here. We are all patiently waiting for the Windows Phone 7 update.&#160; A few lucky ones already got it, but a whole lot of people including myself didn’t get it yet.&#160; I find myself insistently checking blogs, twitter, Facebook and Google to figure out if and when the [...]]]></description>
			<content:encoded><![CDATA[<p><strong><font style="background-color: #ffff00">Update: Version 1.1 Available – more </font></strong><a href="http://bit.ly/g4Yudn" target="_blank"><strong><font style="background-color: #ffff00">info here</font></strong></a><strong><font style="background-color: #ffff00">.</font></strong></p>
<p>We are all patiently waiting for the Windows Phone 7 update.&#160; A few lucky ones already got it, but a whole lot of people including myself didn’t get it yet.&#160; I find myself insistently checking blogs, twitter, Facebook and Google to figure out if and when the update will be available.&#160; To pass the time and to simplify my life and reduce the stress of figuring out the status of the update, I built a free little WP7 app called “Dude, Where Is My Update?” or DWIMU for short.</p>
<p>You simply select your region and phone or operator and get the bad/good news.&#160; I have a Samsung Focus, so unfortunately, I will be waiting for a long time.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2011/03/image.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2011/03/image_thumb.png" width="292" height="484" /></a></p>
<p>But if you are in Sweden with the Hutchison Group then you should be getting the updates a lot sooner.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2011/03/image1.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2011/03/image_thumb1.png" width="292" height="484" /></a></p>
<h2>How does it work?</h2>
<p>Right now, the app simply gets the data from Microsoft’s website at <a title="http://bit.ly/eMukfH" href="http://bit.ly/eMukfH">http://bit.ly/eMukfH</a>.&#160; I will write another blog detailing the technical implementation of the app.&#160; I built a web service in Python and hosted it with Google’s AppEngine (because it is free).&#160; DWIMU pulls the data from my web service which I will keep updated and in sync with Microsoft’s website.</p>
<h2>Why you should download the app?</h2>
<p>The future plans is to crowd source the update detection to get a more accurate indication of when you will be getting the update.&#160; By simply running the app you will be able to see how many people already received the update for your selected network/phone/region.&#160; Of course, this will only work if there is enough people using the app.&#160; If I get enough downloads, I will release an update that will enable automatic detection.&#160; So <a href="http://bit.ly/gamGev" target="_blank">download it now</a> and <a href="http://bit.ly/gwP2BN" target="_blank">spread the word</a>, if you want me to add these features.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2011/03/30/dude-where-is-my-update/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>It&#8217;s been a week since I ditched my iPhone for Windows Phone 7</title>
		<link>http://www.emadibrahim.com/2010/12/16/its-been-a-week-since-i-ditched-my-iphone-for-windows-phone-7/</link>
		<comments>http://www.emadibrahim.com/2010/12/16/its-been-a-week-since-i-ditched-my-iphone-for-windows-phone-7/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 17:01:20 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2010/12/16/its-been-a-week-since-i-ditched-my-iphone-for-windows-phone-7/</guid>
		<description><![CDATA[Its been a week with my new windows phone 7.&#160; I like it a lot and don&#8217;t miss my iPhone that much. I am actually typing this blog post on my Samsung focus in OneNote. Things I like Keyboard is phenomenal UI is awesome.&#160; Its a crazy world we live in when Microsoft does a [...]]]></description>
			<content:encoded><![CDATA[<p>Its been a week with my new windows phone 7.&#160; I like it a lot and don&#8217;t miss my iPhone that much.</p>
<p>I am actually typing this blog post on my Samsung focus in OneNote.</p>
<h2>Things I like</h2>
<ol>
<li>Keyboard is phenomenal</li>
<li>UI is awesome.&#160; Its a crazy world we live in when Microsoft does a better job with a UI than Apple.</li>
<li>Very responsive OS</li>
<li>Speech and voice commands are incredibly accurate.&#160; I know the iPhone has that but it just never worked well.</li>
<li>Outlook works very well</li>
</ol>
<h2>Things I don&#8217;t like</h2>
<ol>
<li>The app bar at the bottom of the screen is very easy to accidentally hit while typing.&#160; So in the middle of an email, i hit send accidentally while hitting the space bar.</li>
<li>No copy and paste.&#160; Initially I didn&#8217;t care but then when I started getting emails with information that i wanted to copy and paste. I also tried to duplicate a formula in excel and i couldn&#8217;t copy and paste.&#160; I am not retyping that formula for every row.</li>
<li>Keys are not very well placed.&#160; I accidentally keep hitting the camera button or the soft buttons at the bottom (back, start and search)</li>
<li>The previous problem wouldn&#8217;t be too bad if I had multi tasking, but if I am playing a game or doing something else and I accidentally hit search, I lose my work/game.&#160; It sucks.</li>
<li>I can&#8217;t buy audiobooks in the marketplace</li>
<li>I am missing some apps that are making it hard to forget the iPhone</li>
</ol>
<h2>Apps I need</h2>
<ol>
<li>Remember the milk</li>
<li>Evernote &#8211; if I can import my evernote notes into OneNote then i will be ok,&#160; but i can&#8217;t.&#160; Actually, I created a note taking application for free to fill the void.&#160; It is not as powerful as evernote but I am constantly adding more features as I see fit.&#160; See it here (<a href="http://social.zune.net/redirect?type=phoneApp&amp;id=168b518d-39ed-df11-9264-00237de2db9e" target="_blank">marketplace link</a>).</li>
<li>Audible player</li>
</ol>
<h2>Apps I would like to have</h2>
<ol>
<li>Kindle</li>
<li>tweetdeck</li>
</ol>
<h2>Best of All</h2>
<p>It is ridiculously easy to develop for.&#160; I have done iPhone and Android development and they have a very steep learning curve.&#160; Don’t just take my word for it.&#160; I created 4 apps in less than a month:</p>
<ol>
<li>envision – basecamp client for windows phone 7 – see <a href="http://envisionhq.com">http://envisionhq.com</a></li>
<li>Picasa Viewer – see <a href="http://picasaviewer.com">http://picasaviewer.com</a></li>
<li>Notesly Free – <a href="http://social.zune.net/redirect?type=phoneApp&amp;id=168b518d-39ed-df11-9264-00237de2db9e" target="_blank">marketplace link</a></li>
<li>QuoteZen – 170k quotes with search and bookmarking capabilities – <a href="http://social.zune.net/redirect?type=phoneApp&amp;id=7068dc3b-57f4-df11-9264-00237de2db9e" target="_blank">marketplace link</a></li>
</ol>
<p>You might think that the above statement is very subjective because I have been doing .NET since version zero, but trust me WP7 development is easier than iPhone and Android.&#160; Don’t believe me.&#160; Try it out for yourself – <a href="http://msdn.com">http://msdn.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2010/12/16/its-been-a-week-since-i-ditched-my-iphone-for-windows-phone-7/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Blackjack Simulator</title>
		<link>http://www.emadibrahim.com/2010/08/12/blackjack-simulator-2/</link>
		<comments>http://www.emadibrahim.com/2010/08/12/blackjack-simulator-2/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 16:24:35 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Interesting]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[blackjack]]></category>
		<category><![CDATA[gambling]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2010/08/12/blackjack-simulator-2/</guid>
		<description><![CDATA[What’s a better way to get ready for a gambling trip other than write a blackjack simulator?  I am no blackjack expert but one of the strategies I heard off is to double your bet every time you lose.  So, if I start with a $100 bankroll and play $10 hands then I lost the [...]]]></description>
			<content:encoded><![CDATA[<p>What’s a better way to get ready for a gambling trip other than write a blackjack simulator?  I am no blackjack expert but one of the strategies I heard off is to double your bet every time you lose.  So, if I start with a $100 bankroll and play $10 hands then I lost the first hand, I am down to $90 and now should bet $20.  If I lose then I am down to $70 and should bet $40 and so on.  If I win then I have $110 and should bet $10.  That is the basic premise of my blackjack simulator.</p>
<h2>Results</h2>
<p>Here is an output of the results after 40 hands for:</p>
<p>player 1 &#8211; <a href="http://pastey.net/139513">http://pastey.net/139513</a></p>
<p>player 2 &#8211; <a href="http://pastey.net/139514">http://pastey.net/139514</a></p>
<p>dealer &#8211; <a href="http://pastey.net/139515">http://pastey.net/139515</a></p>
<p>Here is another run:</p>
<p>player 1 &#8211; <a href="http://pastey.net/139516">http://pastey.net/139516</a></p>
<p>player 2 &#8211; <a href="http://pastey.net/139517">http://pastey.net/139517</a></p>
<p>dealer &#8211; <a href="http://pastey.net/139518">http://pastey.net/139518</a></p>
<h2>Screenshot</h2>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/08/image2.png"><img style="display: inline; border: 0px;" title="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/08/image_thumb2.png" border="0" alt="image" width="424" height="326" /></a></p>
<h2>Disclaimer</h2>
<p>you WILL EVENTUALLY LOSE.  I ran the simulator for a long time and I was <strong><span style="color: #ff0000;">down over $90k</span></strong>.</p>
<h2>Source Code</h2>
<p>You can get the source code for the application here &#8211; <a href="http://github.com/eibrahim/BlackJackSimulator">http://github.com/eibrahim/BlackJackSimulator</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2010/08/12/blackjack-simulator-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Display a Dictionary/KeyValuePair as Grid in WPF</title>
		<link>http://www.emadibrahim.com/2010/07/30/display-a-dictionarykeyvaluepair-as-grid-in-wpf/</link>
		<comments>http://www.emadibrahim.com/2010/07/30/display-a-dictionarykeyvaluepair-as-grid-in-wpf/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 15:38:23 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2010/07/30/display-a-dictionarykeyvaluepair-as-grid-in-wpf/</guid>
		<description><![CDATA[Every time I work with WPF, I constantly think “I hate this shit” and “why is everything so damn hard”, but once I figure it out, I realize how powerful it really is and what I can really do with it.&#160; I remember starting out with WPF and trying to figure out how to bind [...]]]></description>
			<content:encoded><![CDATA[<p>Every time I work with WPF, I constantly think “I hate this shit” and “why is everything so damn hard”, but once I figure it out, I realize how powerful it really is and what I can really do with it.&#160; I remember starting out with WPF and trying to figure out how to bind data to a control took me 2 days of reading several articles and a book.</p>
<p>Anyway, I am currently working on a <a href="http://www.mongodb.org" target="_blank">MongoDb</a> GUI &#8211; there are none out there &#8211; and I decided to build it in WPF.&#160; I am using the MongoDb driver by samus (<a href="http://github.com/samus/mongodb-csharp" target="_blank">link</a>).&#160; Displaying the data returned from a query as a table proved to be a lot harder than I thought.</p>
<p>Remember that the application doesn’t know anything about the domain model, so I can’t use strongly-typed objects.&#160; This is the code to retrieve the list of user objects stored in MongoDb:</p>
</p>
<pre class="csharpcode">var collection = db.GetCollection&lt;Document&gt;(User);
var results = collection.FindAll();</pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>This will return the results as ICursor&lt;Document&gt;.&#160; Think of Document as the base type for all objects stored in MongoDb.&#160; The actual collection of documents/objects/users is in results.Documents.&#160; A Document implements the ICollection&lt;KeyValuePair&lt;string,object&gt;&gt;, this means that each document is basically a key/value list of all the object properties.&#160; So a user object will be stored like this:</p>
<table border="1" cellspacing="0" cellpadding="2" width="391">
<tbody>
<tr>
<td valign="top" width="170"><strong>Key</strong></td>
<td valign="top" width="219"><strong>Value</strong></td>
</tr>
<tr>
<td valign="top" width="170">FirstName</td>
<td valign="top" width="219">John</td>
</tr>
<tr>
<td valign="top" width="170">LastName</td>
<td valign="top" width="219">Smith</td>
</tr>
<tr>
<td valign="top" width="170">CreatedOn</td>
<td valign="top" width="219">2010-07-20T05:39:35.5220000Z</td>
</tr>
<tr>
<td valign="top" width="170">Account</td>
<td valign="top" width="219">{ &quot;SubscriptionType&quot;: &quot;paid&quot;, &quot;CreatedOn&quot;: &quot;2010-07-20T05:39:35.5220000Z&quot;, &quot;ModifiedOn&quot;: &quot;2010-07-20T05:39:35.5220000Z&quot; }</td>
</tr>
</tbody>
</table>
<p>What I want to do is to take this list of documents and display as a grid.&#160; I want it to look something like this:</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/07/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/07/image_thumb1.png" width="481" height="167" /></a> </p>
<p>I am no WPF expert but after Googling and reading tons of articles for hours, here is the solution I came up with.&#160; If you know a better one, please let me know.</p>
<p>Here is the XAML for the ListView</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">ListView</span> <span class="attr">Name</span><span class="kwrd">=&quot;lvItems&quot;</span>  <span class="attr">Margin</span><span class="kwrd">=&quot;12,12,9,12&quot;</span>        <span class="attr">IsSynchronizedWithCurrentItem</span><span class="kwrd">=&quot;True&quot;</span> <span class="attr">Grid</span>.<span class="attr">Column</span><span class="kwrd">=&quot;1&quot;</span>        <span class="attr">ItemsSource</span><span class="kwrd">=&quot;{Binding}&quot;</span> <span class="kwrd">&gt;</span></pre>
<p><span class="kwrd">&lt;/</span><span class="html">ListView</span><span class="kwrd">&gt;</span><br />
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>The first thing I need to do is define the grid columns:</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">static</span> GridView CreateGridViewColumns(Document doc)
{
    <span class="rem">// Create the GridView</span>
    GridView gv = <span class="kwrd">new</span> GridView();
    gv.AllowsColumnReorder = <span class="kwrd">true</span>;

    <span class="kwrd">if</span>(doc ==<span class="kwrd">null</span>) <span class="kwrd">return</span> gv; <span class="rem">//return empty grid if null</span>

    <span class="rem">// Create the GridView Columns)</span>
    <span class="kwrd">foreach</span> (var item <span class="kwrd">in</span> doc.Keys)
    {
        var gvc = <span class="kwrd">new</span> GridViewColumn();
        gvc.Header = item;
        gvc.Width = Double.NaN;
        gvc.CellTemplateSelector = <span class="kwrd">new</span> CustomRowDataTemplateSelector();
        gv.Columns.Add(gvc);
    }

    <span class="kwrd">return</span> gv;
}</pre>
<style type="text/css">
.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>
<div>
  </div>
<p>I use the returned GridView to set the ListView’s Grid </p>
</p>
<pre class="csharpcode">var gridView = CreateGridViewColumns(results.Documents.FirstOrDefault());
lvItems.View = gridView;</pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>So that takes care of setting up the grid column header.&#160; Now, I need to bind the data.&#160; I want to treat each document as a “Row”, so I create a custom class called CustomRow.&#160; Some of the stuff in there won’t make sense right now, but bear with me.</p>
<pre class="csharpcode"><span class="kwrd">internal</span> <span class="kwrd">class</span> CustomRow
{
    <span class="kwrd">private</span> <span class="kwrd">int</span> index = 0;
    <span class="kwrd">private</span> ArrayList _list;

    <span class="kwrd">public</span> CustomRow()
    {
        _list = <span class="kwrd">new</span> ArrayList();
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> Value
    {
        get
        {
            <span class="kwrd">if</span> (index &lt; 0) index = 0;
            <span class="kwrd">if</span> (index &gt;= _list.Count) index = 0; <span class="rem">//wrap around and start from beginning</span>

            <span class="kwrd">return</span> _list[index++];
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> Current
    {
        get
        {
            <span class="kwrd">if</span> (index &lt; 0) index = 0;
            <span class="kwrd">if</span> (index &gt;= _list.Count) index = 0; <span class="rem">//wrap around and start from beginning</span>
            <span class="kwrd">return</span> _list[index];
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> Add(<span class="kwrd">object</span> item)
    {
        _list.Add(item);
    }
}</pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>&#160;</p>
<p>Now, all I have to do is convert each document to CustomRow:</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">object</span> ConvertToRows(IEnumerable&lt;Document&gt; documents)
{
    var results = <span class="kwrd">new</span> List&lt;CustomRow&gt;();

    <span class="kwrd">foreach</span> (var document <span class="kwrd">in</span> documents)
    {
        var row = <span class="kwrd">new</span> CustomRow();
        <span class="kwrd">foreach</span> (var field <span class="kwrd">in</span> document)
        {
            row.Add(field.Value);
        }
        results.Add(row);
    }
    <span class="kwrd">return</span> results;
}</pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>This is the full code used to setup the ListView:</p>
<pre class="csharpcode">lvItems.View = CreateGridViewColumns(results.Documents.FirstOrDefault());
lvItems.DataContext = ConvertToRows(results.Documents);</pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>Did you notice the line with the CellTemplateSelector in the CreateGridViewColumns method above?&#160; Well, I wanted my cells to display differently based on whether they are displaying string, date, an embedded object/document or a list of other documents.&#160; So, I created a custom CellTemplateSelector:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CustomRowDataTemplateSelector : DataTemplateSelector
{
    <span class="kwrd">public</span> <span class="kwrd">override</span> DataTemplate
        SelectTemplate(<span class="kwrd">object</span> item, DependencyObject container)
    {
        FrameworkElement element = container <span class="kwrd">as</span> FrameworkElement;

        <span class="kwrd">if</span> (element != <span class="kwrd">null</span> &amp;&amp; item != <span class="kwrd">null</span>)
        {
            var row = item <span class="kwrd">as</span> CustomRow;
            <span class="kwrd">if</span> (row != <span class="kwrd">null</span>)
            {
                var cell = row.Current;

                <span class="rem">//set template based on cell type</span>
                <span class="kwrd">if</span> (cell <span class="kwrd">is</span> Document)
                {
                    <span class="kwrd">return</span> element.FindResource(<span class="str">&quot;documentCell&quot;</span>) <span class="kwrd">as</span> DataTemplate;
                }
                <span class="kwrd">if</span> (cell <span class="kwrd">is</span> IList)
                {
                    <span class="kwrd">return</span> element.FindResource(<span class="str">&quot;listCell&quot;</span>) <span class="kwrd">as</span> DataTemplate;
                }

                <span class="kwrd">if</span> (cell <span class="kwrd">is</span> DateTime)
                {
                    <span class="kwrd">return</span> element.FindResource(<span class="str">&quot;dateCell&quot;</span>) <span class="kwrd">as</span> DataTemplate;
                }

                <span class="kwrd">if</span> (cell <span class="kwrd">is</span> Oid)
                    <span class="kwrd">return</span> element.FindResource(<span class="str">&quot;idCell&quot;</span>) <span class="kwrd">as</span> DataTemplate;
                <span class="kwrd">return</span> element.FindResource(<span class="str">&quot;stringCell&quot;</span>) <span class="kwrd">as</span> DataTemplate;
            }

        }

        <span class="kwrd">return</span> <span class="kwrd">null</span>;
    }
}</pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>Basically, I am returning a different DataTemplate based on the object stored in the cell.&#160; As an example, here is dateCell template defined in the App.xaml file.</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">DataTemplate</span> <span class="attr">x:Key</span><span class="kwrd">=&quot;dateCell&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">TextBlock</span> <span class="attr">Style</span><span class="kwrd">=&quot;{StaticResource cellStyle}&quot;</span>
                    <span class="attr">Grid</span>.<span class="attr">Row</span><span class="kwrd">=&quot;0&quot;</span> <span class="attr">Grid</span>.<span class="attr">Column</span><span class="kwrd">=&quot;0&quot;</span>
                    <span class="attr">Text</span><span class="kwrd">=&quot;{Binding Path=Value, Converter={StaticResource cellConverter}}&quot;</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">StackPanel</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">DataTemplate</span><span class="kwrd">&gt;</span></pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<p>This is simply a TextBlock that is bound to the Value property of the CustomRow class (will explain later).&#160; The cellConverter is custom logic that will convert the value of the cell accordingly.&#160; For example, date is converted to short date, an embedded object is simply converted to an “(object)” string as shown above.&#160; Here is my converter (ConvertBack hasn’t been implemented yet):</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> CellConverter : IValueConverter
{
    <span class="kwrd">public</span> <span class="kwrd">object</span> Convert(<span class="kwrd">object</span> <span class="kwrd">value</span>, Type targetType, <span class="kwrd">object</span> parameter, CultureInfo culture)
    {
        <span class="kwrd">if</span> (<span class="kwrd">value</span> == <span class="kwrd">null</span>)
            <span class="kwrd">return</span> <span class="str">&quot;(null)&quot;</span>;
        <span class="kwrd">if</span> (<span class="kwrd">value</span> <span class="kwrd">is</span> Document)
            <span class="kwrd">return</span> <span class="str">&quot;(object)&quot;</span>;
        <span class="kwrd">if</span> (<span class="kwrd">value</span> <span class="kwrd">is</span> DateTime)
            <span class="kwrd">return</span> ((DateTime)<span class="kwrd">value</span>).ToShortDateString();
        <span class="kwrd">if</span> (<span class="kwrd">value</span> <span class="kwrd">is</span> IList)
            <span class="kwrd">return</span> <span class="str">&quot;(list)&quot;</span>;

        <span class="kwrd">return</span> <span class="kwrd">value</span>;
    }

    <span class="kwrd">public</span> <span class="kwrd">object</span> ConvertBack(<span class="kwrd">object</span> <span class="kwrd">value</span>, Type targetType, <span class="kwrd">object</span> parameter, CultureInfo culture)
    {
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException();
    }
}</pre>
<style type="text/css">
.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>
<div id="codeSnippetWrapper">
  </div>
<h2>Explanations</h2>
<p>At first, I tried to just bind directly to the list of values in each document.&#160; The problem with that the DataTempalte only display the first item in the collection.&#160; So my grid looks exactly like above but all the cells were set to the value of Id.&#160; That is why I created the CustomRow class and that is why the Value property increments the indexer.&#160; This way everytime I pull out the value for a cell, I increment the indexer so that the next bound cell will get the next value and so on.&#160; </p>
<p>Again, I don’t know if this is the best solution, so if you do know of a better one then please let me know in the comments.&#160; It will help all my readers.&#160; Thanks in advance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2010/07/30/display-a-dictionarykeyvaluepair-as-grid-in-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subversion (SVN) to Mercurial and TeamCity</title>
		<link>http://www.emadibrahim.com/2010/05/03/subversion-svn-to-mercurial-and-teamcity/</link>
		<comments>http://www.emadibrahim.com/2010/05/03/subversion-svn-to-mercurial-and-teamcity/#comments</comments>
		<pubDate>Mon, 03 May 2010 12:27:00 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mercurial]]></category>
		<category><![CDATA[scm]]></category>
		<category><![CDATA[source control]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[teamcity]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2010/05/03/subversion-svn-to-mercurial-and-teamcity/</guid>
		<description><![CDATA[Every time I think I got all the tools I need and I have a killer setup, something comes along that makes me question it all.&#160; I am referring to mercurial.&#160; For some reason, I have just heard of mercurial from TekPub.&#160; I don&#8217;t know why I never heard of it before.&#160; Probably because I [...]]]></description>
			<content:encoded><![CDATA[<p>Every time I think I got all the tools I need and I have a killer setup, something comes along that makes me question it all.&#160; I am referring to mercurial.&#160; For some reason, I have just heard of mercurial from <a href="http://www.tekpub.com" target="_blank">TekPub</a>.&#160; I don&#8217;t know why I never heard of it before.&#160; Probably because I wasn&#8217;t looking to replace subversion as my trusted source control.</p>
<p>I like the subversion and it does the job for me, but after watching tekpub&#8217;s videos, I instantly realized the power of mercurial and why I must have it.&#160; So I went ahead and converted my source control for <a href="http://dartfiles.com" target="_blank">dartfiles</a> to Hg (hg is the chemical symbol for mercury in the periodic table). I was actually surprised by how easy the move went.</p>
<p>Here is what I did:     </p>
<ol>
<li>Downloaded and installed <a href="http://tortoisehg.bitbucket.org/" target="_blank">TortoiseHg</a> </li>
<li>Exported my subversion project to a new folder&#160;
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image.png" width="478" height="311" />       </li>
<li>Created a Mercurial repository at the new folder where I exported the code in step 2
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/05/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image_thumb.png" width="497" height="224" /></a>       <br /><a href="http://www.emadibrahim.com/wp-content/uploads/2010/05/image2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image_thumb1.png" width="452" height="174" /></a>       </li>
<li>Edited the ignore file (.hgignore) in notepad and added the following (which I found somewhere on stackoverflow)      </li>
<pre class="csharpcode"><span class="rem"># Ignore file for Visual Studio 2008</span>

<span class="rem"># use glob syntax</span>
syntax: glob

<span class="rem"># Ignore Visual Studio 2008 files</span>
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
*.lib
*.sbr
*.scc
[Bb]<span class="kwrd">in</span>
[Db]ebug*/
obj/
[Rr]elease*/
_ReSharper*/
[Tt]est[Rr]esult*
[Bb]uild[Ll]og.*
*.[Pp]ublish.xml</pre>
<style type="text/css">
.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>
<li>Committed the repository
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/05/image3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image_thumb2.png" width="244" height="156" /></a> </p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/05/image4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image_thumb3.png" width="398" height="288" /></a> </li>
</ol>
<p>That’s all I had to do to setup mercurial.&#160; </p>
<p>You are probably wondering – where is the source control server and where are you committing too.&#160; This is the part that is different than what I have been used to with subversion.&#160; Obviously if you are using git or similar distributed SCM then you are familiar with this concept.&#160; Mercurial stores all your source code changes and tracking locally right in your folder inside a .hg subfolder (similar to .svn folders but not exactly).&#160; When you commit you are just committing to your local repository.&#160; This is pretty cool because it lets you commit locally, roll back to a different time, branch and do all kind of cool things without having to deal with a server.&#160; </p>
<p>Imagine you are working on feature 1 then you get an email for an urgent bug (lets’ call it bug 2).&#160; You can’t really work on it because feature 1 is not complete and the code won’t compile and things could get messy.&#160; You want to fix the bug but you don’t want to lose all the work you put into feature 1 and you are note ready to deploy.&#160; With mercurial, it’s pretty easy to handle this situation.&#160; Simply go back to a point in your history before you started working on feature 1, create a new branch for bug 2, fix it then commit and push it, you can then deploy this version and then come back and merge the bug 2 branch with your feature 1 branch and continue working on feature 1.&#160; </p>
<p>I probably didn’t do a good job explaining this scenario but hopefully you will at least get an idea of what I am talking about.&#160; If not then head over to <a href="http://mercurial.selenic.com/">http://mercurial.selenic.com/</a> and take a look at their great documentation.</p>
<h2>Remote Repository</h2>
<p>In most cases, you will want to setup a remote repository so you can “push” code to it.&#160; “Pushing” is another concept that was new to me.&#160; Basically, you commit locally and you push remotely (or centrally).&#160; This gives you the flexibility to commit all day long every time you make a change and only when the code is ready and stable you “push” it to the remote repository where other developers can get to it.&#160; I setup my remote repository at bitbucket &#8211; <a href="http://bitbucket.org/">http://bitbucket.org/</a> – they have a free account and some very reasonable pricing.&#160; If you are working on open source/public code, you can always use Google code or codeplex; they both support mercurial.</p>
<p>Here are the steps</p>
<ol>
<li>Created an account at bitbucket.</li>
<li>Setup TortoiseHg to talk with the remote repository (right-click &gt; TortoiseHg &gt; Repository Settings)
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/05/image5.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image_thumb4.png" width="477" height="416" /></a></p>
<p> </li>
<li>Pushed my code to the remote server</li>
</ol>
<h2><strong>TeamCity Integration</strong></h2>
<p>I use TeamCity as my build server (<a href="http://www.emadibrahim.com/2009/08/05/startup-asp-net-mvc-cloud-scale-deployment/" target="_blank">blog post</a>).&#160; Once everything was working nicely, I had to get TeamCity to work with the new SCM.&#160; This was pretty easy as well.&#160; I basically removed my old SVN root from my TeamCity project and added a new root to point to mercurial</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/05/image6.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image_thumb5.png" width="561" height="552" /></a> </p>
<p>To make this work, you have to install mercurial on the server.&#160; Get it from <a title="http://mercurial.selenic.com/downloads/" href="http://mercurial.selenic.com/downloads/">http://mercurial.selenic.com/downloads/</a></p>
<p>Initially, I had a problem with the HG command path and it was giving me some weird error like failed to create process or something like that (I can’t remember).&#160; It turned out the problem was that TeamCity couldn’t deal with spaces in the path.&#160; The install location was “c:\Program Files (x86)\hg” so I just moved it to c:\hg and everything worked.</p>
<p>Once everything was working well and after working for a few hours, doing a few commits and one deployment, I realized the power of mercurial.&#160; Just take a look at my repository history and you might grasp the awesomeness that is mercurial.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2010/05/image7.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2010/05/image_thumb6.png" width="655" height="486" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2010/05/03/subversion-svn-to-mercurial-and-teamcity/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Serialize Object To Clean XML</title>
		<link>http://www.emadibrahim.com/2010/02/23/serialize-object-to-clean-xml/</link>
		<comments>http://www.emadibrahim.com/2010/02/23/serialize-object-to-clean-xml/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 00:08:12 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2010/02/23/serialize-object-to-clean-xml/</guid>
		<description><![CDATA[I hate all the junk that gets added when serializing object to XML, so here is a quick way to do it cleanly. Here is the dirty way: public static string Serialize(this object obj) { string XmlString = String.Empty; using (var memStream = new MemoryStream()) { var serializer = new XmlSerializer(obj.GetType(), string.Empty); using (var xmlText [...]]]></description>
			<content:encoded><![CDATA[<p>I hate all the junk that gets added when serializing object to XML, so here is a quick way to do it cleanly.</p>
<p>Here is the dirty way:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> Serialize(<span style="color: #0000ff">this</span> <span style="color: #0000ff">object</span> obj)
{
    <span style="color: #0000ff">string</span> XmlString = String.Empty;
    <span style="color: #0000ff">using</span> (var memStream = <span style="color: #0000ff">new</span> MemoryStream())
    {
        var serializer = <span style="color: #0000ff">new</span> XmlSerializer(obj.GetType(), <span style="color: #0000ff">string</span>.Empty);
        <span style="color: #0000ff">using</span> (var xmlText = <span style="color: #0000ff">new</span> XmlTextWriter(memStream, Encoding.Default))
        {
            serializer.Serialize(xmlText, obj);
        }
        XmlString = Encoding.Default.GetString(memStream.ToArray());
        memStream.Close();
    }
    <span style="color: #0000ff">return</span> XmlString;
}</pre>
</div>
<p>The resulting XML looks like this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;?xml version=<span style="color: #006080">&quot;1.0&quot;</span> encoding=<span style="color: #006080">&quot;Windows-1252&quot;</span>?&gt;
&lt;ZipContentInfo xmlns:xsi=<span style="color: #006080">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> xmlns:xsd=<span style="color: #006080">&quot;http://www.w3.org/2001/XMLSchema&quot;</span>&gt;
    &lt;FileName&gt;00107_tokyoatnight_1680x1050.jpg&lt;/FileName&gt;
    &lt;FileSize&gt;281268&lt;/FileSize&gt;
&lt;/ZipContentInfo&gt;</pre>
</div>
<p>Here is a cleaner way:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> Serialize(<span style="color: #0000ff">this</span> <span style="color: #0000ff">object</span> obj)
{
    var ser = <span style="color: #0000ff">new</span> XmlSerializer(obj.GetType());
    <span style="color: #0000ff">using</span> (var tw = <span style="color: #0000ff">new</span> StringWriter())
    {
        <span style="color: #0000ff">using</span> (var xw = XmlWriter.Create(tw, <span style="color: #0000ff">new</span> XmlWriterSettings() { OmitXmlDeclaration = <span style="color: #0000ff">true</span> }))
        {
            var ns = <span style="color: #0000ff">new</span> XmlSerializerNamespaces();
            <span style="color: #008000">//Add an empty namespace and empty value</span>
            ns.Add(<span style="color: #006080">&quot;&quot;</span>, <span style="color: #006080">&quot;&quot;</span>);
            ser.Serialize(xw, obj, ns);
            <span style="color: #0000ff">return</span> tw.ToString();
        }
    }
}</pre>
</div>
<p>And the resulting XML looks like this:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">&lt;ZipContentInfo&gt;
    &lt;FileName&gt;00107_tokyoatnight_1680x1050.jpg&lt;/FileName&gt;
    &lt;FileSize&gt;281268&lt;/FileSize&gt;
&lt;/ZipContentInfo&gt;</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2010/02/23/serialize-object-to-clean-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Select Random Records Using Nhibernate</title>
		<link>http://www.emadibrahim.com/2009/12/16/select-random-records-using-nhibernate/</link>
		<comments>http://www.emadibrahim.com/2009/12/16/select-random-records-using-nhibernate/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 03:54:15 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2009/12/16/select-random-records-using-nhibernate/</guid>
		<description><![CDATA[Selecting 5 random rows in SQL is easy select top 5 * from users order by newid() .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 [...]]]></description>
			<content:encoded><![CDATA[<p>Selecting 5 random rows in SQL is easy</p>
</p>
<pre class="csharpcode"><span class="kwrd">select</span> <span class="kwrd">top</span> 5 * <span class="kwrd">from</span> users <span class="kwrd">order</span> <span class="kwrd">by</span> newid()</pre>
<p>
<style type="text/css">
.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>Nhibernate is a different story.&#160; I am new to NHibernate and I both love and hate it.&#160; I hate the steep learning curve and the unobvious ways of doing things.&#160; Anyway, here is how to select 5 random records using nhibernate.</p>
<p>First class a new order class to perform random ordering:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> RandomOrder : Order
{
    <span style="color: #0000ff">public</span> RandomOrder() : <span style="color: #0000ff">base</span>(<span style="color: #006080">&quot;&quot;</span>, <span style="color: #0000ff">true</span>) { }
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery)
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> SqlString(<span style="color: #006080">&quot;newid()&quot;</span>);
    }
}</pre>
</div>
<p>Then use the new class in your query:</p>
<div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px">
<pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">IList&lt;User&gt; users = session
    .CreateCriteria(<span style="color: #0000ff">typeof</span>(User))
    .AddOrder(<span style="color: #0000ff">new</span> RandomOrder())
    .SetMaxResults(5)
    .List&lt;User&gt;();</pre>
</div>
<p>Done…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2009/12/16/select-random-records-using-nhibernate/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery Wildcard Selector</title>
		<link>http://www.emadibrahim.com/2009/07/25/jquery-wildcard-selector/</link>
		<comments>http://www.emadibrahim.com/2009/07/25/jquery-wildcard-selector/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 13:27:49 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2009/07/25/jquery-wildcard-selector/</guid>
		<description><![CDATA[This post is a reminder for me on how to do wildcard selection in jQuery.&#160; It comes up more than you think and for some reason, I can never remember it. &#60;div id='pnlUsers'&#62;&#60;/div&#62; &#60;div id='pnlMessages'&#62;&#60;/div&#62; &#60;div id='pnlStatus'&#62;&#60;/div&#62; .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a reminder for me on how to do wildcard selection in jQuery.&#160; It comes up more than you think and for some reason, I can never remember it.</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">id</span><span class="kwrd">='pnlUsers'</span><span class="kwrd">&gt;&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">id</span><span class="kwrd">='pnlMessages'</span><span class="kwrd">&gt;&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">id</span><span class="kwrd">='pnlStatus'</span><span class="kwrd">&gt;&lt;/</span><span class="html">div</span><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>I can easily hide all the divs up by doing a wildcard selection on elements with an id starting with “pnl”, like this:</p>
<pre class="csharpcode">$(<span class="str">&quot;[id^=pnl]&quot;</span>).hide();</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>You can even narrow it to only divs like this:</p>
<pre class="csharpcode">$(<span class="str">&quot;div[id^=pnl]&quot;</span>).hide();</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>There is another example with even more explanations <a href="http://www.insideria.com/2009/04/jquery-wild-card-example.html" target="_blank">over here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2009/07/25/jquery-wildcard-selector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular Express For Parsing URLs</title>
		<link>http://www.emadibrahim.com/2009/07/05/regular-express-for-parsing-urls/</link>
		<comments>http://www.emadibrahim.com/2009/07/05/regular-express-for-parsing-urls/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 15:51:00 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2009/07/05/regular-express-for-parsing-urls/</guid>
		<description><![CDATA[I am always looking for this one, so I am blogging it for future reference and for everyone to use. ^(?#Protocol)(?:(?:ht&#124;f)tp(?:s?)\:\/\/&#124;~/&#124;/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com&#124;org&#124;net&#124;gov&#124;mil&#124;biz&#124;info&#124;mobi&#124;name&#124;aero&#124;jobs&#124;museum&#124;travel&#124;[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+&#124;.,=]&#124;%[a-f\d]{2})+)+&#124;/)+&#124;\?&#124;#)?(?#Query)(?:(?:\?(?:[-\w~!$+&#124;.,*:]&#124;%[a-f\d{2}])+=(?:[-\w~!$+&#124;.,*:=]&#124;%[a-f\d]{2})*)(?:&#38;(?:[-\w~!$+&#124;.,*:]&#124;%[a-f\d{2}])+=(?:[-\w~!$+&#124;.,*:=]&#124;%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+&#124;.,*:=]&#124;%[a-f\d]{2})*)?$ There is more information on Ivan’s blog.]]></description>
			<content:encoded><![CDATA[<p>I am always looking for this one, so I am blogging it for future reference and for everyone to use.</p>
<blockquote><p>^(?#Protocol)(?:(?:ht|f)tp(?:s?)\:\/\/|~/|/)?(?#Username:Password)(?:\w+:\w+@)?(?#Subdomains)(?:(?:[-\w]+\.)+(?#TopLevel Domains)(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|travel|[a-z]{2}))(?#Port)(?::[\d]{1,5})?(?#Directories)(?:(?:(?:/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|/)+|\?|#)?(?#Query)(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&amp;(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?#Anchor)(?:#(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)?$</p>
</blockquote>
<p>There is more information on <a href="http://geekswithblogs.net/casualjim/archive/2005/12/01/61722.aspx" target="_blank">Ivan’s blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2009/07/05/regular-express-for-parsing-urls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Shortening URLs with bit.ly’s API in .NET</title>
		<link>http://www.emadibrahim.com/2009/05/07/shortening-urls-with-bitlys-api-in-net/</link>
		<comments>http://www.emadibrahim.com/2009/05/07/shortening-urls-with-bitlys-api-in-net/#comments</comments>
		<pubDate>Thu, 07 May 2009 18:34:02 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[bitly]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[tinyurl]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2009/05/07/shortening-urls-with-bitlys-api-in-net/</guid>
		<description><![CDATA[I previously wrote about shortening URLs with TinyURL’s API but my new favorite URL shortener is http://bit.ly and it also has a great API that you can use from within your .net code.  I just recently used in my twitter contest website – tweetastica. The code is also very simple.  I didn’t add all the [...]]]></description>
			<content:encoded><![CDATA[<p>I previously wrote about shortening URLs with <a href="http://www.emadibrahim.com/2008/04/15/shortening-urls-using-tinyurl-api-in-net/">TinyURL’s API</a> but my new favorite URL shortener is <a href="http://bit.ly">http://bit.ly</a> and it also has a great API that you can use from within your .net code.  I just recently used in my twitter contest website – <a href="http://tweetastica.com">tweetastica</a>.</p>
<p>The code is also very simple.  I didn’t add all the extra options that the API makes available and just wrote enough for me to shorten a URL.  Here it is:</p>
<div>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static class</span> BitlyApi
{
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> apiKey = <span style="color: #006080">"[add api key here]"</span>;
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> login = <span style="color: #006080">"[add login name here]"</span>;

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> BitlyResults ShortenUrl(<span style="color: #0000ff">string</span> longUrl)
    {
        var url =
            <span style="color: #0000ff">string</span>.Format(<span style="color: #006080">"http://api.bit.ly/shorten?format=xml&amp;version=2.0.1&amp;longUrl={0}&amp;login={1}&amp;apiKey={2}"</span>,
                          HttpUtility.UrlEncode(longUrl), login, apiKey);
        var resultXml = XDocument.Load(url);
        var x = (from result <span style="color: #0000ff">in</span> resultXml.Descendants(<span style="color: #006080">"nodeKeyVal"</span>)
                 select <span style="color: #0000ff">new</span> BitlyResults
                            {
                                UserHash = result.Element(<span style="color: #006080">"userHash"</span>).Value,
                                ShortUrl = result.Element(<span style="color: #006080">"shortUrl"</span>).Value
                            }
                );
        <span style="color: #0000ff">return</span> x.Single();
    }
}

<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> BitlyResults
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> UserHash { get; set; }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ShortUrl { get; set; }
}</pre>
</div>
<p>Using this code is very straight forward.</p>
<div>
<pre style="line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: consolas, 'Courier New', courier, monospace; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">var shortUrl = BitlyApi.ShortenUrl(<span style="color: #006080">"http://www.verylongUrl.com"</span>).ShortUrl;</pre>
</div>
<p>bit.ly has tons of features and a great API.  You can even see stats of any bit.ly generate url using /info.  for example: <a href="http://bit.ly/info/S0vRy">http://bit.ly/info/S0vRy</a> shows you stats for <a href="http://bit.ly/S0vRy">http://bit.ly/S0vRy</a></p>
<p>Another feature I really like is their bookmarklet which you can keep on your browser’s bookmark bar and click it to shrink the site you are at…  It even pops this side bar with a bunch of useful info.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2009/05/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" src="http://www.emadibrahim.com/wp-content/uploads/2009/05/image-thumb.png" border="0" alt="image" width="410" height="298" /></a></p>
<p>The API is pretty well documented at <a href="http://code.google.com/p/bitly-api/wiki/ApiDocumentation">http://code.google.com/p/bitly-api/wiki/ApiDocumentation</a></p>
<p>Don’t forget to follow me on <a href="http://twitter.com/eibrahim" target="_blank">twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2009/05/07/shortening-urls-with-bitlys-api-in-net/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Themes and ASP.NET MVC</title>
		<link>http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/</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[ASP.NET MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Web Dev]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/</guid>
		<description><![CDATA[I wanted to easily add theme support to yonkly, 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 wordpress. I created a themes folder under the content folder Then I referenced my css file in the [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to easily add theme support to <a href="http://yonkly.com" target="_blank">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">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">www.yonkly.com</a></li>
<li><a href="http://www.isweat.com">www.isweat.com</a></li>
<li><a href="http://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>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/10/02/themes-and-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Mocking and Dependency Injection in ASP.NET MVC</title>
		<link>http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/</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[ASP.NET MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[mbunit]]></category>
		<category><![CDATA[moq]]></category>
		<category><![CDATA[MVC Book]]></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><![CDATA[Here is the situation, my controller constructors take multiple interfaces as parameters.&#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 [...]]]></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">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/">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">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">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"><span style="color: #606060">  12:</span>                 .GetControllerWithFakeContext&lt;AccountController&gt;(<span style="color: #006080">"POST"</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>&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">  14:</span>     var results = ac.ResetPasswordQuestion(username, question, 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">  15:</span>     <span style="color: #008000">//write some asserts in here to make sure things worked</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">  16:</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">  17:</span>     <span style="color: #008000">//verify all mocks</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>     MyMocks.MockMembershipProvider.VerifyAll();</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>     MyMocks.MockEmailService.VerifyAll();</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> }</pre>
</div>
</div>
<p>Line #5: I mock the ResetPassword call on the membership provider and tell it to return the new password</p>
<p>Line #8: I mock the SendPasswordReset method on the email service</p>
<p>Line #11: Get an instance of AccountController from the Ninject Kernel</p>
<p>I just write some code to make sure the expected results took place and that my mocks were properly exercised and that&#8217;s pretty much it.&nbsp; No need to have an SMTP server working to test this, no need to have a database, no need to have an authentication method, no need to implement the interfaces or write dummy methods.</p>
<p>I am like a kid in a candy store with all these things: mocking, dependency injection, inversion of control, unit testing&#8230;&nbsp; I am loving it.</p>
<p>So what do you think?&nbsp; Is this a good way to go about it?&nbsp; Is there a better way and what is it?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Ninject: Killer IoC</title>
		<link>http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/</link>
		<comments>http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 21:11:11 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[MVC Book]]></category>
		<category><![CDATA[ninject]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/</guid>
		<description><![CDATA[In my previous post, The Best IoC Container, I decided to go with StructureMap as the framework of choice.&#160; I received a comment telling me to check out Ninject and then a day or two after, I saw Corey Gaudin&#8217;s post on using Ninject with MVC, so I decided to try it out. It wasn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post, <a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/">The Best IoC Container</a>, I decided to go with StructureMap as the framework of choice.&nbsp; I received a comment telling me to check out <a href="http://ninject.org/">Ninject</a> and then a day or two after, I saw Corey Gaudin&#8217;s <a href="http://polymorphicview.blogspot.com/2008/08/utilizing-ninject-with-aspnet-mvc.html">post</a> on using Ninject with MVC, so I decided to try it out.</p>
<p>It wasn&#8217;t too hard to get up and running in asp.net mvc.&nbsp; Corey&#8217;s post was a good starting point but I was too lazy to type all his code in, I wrote my own.&nbsp; It was pretty easy to get Ninject to work.</p>
<p>I basically created a NinjecteControllerFactory class that inherits from the DefaultControllerFactory and overrode a couple of methods.&nbsp; The class 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> NinjectControllerFactory : DefaultControllerFactory
{
    <span style="color: #0000ff">private</span> IKernel _kernel;
    <span style="color: #0000ff">public</span> NinjectControllerFactory(<span style="color: #0000ff">params</span> IModule[] modules)
    {
        _kernel = <span style="color: #0000ff">new</span> StandardKernel(modules);
    }

    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> IController GetControllerInstance(Type controllerType)
    {
        <span style="color: #0000ff">return</span> _kernel.Get(controllerType) <span style="color: #0000ff">as</span> IController;
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>Then in my Gloval.asax.cs file, I called this code to setup Ninject.</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">IModule[] modules = <span style="color: #0000ff">new</span> IModule[] { <span style="color: #0000ff">new</span> WebModule() };
ControllerBuilder.Current.SetControllerFactory(<span style="color: #0000ff">new</span> NinjectControllerFactory(modules));</pre>
</div>
<p>&nbsp;</p>
<p>The WebModule module has my configuration and 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> WebModule : StandardModule
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Load()
    {
        Bind&lt;IAppService&gt;().To&lt;AspAppService&gt;();
        Bind&lt;IEmailService&gt;().To&lt;EmailService&gt;();
        Bind&lt;IContactService&gt;().To&lt;ContactService&gt;();
        Bind&lt;IContactRepository&gt;().To&lt;SqlContactRepository&gt;();
        Bind&lt;IFormsAuthentication&gt;().To&lt;FormsAuthenticationWrapper&gt;();
        Bind&lt;MembershipProvider&gt;().ToConstant(Membership.Provider);
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>Of course, you can create as many modules as you want to configure your application and pass them in the controller Factory.</p>
<p>This code was working fine for me and then <a href="http://groups.google.com/group/ninject/browse_thread/thread/e9ded383afe77a6c?hl=en">Nate Kohari</a> mentioned that there is a Ninject.Framework.Mvc extension that allows me to easily integrate Ninject into the MVC pipeline.&nbsp; So, I decided to download the <a href="http://ninject.googlecode.com/svn/trunk/">code</a>, build it and use it.&nbsp; I initially had some issues because it was referencing a different version of the core dll, so I had to rebuild that as well.</p>
<p>I changed my Global.asax.cs file to the following:</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, 'Courier New', 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, '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> GlobalApplication : NinjectHttpApplication
{
    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute(<span style="color: #006080">"{resource}.axd/{*pathInfo}"</span>);

        routes.MapRoute(
            <span style="color: #006080">"Default"</span>,                                              <span style="color: #008000">// Route name</span>
            <span style="color: #006080">"{controller}/{action}/{id}"</span>,                           <span style="color: #008000">// URL with parameters</span>
            <span style="color: #0000ff">new</span> { controller = <span style="color: #006080">"Home"</span>, action = <span style="color: #006080">"Index"</span>, id = <span style="color: #006080">""</span> }  <span style="color: #008000">// Parameter defaults</span>
        );
    }

    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> IKernel CreateKernel()
    {
        IModule[] modules = <span style="color: #0000ff">new</span> IModule[] { <span style="color: #0000ff">new</span> AutoControllerModule(Assembly.GetExecutingAssembly()), <span style="color: #0000ff">new</span> WebModule() };
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> StandardKernel(modules);
    }
}</pre>
</div>
<p>Initially I was getting errors when navigating to an injected controller, which was fixed by add the AutoControllerModule to my modules and passing it the current assembly.</p>
<p>So far Ninject is looking great, the API is fluent and very discoverable and the <a href="http://dojo.ninject.org/wiki/display/NINJECT/Contextual+Binding">contextual binding</a> is very slick.&nbsp; So I am going to stick with it for now and see how it goes.&nbsp; The <a href="http://dojo.ninject.org/wiki/display/NINJECT/Home">documentation</a> seems pretty good and the Nate is very responsive in the <a href="http://groups.google.com/group/ninject">Google Group</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Best IoC Container?</title>
		<link>http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/</link>
		<comments>http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 12:11:26 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[autofac]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[ioc]]></category>
		<category><![CDATA[MVC Book]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[postsharp]]></category>
		<category><![CDATA[spring.net]]></category>
		<category><![CDATA[structuremap]]></category>
		<category><![CDATA[windsor castle]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/</guid>
		<description><![CDATA[As I previously mentioned in my post &#8220;The Best JavaScript Library&#8220;, I am in the process of developing an application/writing a book.  I will be using asp.net MVC and a TDD approach to the application and book.  As I have done with the JavaScript framework selection, I decided to look around and evaluate/review my options [...]]]></description>
			<content:encoded><![CDATA[<p>As I previously mentioned in my post &#8220;<a href="http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/">The Best JavaScript Library</a>&#8220;, I am in the process of developing an application/writing a book.  I will be using asp.net MVC and a <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> approach to the application and book.  As I have done with the JavaScript framework selection, I decided to look around and evaluate/review my options for an Inversion of Control (IoC) Container.</p>
<p>Naturally, my research lead me to a post by Scott Hanselman (see it <a href="http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx" target="_blank">here</a>) which lists some of the more popular IoC and dependency injection frameworks out there.</p>
<h2>Spring.Net</h2>
<p>I started of looking at <a href="http://www.springframework.net/" target="_blank">Spring.Net</a> and was very impressed by its features, <a href="http://www.springframework.net/examples.html" target="_blank">samples/tutorials</a> and <a href="http://www.springframework.net/documentation.html" target="_blank">documentation</a> but it felt like it would be too much for this project and the learning curve seemed somewhat steep.  Its configuration syntax also looked very verbose.  But if you want to learn more there is a good article over <a href="http://www.developer.com/net/csharp/article.php/10918_3722931_1" target="_blank">here</a>.</p>
<h2>Castle Windsor</h2>
<p>It looked easier to learn/use than spring.net but there <a href="http://www.castleproject.org/container/gettingstarted/index.html" target="_blank">getting started</a> section was incomplete even though Scott says that it is well documented.  So that was a little discouraging.</p>
<h2>Autofac</h2>
<p>I really liked there syntax and looked really easy to use and figure out, but the documentation was very limited.  It was just a bunch of <a href="http://code.google.com/p/autofac/w/list" target="_blank">wiki pages</a> in Google Code.  Though, they did have instructions on how to <a href="http://code.google.com/p/autofac/wiki/MvcIntegration" target="_blank">integrate it with MVC</a></p>
<h2>StructureMap</h2>
<p>Initially, I learned about structure map from a <a href="http://haacked.com/archive/2007/12/07/tdd-and-dependency-injection-with-asp.net-mvc.aspx" target="_blank">post</a> by Phil Haack&#8217;s and I kind of liked it right away.  It was easy to pick up and figure out and Phil&#8217;s example helped to get me started quickly.  I checked there <a href="http://structuremap.sourceforge.net/Default.htm" target="_blank">website</a> and it has an impressive list of features and is well documented.</p>
<h2>PostSharp</h2>
<p>PostSharp is really cool but is <strong><span style="text-decoration: underline;">not</span></strong> an IoC container.  It is a policy injector and a really easy way to do Aspect Oriented Programming (AOP).  Rather than trying to explain what exactly it does and screw it up, take a look at the &#8220;<a href="http://www.postsharp.org/about/" target="_blank">About PostSharp</a>&#8221; page.  Even better check out this &#8220;<a href="http://www.postsharp.org/about/getting-started/" target="_blank">getting started</a>&#8221; walkthrough &#8211; you will be very impressed.</p>
<h2>Must Pick One</h2>
<p>I know there is a lot more IoC containers out there (which I glanced over), but these were sufficient for me.  Initially, I thought about using AutoFac but when I started to actually use it and ran into some issues, the documentation was not helpful at all.</p>
<p>I have decided to go with StructureMap as my IoC Container and dependency injector.  I might also use PostSharp to implement logging and tracing as aspects &#8211; there is no cleaner way.</p>
<h2>Helpful Links</h2>
<p>For a good explanation of IoC Containers and the Dependency Injection patter, read this <a href="http://martinfowler.com/articles/injection.html" target="_blank">article</a> by Martin Fowler.</p>
<p>This also a good explanation that might help you <a href="http://weblogs.asp.net/sfeldman/archive/2008/02/14/understanding-ioc-container.aspx" target="_blank">understand IoC Containers</a>.</p>
<p>You should definilty take a look <a href="http://weblogs.asp.net/podwysocki/archive/2008/02/26/ioc-and-the-unity-application-block-once-again.aspx" target="_blank">Matthew Podwysocki&#8217;s</a> comparison of the different IoC containers out there and their different (or rather similar) configuration and syntax.</p>
<h2>Books you must Read</h2>
<p><a href="http://www.amazon.com/gp/product/0321268202/102-6306185-8356917?ie=UTF8&amp;tag=emadibrahim-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=0321268202" target="_blank">Applying Domain-Driven Design and Patterns</a></p>
<p>And Martin Fowler&#8217;s indispensable reference for software patterns <a href="http://www.amazon.com/gp/product/0321127420/102-6306185-8356917?ie=UTF8&amp;tag=emadibrahim-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=0321127420" target="_blank">Patterns of Enterprise Application Architecture</a></p>
<h2>Validate my Choice</h2>
<p>What do you think of my choice?  Does it really matter which one I go with?  Do you prefer a different IoC container and <strong>why</strong>?</p>
<p>Time to choose a unit testing framework&#8230; I love choices!!!</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>The Best JavaScript Library</title>
		<link>http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/</link>
		<comments>http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 19:02:40 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[MVC Book]]></category>
		<category><![CDATA[mvcbook]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[script.aculo.us]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[Web Dev]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/08/10/choosing-a-javascript-library/</guid>
		<description><![CDATA[I am in the process of creating a new web application using asp.net mvc and I am trying to choose the best JavaScript library to use.  I am pretty much sure that I am going to go with jQuery but nonetheless I wanted to review the libraries out there.  One reason is that I am [...]]]></description>
			<content:encoded><![CDATA[<p>I am in the process of creating a new web application using asp.net mvc and I am trying to choose the best JavaScript library to use.  I am pretty much sure that I am going to go with jQuery but nonetheless I wanted to review the libraries out there.  One reason is that I am writing a book and I want to be able to justify to the readers why I am going with jQuery and not the others.</p>
<h2>What do I want from my JavaScript Library?</h2>
<p>I want it to be easy (I am not a JavaScript expert)<br />
I want it to be small<br />
I want it to be fast<br />
I want it to be extensible<br />
I want good documentation and/or community support<br />
I want good/easy AJAX support<br />
I want it to be asp.net-friendly<br />
I also want it to be testable (I am using TDD for this project/book)<br />
Open Source would be nice but not required</p>
<h2>What are my options?</h2>
<p>jQuery &#8211; <a title="http://jquery.com/" href="http://jquery.com/">http://jquery.com/</a><br />
Microsoft AJAX &#8211; <a title="http://www.asp.net/ajax/" href="http://www.asp.net/ajax/">http://www.asp.net/ajax/</a><br />
Dojo &#8211; <a title="http://dojotoolkit.org/" href="http://dojotoolkit.org/">http://dojotoolkit.org/</a><br />
Prototype &#8211; <a title="http://www.prototypejs.org/" href="http://www.prototypejs.org/">http://www.prototypejs.org/</a><br />
YUI &#8211; <a title="http://developer.yahoo.com/yui/" href="http://developer.yahoo.com/yui/">http://developer.yahoo.com/yui/</a></p>
<p>This is a shortlist but if you want a more exhaustive, <a href="http://ntt.cc/2008/02/13/the-most-complete-ajax-framework-and-javascript-libraries-list.html" target="_blank">read this</a>.  I only picked these, for the simple reasons that I have heard of them and are somewhat known by the community.</p>
<h2>3 days later</h2>
<p>It&#8217;s been 3 days since I started this post and I have spent all that time playing around with these libraries&#8230;  I have gone back and forth in my opinion on which library to use.  I love jQuery&#8217;s selectors, ease of use and community support.  Porototype is also very popular and somewhat similar to jQuery; but I would take jQuery over Prototype for the simple reason that I like jQuery.  YUI (Yahoo! UI) library is beautiful looking and very comprehensive.</p>
<h3>jQuery</h3>
<p>Pros</p>
<ol>
<li>Ease to learn and use</li>
<li>Beautiful syntax (the least typing)</li>
<li>Great community support and lots of fans</li>
<li>Decent Documentation</li>
<li>I already own <a href="http://www.amazon.com/exec/obidos/ASIN/1933988355/emadibrahim-20" target="_blank">the book</a></li>
<li>QUnit test framework (<a title="http://docs.jquery.com/QUnit" href="http://docs.jquery.com/QUnit">http://docs.jquery.com/QUnit</a>)</li>
</ol>
<p>Cons</p>
<ol>
<li>UI isn&#8217;t that great</li>
<li>Library is not as full-featured without plugins</li>
<li>Plugins are somewhat inconsistent in quality depending on the author</li>
</ol>
<h3>Prototype</h3>
<p>Pros</p>
<ol>
<li>Good documentation</li>
<li>Good community support</li>
<li>Lots of books
<ol>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/1590599195/emadibrahim-20" target="_blank">Practical Prototype and script-aculo.us</a></li>
<li><a href="http://www.amazon.com/exec/obidos/ASIN/1934356018/emadibrahim-20" target="_blank">Prototype and script.aculo.us: You Never Knew JavaScript Could Do This!</a></li>
<li>and many more&#8230;</li>
</ol>
</li>
<li>Great UI with the add-on script.aculo.us (<a title="http://script.aculo.us/" href="http://script.aculo.us/">http://script.aculo.us/</a>)</li>
</ol>
<p>Cons</p>
<ol>
<li>Couldn&#8217;t find a testing framework (but I didn&#8217;t look too hard)</li>
<li>For some purely emotional reason, I prefer jQuery</li>
</ol>
<h3>YUI</h3>
<p>Pros</p>
<ol>
<li>Great UI components and styles</li>
<li>The best documented library out there, hands down.</li>
<li>Distributed hosting of JS files &#8211; the script files will be downloaded from Yahoo server, so when my site becomes a mega-hit, it will scale better.</li>
<li>Great test framework and test runner &#8211; check these <a href="http://developer.yahoo.com/yui/examples/yuitest/index.html" target="_blank">demos</a><br />
<a href="http://www.emadibrahim.com/wp-content/uploads/2008/08/image1.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" src="http://www.emadibrahim.com/wp-content/uploads/2008/08/image-thumb1.png" border="0" alt="image" width="388" height="359" /></a></li>
<li>The most complete library</li>
<li>PDF cheat sheets for every component</li>
</ol>
<p>Cons</p>
<ol>
<li>Very verbose &#8211; not as elegant as jQuery, but that&#8217;s not too hard to fix e.g. I can assign YAHOO.util.Event to a variable $E and use $E as the shorthand.</li>
</ol>
<h3>The rest</h3>
<p>I got tired of all the research and decided to wrap it up, so I glanced over Microsoft AJAX and Dojo and realized that there isn&#8217;t enough there to justify more research.</p>
<h2>The winner&#8230;</h2>
<p>When I started this post, I was pretty certain my final choice will be jQuery.  Then I started playing with Prototype and it looked really good, which kind of opened my eyes to the need to be open minded and objective.  This lead to a more objective look at YUI and the conclusion to use YUI.  I was very impressed with YUI&#8217;s look &amp; feel, extensive documentation, testing framework and the icing on the cake was the free hosting of the JavaScript libraries.  This was a really tough decision because I still love jQuery the best.</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
		<item>
		<title>Database Schema Compare &amp; Upgrade</title>
		<link>http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/</link>
		<comments>http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 13:26:43 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[migration]]></category>
		<category><![CDATA[schema]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/</guid>
		<description><![CDATA[I spent a few days playing with Ruby on Rails a while back.&#160; During the learning experience, there was one particular feature that I really liked.&#160; It was the database migration scripts that get automatically generated for you.&#160; I always wished I had something like this in the windows (asp.net) world.&#160; It turns out there [...]]]></description>
			<content:encoded><![CDATA[<p>I spent a few days playing with <a href="http://www.emadibrahim.com/tag/ruby/">Ruby on Rails</a> a while back.&#160; During the learning experience, there was one particular feature that I really liked.&#160; It was the database migration scripts that get automatically generated for you.&#160; I always wished I had something like this in the windows (asp.net) world.&#160; It turns out there is something out there and it is right there within Visual Studio.</p>
<p>When upgrading my production applications, I have always struggled with trying to update my production database schema to match the latest schema.&#160; This has always been a manual, error-prone and time consuming task.&#160; One that I always dreaded and postponed to the last minute.&#160; It usually involved crossing my figures, praying to the SQL Gods and running a hand-made migration script against the production database.&#160; I am not a DB guy, so you can imagine how much un-fun this was.</p>
<p>Long story short, you can do this with a few clicks in Visual Studio 2008.&#160; </p>
<p>Start a new schema comparison</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image001.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="218" alt="clip_image001" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image001-thumb.png" width="524" border="0" /></a></p>
<p>Select your source database (e.g. development database) and your target database (e.g. production database)</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image0014.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="238" alt="clip_image001[4]" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image0014-thumb.png" width="525" border="0" /></a></p>
<p>Click Ok.&#160; Visual studio will compare the two schemas and display the results in a grid, showing you what objects (tables, views, procs, etc&#8230;) have changed and the action you want to take.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image0016.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="148" alt="clip_image001[6]" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image0016-thumb.png" width="528" border="0" /></a></p>
<p>Select any item that has changed and you will see the differences between source and target.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image0018.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="124" alt="clip_image001[8]" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image0018-thumb.png" width="527" border="0" /></a></p>
<p>The last pane at the bottom contains the update (migration) script that will run against the target to make it identical to the source.&#160; You can quickly scan it to make sure you are not wiping out your entire production database (not recommended).</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image00112.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="219" alt="clip_image001[12]" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image00112-thumb.png" width="526" border="0" /></a></p>
<p>You can also customize the update by clicking the drop downs in the grid to customize the script</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image00114.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="89" alt="clip_image001[14]" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image00114-thumb.png" width="165" border="0" /></a></p>
<p>Once everything looks good to go, just hit the button &quot;Write Updates&quot; and you are done.</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image00116.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="77" alt="clip_image001[16]" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/clip-image00116-thumb.png" width="314" border="0" /></a></p>
<p>This has been a sore in my side for a long time and I am glad I discovered this.&#160; I am actually kind of pissed off because I have always seen that menu and never really tried to click it.&#160; Oh well!!!</p>
<p>Hmm&#8230;&#160; What to do with all the time I just freed up???</p>
<p><strong>NOTE</strong>: According to <a href="http://msdn.microsoft.com/en-us/vs2008/products/cc149003.aspx" target="_blank">msdn</a> this feature is only available in the <a href="http://msdn.microsoft.com/en-us/vsts2008/products/bb933747.aspx" target="_blank">Database Edition</a> and <a href="http://msdn.microsoft.com/en-us/vsts2008/products/bb933735.aspx" target="_blank">Team Suite</a> versions.&#160; I am running Team Suite (click Help &gt; About Microsoft Visual Studio to find out your version)</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/07/image.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="157" alt="image" src="http://www.emadibrahim.com/wp-content/uploads/2008/07/image-thumb.png" width="523" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit Test Private Methods in Visual Studio</title>
		<link>http://www.emadibrahim.com/2008/07/09/unit-test-private-methods-in-visual-studio/</link>
		<comments>http://www.emadibrahim.com/2008/07/09/unit-test-private-methods-in-visual-studio/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 17:33:42 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[mstest]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing private methods]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[vsts]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/07/09/unit-test-private-methods-in-visual-studio/</guid>
		<description><![CDATA[I am working on a feature that will let me import twitter messages to yonkly and wanted to write a test for it.&#160; The method is private and I couldn&#8217;t get the unit test to see it.&#160; I also didn&#8217;t want to use the private accessor class generate by Visual Studio because I was mocking [...]]]></description>
			<content:encoded><![CDATA[<p>I am working on a feature that will let me import twitter messages to <a href="http://yonkly.com" target="_blank">yonkly</a> and wanted to write a test for it.&#160; The method is private and I couldn&#8217;t get the unit test to see it.&#160; I also didn&#8217;t want to use the private accessor class generate by Visual Studio because I was mocking some functionality in the actual class and didn&#8217;t really fell like re-mocking it on the private accessor.</p>
<p>So, after several minutes of googling, I found several solutions that I didn&#8217;t like.&#160; <a href="http://johnhann.blogspot.com/2007/04/unit-testing-private-methods.html" target="_blank">John Hann</a> uses reflection to test private methods.&#160; <a href="http://www.codeproject.com/KB/cs/testnonpublicmembers.aspx" target="_blank">Tim Stall</a> has a similar solution at the code project.&#160; <a href="http://weblogs.asp.net/astopford/archive/2005/06/03/410248.aspx" target="_blank">Andrew Stopford</a> suggested that I don&#8217;t test private methods and use code coverage to make sure that they are being exercised.&#160; I was about to use the methods suggested by John Hann and Tim Stall to test my private methods, but then I accidentally (thanks to IntelliSense) discovered PrivateObject.</p>
<p>The PrivateObject class is part of the Team Test API.</p>
<blockquote><p>Allows test code to call methods and properties on the code under test that would be inaccessible because they are not <b>public</b>.       <br />from <font style="background-color: #e8e9dc"><a href="http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.privateobject(VS.80).aspx" target="_blank">msdn</a></font></p>
</blockquote>
<p>It turned out to be pretty easy to test private methods and the code looked 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">var myController = <span style="color: #0000ff">new</span> TwitterController();
var po = <span style="color: #0000ff">new</span> PrivateObject(myController);
var page = 1;
var count = 25;
po.Invoke(<span style="color: #006080">&quot;ImportTweets&quot;</span>, <span style="color: #0000ff">new</span> <span style="color: #0000ff">object</span>[] { page, count });</pre>
</div>
<p>&#160;</p>
<p>The code above will call the private method ImportTweets and pass it two integer parameters.&#160; This is the equivalent of calling</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">myController.ImportTweets( page, count);</pre>
</div>
<p>&#160;</p>
<p>Note that this is essentially what John Hann and Tim Stall suggested but why use extra code when PrivateObject is already available for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/07/09/unit-test-private-methods-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
