<?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>Mon, 03 May 2010 12:27:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2009/08/05/startup-asp-net-mvc-cloud-scale-deployment/" rel="bookmark" title="Permanent Link: Startup &ndash; ASP.NET MVC, Cloud Scale &amp; Deployment" >Startup &ndash; ASP.NET MVC, Cloud Scale &amp; Deployment</a></span><div class="aizattos_related_posts_excerpt">This is a second post in my startup series (first one).&#160; Warning, this one is a lot more techni...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/23/my-altnet-code/" rel="bookmark" title="Permanent Link: My ALT.NET Code" >My ALT.NET Code</a></span><div class="aizattos_related_posts_excerpt">Here is my alt.net code...  Click on the link to decode it.
IOC(SM):IOC(Nin):
MOC(MQ):
TDD(NU):T...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/10/09/marketing-your-services-using-affiliate-programs/" rel="bookmark" title="Permanent Link: Marketing Your Services Using Affiliate Programs" >Marketing Your Services Using Affiliate Programs</a></span><div class="aizattos_related_posts_excerpt">I just launched a hosted microblogging platform (aka twitter clone) at www.yonklyapp.com and right n...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/" rel="bookmark" title="Permanent Link: Ninject: Killer IoC" >Ninject: Killer IoC</a></span></li></ul></div>]]></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/31/calling-the-wordpress-xmlrpc-api-from-vbnet/" rel="bookmark" title="Permanent Link: Calling the WordPress XMLRPC API from VB.net" >Calling the WordPress XMLRPC API from VB.net</a></span><div class="aizattos_related_posts_excerpt">As an example, I will call the getCategories API method.

Add a reference to the XML-RPC.net libra...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/19/windows-live-writer-image-upload-and-wordpress-wpmu/" rel="bookmark" title="Permanent Link: Windows Live Writer Image Upload and WordPress (WPMU)" >Windows Live Writer Image Upload and WordPress (WPMU)</a></span><div class="aizattos_related_posts_excerpt">My previous post http://emad.blogstogo.com/2007/07/19/window-live-writer-and-pictures/ didn't work r...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/04/22/highlight-syntax-in-your-html-blog-posts/" rel="bookmark" title="Permanent Link: Highlight syntax in your HTML blog posts" >Highlight syntax in your HTML blog posts</a></span><div class="aizattos_related_posts_excerpt">Check out this site which lets you create HTML formatted with syntax highlighting for your code snip...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/" rel="bookmark" title="Permanent Link: Deciding Between ASP.NET MVC and WebForms" >Deciding Between ASP.NET MVC and WebForms</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/28/object-and-array-initializers-in-vb-9/" rel="bookmark" title="Permanent Link: Object and Array Initializers in VB 9" >Object and Array Initializers in VB 9</a></span></li></ul></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/12/22/a-gui-for-amazon-simpledb-simpledb-manager/" rel="bookmark" title="Permanent Link: A GUI for Amazon SimpleDb &#8211; SimpleDb Manager" >A GUI for Amazon SimpleDb &#8211; SimpleDb Manager</a></span><div class="aizattos_related_posts_excerpt">I just released a new software called SimpleDb Manager for managing your SimpleDb databases.&#160; Y...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/02/19/improve-linq-query-performance/" rel="bookmark" title="Permanent Link: Improve LINQ Query Performance" >Improve LINQ Query Performance</a></span><div class="aizattos_related_posts_excerpt">I was writing a small utility for Outlook 2007 and was using LINQ to query Outlook Tasks.&#160; This...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/04/25/use-skype-to-setup-a-wakeup-call-in-vista/" rel="bookmark" title="Permanent Link: Use Skype to setup a wakeup call in Vista" >Use Skype to setup a wakeup call in Vista</a></span><div class="aizattos_related_posts_excerpt">You can setup a wakeup call using Skype in Vista (and XP too)…
Start Task Scheduler
Click on "Crea...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/05/25/custom-sort-order-in-a-sql-statement/" rel="bookmark" title="Permanent Link: Custom sort order in a SQL statement" >Custom sort order in a SQL statement</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/12/dynamically-build-linq-queries-using-reflection/" rel="bookmark" title="Permanent Link: Dynamically Build LINQ Queries Using Reflection" >Dynamically Build LINQ Queries Using Reflection</a></span></li></ul></div>]]></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/" rel="bookmark" title="Permanent Link: The Best JavaScript Library" >The Best JavaScript Library</a></span><div class="aizattos_related_posts_excerpt">I am in the process of creating a new web application using asp.net mvc and I am trying to choose th...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/13/cmap-code-camp-2008-spring-edition/" rel="bookmark" title="Permanent Link: CMAP Code Camp 2008 Spring Edition" >CMAP Code Camp 2008 Spring Edition</a></span><div class="aizattos_related_posts_excerpt">Yesterday, I gave a talk at CMAP Code Camp about the asp.net mvc framework and AJAX using JQuery.  T...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/22/is-microsoft-buying-the-community/" rel="bookmark" title="Permanent Link: Is Microsoft Buying the Community?" >Is Microsoft Buying the Community?</a></span><div class="aizattos_related_posts_excerpt">I love the blogosphere - specially for technical information and news.&nbsp; My guess is that probab...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/09/08/client-server-side-validation-in-aspnet-mvc/" rel="bookmark" title="Permanent Link: Client &amp; Server Side Validation in ASP.NET MVC" >Client &amp; Server Side Validation in ASP.NET MVC</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/11/yonkly-open-source-twitter/" rel="bookmark" title="Permanent Link: Yonkly &#8211; Open Source Twitter" >Yonkly &#8211; Open Source Twitter</a></span></li></ul></div>]]></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. Related PostsFailed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be [...]]]></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/06/08/failed-to-generate-a-user-instance-of-sql-server-due-to-a-failure-in-starting-the-process-for-the-user-instance-the-connection-will-be-closed/" rel="bookmark" title="Permanent Link: Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed." >Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.</a></span><div class="aizattos_related_posts_excerpt">I had to uninstall and reinstall SQL server 2005 and Express on my development machine.  After the u...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2009/05/07/shortening-urls-with-bitlys-api-in-net/" rel="bookmark" title="Permanent Link: Shortening URLs with bit.ly’s API in .NET" >Shortening URLs with bit.ly’s API in .NET</a></span><div class="aizattos_related_posts_excerpt">I previously wrote about shortening URLs with TinyURL’s API but my new favorite URL shortener is h...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/04/18/shrink-long-urls-easily-in-firefox/" rel="bookmark" title="Permanent Link: Shrink long URLs easily in FireFox" >Shrink long URLs easily in FireFox</a></span><div class="aizattos_related_posts_excerpt">Almost everyone knows about http://tinyurl.com it's a great idea but I hate copying the URL, going t...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/10/tricky-aspnet-mvc-url-rewriting/" rel="bookmark" title="Permanent Link: Tricky Asp.net MVC URL Rewriting" >Tricky Asp.net MVC URL Rewriting</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/15/shortening-urls-using-tinyurl-api-in-net/" rel="bookmark" title="Permanent Link: Shortening URLs Using TinyUrl API in .net" >Shortening URLs Using TinyUrl API in .net</a></span></li></ul></div>]]></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/15/shortening-urls-using-tinyurl-api-in-net/" rel="bookmark" title="Permanent Link: Shortening URLs Using TinyUrl API in .net" >Shortening URLs Using TinyUrl API in .net</a></span><div class="aizattos_related_posts_excerpt">Here is a quick method to shorten URLs using the TinyUrl API in .net - C# and VB
C#

public stati...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/31/calling-the-wordpress-xmlrpc-api-from-vbnet/" rel="bookmark" title="Permanent Link: Calling the WordPress XMLRPC API from VB.net" >Calling the WordPress XMLRPC API from VB.net</a></span><div class="aizattos_related_posts_excerpt">As an example, I will call the getCategories API method.

Add a reference to the XML-RPC.net libra...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2009/07/05/regular-express-for-parsing-urls/" rel="bookmark" title="Permanent Link: Regular Express For Parsing URLs" >Regular Express For Parsing URLs</a></span><div class="aizattos_related_posts_excerpt">I am always looking for this one, so I am blogging it for future reference and for everyone to use. ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2009/08/18/asp-net-mvc-tdd-free-book-chapter/" rel="bookmark" title="Permanent Link: ASP.NET MVC &amp; TDD Free Book Chapter" >ASP.NET MVC &amp; TDD Free Book Chapter</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2009/07/28/product-ideas-help-me-decide/" rel="bookmark" title="Permanent Link: Product Ideas &ndash; Help Me Decide" >Product Ideas &ndash; Help Me Decide</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2009/05/07/shortening-urls-with-bitlys-api-in-net/feed/</wfw:commentRss>
		<slash:comments>7</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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/" rel="bookmark" title="Permanent Link: Ajax with ASP.NET MVC Framework Preview 2" >Ajax with ASP.NET MVC Framework Preview 2</a></span><div class="aizattos_related_posts_excerpt">This is the updated version of the Nikhil's excellent example on how to use Ajax with ASP.net MVC.
...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/" rel="bookmark" title="Permanent Link: Deciding Between ASP.NET MVC and WebForms" >Deciding Between ASP.NET MVC and WebForms</a></span><div class="aizattos_related_posts_excerpt">I am trying to create a decision flowchart for my ASP.NET MVC book and wanted to get some community ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/05/resolving-url-in-aspnet-using-tilda/" rel="bookmark" title="Permanent Link: Resolving URL in ASP.net using Tilda (~)" >Resolving URL in ASP.net using Tilda (~)</a></span><div class="aizattos_related_posts_excerpt">I have been using .net since it has come out and I just found this out (so, I am kind of pissed).  W...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2009/08/18/asp-net-mvc-tdd-free-book-chapter/" rel="bookmark" title="Permanent Link: ASP.NET MVC &amp; TDD Free Book Chapter" >ASP.NET MVC &amp; TDD Free Book Chapter</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/04/07/wordpress-themes/" rel="bookmark" title="Permanent Link: WordPress themes" >WordPress themes</a></span></li></ul></div>]]></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/" rel="bookmark" title="Permanent Link: The Best IoC Container?" >The Best IoC Container?</a></span><div class="aizattos_related_posts_excerpt">As I previously mentioned in my post "The Best JavaScript Library", I am in the process of developin...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/" rel="bookmark" title="Permanent Link: Ajax with ASP.NET MVC Framework Preview 2" >Ajax with ASP.NET MVC Framework Preview 2</a></span><div class="aizattos_related_posts_excerpt">This is the updated version of the Nikhil's excellent example on how to use Ajax with ASP.net MVC.
...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/09/07/deciding-between-aspnet-mvc-and-webforms/" rel="bookmark" title="Permanent Link: Deciding Between ASP.NET MVC and WebForms" >Deciding Between ASP.NET MVC and WebForms</a></span><div class="aizattos_related_posts_excerpt">I am trying to create a decision flowchart for my ASP.NET MVC book and wanted to get some community ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/04/unit-test-linq-to-sql-in-aspnet-mvc-with-moq/" rel="bookmark" title="Permanent Link: Unit Test Linq to Sql in ASP.Net MVC with Moq" >Unit Test Linq to Sql in ASP.Net MVC with Moq</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/05/resolving-url-in-aspnet-using-tilda/" rel="bookmark" title="Permanent Link: Resolving URL in ASP.net using Tilda (~)" >Resolving URL in ASP.net using Tilda (~)</a></span></li></ul></div>]]></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/29/is-this-better-than-constructor-injection/" rel="bookmark" title="Permanent Link: Is this Better than Constructor Injection?" >Is this Better than Constructor Injection?</a></span><div class="aizattos_related_posts_excerpt">So after I finished my post on property injection I thought of something, maybe I shouldn't use cons...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/23/my-altnet-code/" rel="bookmark" title="Permanent Link: My ALT.NET Code" >My ALT.NET Code</a></span><div class="aizattos_related_posts_excerpt">Here is my alt.net code...  Click on the link to decode it.
IOC(SM):IOC(Nin):
MOC(MQ):
TDD(NU):T...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/" rel="bookmark" title="Permanent Link: Mocking and Dependency Injection in ASP.NET MVC" >Mocking and Dependency Injection in ASP.NET MVC</a></span><div class="aizattos_related_posts_excerpt">Here is the situation, my controller constructors take multiple interfaces as parameters.&nbsp; I do...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/29/property-injection-in-aspnet-mvc-with-ninject/" rel="bookmark" title="Permanent Link: Property Injection in ASP.NET MVC with Ninject" >Property Injection in ASP.NET MVC with Ninject</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/" rel="bookmark" title="Permanent Link: The Best IoC Container?" >The Best IoC Container?</a></span></li></ul></div>]]></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.
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/23/my-altnet-code/" rel="bookmark" title="Permanent Link: My ALT.NET Code" >My ALT.NET Code</a></span><div class="aizattos_related_posts_excerpt">Here is my alt.net code...  Click on the link to decode it.
IOC(SM):IOC(Nin):
MOC(MQ):
TDD(NU):T...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/21/ninject-killer-ioc/" rel="bookmark" title="Permanent Link: Ninject: Killer IoC" >Ninject: Killer IoC</a></span><div class="aizattos_related_posts_excerpt">In my previous post, The Best IoC Container, I decided to go with StructureMap as the framework of c...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/29/property-injection-in-aspnet-mvc-with-ninject/" rel="bookmark" title="Permanent Link: Property Injection in ASP.NET MVC with Ninject" >Property Injection in ASP.NET MVC with Ninject</a></span><div class="aizattos_related_posts_excerpt">I got a design challenge with asp.net mvc.  I want to keep track of the currently logged in user in...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/27/mocking-and-dependency-injection-in-aspnet-mvc/" rel="bookmark" title="Permanent Link: Mocking and Dependency Injection in ASP.NET MVC" >Mocking and Dependency Injection in ASP.NET MVC</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/29/is-this-better-than-constructor-injection/" rel="bookmark" title="Permanent Link: Is this Better than Constructor Injection?" >Is this Better than Constructor Injection?</a></span></li></ul></div>]]></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[Featured]]></category>
		<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.
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/02/10/validating-the-page-using-javascript/" rel="bookmark" title="Permanent Link: Validating the page using JavaScript" >Validating the page using JavaScript</a></span><div class="aizattos_related_posts_excerpt">I was using an AJAX tab control and wanted to validate the tab page before the switching away to ano...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/09/09/online-book-library/" rel="bookmark" title="Permanent Link: Online Book Library" >Online Book Library</a></span><div class="aizattos_related_posts_excerpt">Here is an easy and quick way to create a virtual online book library.  Go to http://books.google.co...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/25/sitemeter-and-wordpress/" rel="bookmark" title="Permanent Link: Sitemeter and wordpress" >Sitemeter and wordpress</a></span><div class="aizattos_related_posts_excerpt">I just added sitemeter to BlogsToGo (WordPres Mu).  I have a plugins file in my mu-plugins directory...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/18/the-best-ioc-container/" rel="bookmark" title="Permanent Link: The Best IoC Container?" >The Best IoC Container?</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/14/sync-music-and-other-files-between-mac-and-pc/" rel="bookmark" title="Permanent Link: Sync music and other files between Mac and PC" >Sync music and other files between Mac and PC</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/feed/</wfw:commentRss>
		<slash:comments>45</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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/25/ruby-on-rails-for-a-net-developer/" rel="bookmark" title="Permanent Link: Ruby on Rails for a .NET Developer &#8211; Part 1" >Ruby on Rails for a .NET Developer &#8211; Part 1</a></span><div class="aizattos_related_posts_excerpt">Looking at my blog or resume, you will immediately know that I am a .net developer and my entire pro...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/08/13/iphoto-08-and-iptc/" rel="bookmark" title="Permanent Link: iPhoto &#8217;08 and IPTC" >iPhoto &#8217;08 and IPTC</a></span><div class="aizattos_related_posts_excerpt">I love the new iPhoto and its new features, specially the organize by events feature.  Very slick.  ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/07/best-wordpress-theme-ever/" rel="bookmark" title="Permanent Link: Best WordPress Theme Ever" >Best WordPress Theme Ever</a></span><div class="aizattos_related_posts_excerpt">This has got to be the best WordPress theme I have seen so far.&nbsp; The theme name is WP-Vybe and ...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/06/08/failed-to-generate-a-user-instance-of-sql-server-due-to-a-failure-in-starting-the-process-for-the-user-instance-the-connection-will-be-closed/" rel="bookmark" title="Permanent Link: Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed." >Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/09/11/microsoft-money-plus-2008-review/" rel="bookmark" title="Permanent Link: Microsoft Money Plus (2008) Review (Part 1)" >Microsoft Money Plus (2008) Review (Part 1)</a></span></li></ul></div>]]></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>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/03/11/visual-studio-2005-tips/" rel="bookmark" title="Permanent Link: Visual Studio 2005 Tips" >Visual Studio 2005 Tips</a></span><div class="aizattos_related_posts_excerpt">Check out this time-saving article on visual studio 2005 tips and tricks:  http://msdn2.microsoft.co...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/04/unit-test-linq-to-sql-in-aspnet-mvc-with-moq/" rel="bookmark" title="Permanent Link: Unit Test Linq to Sql in ASP.Net MVC with Moq" >Unit Test Linq to Sql in ASP.Net MVC with Moq</a></span><div class="aizattos_related_posts_excerpt">I have just spent the entire day playing with Moq to unit test an asp.net mvc application I am worki...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/02/geeks-use-keyboard-shortcuts/" rel="bookmark" title="Permanent Link: Geeks use Keyboard Shortcuts" >Geeks use Keyboard Shortcuts</a></span><div class="aizattos_related_posts_excerpt">If you are using the mouse to copy and paste then this site is not for you. And for Pete's sake lear...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/07/14/visual-studio-window-manager/" rel="bookmark" title="Permanent Link: Visual Studio Window Manager" >Visual Studio Window Manager</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/03/04/visual-studio-orcas-and-wpf-first-impressions/" rel="bookmark" title="Permanent Link: Visual Studio Orcas and WPF First Impressions" >Visual Studio Orcas and WPF First Impressions</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/07/09/unit-test-private-methods-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Dynamically Build LINQ Queries Using Reflection</title>
		<link>http://www.emadibrahim.com/2008/05/12/dynamically-build-linq-queries-using-reflection/</link>
		<comments>http://www.emadibrahim.com/2008/05/12/dynamically-build-linq-queries-using-reflection/#comments</comments>
		<pubDate>Mon, 12 May 2008 10:53:27 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[dynamic linq]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/05/12/dynamically-build-linq-queries-using-reflection/</guid>
		<description><![CDATA[I was recently working on a project where there was an option to export data from the database.&#160; The export function simply exported 2 hard-coded columns that were returned by a stored procedure into a tab-delimited text file.&#160; The sproc looked something like select id, name from mytable (I am over simplifying of course).&#160; I [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently working on a project where there was an option to export data from the database.&nbsp; The export function simply exported 2 hard-coded columns that were returned by a stored procedure into a tab-delimited text file.&nbsp; </p>
<p>The sproc looked something like <em>select id, name from mytable</em> (I am over simplifying of course).&nbsp; I was tasked to customize the export process, so that the user can select the columns/fields to be exported.&nbsp; There were several constraints though:</p>
<ol>
<li>Stored procedure should not change
<li>Work within the existing export framework</li>
</ol>
<p>The problem was that the existing framework used an Export function defined in a base class that all Exporters inherited.&nbsp; The Export function expected a DataReader and simply exported whatever columns were in the reader.</p>
<p>One solution was to dynamically generate the SQL string in code and then execute it.&nbsp; Another better solution was to use Dynamic LINQ.&nbsp; Wait, but there is no such thing as dynamic LINQ, you say.&nbsp; You are half right.&nbsp; There is no built-in dynamic LINQ query generation into the framework but SOME GENIOUS created a bunch of extension methods to facilitate such a miraculous feat.</p>
<h2>What is Dynamic LINQ?&nbsp; </h2>
<p>I hear you ask.&nbsp; Let&#8217;s start by looking at regular (or static) LINQ, which 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">var query = from person <span style="color: #0000ff">in</span> people
            <span style="color: #0000ff">where</span> person.City == <span style="color: #006080">"Arlington"</span>
            select <span style="color: #0000ff">new</span>
            {
                Id = person.Id,
                FirstName = person.FirstName
            };</pre>
</div>
<p>This is static because there is no way to change the <em>select</em> so that it dynamically selects different attributes from the person class based on criteria specified by the user.&nbsp; Also, for example, if you were creating an advanced search page and wanted the user to specify custom filter criteria, you won&#8217;t be able to dynamically generate the <em>where</em> conditions.</p>
<p>Dynamic LINQ, lets you do all these things and more.&nbsp; Let&#8217;s take a look at a sample dynamic LINQ query:</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">people.Where(<span style="color: #006080">"city = @0"</span>, <span style="color: #006080">"Arlington"</span>)
      .Select(<span style="color: #006080">"new (Id, FirstName, LastName, State)"</span>);</pre>
</div>
<div>&nbsp;</div>
<div>Note that the <em>select</em> and <em>where</em> are strings.&nbsp; That means you can dynamically generate the strings based on user input.</div>
<p>So, I dynamically created the select using a collection of columns.&nbsp; This is just a string collection that contains all the columns selected by the user to be exported.&nbsp; 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, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">string</span> dynamicClass = <span style="color: #0000ff">string</span>.Empty;
<span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; ColumnsToExport.Count; i++)
{
    <span style="color: #0000ff">string</span> col = ColumnsToExport[i];
    <span style="color: #0000ff">if</span> (i == 0)
    {
        dynamicClass = <span style="color: #006080">"new("</span> + col;
    }
    <span style="color: #0000ff">else</span>
    {
        dynamicClass += <span style="color: #006080">","</span> + col;
    }
    <span style="color: #0000ff">if</span> (i == ColumnsToExport.Count - 1)
    {
        dynamicClass += <span style="color: #006080">")"</span>;
    }
}</pre>
</div>
<p>All I have to do now is pass the dynamicClass string variable to the Select method.</p>
<h2>Get Dynamic</h2>
<p>To get dynamic LINQ in your project:</p>
<ol>
<li>Copy Dynamic.cs file from C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples\LinqSamples\DynamicQuery</li>
<li>If you don&#8217;t have that folder, click Help &gt; Samples in Visual Studio and follow the instructions to install the samples</li>
<li>All you have to do is import the System.Linq.Dynamic namespace wherever you want to use Dynamic LINQ.</li>
</ol>
<h2>Let&#8217;s Reflect</h2>
<p>I forgot to mention a few minor details.&nbsp; The data to be exported is coming from a view.&nbsp; The view is defined as a LINQ to SQL entity.</p>
<p>Ok, now that that&#8217;s out of the way, I needed to create a checkbox list of all the columns to allow the user to select the ones to export.&nbsp; I obviously knew that this is doable using reflection, but had to dig around for the right calls to make.&nbsp; All I needed to do is basically loop through all the properties of the LINQ to SQL entity class (the data class) for the view and bind them to the checkbox list control.&nbsp; Here is the code:</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: 300px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 257px; background-color: #f4f4f4">
<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: white; border-bottom-style: none"><span style="color: #606060">   1:</span> var props = <span style="color: #0000ff">typeof</span>(PersonData).GetProperties();</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> var columns = from prop <span style="color: #0000ff">in</span> props</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: white; border-bottom-style: none"><span style="color: #606060">   3:</span>               <span style="color: #0000ff">where</span> prop.GetCustomAttributes(<span style="color: #0000ff">typeof</span>(ExportableAttribute), <span style="color: #0000ff">false</span>).Length &gt; 0</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>               select <span style="color: #0000ff">new</span> ColumnCheckBoxItem</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: white; 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>                   DisplayName = (prop.GetCustomAttributes(<span style="color: #0000ff">typeof</span>(ExportableAttribute), <span style="color: #0000ff">false</span>)[0] <span style="color: #0000ff">as</span> ExportableAttribute).DisplayName,</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: white; border-bottom-style: none"><span style="color: #606060">   7:</span>                   PropertyName = prop.Name</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>               };</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: white; border-bottom-style: none"><span style="color: #606060">   9:</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">  10:</span> clbColumns.DisplayMember = <span style="color: #006080">"DisplayName"</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: white; border-bottom-style: none"><span style="color: #606060">  11:</span> clbColumns.ValueMember = <span style="color: #006080">"PropertyName"</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">  12:</span> <span style="color: #0000ff">foreach</span> (var column <span style="color: #0000ff">in</span> columns)</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: white; border-bottom-style: none"><span style="color: #606060">  13:</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">  14:</span>     clbColumns.Items.Add(column);</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: white; border-bottom-style: none"><span style="color: #606060">  15:</span> }</pre>
</div>
</div>
<p>Let&#8217;s look at this code in more details.&nbsp; In line 1, I get all the properties for my entity class <em>PersonData</em>.&nbsp; Then I select only the properties that have the &#8220;Exportable&#8221; attribute.&nbsp; This allowed me to filter out some properties from showing up in the checkbox list, such as timestamp and Guid columns.&nbsp; For example the zip code is defined as a property called ZIP, but I wanted it to show up in the checkbox list as &#8220;Zip Code&#8221;, so I added a property to my ExportableAttribute class called DisplayName which allowed me to customize the diplay name.&nbsp; So, the ZIP property 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, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[Column(Storage = <span style="color: #006080">"_ZIP"</span>, DbType = <span style="color: #006080">"NVarChar(100)"</span>)]
[Exportable(DisplayName = <span style="color: #006080">"Zip Code"</span>)]
<span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ZIP
{
    get
    {
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>._ZIP;
    }
    set
    {
        <span style="color: #0000ff">if</span> ((<span style="color: #0000ff">this</span>._ZIP != <span style="color: #0000ff">value</span>))
        {
            <span style="color: #0000ff">this</span>._ZIP = <span style="color: #0000ff">value</span>;
        }
    }
}</pre>
</div>
<p>&nbsp;</p>
<p>In lines 4 to 8, I create an anonymous object that contains the display name and the actually property name which will respectively correspond to DisplayMember and ValueMember in the checkbox list (lines 10 and 11).</p>
<p>Note that in line 6, I retrieve all the attributes of the property that are of type ExportableAttribute.&nbsp; This returns an array, so I use the first element to retrieve the DisplayName.&nbsp; There is no need to test for null since the where condition will ensure that only properties with the Exportable attribute are included.</p>
<p>You are also probably wondering why I didn&#8217;t just set the checkbox list DataSource to the columns collection.&nbsp; Well, I did an it didn&#8217;t bind correctly.&nbsp; I am not sure why but the for loop worked and I didn&#8217;t want to waste too much time.&nbsp; When I bound the list using the DataSource property, the checkboxes contained text like <em>{DisplayMember = &#8220;Zip Code&#8221;, PropertyName=&#8221;ZIP&#8221;}</em> instead of just containing <em>Zip Code</em>.&nbsp; Does anyone know why?</p>
<p>Finally, the ExportableAttribute 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> ExportableAttribute : Attribute
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> DisplayName;
}</pre>
</div>
<p>&nbsp;</p>
<h2>The Export</h2>
<p>Now that I have prompted the user to customize the export and I have dynamically built the LINQ query based of the user&#8217;s selection, all I have to do is export it.&nbsp; As I mentioned above, the Export method in the base class needed a DataReader, so I had to convert my LINQ expression to a DataReader.&nbsp; Here is how you get a DataReader out of a LINQ expression (note that this will work with both static and dynamic queries):</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">Database db = DatabaseFactory.CreateDatabase();
var cmd = context.GetCommand(query <span style="color: #0000ff">as</span> IQueryable))
var myReader =  db.ExecuteReader(cmd);</pre>
</div>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/02/19/improve-linq-query-performance/" rel="bookmark" title="Permanent Link: Improve LINQ Query Performance" >Improve LINQ Query Performance</a></span><div class="aizattos_related_posts_excerpt">I was writing a small utility for Outlook 2007 and was using LINQ to query Outlook Tasks.&#160; This...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/02/geeks-use-keyboard-shortcuts/" rel="bookmark" title="Permanent Link: Geeks use Keyboard Shortcuts" >Geeks use Keyboard Shortcuts</a></span><div class="aizattos_related_posts_excerpt">If you are using the mouse to copy and paste then this site is not for you. And for Pete's sake lear...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/11/30/vb9-linq-and-extension-methods-in-action/" rel="bookmark" title="Permanent Link: VB9 &#8211; LINQ and Extension Methods in Action" >VB9 &#8211; LINQ and Extension Methods in Action</a></span><div class="aizattos_related_posts_excerpt">Here is the scenario, I have collection of books and I want to get a book with a specific ISBN.&nbsp...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/12/22/a-gui-for-amazon-simpledb-simpledb-manager/" rel="bookmark" title="Permanent Link: A GUI for Amazon SimpleDb &#8211; SimpleDb Manager" >A GUI for Amazon SimpleDb &#8211; SimpleDb Manager</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/04/unit-test-linq-to-sql-in-aspnet-mvc-with-moq/" rel="bookmark" title="Permanent Link: Unit Test Linq to Sql in ASP.Net MVC with Moq" >Unit Test Linq to Sql in ASP.Net MVC with Moq</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/05/12/dynamically-build-linq-queries-using-reflection/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails Crumbs &#8211; Part 1</title>
		<link>http://www.emadibrahim.com/2008/05/02/ruby-on-rails-crumbs-part-1/</link>
		<comments>http://www.emadibrahim.com/2008/05/02/ruby-on-rails-crumbs-part-1/#comments</comments>
		<pubDate>Fri, 02 May 2008 11:03:28 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[Web Dev]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/05/02/ruby-on-rails-crumbs-part-1/</guid>
		<description><![CDATA[If you have been following along, I blogged previously about my experience trying Ruby on Rails as a .net developer (here and here).&#160; I quickly realized how different things are and that I desperately need some help, so I got myself the beta PDF version of Agile Web Development with Rails and Rails Recipes.&#160; Both [...]]]></description>
			<content:encoded><![CDATA[<p>If you have been following along, I blogged previously about my experience trying Ruby on Rails as a .net developer (<a href="http://www.emadibrahim.com/2008/04/25/ruby-on-rails-for-a-net-developer/">here</a> and <a href="http://www.emadibrahim.com/2008/04/30/ruby-on-rails-for-a-net-developer-part-2/">here</a>).&nbsp; I quickly realized how different things are and that I desperately need some help, so I got myself the beta PDF version of <a href="http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition" target="_blank">Agile Web Development with Rails</a> and <a href="http://www.amazon.com/gp/product/0977616606/104-1693550-6935154?ie=UTF8&amp;tag=dotnetfactory-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=0977616606">Rails Recipes</a>.&nbsp; Both great books but not Rails Recipes doesn&#8217;t cover rails latest version &#8211; 2.0.&nbsp; </p>
<p>As I read through these books, I realized there are lots of things I need to keep track of, that is lots of crumbs.&nbsp; This series of blog posts are a way for me to keep track of these crumbs as well as share them with others.&nbsp; I don&#8217;t plan to explain everything in detail (because I can&#8217;t).&nbsp; This is more of a quick reference to remind me of features and things that I read about in the books because there is no FREAKING INTELLISENSE in rails.&nbsp; You will realize how valuable Intellisense is when you don&#8217;t have it.</p>
<p>These crumbs are not in any specific order.&nbsp; For the most part, they are in the order I discovered them.</p>
<h3>content_tag</h3>
<p>Use it to surround a block of text with a tag.&nbsp; The code below creates &lt;b&gt;make me bold&lt;/b&gt;</p>
<blockquote><p>content_tag (:b, &#8220;make me bold&#8221;) </p>
</blockquote>
<h3>&amp;block</h3>
<p>I can&#8217;t find a good explanation of this but when you call a method from your views like this</p>
<blockquote><p>&lt;% my_foo() do %&gt;<br />&nbsp; [content]<br />&lt;% end %&gt;</p>
</blockquote>
<p>And the method is defined like this</p>
<blockquote><p>def my_foo(&amp;block)<br />end</p>
</blockquote>
<p>Then the &amp;block parameter will contain all the text that is wrapped by this method in the view.&nbsp; In the above example &amp;block would contain <em>[content]</em></p>
<h3>blind_down</h3>
<p>Use to generate a blind down effect in JavaScript to make hiding an element smoother than just setting display to none.&nbsp; In you rjs template, write:</p>
<blockquote><p>page[:element_id_to_blind].visual_effect :blind_down</p>
</blockquote>
<h3>request.xhr?</h3>
<p>This returns true if the request is xhr i.e. an AJAX request.&nbsp; I like the trailing question mark.&nbsp; In .net, it would be named IsXhr.</p>
<h3>has_many</h3>
<p>Use in your model to create a relationship.&nbsp; For example, an article model class might look like this</p>
<blockquote><p>class Article &lt; ActiveRecord::Base<br />has_many :categories<br />end</p>
</blockquote>
<p>NOTE: your category model class name is Category but the has_many uses the actual table name which categories.&nbsp; This is so weird for me &#8211; no compiler errors, no warnings, nothing.&nbsp; I know, I know, convention over configuration.</p>
<h3>belongs_to</h3>
<p>Same idea as has_many but in reverse, so the category model class would look like this</p>
<blockquote><p>class Category &lt; ActiveRecord::Base<br />belongs_to :articles<br />end</p>
</blockquote>
<h3>button_to</h3>
<p>A helper method that generates an HTML form containing a button that submits to a URL when clicked.&nbsp; For example:</p>
<blockquote><p>button_to &#8220;Edit&#8221;, :action =&gt; &#8216;edit&#8217;, :id =&gt; 3</p>
</blockquote>
<p>Creates a form with an Edit button that submits to the edit action and pass 3 as an id parameter.&nbsp; If we were editing an article, the generated html looks like this:</p>
<blockquote><p>&lt;form class=&#8221;button-to&#8221; action=&#8221;/articles/edit/3&#8243; method=&#8221;post&#8221;&gt;<br />&nbsp;&nbsp;&nbsp; &lt;div&gt;&lt;input type=&#8221;submit&#8221; value=&#8221;Edit&#8221; /&gt;&lt;/div&gt;<br />&lt;/form&gt;</p>
</blockquote>
<p>More crumbs to come &#8211; stay tuned.&nbsp; Also feel free to add your own crumbs to the comments, it might help someone.</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/30/ruby-on-rails-for-a-net-developer-part-2/" rel="bookmark" title="Permanent Link: Ruby on Rails for a .NET Developer &#8211; Part 2" >Ruby on Rails for a .NET Developer &#8211; Part 2</a></span><div class="aizattos_related_posts_excerpt">Ok, so I thought the whole scaffolding thing was so easy and I would be done in a few minutes. I was...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/25/ruby-on-rails-for-a-net-developer/" rel="bookmark" title="Permanent Link: Ruby on Rails for a .NET Developer &#8211; Part 1" >Ruby on Rails for a .NET Developer &#8211; Part 1</a></span><div class="aizattos_related_posts_excerpt">Looking at my blog or resume, you will immediately know that I am a .net developer and my entire pro...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/" rel="bookmark" title="Permanent Link: Database Schema Compare &amp; Upgrade" >Database Schema Compare &amp; Upgrade</a></span><div class="aizattos_related_posts_excerpt">I spent a few days playing with Ruby on Rails a while back.&#160; During the learning experience, th...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/06/27/free-it-consultation-in-the-dc-metro-area/" rel="bookmark" title="Permanent Link: Free IT Consultation in the DC Metro Area" >Free IT Consultation in the DC Metro Area</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/09/22/book-how-to-win-friends-and-influence-people/" rel="bookmark" title="Permanent Link: How to Win Friends and Influence People" >How to Win Friends and Influence People</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/05/02/ruby-on-rails-crumbs-part-1/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails for a .NET Developer &#8211; Part 2</title>
		<link>http://www.emadibrahim.com/2008/04/30/ruby-on-rails-for-a-net-developer-part-2/</link>
		<comments>http://www.emadibrahim.com/2008/04/30/ruby-on-rails-for-a-net-developer-part-2/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 13:30:25 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/?p=406</guid>
		<description><![CDATA[Ok, so I thought the whole scaffolding thing was so easy and I would be done in a few minutes. I was so wrong. Nothing seemed to work right. I would call ruby script/generate scaffold Tag and it will generate all the files but the views were empty. Hmm&#8230; This is exactly what has been [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so I thought the whole scaffolding thing was so easy and I would be done in a few minutes. I was so wrong. Nothing seemed to work right. I would call ruby script/generate scaffold Tag and it will generate all the files but the views were empty. Hmm&#8230; This is exactly what has been done in all the demos and tutorials. Apparently, things have changed in Rails 2.0 and I had to call this instead script/generate scaffold Tag tag:string.</p>
<p>One thing I don&#8217;t like so far is the lack of good documentation for Rails 2.0 and there aren&#8217;t any books out there. I went ahead and bought me of <a href="http://www.amazon.com/gp/product/0977616630/102-3964063-2035311?ie=UTF8&amp;tag=dotnetfactory-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=0977616630" target="_blank">Agile Web Development with Rails</a> which has been updated for 2.0 but you can only get as beta from <a href="http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition" target="_blank">The Pragmatic Bookshelf</a>. I bought the PDF version and have been reading it for the past few days.</p>
<p>It occurred to me that before I can get too deep into Rails, I need to learn a little about Ruby.  After all the syntax is very different from  C# or vb.net. Everyone claims that it is an easier language to understand but I think that is very subjective. Spanish might be easier than Chinese but if you are Chinese you might not think so.</p>
<p>With that said, in this post I am going to list a few things I have learned over the past few days about Ruby Syntax and how it compares to C# syntax.</p>
<h2>Comments</h2>
<p><strong>C#</strong></p>
<blockquote><p>//this is a one line comment</p>
<p>/* This is a multi-line<br />
comment<br />
*/</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p># this is a one line comment</p>
<p>=begin<br />
this is a mult-line<br />
comment<br />
=end</p></blockquote>
<p><strong>Winner</strong>: C#</p>
<h2>Class Definition</h2>
<p><strong>C#</strong></p>
<blockquote><p>class Car : Vehicle<br />
{<br />
public Car()<br />
{<br />
}<br />
}</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>class Car &lt; Vehicle<br />
def initialize<br />
end<br />
end</p></blockquote>
<p><strong>Winner</strong>: tie but if you hate curly brackets then Ruby wins</p>
<h2>Method Definition</h2>
<p><strong>C#</strong></p>
<blockquote><p>public int GetYear()<br />
{<br />
}</p>
<p>public static int GetYear(string carName)<br />
{<br />
}</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>def GetYear<br />
end</p>
<p>def self.GetYear(carName)<br />
end</p></blockquote>
<p>Note: You can use the class name instead of <em>self</em> to make the method static. Also, in Ruby they are called class methods and not static methods. Come to think of it, that makes sense, since the method is defined for the class and not the instance or the object.</p>
<p><strong>Winner</strong>: Ruby</p>
<h2>Variables</h2>
<p><span style="text-decoration: underline;"><strong>Local variables</strong></span></p>
<p><strong>C#</strong></p>
<blockquote><p>int aNumber = 4; //declared inside a method</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>aNumber=4 # no declaration needed just use it inside a method</p></blockquote>
<p><span style="text-decoration: underline;"><strong>Instance variables (fields)</strong></span></p>
<p><strong>C#</strong></p>
<blockquote><p>int aNumber; //declared inside a class not a method</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>@aNumber #no declaration needed just use it anywhere</p></blockquote>
<p><span style="text-decoration: underline;"><strong>Static variables (fields)</strong></span></p>
<p><strong>C#</strong></p>
<blockquote><p>static int aNumber; //declared inside a class not a method</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>@@aNumber #no declaration needed just it anywhere</p></blockquote>
<h2>Control Structure</h2>
<p><strong><span style="text-decoration: underline;">if</span></strong></p>
<p><strong>C#</strong></p>
<blockquote><p>if (aCondition)<br />
{<br />
}<br />
else<br />
{<br />
}</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>if aCondition then<br />
else<br />
end</p></blockquote>
<p><strong><span style="text-decoration: underline;">unless</span></strong></p>
<p><strong>C#</strong></p>
<p>There is no <em>unless</em> in C# but the same logic can be written like this:</p>
<blockquote><p>if (!aCondition)<br />
{<br />
}<br />
else<br />
{<br />
}</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>unless isBlue</p>
<p>else</p>
<p>end</p></blockquote>
<p><strong><span style="text-decoration: underline;">case</span></strong></p>
<p><strong>C#</strong></p>
<blockquote><p>switch (year)<br />
{<br />
case 1960:<br />
case 1961:<br />
case 1962:<br />
&#8230;<br />
case 1969:<br />
break;<br />
case 1970:<br />
&#8230;<br />
case 1979:<br />
break;<br />
default:<br />
}</p></blockquote>
<p>The code above has been truncated for brevity because you cannot do ranges in C# as easy you can in Ruby</p>
<p><strong>Ruby</strong></p>
<blockquote><p>case year<br />
when 1960 .. 1969<br />
when 1970 .. 1979<br />
else<br />
end</p></blockquote>
<p>The above Ruby code is complete and not truncated.  The express 1960 .. 1969 specifies a range.</p>
<p><strong><span style="text-decoration: underline;">while</span></strong></p>
<p><strong>C#</strong></p>
<blockquote><p>while (aCondition)<br />
{<br />
}</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>while aCondition<br />
end</p></blockquote>
<p><strong><span style="text-decoration: underline;">until</span></strong></p>
<p><strong>C#</strong></p>
<p>There is no <em>until</em> in C# but the same logic can be expressed like this:</p>
<blockquote><p>while (!aCondition)<br />
{<br />
}</p></blockquote>
<p><strong>Ruby</strong></p>
<blockquote><p>until aCondition<br />
end</p></blockquote>
<p>This is not a comparison of language features, it is just a comparison of the syntactical differences of *some* of both languages.  This is *not* an exhaustive list and is not meant to be &#8211; a simple Google search will get you thousands of pages of syntax goodies for either language.</p>
<p>So far, I can&#8217;t say I love Ruby&#8217;s syntax but if I were new to programming, I would say it looks more readable and easier to understand.  Being a C#/VB developer for so long, I am a little confused and uncomfortable with the Ruby syntax.  But, the facts that I have used VB for the past 10 or so years and that I hate semi-colons and curly brackets in C#, it is not that bad.</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/02/ruby-on-rails-crumbs-part-1/" rel="bookmark" title="Permanent Link: Ruby on Rails Crumbs &#8211; Part 1" >Ruby on Rails Crumbs &#8211; Part 1</a></span><div class="aizattos_related_posts_excerpt">If you have been following along, I blogged previously about my experience trying Ruby on Rails as a...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/25/ruby-on-rails-for-a-net-developer/" rel="bookmark" title="Permanent Link: Ruby on Rails for a .NET Developer &#8211; Part 1" >Ruby on Rails for a .NET Developer &#8211; Part 1</a></span><div class="aizattos_related_posts_excerpt">Looking at my blog or resume, you will immediately know that I am a .net developer and my entire pro...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/06/27/free-it-consultation-in-the-dc-metro-area/" rel="bookmark" title="Permanent Link: Free IT Consultation in the DC Metro Area" >Free IT Consultation in the DC Metro Area</a></span><div class="aizattos_related_posts_excerpt">I have been busy working on several things since I quit my job.&nbsp; Things like the open source tw...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/02/10/help-debugging-ajax-and-javascript/" rel="bookmark" title="Permanent Link: Help debugging AJAX and JavaScript" >Help debugging AJAX and JavaScript</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/" rel="bookmark" title="Permanent Link: Database Schema Compare &amp; Upgrade" >Database Schema Compare &amp; Upgrade</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/04/30/ruby-on-rails-for-a-net-developer-part-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails for a .NET Developer &#8211; Part 1</title>
		<link>http://www.emadibrahim.com/2008/04/25/ruby-on-rails-for-a-net-developer/</link>
		<comments>http://www.emadibrahim.com/2008/04/25/ruby-on-rails-for-a-net-developer/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 12:18:19 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[favewares]]></category>
		<category><![CDATA[My Projects]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/?p=404</guid>
		<description><![CDATA[Looking at my blog or resume, you will immediately know that I am a .net developer and my entire programming career has been Microsoft-centric.&#160; I have been wanting to play with Ruby on Rails due to its inescapable popularity and I was actually surprised to learn that the language itself (Ruby) was developed in the [...]]]></description>
			<content:encoded><![CDATA[<p>Looking at my blog or resume, you will immediately know that I am a .net developer and my entire programming career has been Microsoft-centric.&nbsp; I have been wanting to play with Ruby on Rails due to its inescapable popularity and I was actually surprised to learn that the language itself (Ruby) was developed in the early 90&#8242;s&#8230;&nbsp; So it&#8217;s not really new.&nbsp; Ruby on Rails is a framework that sits on top of it&#8230;</p>
<p>NOTE: forgive me if any of this is wrong, my RoR experience as of right now is 35 minutes&#8230;</p>
<p>Anyway, this post is not about the history of Ruby or a comparative analysis of which language or platform is better&#8230;&nbsp; It is just a part of a series of blog posts that I will be writing as I learn more about RoR.&nbsp; Everything is written from my perspective i.e. a 100% windows user and a .net developer.&nbsp; So I am in foreign lands here.</p>
<h2>The Setup</h2>
<p>My current setup is a Mac Mini running Leopard with</p>
<p><a title="Photo Sharing" href="http://www.flickr.com/photos/eibrahim/2438791241/"><img height="379" alt="(null)" src="http://farm4.static.flickr.com/3178/2438791241_94c0baf47d.jpg" width="307"></a></p>
<h2>The Environment</h2>
<p>So after downloading all the RoR stuff from this <a href="http://www.rubyonrails.org/down">website</a>, I followed the steps and managed to create an empty application like this:</p>
<ol>
<li>Open a terminal window&nbsp;
<li>Run rails /users/emad/code/favewares
<li>Run cd path/to/your/new/application
<li>Run ruby script/server </li>
</ol>
<p>Then I opened http://localhost:3000 in my browser and got to the welcome page shown below.</p>
<p><a title="Photo Sharing" href="http://www.flickr.com/photos/eibrahim/2438822307/"><img style="border-right: black 1px solid; border-top: black 1px solid; border-left: black 1px solid; border-bottom: black 1px solid" height="482" alt="RoR Welcome aboard" src="http://farm4.static.flickr.com/3157/2438822307_07e9d578e5.jpg" width="450"></a></p>
<p>Time to create the database, so I had to go download <a href="http://dev.mysql.com/downloads/">MySQL</a> and install it which was surprisingly very straight forward:</p>
<ol>
<li>Double click the MySQL installer
<li>Double click the MySQL Startup Item installer </li>
</ol>
<p>Then I ran into problems trying to get things going and quickly realized that being a windows user, I needed a GUI tool.&nbsp; So I downloaded one from the MySQL website and installed it which was also pretty straight forward.&nbsp; Until I tried to connect and got the error message &#8220;Can&#8217;t connect to local MySQL server through socket &#8216;/tmp/mysql.sock&#8217; (2)&#8221;</p>
<p><a title="Photo Sharing" href="http://www.flickr.com/photos/eibrahim/2439721528/"><img height="202" alt="mysql connection error" src="http://farm3.static.flickr.com/2019/2439721528_937a601c14.jpg" width="490"></a></p>
<p>After about an hour of trying different things and reading all sort of blogs and forums, I finally decided to restart my mac and voila, it worked&#8230;&nbsp; At least Windows tells me that I need to restart.</p>
<p>Ok, so I got the GUI MySQL administrator up.&nbsp; I added a database called favewares_development (to follow RoR naming conventions) and created some tables.</p>
<p><a title="Photo Sharing" href="http://www.flickr.com/photos/eibrahim/2439047093/"><img style="border-right: black 1px solid; border-top: black 1px solid; border-left: black 1px solid; border-bottom: black 1px solid" height="356" alt="mysql administrator" src="http://farm3.static.flickr.com/2009/2439047093_63a0999615.jpg" width="407"></a></p>
<p>If you read anything about Rails you will end up reading the phrase &#8220;convention over configuration&#8221; a bazillion times; so here is a quick note on database naming conventions:</p>
<ol>
<li>Database name = appname_development, appname_test, appname_production
<li>Table name = plural e.g. authors, wares, stories, categories, etc&#8230;
<li>Primary key name = id
<li>Foreign keys = singularOfForeign_id e.g. category_id, author_id, etc&#8230;
<li>Many to many tables = tablea_tableb in alphabetical order e.g. items_orders, classes_students </li>
</ol>
<p>There is more information about Rails and Ruby naming conventions <a href="http://itsignals.cascadia.com.au/?p=7">over here</a>.</p>
<p>Database is now created, let&#8217;s edit the code.&nbsp; Oops, no code editor &#8211; damn it.&nbsp; I went to <a href="http://macromates.com/">textmate</a> and downloaded their 30-day trial.</p>
<p>Once I was done creating my tables, I generated the schema.rb file in the project&#8217;s db folder in textmate by clicking Bundles &gt; Ruby on Rails &gt; Database &gt; Dump DB to schema.rb</p>
<p><a title="Photo Sharing" href="http://www.flickr.com/photos/eibrahim/2439054647/"><img style="border-right: black 1px solid; border-top: black 1px solid; border-left: black 1px solid; border-bottom: black 1px solid" height="202" alt="Dumb db to schema.rb" src="http://farm4.static.flickr.com/3167/2439054647_32bbe8d3f8.jpg" width="500"></a></p>
<p>My scheam.rb file looked like this:</p>
<p><a title="Photo Sharing" href="http://www.flickr.com/photos/eibrahim/2439061717/"><img style="border-right: black 1px solid; border-top: black 1px solid; border-left: black 1px solid; border-bottom: black 1px solid" height="427" alt="schema.rb" src="http://farm3.static.flickr.com/2038/2439061717_e193aaa069.jpg" width="441"></a></p>
<p>Although I didn&#8217;t really have much so far, my brain had had enough of being outside its comfort zone &#8211; I miss windows&#8230;&nbsp; </p>
<p>Stay tuned for the next part of this post.</p>
<p>[update] Part 2 is available <a href="http://www.emadibrahim.com/2008/04/30/ruby-on-rails-for-a-net-developer-part-2/" target="_blank">here</a>.</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/02/ruby-on-rails-crumbs-part-1/" rel="bookmark" title="Permanent Link: Ruby on Rails Crumbs &#8211; Part 1" >Ruby on Rails Crumbs &#8211; Part 1</a></span><div class="aizattos_related_posts_excerpt">If you have been following along, I blogged previously about my experience trying Ruby on Rails as a...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/30/ruby-on-rails-for-a-net-developer-part-2/" rel="bookmark" title="Permanent Link: Ruby on Rails for a .NET Developer &#8211; Part 2" >Ruby on Rails for a .NET Developer &#8211; Part 2</a></span><div class="aizattos_related_posts_excerpt">Ok, so I thought the whole scaffolding thing was so easy and I would be done in a few minutes. I was...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/06/27/free-it-consultation-in-the-dc-metro-area/" rel="bookmark" title="Permanent Link: Free IT Consultation in the DC Metro Area" >Free IT Consultation in the DC Metro Area</a></span><div class="aizattos_related_posts_excerpt">I have been busy working on several things since I quit my job.&nbsp; Things like the open source tw...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/02/10/help-debugging-ajax-and-javascript/" rel="bookmark" title="Permanent Link: Help debugging AJAX and JavaScript" >Help debugging AJAX and JavaScript</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/" rel="bookmark" title="Permanent Link: Database Schema Compare &amp; Upgrade" >Database Schema Compare &amp; Upgrade</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/04/25/ruby-on-rails-for-a-net-developer/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Ajax with ASP.NET MVC Framework Preview 2</title>
		<link>http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/</link>
		<comments>http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/#comments</comments>
		<pubDate>Mon, 31 Mar 2008 13:27:25 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[Web Dev]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/?p=379</guid>
		<description><![CDATA[This is the updated version of the Nikhil&#8217;s excellent example on how to use Ajax with ASP.net MVC. I commented all the changes made in the code and prefixed them with //emad. So just search for //emad and you will see all the changes I made&#8230; Here is a quick list of the changes I [...]]]></description>
			<content:encoded><![CDATA[<p>This is the updated version of the Nikhil&#8217;s excellent <a href="http://www.nikhilk.net/Ajax-MVC.aspx" target="_blank">example</a> on how to use Ajax with ASP.net MVC.</p>
<p>I commented all the changes made in the code and prefixed them with //emad.  So just search for //emad and you will see all the changes I made&#8230;  Here is a quick list of the changes I made to make this compile and run on MVC Preview 2:</p>
<ol>
<li>Removed the [ControllerAction] tags &#8211; they are no longer needed.</li>
<li>Updated the Route Table creation in the global.asax.cs file to work with the new format</li>
<li>Updated the Ajax framework project to use the new classes
<ol>
<li>HttpContextBase instead of IHttpContext</li>
<li>HttpRequestBased instead of IHttpRequest</li>
<li>HttpResponseBase instead of IHttpResponse</li>
</ol>
</li>
<li>Updated the AjaxViewContext constructor to match ViewContext constructor</li>
<li>Changed the web.config to work with the new version (copied and pasted from another preview 2 project)</li>
</ol>
<p>Note: Test project was not updated and will not compile &#8211; sorry, I didn&#8217;t have time to do it.</p>
<p>Here is the updated code: <a href="http://www.emadibrahim.com/wp-content/uploads/2008/03/tasklist.zip">tasklist.zip</a></p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/06/10/featured-on-aspnet/" rel="bookmark" title="Permanent Link: Featured on ASP.NET" >Featured on ASP.NET</a></span><div class="aizattos_related_posts_excerpt">One of my sites (www.kbshortcuts.com) is featured on Microsoft's AJAX Showcase at http://ajax.asp.ne...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/30/upgrading-yonkly-to-aspnet-mvc-preview-3/" rel="bookmark" title="Permanent Link: Upgrading Yonkly to ASP.NET MVC Preview 3" >Upgrading Yonkly to ASP.NET MVC Preview 3</a></span><div class="aizattos_related_posts_excerpt">I just spent this morning upgrading the Yonkly code to work with the newly released ASP.NET MVC Fram...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/11/yonkly-open-source-twitter/" rel="bookmark" title="Permanent Link: Yonkly &#8211; Open Source Twitter" >Yonkly &#8211; Open Source Twitter</a></span><div class="aizattos_related_posts_excerpt">[update: Yonkly source code is now public]

[update 2: I just created www.isweat.com using the yon...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/08/10/the-best-javascript-library/" rel="bookmark" title="Permanent Link: The Best JavaScript Library" >The Best JavaScript Library</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/13/cmap-code-camp-2008-spring-edition/" rel="bookmark" title="Permanent Link: CMAP Code Camp 2008 Spring Edition" >CMAP Code Camp 2008 Spring Edition</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/03/31/ajax-with-aspnet-mvc-framework-preview-2/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Set a Clip in Code-Behind</title>
		<link>http://www.emadibrahim.com/2008/03/17/set-a-clip-in-code-behind/</link>
		<comments>http://www.emadibrahim.com/2008/03/17/set-a-clip-in-code-behind/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 00:36:46 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/03/17/set-a-clip-in-code-behind/</guid>
		<description><![CDATA[You can use the Clip property on any item to clip its display area. For example if you have this two items rectBlue and rectRed like this And you want to animate rectRed by sliding it out from the right side, without clipping it would look like this But if you set the Clip property [...]]]></description>
			<content:encoded><![CDATA[<p>You can use the Clip property on any item to clip its display area.  For example if you have this two items rectBlue and rectRed like this</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/03/windowslivewritersetaclipincodebehind-120c0image-2.png"><img src="http://www.emadibrahim.com/wp-content/uploads/2008/03/windowslivewritersetaclipincodebehind-120c0image-thumb.png" style="border: 0px none " alt="image" border="0" height="123" width="222" /></a></p>
<p>And you want to animate rectRed by sliding it out from the right side, without clipping it would look like this</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/03/windowslivewritersetaclipincodebehind-120c0image-4.png"><img src="http://www.emadibrahim.com/wp-content/uploads/2008/03/windowslivewritersetaclipincodebehind-120c0image-thumb-1.png" style="border: 0px none " alt="image" border="0" height="123" width="250" /></a></p>
<p>But if you set the Clip property on the blue rectangle to equal its dimension then it would look like this</p>
<p><a href="http://www.emadibrahim.com/wp-content/uploads/2008/03/windowslivewritersetaclipincodebehind-120c0image-6.png"><img src="http://www.emadibrahim.com/wp-content/uploads/2008/03/windowslivewritersetaclipincodebehind-120c0image-thumb-2.png" style="border: 0px none " alt="image" border="0" height="123" width="213" /></a></p>
<p>You can set the clip in code-behind like this:</p>
<blockquote>
<pre class="code"><span style="color: #2b91af">RectangleGeometry</span> recClip =  <span style="color: #0000ff">new</span> <span style="color: #2b91af">RectangleGeometry</span>();
recClip.Rect = <span style="color: #0000ff">new</span> <span style="color: #2b91af">Rect</span>(0, 0, recBlue.ActualWidth, recBlue.ActualHeight);
pnlContent.Clip = recClip;</pre>
</blockquote>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This works in both Silverlight and WPF.</p>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/13/silverlight-tip-stretch-canvas-to-fill-the-screen/" rel="bookmark" title="Permanent Link: Silverlight Tip: Stretch Canvas to Fill the Screen" >Silverlight Tip: Stretch Canvas to Fill the Screen</a></span><div class="aizattos_related_posts_excerpt">I am not sure if this is a bug or by-design but Canvases don't stretch the way I expect them to - th...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/07/10/database-schema-compare-upgrade/" rel="bookmark" title="Permanent Link: Database Schema Compare &amp; Upgrade" >Database Schema Compare &amp; Upgrade</a></span><div class="aizattos_related_posts_excerpt">I spent a few days playing with Ruby on Rails a while back.&#160; During the learning experience, th...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/05/29/yonkly-source-code/" rel="bookmark" title="Permanent Link: Yonkly Source Code" >Yonkly Source Code</a></span><div class="aizattos_related_posts_excerpt">[UPDATE] Source code is now available in codeplex at http://www.codeplex.com/yonkly

I apologize f...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/04/07/adding-code-to-the-top-of-every-post-in-wordpress/" rel="bookmark" title="Permanent Link: Adding code to the top of every post in WordPress" >Adding code to the top of every post in WordPress</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/14/silverlight-templatebinding-throwing-an-error/" rel="bookmark" title="Permanent Link: Silverlight: TemplateBinding Throwing an Error" >Silverlight: TemplateBinding Throwing an Error</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/03/17/set-a-clip-in-code-behind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight: TemplateBinding Throwing an Error</title>
		<link>http://www.emadibrahim.com/2008/03/14/silverlight-templatebinding-throwing-an-error/</link>
		<comments>http://www.emadibrahim.com/2008/03/14/silverlight-templatebinding-throwing-an-error/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 21:24:02 +0000</pubDate>
		<dc:creator>Emad Ibrahim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.emadibrahim.com/2008/03/14/silverlight-templatebinding-throwing-an-error/</guid>
		<description><![CDATA[This drove me crazy for about half an hour&#8230;&#160; I attached this style to a button &#60;Style x:Key="NavButtonStyle" TargetType="Button"&#62; &#60;Setter Property="Opacity" Value="0.8"&#62;&#60;/Setter&#62; &#60;Setter Property="Template"&#62; &#60;Setter.Value&#62; &#60;ControlTemplate&#62; &#60;Grid x:Name="RootElement" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&#62; &#60;ContentPresenter Content="{TemplateBinding Content}"/&#62; &#60;/Grid&#62; &#60;/ControlTemplate&#62; &#60;/Setter.Value&#62; &#60;/Setter&#62; &#60;/Style&#62; Everything compiled fine but the page wouldn&#8217;t look right and expression blend would not display it correctly [...]]]></description>
			<content:encoded><![CDATA[<p>This drove me crazy for about half an hour&#8230;&nbsp; </p>
<p>I attached this style to a button </p>
<blockquote><pre class="code"><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Style</span><span style="color: rgb(255,0,0)"> x</span><span style="color: rgb(0,0,255)">:</span><span style="color: rgb(255,0,0)">Key</span><span style="color: rgb(0,0,255)">="NavButtonStyle"</span><span style="color: rgb(255,0,0)"> TargetType</span><span style="color: rgb(0,0,255)">="Button"&gt;
</span><span style="color: rgb(163,21,21)">    </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(255,0,0)"> Property</span><span style="color: rgb(0,0,255)">="Opacity"</span><span style="color: rgb(255,0,0)"> Value</span><span style="color: rgb(0,0,255)">="0.8"&gt;&lt;/</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">    </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(255,0,0)"> Property</span><span style="color: rgb(0,0,255)">="Template"&gt;
</span><span style="color: rgb(163,21,21)">        </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Setter.Value</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">            </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">ControlTemplate</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">                </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Grid</span><span style="color: rgb(255,0,0)"> x</span><span style="color: rgb(0,0,255)">:</span><span style="color: rgb(255,0,0)">Name</span><span style="color: rgb(0,0,255)">="RootElement"</span><span style="color: rgb(255,0,0)"> </span></pre>
<pre class="code"><span style="color: rgb(255,0,0)">                     HorizontalAlignment</span><span style="color: rgb(0,0,255)">="Stretch"</span><span style="color: rgb(255,0,0)"> </span></pre>
<pre class="code"><span style="color: rgb(255,0,0)">                     VerticalAlignment</span><span style="color: rgb(0,0,255)">="Stretch"&gt;
</span><span style="color: rgb(163,21,21)">                    </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">ContentPresenter</span><span style="color: rgb(255,0,0)"> </span></pre>
<pre class="code"><span style="color: rgb(255,0,0)">                        Content</span><span style="color: rgb(0,0,255)">="{</span><span style="color: rgb(163,21,21)">TemplateBinding</span><span style="color: rgb(255,0,0)"> Content</span><span style="color: rgb(0,0,255)">}"</span><span style="color: rgb(0,0,255)">/&gt;
</span><span style="color: rgb(163,21,21)">                </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">Grid</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">            </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">ControlTemplate</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">        </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">Setter.Value</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">    </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(0,0,255)">&gt;
&lt;/</span><span style="color: rgb(163,21,21)">Style</span><span style="color: rgb(0,0,255)">&gt;</span></pre>
</blockquote>
<p>Everything compiled fine but the page wouldn&#8217;t look right and expression blend would not display it correctly and would display the error message:</p>
<p>The member &#8220;Content&#8221; is not recognized or is not accessible.</p>
<p>I have not idea how I figured out but it turns out that all I had to do is add a TargetType to the ControlTemplate&#8230;&nbsp; I don&#8217;t understand why I have to do this since I already set the TargetType on the Style element&#8230;&nbsp; Anyone?</p>
<p>The final style looked like this:</p>
<blockquote><pre class="code"><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Style</span><span style="color: rgb(255,0,0)"> x</span><span style="color: rgb(0,0,255)">:</span><span style="color: rgb(255,0,0)">Key</span><span style="color: rgb(0,0,255)">="NavButtonStyle"</span><span style="color: rgb(255,0,0)"> TargetType</span><span style="color: rgb(0,0,255)">="Button"&gt;
</span><span style="color: rgb(163,21,21)">    </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(255,0,0)"> Property</span><span style="color: rgb(0,0,255)">="Opacity"</span><span style="color: rgb(255,0,0)"> Value</span><span style="color: rgb(0,0,255)">="0.8"&gt;&lt;/</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">    </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(255,0,0)"> Property</span><span style="color: rgb(0,0,255)">="Template"&gt;
</span><span style="color: rgb(163,21,21)">        </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Setter.Value</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">            </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">ControlTemplate</span><span style="color: rgb(255,0,0)"> <u>TargetType</u></span><span style="color: rgb(0,0,255)"><u>="Button"</u>&gt;
</span><span style="color: rgb(163,21,21)">                </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">Grid</span><span style="color: rgb(255,0,0)"> x</span><span style="color: rgb(0,0,255)">:</span><span style="color: rgb(255,0,0)">Name</span><span style="color: rgb(0,0,255)">="RootElement"</span><span style="color: rgb(255,0,0)"> </span></pre>
<pre class="code"><span style="color: rgb(255,0,0)">                     HorizontalAlignment</span><span style="color: rgb(0,0,255)">="Stretch"</span><span style="color: rgb(255,0,0)"> </span></pre>
<pre class="code"><span style="color: rgb(255,0,0)">                     VerticalAlignment</span><span style="color: rgb(0,0,255)">="Stretch"&gt;
</span><span style="color: rgb(163,21,21)">                    </span><span style="color: rgb(0,0,255)">&lt;</span><span style="color: rgb(163,21,21)">ContentPresenter</span><span style="color: rgb(255,0,0)"> </span></pre>
<pre class="code"><span style="color: rgb(255,0,0)">                        Content</span><span style="color: rgb(0,0,255)">="{</span><span style="color: rgb(163,21,21)">TemplateBinding</span><span style="color: rgb(255,0,0)"> Content</span><span style="color: rgb(0,0,255)">}"</span><span style="color: rgb(0,0,255)">/&gt;
</span><span style="color: rgb(163,21,21)">                </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">Grid</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">            </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">ControlTemplate</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">        </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">Setter.Value</span><span style="color: rgb(0,0,255)">&gt;
</span><span style="color: rgb(163,21,21)">    </span><span style="color: rgb(0,0,255)">&lt;/</span><span style="color: rgb(163,21,21)">Setter</span><span style="color: rgb(0,0,255)">&gt;
&lt;/</span><span style="color: rgb(163,21,21)">Style</span><span style="color: rgb(0,0,255)">&gt;</span></pre>
</blockquote>
<div class="aizattos_related_posts"><span class="aizattos_related_posts_header" >Related Posts</span><ul><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/18/cross-domain-error-fix-for-silverlight/" rel="bookmark" title="Permanent Link: Cross Domain Error Fix for Silverlight" >Cross Domain Error Fix for Silverlight</a></span><div class="aizattos_related_posts_excerpt">The easiest/quickest way to fix a cross domain error is to create a test website to host your Silver...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2007/01/11/aspnet-20-error-title-is-not-a-member-of-asp%e2%80%a6/" rel="bookmark" title="Permanent Link: asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…" >asp.net 2.0 Error: &#8216;Title&#8217; is not a member of &#8216;ASP…</a></span><div class="aizattos_related_posts_excerpt">I had this error come up on a few pages after I deployed my website and it was driving me crazy.  Af...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/12/silverlight-tip-fill-the-browser-window/" rel="bookmark" title="Permanent Link: Silverlight Tip: Fill the browser window" >Silverlight Tip: Fill the browser window</a></span><div class="aizattos_related_posts_excerpt">RE: Silverlight 2 Beta 1 with Blend 2.5 March 2008 Preview You can Make your XAML silverlight page f...</div></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/03/13/silverlight-tip-stretch-canvas-to-fill-the-screen/" rel="bookmark" title="Permanent Link: Silverlight Tip: Stretch Canvas to Fill the Screen" >Silverlight Tip: Stretch Canvas to Fill the Screen</a></span></li><li><span class="aizattos_related_posts_title"><a href="http://www.emadibrahim.com/2008/04/14/yonkly-new-release-04-14-08/" rel="bookmark" title="Permanent Link: Yonkly: New Release 04-14-08" >Yonkly: New Release 04-14-08</a></span></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emadibrahim.com/2008/03/14/silverlight-templatebinding-throwing-an-error/feed/</wfw:commentRss>
		<slash:comments>4</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! -->