<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;CkYDRHczeip7ImA9WhRbGE0.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921</id><updated>2012-02-09T07:09:35.982-08:00</updated><category term="VBScript" /><category term="Win32 API" /><category term="Apache Axis" /><category term="SQL" /><category term="Interviewing" /><category term="Infrastructure" /><category term="TortiseSVN" /><category term="Regular Expressions" /><category term="Quantum Theory" /><category term="VB.NET" /><category term="Math" /><category term="Windows" /><category term="Algebra" /><category term="ASP.NET" /><category term="Testing" /><category term="MongoDB" /><category term="Feynman" /><category term="Productivity" /><category term="Flash" /><category term="Visual Studio 2008" /><category term="Virtual Server 2005" /><category term="Windows Server 2008 R2" /><category term="Graphics" /><category term="Git" /><category term="FTP" /><category term="Mac" /><category term="Expressions" /><category term="TSQL/SQL Server" /><category term="Lua" /><category term="Apache" /><category term="Atomic Physics" /><category term=".NET 2.0" /><category term=".NET 3.5" /><category term="MIT OCW" /><category term="3.091 Introduction to Solid State Chemistry" /><category term="NUnit" /><category term="Threading" /><category term="UC Berkeley" /><category term="Windows Vista" /><category term="Underscore.js" /><category term="CAML (Office)" /><category term="SharePoint 2007" /><category term="jQuery" /><category term="Computer Science" /><category term="Yada Yada Yada" /><category term="Subversion" /><category term="ASP.NET MVC3" /><category term="VPC 2007" /><category term="Web Services" /><category term="SQL Server 2005" /><category term="MVP" /><category term="Logging" /><category term=".NET 3.0" /><category term="RhinoMocks" /><category term="F#" /><category term="MSBuild" /><category term="ThoughtWorks Go" /><category term="NetBeans" /><category term="Build/Deploy" /><category term="Jabber" /><category term="NantContrib" /><category term="SharePoint Solution Deployment" /><category term="CruiseControl.NET" /><category term="Sockets" /><category term="CoffeeScript" /><category term="OpenFire" /><category term="MATLAB" /><category term="Labs" /><category term="IIS 6" /><category term="Nant" /><category term="Gallio" /><category term="NHibernate" /><category term="Logging Application Block" /><category term="Rutherford" /><category term="GDI+" /><category term="Internet Explorer" /><category term="Bohr" /><category term="JavaScript" /><category term="Windows Server 2003" /><category term="USB HID" /><category term="ColdFusion" /><category term="Entity Framework" /><category term="OS" /><category term="Developer Resources" /><category term=".NET" /><category term="NTFS" /><category term="Interoperability" /><category term="IIS 7" /><category term="MVC" /><category term="Classical Mechanics" /><category term="Dependency Injection" /><category term="VirtualBox" /><category term="Visual Studio 2005" /><category term="AJAX" /><category term="Security" /><category term="Visual Studio 2010" /><category term="ASP.NET MVC" /><category term="Configuration" /><category term="VMWare Server" /><category term="Software Licensing" /><category term="IIS7.5" /><category term="SaaS" /><category term="PowerShell" /><category term="SMTP" /><category term="kthxbye" /><category term="WiX" /><category term="Patterns And Practices" /><category term="Web Deployment Projects" /><category term="Nuget" /><category term="StructureMap" /><category term="Presentation" /><category term="Project Euler" /><category term="SSL" /><category term="DDD" /><category term="VSeWSS" /><category term="Android" /><category term="Classic ASP" /><category term="Rake" /><category term="XML/XSL" /><category term="REST" /><category term="Physics" /><category term="PInvoke" /><category term="WS-Security" /><category term="Windows Services" /><category term="FubuMVC" /><category term="ThoughtWorks Cruise" /><category term="Science" /><category term="Java" /><category term="TSQL" /><category term="MIT" /><category term="NoRM" /><category term="Open Source Projects" /><category term="Binary" /><category term="C#" /><category term="SOAP" /><category term="SMS Installer" /><category term="PHP" /><category term="Ruby Gems" /><category term="WCF" /><category term="Functional Programming" /><category term="Ruby" /><category term="TeamCity" /><category term="Active Directory" /><category term=".NET 4.0" /><category term="Linq" /><category term="Enterprise Library" /><category term="Apache Axis2" /><category term="Chemistry" /><category term="IronRuby" /><category term="Razor View Engine" /><category term="Nu" /><category term="SQL Server 2008 R2" /><category term="Ubuntu" /><category term="Prototypal" /><category term="WPF" /><category term="Albacore" /><category term="Silverlight" /><title>Mob's Dev Blog</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://blog.mikeobrien.net/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>296</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/MobsDevBlog" /><feedburner:info uri="mobsdevblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;CEYNRXk7cCp7ImA9WhRbF0k.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-7605544794197185457</id><published>2012-02-08T15:03:00.001-08:00</published><updated>2012-02-08T15:03:14.708-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-08T15:03:14.708-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="FubuMVC" /><title>FubuMVC Automatic Binding of Request Properties</title><content type="html">&lt;p&gt;One very nifty feature of FubuMVC is the automatic binding of request properties to your input model. From what I can tell, any item in the &lt;a href="https://github.com/DarthFubuMVC/fubumvc/blob/master/src/FubuMVC.Core/Http/AspNet/AspNetAggregateDictionary.cs"&gt;AspNetAgregateDictionary&lt;/a&gt; will be bound if the name/type of your input model property match. So for example, the following input model would have the user agent bound to the UserAgent property, talk about loose coupling:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; InputModel
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; UserAgent { get; set; }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;







.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; }&lt;/style&gt;The following items can be bound to your model: 

&lt;br /&gt;

&lt;br /&gt;

&lt;table style="border-bottom: #000 1px solid; border-left: #000 1px solid; border-collapse: collapse; border-top: #000 1px solid; border-right: #000 1px solid"&gt;&lt;thead&gt;
    &lt;tr&gt;
      &lt;th style="padding-left: 10px; background: #006; color: #fff"&gt;Name&lt;/th&gt;

      &lt;th style="padding-left: 10px; background: #006; color: #fff"&gt;Type&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;&lt;/thead&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;AcceptTypes&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string[]&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;ApplicationPath&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;AppRelativeCurrentExecutionFilePath&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Browser&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Web.HttpBrowserCompatibilityBase&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;ClientCertificate&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Web.HttpClientCertificate&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;ContentEncoding&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Text.Encoding&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;ContentLength&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;int&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;ContentType&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Cookies&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Web.HttpCookieCollection&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;CurrentExecutionFilePath&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;FilePath&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Files&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Web.HttpFileCollectionBase&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Filter&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.IO.Stream&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Form&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Collections.Specialized.NameValueCollection&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Headers&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Collections.Specialized.NameValueCollection&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;HttpMethod&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;IsAuthenticated&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;bool&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;IsLocal&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;bool&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;IsSecureConnection&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;bool&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;LogonUserIdentity&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;system.Security.Principal.WindowsIdentity&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Params&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Collections.Specialized.NameValueCollection&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Path&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;PathInfo&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;PhysicalApplicationPath&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;PhysicalPath&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;QueryString&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Collections.Specialized.NameValueCollection&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;RawUrl&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;RequestType&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;ServerVariables&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;System.Collections.Specialized.NameValueCollection&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;TotalBytes&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;int&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;Url&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;Uri&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;UrlReferrer&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;Uri&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;UserAgent&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;UserHostAddress&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;UserHostName&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;UserLanguages&lt;/td&gt;

      &lt;td style="border-bottom: #000 1px solid; text-align: left; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;string[]&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-7605544794197185457?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/KABsKBNBYSA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/7605544794197185457/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2012/02/fubumvc-automatic-binding-of-request.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7605544794197185457?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7605544794197185457?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/KABsKBNBYSA/fubumvc-automatic-binding-of-request.html" title="FubuMVC Automatic Binding of Request Properties" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2012/02/fubumvc-automatic-binding-of-request.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEMASXY9eCp7ImA9WhRbFUg.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-3390378958061974721</id><published>2012-02-06T10:20:00.001-08:00</published><updated>2012-02-06T10:20:48.860-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-02-06T10:20:48.860-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="TeamCity" /><title>Adding Build Time and Error Details to TeamCity Email Notifications</title><content type="html">&lt;p&gt;After setting up TeamCity I wanted to add some more information to the notification email. TeamCity allows you to customize all notifications (Not just email) through templates. The following resources were helpful in figuring out how to do this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://confluence.jetbrains.net/display/TCD65/Customizing+Notifications"&gt;TeamCity: Customizing Notifications Templates&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://confluence.jetbrains.net/display/TCD65/Server-side+Object+Model"&gt;TeamCity: Server-side Object Model&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://freemarker.org/docs/index.html"&gt;FreeMarker Manual (Templating engine)&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;First I wanted to add build time to the subject of successful build notifications so that we could easily monitor it:&lt;/p&gt;  &lt;p&gt;build_successful.ftl:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;&amp;lt;#global subject&amp;gt;${project.name} ${buildType.name} v${build.buildNumber} Succeeded &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;(${((build.duration / 60) % 60)}:${((build.duration) % 60)?string?left_pad(2, &amp;quot;0&amp;quot;)})&amp;lt;/#global&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This will give you a subject along the lines of:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;Frogger Stable CI v1.0.0.331 Succeeded (11:58)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;When a build fails, TeamCity will include failed test results in the email but not other failure information. I wanted to add the failure summary (Like the one seen on the build summary page) in every failed build notification:&lt;/p&gt;  &lt;p&gt;build_failed.ftl&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;&amp;lt;#global bodyHtml&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div style=&amp;quot;color:red&amp;quot;&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;#list build.buildLog.errorMessages as message&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ${message.text?replace(&amp;quot;\n&amp;quot;, &amp;quot;\lbr/\g&amp;quot;)}&amp;lt;br/&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/#list&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/div&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ...      &lt;br /&gt;&amp;lt;/#global&amp;gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This will give you a notification along the lines of:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;Build Frogger::Stable CI #1.0.0.336 failed     &lt;br /&gt;Agent: ENDOR1&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000" face="Consolas"&gt;bt3     &lt;br /&gt;Execute build_projects_ui      &lt;br /&gt;RuntimeError: MSBuild Failed. See Build Log For Detail&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000" face="Consolas"&gt;Stacktrace:     &lt;br /&gt;C:/.../gems/albacore-0.2.7/lib/albacore/support/failure.rb:12:in `fail_with_message'      &lt;br /&gt;... stack trace ...      &lt;br /&gt;C:/Program Files/TeamCity/BuildAgent1/plugins/rake-runner/lib/rb/runner/rakerunner.rb:40:in `'      &lt;br /&gt;Rake aborted!&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;Changes included (2 changes).     &lt;br /&gt;Change f735f1f299dc10005d8cc33d80003ea6915cbfa4 by mobrien (2 files): Added frog.      &lt;br /&gt;Change 4b023210511568f5159a28e493ec01134285c363 by mobrien (1 file): Added cars.&lt;/font&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-3390378958061974721?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/ettbXaGt0do" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/3390378958061974721/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2012/02/adding-build-time-and-error-details-to.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3390378958061974721?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3390378958061974721?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/ettbXaGt0do/adding-build-time-and-error-details-to.html" title="Adding Build Time and Error Details to TeamCity Email Notifications" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2012/02/adding-build-time-and-error-details-to.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08GQXszeyp7ImA9WhRbEE4.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-8432936250175243226</id><published>2012-01-31T09:30:00.001-08:00</published><updated>2012-01-31T09:43:40.583-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-31T09:43:40.583-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="MVC" /><category scheme="http://www.blogger.com/atom/ns#" term="MVP" /><title>MVC vs MVP</title><content type="html">&lt;p&gt;After working on the backend for so long I’m having a bit of a frontend renaissance. One area of confusion is the difference between MVC and MVP. There is a lot of conflicting/confusing information out there. I noticed that many of the posts and comments get it partially right which is more confusing than getting it completely wrong. So after trudging through many posts, the ones linked below seem to explain the difference accurately and clearly. &lt;/p&gt;  &lt;p&gt;TL;DR, here was my take, for better or worse:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;MVC&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;…had a view/controller pair for each element on the screen (textbox, button, etc).&lt;/li&gt;      &lt;li&gt;…the controller handled raw user input and decided if it was relevant to its view (i.e there was a mouse click or keystroke, was it within the coordinates of my view? Yes, I’ll handle it; no, I’ll ignore it).&lt;!--EndFragment--&gt;&lt;/li&gt;      &lt;li&gt;…&lt;strong&gt;&lt;em&gt;the controller did not manipulate the view&lt;/em&gt;&lt;/strong&gt;. It just handled user input and updated the model…period.&lt;/li&gt;      &lt;li&gt;…the view observes the model and updates itself.&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;MVP&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;…has one view/presenter for multiple widgets, perhaps an entire screen.&lt;/li&gt;      &lt;li&gt;…the presenter handles high level input that is passed along by widgets on the screen.&lt;/li&gt;      &lt;li&gt;…&lt;em&gt;&lt;strong&gt;the presenter manipulates the view&lt;/strong&gt;&lt;/em&gt;…&lt;/li&gt;      &lt;ul&gt;       &lt;li&gt;&lt;strong&gt;supervising controllers&lt;/strong&gt; manipulate the view only in complex scenarios but let view update itself as much as possible, either by data binding to the model or observing the model.&lt;/li&gt;        &lt;li&gt;&lt;strong&gt;passive views&lt;/strong&gt; are manipulated only by the presenter and they have no knowledge of the model. Changes in the model are strictly observed by the presenter which in turn manipulates the model.&lt;/li&gt;     &lt;/ul&gt;   &lt;/ul&gt;    &lt;li&gt;Comparison&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Controllers in MVC were focused on processing low level, raw input (Which was a lot of work), updating the model was a by product. Handling of low level, raw input is no longer necessary these days as it is handled by widgets; widgets then fire higher level events (Like button click instead of mouse click). So since the job of handling of low level input is now on the widgets, the MVP presenters are focused on updating the model in response to higher level events.&lt;/li&gt;      &lt;li&gt;Controller/view pairs in classic MVC were responsible for one “dumb” UI element. The view and presenter in MVP are responsible for an ensemble of “smart” widgets.&lt;/li&gt;      &lt;li&gt; MVC controllers did not manipulate the view whereas MVP presenters do (By varying degrees with supervising controller and passive view).&lt;/li&gt;   &lt;/ul&gt; &lt;/ul&gt;  &lt;p&gt;To me the biggest difference between the two is the &lt;strong&gt;&lt;em&gt;relationship between the view and controller/presenter&lt;/em&gt;&lt;/strong&gt;. The rest seems less relevant, especially now-a-days; who cares if the controller is handling low level or high level events or if it is handling one UI element or many widgets. These seem to simply be a function of the available technology. In MVC the controller does not manipulate the view, the view only updates itself as a result of changes in the model. In MVP the presenter manipulates the view to varying degrees. That to me is the real conceptual difference that has relevance today. I think it also explains why MVC is not used today in the UI; it’s just not practical to restrict the controller from manipulating the view (Although the presentation model &amp;amp; mvvm attempt to successfully do this). That restriction would make it difficult to handle and test non-trivial UI logic. It also explains why MVC (flavored a bit differently) is used in many server side web frameworks as this separation between view and controller is practical and valuable in a stateless environment.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;History/differences:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://aviadezra.blogspot.com/2008/06/mvc-model-view-controller-design.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://aviadezra.blogspot.com/2008/06/mvc-model-view-controller-design.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://aviadezra.blogspot.com/2007/07/twisting-mvp-triad-say-hello-to-mvpc.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://aviadezra.blogspot.com/2007/07/twisting-mvp-triad-say-hello-to-mvpc.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://martinfowler.com/eaaDev/uiArchs.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://martinfowler.com/eaaDev/uiArchs.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lostechies.com/derekgreer/2007/08/25/interactive-application-architecture/"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://lostechies.com/derekgreer/2007/08/25/interactive-application-architecture/&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Definitions:&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://martinfowler.com/eaaCatalog/modelViewController.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://martinfowler.com/eaaCatalog/modelViewController.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; (Just a stub with the structure and a pointer to POEAA where you can read more)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://martinfowler.com/eaaDev/ModelViewPresenter.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://martinfowler.com/eaaDev/ModelViewPresenter.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://martinfowler.com/eaaDev/SupervisingPresenter.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://martinfowler.com/eaaDev/SupervisingPresenter.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://martinfowler.com/eaaDev/PassiveScreen.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://martinfowler.com/eaaDev/PassiveScreen.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://martinfowler.com/eaaDev/PresentationModel.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://martinfowler.com/eaaDev/PresentationModel.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; (Good to understand in comparison to the other patterns)&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-8432936250175243226?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/_SWy3XqS_gc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/8432936250175243226/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2012/01/mvc-vs-mvp.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/8432936250175243226?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/8432936250175243226?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/_SWy3XqS_gc/mvc-vs-mvp.html" title="MVC vs MVP" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2012/01/mvc-vs-mvp.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08GSHY8eyp7ImA9WhRXFE8.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-4999671545620315098</id><published>2011-12-20T09:30:00.001-08:00</published><updated>2011-12-20T14:57:09.873-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-20T14:57:09.873-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term="CoffeeScript" /><title>JavaScript Prototyping Reference</title><content type="html">&lt;p&gt;I’ve had problems remembering what is actually happening when working with constructor functions and prototyping. I created the following table to demonstrate what's going on. If I’m missing something please let me know as I’m not a JavaScript ninja by any means. &lt;/p&gt;  &lt;p&gt;A couple of things to note:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/proto"&gt;__proto__ property is deprecated and non standard&lt;/a&gt;. I include it only as a way to indicate an object’s prototype. &lt;/li&gt;    &lt;li&gt;I included the &lt;a href="http://documentcloud.github.com/underscore/#extend"&gt;underscore.js extend function&lt;/a&gt; only to show how it compares to protypal inheritance in JS or CS. I think the name of this function is not clear. &lt;/li&gt;    &lt;li&gt;The &lt;a href="http://documentcloud.github.com/backbone/docs/backbone.html#section-121"&gt;backbone.js extend method&lt;/a&gt; is functionally equivalent to the &lt;a href="http://jashkenas.github.com/coffee-script/#classes"&gt;CoffeeScript extends operator&lt;/a&gt;. &lt;/li&gt; &lt;/ul&gt;  &lt;br /&gt;&lt;center&gt;   &lt;table style="border-bottom: #000 1px solid; border-left: #000 1px solid; border-collapse: collapse; border-top: #000 1px solid; border-right: #000 1px solid"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;th style="background: #006; color: #fff" colspan="2"&gt;JavaScript&lt;/td&gt;&lt;/tr&gt; &lt;/th&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;           &lt;div align="left"&gt;             &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; x = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;


















.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;x = {
    &lt;span class="rem"&gt;// Only meaningful for constructors not functions.&lt;/span&gt;
    prototype: { 
        constructor: x,
        __proto__: Object.prototype
    },
    __proto__: Function.prototype
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;


















.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; Y = &lt;span class="kwrd"&gt;function&lt;/span&gt;(name) {
    &lt;span class="rem"&gt;// In a constructor 'this' refers to the&lt;/span&gt;
    &lt;span class="rem"&gt;// new object.&lt;/span&gt;
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.name = name;
}

Y.prototype.printName = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="rem"&gt;// In a method, 'this' refers to the &lt;/span&gt;
    &lt;span class="rem"&gt;// declaring object (Unless overridden).&lt;/span&gt;
    console.log(&lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
}

&lt;span class="kwrd"&gt;var&lt;/span&gt; y = &lt;span class="kwrd"&gt;new&lt;/span&gt; Y(&lt;span class="str"&gt;'Fritz London'&lt;/span&gt;); &lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;Y = {
    &lt;span class="rem"&gt;// This will be set as the new objects prototype.&lt;/span&gt;
    prototype: { 
        constructor: Y,
        __proto__: Object.prototype,
        printName: &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
            console.log(&lt;span class="kwrd"&gt;this&lt;/span&gt;.name);
        }
    },
    __proto__: Function.prototype
}

y = {
    __proto__: Y.prototype,
    name: &lt;span class="str"&gt;'Fritz London'&lt;/span&gt;
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; z = { name: &lt;span class="str"&gt;'Richard Feynman'&lt;/span&gt; }&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;z = {
    __proto__: Object.prototype,
    name: &lt;span class="str"&gt;'Richard Feynman'&lt;/span&gt;
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; a = { name: &lt;span class="str"&gt;'Werner Heisenberg'&lt;/span&gt; }

&lt;span class="kwrd"&gt;var&lt;/span&gt; createObject = &lt;span class="kwrd"&gt;function&lt;/span&gt;(prototype) {
    &lt;span class="kwrd"&gt;var&lt;/span&gt; F = &lt;span class="kwrd"&gt;function&lt;/span&gt;() {};
    F.prototype = prototype;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; F();
}

&lt;span class="kwrd"&gt;var&lt;/span&gt; b = createObject(a);
&lt;span class="rem"&gt;// Or the built in&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; b = Object.create(a);&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;a = {
    __proto__: Object.prototype,
    name: &lt;span class="str"&gt;'Werner Heisenberg'&lt;/span&gt;
}

F = {
    prototype: a,
    __proto__: Function.prototype
}

b = {
    __proto__: a
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;th style="background: #006; color: #fff" colspan="2"&gt;CoffeeScript&lt;/td&gt;&lt;/tr&gt; &lt;/th&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Parent
    constructor: -&amp;gt;
        &lt;span class="preproc"&gt;@id&lt;/span&gt; = ++Parent.identity
    name: &lt;span class="str"&gt;'Niels Bohr'&lt;/span&gt;
    @identity: 0&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;function&lt;/span&gt; Parent() {
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.id = ++Parent.identity;
}
Parent.prototype.name = &lt;span class="str"&gt;'Niels Bohr'&lt;/span&gt;;
Parent.identity = 0;

---

Parent = {
    prototype: { 
        constructor: Parent,
        __proto__: Object.prototype,
        name: &lt;span class="str"&gt;'Niels Bohr'&lt;/span&gt;
    },
    __proto__: Function.prototype,
    identity: 0
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;parent = &lt;span class="kwrd"&gt;new&lt;/span&gt; Parent()&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;parent = {
    __proto__: Parent.prototype,
    id: 1
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Child extends Parent
    url: &lt;span class="str"&gt;'http://www.google.com'&lt;/span&gt;&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;__extends(Child, Parent);
&lt;span class="kwrd"&gt;function&lt;/span&gt; Child() {
    Child.__super__.constructor.apply(&lt;span class="kwrd"&gt;this&lt;/span&gt;, arguments);
}
Child.prototype.url = &lt;span class="str"&gt;'http://www.google.com'&lt;/span&gt;;

---

Child = {
    prototype: { 
        constructor: Child,
        __proto__: Parent,
        url: &lt;span class="str"&gt;'http://www.google.com'&lt;/span&gt;
    },
    __proto__: Function.prototype,
    &lt;span class="rem"&gt;// CoffeeScript generated property&lt;/span&gt;
    __super__: Parent.prototype
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;child = &lt;span class="kwrd"&gt;new&lt;/span&gt; Child()&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;child = {
    __proto__: Child.prototype,
    id: 1
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;th style="background: #006; color: #fff" colspan="2"&gt;backbone.js&lt;/td&gt;&lt;/tr&gt; &lt;/th&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// [backbone.js].extend(protoProps, classProps)&lt;/span&gt;
&lt;span class="kwrd"&gt;var&lt;/span&gt; Person = Backbone.Model.extend({
    constructor: &lt;span class="kwrd"&gt;function&lt;/span&gt;() {
        &lt;span class="kwrd"&gt;this&lt;/span&gt;.id = ++Person.identity;
    },
    name: &lt;span class="str"&gt;'Niels Bohr'&lt;/span&gt;
}, {
    identity: 0
});&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;





.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;Person = {
    prototype: {
        &lt;span class="rem"&gt;// If a constructor property is not &lt;/span&gt;
        &lt;span class="rem"&gt;// specified, a new function is used  &lt;/span&gt;
        &lt;span class="rem"&gt;// that calls the parent constructor&lt;/span&gt;
        &lt;span class="rem"&gt;// in the context of the new object.&lt;/span&gt;
        constructor: protoProps.constructor,
        __proto__: Backbone.Model.prototype,
        name: &lt;span class="str"&gt;'Niels Bohr'&lt;/span&gt;
    },
    __proto__: Function.prototype,
    &lt;span class="rem"&gt;// Backbone added property&lt;/span&gt;
    __super__: Backbone.Model.prototype
    &lt;span class="rem"&gt;// Backbone attaches this to new objects&lt;/span&gt;
    extend: Backbone.Model.extend,
    identity: 0
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;





.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;th style="background: #006; color: #fff" colspan="2"&gt;underscore.js&lt;/td&gt;&lt;/tr&gt; &lt;/th&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; border-right: #c0c0c0 1px solid; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; person = { id: 5 }
&lt;span class="kwrd"&gt;var&lt;/span&gt; a = { name: &lt;span class="str"&gt;'Werner Heisenberg'&lt;/span&gt; }
&lt;span class="kwrd"&gt;var&lt;/span&gt; b = { occupation: &lt;span class="str"&gt;'Physicist'&lt;/span&gt; }
_.extend(person, a, b);&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;

        &lt;td style="border-bottom: #000 1px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"&gt;
          &lt;div align="left"&gt;
            &lt;pre class="csharpcode"&gt;person = {
    id: 5,
    name: &lt;span class="str"&gt;'Werner Heisenberg'&lt;/span&gt;,
    occupation: &lt;span class="str"&gt;'Physicist'&lt;/span&gt;
}&lt;/pre&gt;
          &lt;/div&gt;
          &lt;style type="text/css"&gt;































.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; }&lt;/style&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/center&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-4999671545620315098?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/pOJ3z-98My8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/4999671545620315098/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/12/javascript-prototyping-reference.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/4999671545620315098?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/4999671545620315098?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/pOJ3z-98My8/javascript-prototyping-reference.html" title="JavaScript Prototyping Reference" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/12/javascript-prototyping-reference.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEQMRXs4cCp7ImA9WhRXE0g.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-133546721511981794</id><published>2011-12-19T19:33:00.001-08:00</published><updated>2011-12-19T19:39:44.538-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-19T19:39:44.538-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term="CoffeeScript" /><category scheme="http://www.blogger.com/atom/ns#" term="Underscore.js" /><title>Dissecting the output of the CoffeeScript class and extends keyword</title><content type="html">&lt;p&gt;CoffeeScript has a convenient way of emulating class and inheritance semantics via the &lt;a href="http://jashkenas.github.com/coffee-script/#classes"&gt;class and extends keyword&lt;/a&gt;. I wanted to better understand what was happening under the covers so the following is an analysis of the transpiler output. We’ll look at a simple relationship between two CS “classes”:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Parent
    constructor: -&amp;gt;
        &lt;span class="preproc"&gt;@id&lt;/span&gt; = ++Parent.identity
    @identity: 0

&lt;span class="kwrd"&gt;class&lt;/span&gt; Child extends Parent
    url: &lt;span class="str"&gt;'http://www.google.com'&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.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; }&lt;/style&gt;

&lt;p&gt;CS outputs the following (As of &lt;a href="http://github.com/jashkenas/coffee-script/tarball/1.2.0"&gt;1.2&lt;/a&gt;):&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;var&lt;/span&gt; Child, Parent;
&lt;span class="kwrd"&gt;var&lt;/span&gt; __hasProp = Object.prototype.hasOwnProperty, 
    __extends = &lt;span class="kwrd"&gt;function&lt;/span&gt;(child, parent) {&lt;span class="rem"&gt;/* discussed later */&lt;/span&gt;};

Parent = (&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    &lt;span class="kwrd"&gt;function&lt;/span&gt; Parent() {
        &lt;span class="kwrd"&gt;this&lt;/span&gt;.id = ++Parent.identity;
    }
    Parent.identity = 0;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; Parent;
})();

Child = (&lt;span class="kwrd"&gt;function&lt;/span&gt;() {
    __extends(Child, Parent);
    &lt;span class="kwrd"&gt;function&lt;/span&gt; Child() {
        Child.__super__.constructor.apply(&lt;span class="kwrd"&gt;this&lt;/span&gt;, arguments);
    }
    Child.prototype.url = &lt;span class="str"&gt;'http://www.google.com'&lt;/span&gt;;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; Child;
)();&lt;/pre&gt;
&lt;style type="text/css"&gt;

.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; }&lt;/style&gt;

&lt;p&gt;Some things I noted:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The CS transpiler builds the constructor functions in &lt;a href="http://benalman.com/news/2010/11/immediately-invoked-function-expression/"&gt;IIFE’s&lt;/a&gt; so we don’t clutter up the global object. &lt;/li&gt;

  &lt;li&gt;The CS “constructor” property is the body of the constructor function. &lt;/li&gt;

  &lt;li&gt;The CS @ prefix… 
    &lt;ul&gt;
      &lt;li&gt;… refers to the new object &lt;em&gt;&lt;strong&gt;only&lt;/strong&gt;&lt;/em&gt; when used in the constructor. &lt;/li&gt;

      &lt;li&gt;… refers to the function object outside of the constructor (to declare “static” properties). &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Properties not prefixed with the CS @ are prototype properties. &lt;/li&gt;

  &lt;li&gt;The “parent” constructor function can be executed &lt;em&gt;&lt;strong&gt;in the context of the new child object&lt;/strong&gt;&lt;/em&gt;. This happens automatically (As shown above) if the child does not explicitly declare a constructor. If the child object does supply a constructor (And you want to call the parent constructor) you will need to manually call super(…) in the child constructor with zero or more arguments. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now lets take a look at the __extends method that is generated by CS. This method sets up the prototype chain and a property that links to the “parent” prototype. &lt;/p&gt;

&lt;pre class="csharpcode"&gt;var __hasProp = Object.prototype.hasOwnProperty;
var __extends = function(child, parent) { 
    &lt;span class="kwrd"&gt;for&lt;/span&gt; (var key &lt;span class="kwrd"&gt;in&lt;/span&gt; parent) {
         &lt;span class="kwrd"&gt;if&lt;/span&gt; (__hasProp.call(parent, key)) 
            child[key] = parent[key]; 
    } 
    function ctor() { 
        &lt;span class="kwrd"&gt;this&lt;/span&gt;.constructor = child; 
    } 
    ctor.prototype = parent.prototype; 
    child.prototype = &lt;span class="kwrd"&gt;new&lt;/span&gt; ctor; 
    child.__super__ = parent.prototype; 
    &lt;span class="kwrd"&gt;return&lt;/span&gt; child; 
};&lt;/pre&gt;
&lt;style type="text/css"&gt;

.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; }&lt;/style&gt;

&lt;p&gt;Some things I noted:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;All constructor function properties (Or “static” properties) are copied from the “parent” to the “child” constructor function.&lt;/li&gt;

  &lt;li&gt;An intermediate prototype object is created so that you can augment the “child’s” prototype without changing the “parent’s” prototype. &lt;/li&gt;

  &lt;ul&gt;
    &lt;li&gt;It’s prototype is the “parent” prototype.&lt;/li&gt;

    &lt;li&gt;It serves as the prototype of “child” objects. &lt;/li&gt;

    &lt;li&gt;It’s constructor is set to the “child” constructor function.&lt;/li&gt;
  &lt;/ul&gt;

  &lt;li&gt;A special property called __super__ is created on the “child” constructor function. Evidentially &lt;a href="http://groups.google.com/group/nodejs/browse_thread/thread/b0fc3b31a90dc906"&gt;CS likes to be backwards compatible&lt;/a&gt; as it doesn't make use of the &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf"&gt;relatively new Object.getPrototypeOf method&lt;/a&gt;. Calling super() in the CS constructor, as noted above, will make use of this property to call the “parent” constructor function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From this we can see that CS sets up the prototype chain like so:&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh5.ggpht.com/-WiPxpjbnOwc/TvAB_UYXd5I/AAAAAAAAAio/zVaHAQXJjGU/s1600-h/CoffeeScriptPrototypeChain%25255B11%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="CoffeeScriptPrototypeChain" border="0" alt="CoffeeScriptPrototypeChain" src="http://lh5.ggpht.com/-KJlqGZG20WU/TvACAVZbc3I/AAAAAAAAAiw/plG1H0d3bdM/CoffeeScriptPrototypeChain_thumb%25255B9%25255D.png?imgmax=800" width="451" height="322" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;One more thing I was interested in was how the CS extends keyword compares to the &lt;a href="http://documentcloud.github.com/underscore/docs/underscore.html#section-68"&gt;underscore.js method of a similar name&lt;/a&gt;:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;_.extend = &lt;span class="kwrd"&gt;function&lt;/span&gt;(obj) {
    each(slice.call(arguments, 1), &lt;span class="kwrd"&gt;function&lt;/span&gt;(source) {
        &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;var&lt;/span&gt; prop &lt;span class="kwrd"&gt;in&lt;/span&gt; source) {
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (source[prop] !== &lt;span class="kwrd"&gt;void&lt;/span&gt; 0) obj[prop] = source[prop];
        }
    });
    &lt;span class="kwrd"&gt;return&lt;/span&gt; obj;
};&lt;/pre&gt;
&lt;style type="text/css"&gt;
.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; }&lt;/style&gt;Clearly they are very different. The underscore extend method basically does a mixin as it simply copies properties from the source objects to the target object.   &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-133546721511981794?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/JZF45AjzkMo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/133546721511981794/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/12/dissecting-output-of-coffeescript-class.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/133546721511981794?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/133546721511981794?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/JZF45AjzkMo/dissecting-output-of-coffeescript-class.html" title="Dissecting the output of the CoffeeScript class and extends keyword" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-KJlqGZG20WU/TvACAVZbc3I/AAAAAAAAAiw/plG1H0d3bdM/s72-c/CoffeeScriptPrototypeChain_thumb%25255B9%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/12/dissecting-output-of-coffeescript-class.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkYGRXozeyp7ImA9WhRXE08.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-2634985131047663839</id><published>2011-12-19T12:16:00.001-08:00</published><updated>2011-12-19T12:55:24.483-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-19T12:55:24.483-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="JavaScript" /><category scheme="http://www.blogger.com/atom/ns#" term="Lua" /><category scheme="http://www.blogger.com/atom/ns#" term="Prototypal" /><title>Prototyping in Lua</title><content type="html">&lt;p&gt;One of the hardest things to get my head around in JavaScript (Besides “&lt;a href="http://www.quirksmode.org/js/this.html"&gt;this&lt;/a&gt;”) was prototyping. I wasn't sure if it was prototyping itself that was hard to grok (It seemed amazingly simple to me in principle) or if it was how JavaScript implemented it. I thought it would be an interesting exercise to explore how another prototypal language worked to see where the difficulty lay. So let’s take a look at how &lt;a href="http://www.lua.org/"&gt;Lua&lt;/a&gt; implements prototyping (Disclaimer: I only have a passing knowledge of Lua so please excuse my naïveté).&lt;/p&gt;  &lt;p&gt;Lua “&lt;a href="http://www.lua.org/pil/16.html"&gt;tables&lt;/a&gt;” form the basis of objects. They are essentially hash tables, à la JavaScript objects. Prototyping in Lua is &lt;a href="http://www.lua.org/pil/16.1.html"&gt;enabled by two actions&lt;/a&gt;; first setting the objects’ metatable (Which can be any table) and second, setting the prototype field in that metatable (Which is the __index field). Here is how it works (NB: see &lt;a href="http://www.lua.org/pil/16.html"&gt;here about the use of colon vs. dot&lt;/a&gt;. Compare it to call/apply in JavaScript):&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;-- Create the prototype&lt;/span&gt;
Greeting = { hello = &lt;span class="str"&gt;'hello'&lt;/span&gt; }

&lt;span class="kwrd"&gt;function&lt;/span&gt; Greeting:say_hello()
    &lt;span class="kwrd"&gt;print&lt;/span&gt;(self.hello)
&lt;span class="kwrd"&gt;end&lt;/span&gt;

&lt;span class="rem"&gt;-- Create an object&lt;/span&gt;
lolcat = { hello = &lt;span class="str"&gt;'hai'&lt;/span&gt; }

&lt;span class="rem"&gt;-- Set the meta table (A table where metadata is held), in this case we’ll just set it to be the object itself&lt;/span&gt;
setmetatable(lolcat, lolcat)

&lt;span class="rem"&gt;-- Set the prototype&lt;/span&gt;
lolcat.__index = Greeting&lt;/pre&gt;
&lt;style type="text/css"&gt;
.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; }&lt;/style&gt;

&lt;p&gt;Other than the setting of the metatable I’d have to say this is incredibly straight forward. You can encapsulate the creation logic into a constructor which simplifies the process:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;-- Constructor&lt;/span&gt;
&lt;span class="kwrd"&gt;function&lt;/span&gt; Greeting:&lt;span class="kwrd"&gt;new&lt;/span&gt;(o)
    o = o &lt;span class="kwrd"&gt;or&lt;/span&gt; {}
    o.__index = self
    &lt;span class="kwrd"&gt;return&lt;/span&gt; setmetatable(o, o)
&lt;span class="kwrd"&gt;end&lt;/span&gt;

lolcat = Greeting:&lt;span class="kwrd"&gt;new&lt;/span&gt;{ hello = &lt;span class="str"&gt;'hai'&lt;/span&gt; }

&lt;span class="rem"&gt;-- https://github.com/silentbicycle/lunatest&lt;/span&gt;
assert_equal(&lt;span class="str"&gt;'hai'&lt;/span&gt;, lolcat.hello)&lt;/pre&gt;
&lt;style type="text/css"&gt;
.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; }&lt;/style&gt;

&lt;p&gt;Although prototyping still presents a mental shift for me the Lua approach feels much cleaner and simpler. I find the dot/colon approach (mentioned above) much cleaner as well.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-2634985131047663839?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/Kv83Mnd2dVQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/2634985131047663839/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/12/prototyping-in-lua.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/2634985131047663839?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/2634985131047663839?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/Kv83Mnd2dVQ/prototyping-in-lua.html" title="Prototyping in Lua" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/12/prototyping-in-lua.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0QHQ3w6eSp7ImA9WhZaEEk.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-6474078459296581767</id><published>2011-06-25T17:37:00.001-07:00</published><updated>2011-06-25T17:48:52.211-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-25T17:48:52.211-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="FubuMVC" /><title>Welcome to the FubuMVC Cafeteria</title><content type="html">&lt;p&gt;I was wondering what FubuMVC had wired up in the IoC container out-of-the-box and it happens to be quite a bit. Here is a dump of the container configuration (Using the spark view engine):&lt;/p&gt; &lt;style&gt;

.fubutable { border: 1px solid black; border-collapse: collapse;} .fubutablecell {font-family: arial; font-size:10pt} .fubutablerow:nth-child(odd) { background-color:#d1d1d1; }&lt;/style&gt;  &lt;table class="fubutable"&gt;&lt;tbody&gt;     &lt;tr class="fubutablerow"&gt;       &lt;th class="fubutablecell"&gt;PluginType&lt;/th&gt;        &lt;th class="fubutablecell"&gt;Description&lt;/th&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;Bottles.IActivator&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.SparkActivator, FubuMVC.Spark          &lt;br /&gt;FubuMVC.Core.Packaging.PackageFileActivator, FubuMVC.Core           &lt;br /&gt;FubuMVC.Core.Content.ScriptGraphConfigurationActivator, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.AggregateDictionary&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Binding.AggregateDictionary&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IBindingContext&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Binding.BindingContext, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.ICollectionTypeProvider&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Binding.DefaultCollectionTypeProvider, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IConverterFamily&lt;/td&gt;        &lt;td&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IModelBinder&lt;/td&gt;        &lt;td&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IModelBinderCache&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Binding.ModelBinderCache, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IObjectResolver&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.RecordingObjectResolver, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IPropertyBinder&lt;/td&gt;        &lt;td&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IPropertyBinderCache&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Binding.PropertyBinderCache, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IRequestData&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.RecordingRequestData, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.ISmartRequest&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.FubuSmartRequest, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.IValueConverterRegistry&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Binding.ValueConverterRegistry, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Binding.ObjectResolver&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Binding.ObjectResolver, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.IDisplayFormatter&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.DisplayFormatter, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.IFileSystem&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.FileSystem, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.IObjectConverter&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.ObjectConverter, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.ITypeResolver&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.TypeResolver, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Reflection.ITypeDescriptorCache&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Reflection.TypeDescriptorCache, FubuCore&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuCore.Reflection.TypeDescriptorCache&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuCore.Reflection.TypeDescriptorCache&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Behaviors.IJsonReader&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Behaviors.JavaScriptJsonReader, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Conneg.IFormatter&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Conneg.JsonFormatter, FubuMVC.Core          &lt;br /&gt;FubuMVC.Core.Conneg.XmlFormatter, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Conneg.IMediaProcessor`1&amp;lt;T&amp;gt;&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Conneg.MediaProcessor`1, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Content.IContentFolderService&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Content.ContentFolderService&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Content.IContentRegistry&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Content.ContentRegistryCache, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Content.IMimeTypeProvider&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Content.DefaultMimeTypeProvider&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.CurrentRequest&lt;/td&gt;        &lt;td class="fubutablecell"&gt;Instance is created by Func&amp;lt;object&amp;gt; function:System.Func`2[StructureMap.IContext,FubuMVC.Core.CurrentRequest]&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.DiagnosticsIndicator&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.DiagnosticsIndicator&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.IDebugDetector&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.DebugDetector, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.IDebugReport&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.DebugReport, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.IRequestHistoryCache&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.RequestHistoryCache, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.BehaviorTracer&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.BehaviorTracer, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.DiagnosticBehavior&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.DiagnosticBehavior, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.IEndpointService&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.EndpointService, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Packaging.IPackageFiles&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Packaging.PackageFilesCache, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Registration.BehaviorGraph&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Registration.BehaviorGraph&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Registration.Querying.IChainResolver&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Registration.Querying.ChainResolver, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Routing.IRoutePolicy&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Routing.StandardRoutePolicy, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.IBehaviorFactory&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.StructureMap.PartialBehaviorFactory, FubuMVC.StructureMap&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.IFubuRequest&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.RecordingFubuRequest, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.IJsonWriter&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.JsonWriter, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.IOutputWriter&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.RecordingOutputWriter, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.IPartialFactory&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.PartialFactory, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.ISessionState&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.SimpleSessionState, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.IStreamingData&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Runtime.StreamingData, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.IAuthenticationContext&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Web.Security.WebAuthenticationContext, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.IAuthorizationFailureHandler&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.DefaultAuthorizationFailureHandler, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.IAuthorizationPolicyExecutor&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Diagnostics.Tracing.RecordingAuthorizationPolicyExecutor, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.IAuthorizationPreviewService&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.AuthorizationPreviewService, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.IChainAuthorizor&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.ChainAuthorizor, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.IEndPointAuthorizor&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.NulloEndPointAuthorizor&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.IEndPointAuthorizorFactory&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.EndPointAuthorizorFactory, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Security.ISecurityContext&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Web.Security.WebSecurityContext, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.SessionState.IFlash&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.SessionState.FlashProvider, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.SessionState.IRequestDataProvider&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.SessionState.RequestDataProvider, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Configuration.IElementNamingConvention&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Configuration.DefaultElementNamingConvention, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.ICssLinkTagWriter&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.CssLinkTagWriter, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.IPartialInvoker&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.PartialInvoker, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Scripts.IScriptTagWriter&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Scripts.BasicScriptTagWriter, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Scripts.ScriptGraph&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Scripts.ScriptGraph&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Security.IFieldAccessRightsExecutor&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Diagnostics.RecordingFieldAccessRightsExecutor, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Security.IFieldAccessService&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Security.FieldAccessService, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Tags.ITagGenerator`1&amp;lt;T&amp;gt;&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Tags.TagGenerator`1, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Tags.TagProfileLibrary&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.UI.Tags.TagProfileLibrary&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Urls.IUrlRegistry&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Urls.UrlRegistry, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.Urls.IUrlTemplatePattern&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.Urls.NulloUrlTemplate, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.View.Activation.IPageActivationRules&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.View.Activation.PageActivationRuleCache, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.View.Activation.IPageActivationSource&lt;/td&gt;        &lt;td&gt;&amp;#160;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.View.Activation.IPageActivator&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.View.Activation.PageActivator, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Core.View.Activation.PageActivationRuleCache&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Core.View.Activation.PageActivationRuleCache, FubuMVC.Core&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.IRenderStrategy&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.NestedRenderStrategy, FubuMVC.Spark          &lt;br /&gt;FubuMVC.Spark.Rendering.AjaxRenderStrategy, FubuMVC.Spark           &lt;br /&gt;FubuMVC.Spark.Rendering.DefaultRenderStrategy, FubuMVC.Spark&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.IViewEntryProvider&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.ViewEntryProviderCache, FubuMVC.Spark&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.IViewModifier&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.PageActivation, FubuMVC.Spark          &lt;br /&gt;FubuMVC.Spark.Rendering.SiteResourceAttacher, FubuMVC.Spark           &lt;br /&gt;FubuMVC.Spark.Rendering.ContentActivation, FubuMVC.Spark           &lt;br /&gt;FubuMVC.Spark.Rendering.OnceTableActivation, FubuMVC.Spark           &lt;br /&gt;FubuMVC.Spark.Rendering.OuterViewOutputActivator, FubuMVC.Spark           &lt;br /&gt;FubuMVC.Spark.Rendering.NestedViewOutputActivator, FubuMVC.Spark           &lt;br /&gt;FubuMVC.Spark.Rendering.ViewContentDisposer, FubuMVC.Spark           &lt;br /&gt;FubuMVC.Spark.Rendering.NestedOutputActivation, FubuMVC.Spark&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.IViewModifierService&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.ViewModifierService, FubuMVC.Spark&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.NestedOutput&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.NestedOutput, FubuMVC.Spark&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.ViewOutput&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.Rendering.ViewOutput, FubuMVC.Spark&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;FubuMVC.Spark.SparkModel.ITemplateRegistry&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.Spark.SparkModel.TemplateRegistry&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;Microsoft.Practices.ServiceLocation.IServiceLocator&lt;/td&gt;        &lt;td class="fubutablecell"&gt;FubuMVC.StructureMap.StructureMapServiceLocator, FubuMVC.StructureMap&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;Spark.ISparkViewEngine&lt;/td&gt;        &lt;td class="fubutablecell"&gt;Spark.SparkViewEngine&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;StructureMap.IContainer&lt;/td&gt;        &lt;td class="fubutablecell"&gt;StructureMap.Container&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;System.Web.HttpContextBase&lt;/td&gt;        &lt;td class="fubutablecell"&gt;System.Web.HttpContextWrapper, System.Web&lt;/td&gt;     &lt;/tr&gt;      &lt;tr class="fubutablerow"&gt;       &lt;td class="fubutablecell"&gt;System.Web.HttpRequestWrapper&lt;/td&gt;        &lt;td class="fubutablecell"&gt;Instance is created by Func&amp;lt;object&amp;gt; function:System.Func`2[StructureMap.IContext,System.Web.HttpRequestWrapper]&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-6474078459296581767?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/Kk86-MSIKF4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/6474078459296581767/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/06/welcome-to-fubumvc-cafeteria.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/6474078459296581767?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/6474078459296581767?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/Kk86-MSIKF4/welcome-to-fubumvc-cafeteria.html" title="Welcome to the FubuMVC Cafeteria" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>1</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/06/welcome-to-fubumvc-cafeteria.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQHR3wzfip7ImA9WhZQE0g.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-6749811842623789645</id><published>2011-04-20T18:39:00.001-07:00</published><updated>2011-04-20T18:45:36.286-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-20T18:45:36.286-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC3" /><title>Building a Better MVC Filter Attribute Provider</title><content type="html">&lt;p&gt;As I mentioned &lt;a href="http://blog.mikeobrien.net/2011/04/on-filters-in-mvc3.html"&gt;here&lt;/a&gt; the filter attribute design in MVC is terrible. That other post explains why so I’m not going to rehash. This post is about how we can do it better. I don’t really have anything against using the attributes to annotate an action or controller it’s mainly that MVC makes the attribute itself the filter. What if, instead, the attribute was simply a marker that said that a particular filter should be applied and allowed you to customize it:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IFilterAttribute
&lt;/span&gt;{
    &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;FilterType { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;void &lt;/span&gt;InitializeFilter(&lt;span style="color: blue"&gt;object &lt;/span&gt;filter);
}&lt;/pre&gt;


&lt;p&gt;This interface specifies the filter type then a method to initialize the filter. We could then implement this interface on an attribute like so:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SecureAttribute &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Attribute&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IFilterAttribute
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private readonly bool &lt;/span&gt;_enabled;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;SecureAttribute(&lt;span style="color: blue"&gt;bool &lt;/span&gt;enabled)
    {
        _enabled = enabled;
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;FilterType { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;SecureFilter&lt;/span&gt;); } }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;InitializeFilter(&lt;span style="color: blue"&gt;object &lt;/span&gt;filter)
    {
        ((&lt;span style="color: #2b91af"&gt;SecureFilter&lt;/span&gt;) filter).Enabled = _enabled;
    }
}&lt;/pre&gt;

&lt;p&gt;This attribute allows us to turn security on and off on an action or controller. You can see that it specifies the type and initialization of the filter. The filter is nothing special; in this case it implements IMvcFilter so it can set the multiplicity (In this case we only want one filter to be applied) and IAuthorizationFilter for authentication. It is also setup to be constructor injected:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SecureFilter &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IAuthorizationFilter&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IMvcFilter
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IAuthenticationService &lt;/span&gt;_authenticationService;

    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;Enabled { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;SecureFilter(&lt;span style="color: #2b91af"&gt;IAuthenticationService &lt;/span&gt;authenticationService)
    {
        _authenticationService = authenticationService;
        Enabled = &lt;span style="color: blue"&gt;true&lt;/span&gt;;
    }

    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;AllowMultiple { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return false&lt;/span&gt;; } }
    &lt;span style="color: blue"&gt;public int &lt;/span&gt;Order { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;OnAuthorization(&lt;span style="color: #2b91af"&gt;AuthorizationContext &lt;/span&gt;filterContext)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(Enabled &amp;amp;&amp;amp; !_authenticationService.Authenticate())
        {
            &lt;span style="color: green"&gt;// Access denied
        &lt;/span&gt;}
    }
}&lt;/pre&gt;


&lt;p&gt;Now our filter provider can look for this new interface, IFilterAttribute and create the filters from the information provided from the attribute:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FilterAttributeProvider &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IFilterProvider
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata&lt;/span&gt;&amp;gt;&amp;gt; AttributeCache =
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata&lt;/span&gt;&amp;gt;&amp;gt;();

    &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; _filterFactory;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;FilterAttributeProvider(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; filterFactory)
    {
        _filterFactory = filterFactory;
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Filter&lt;/span&gt;&amp;gt; GetFilters(&lt;span style="color: #2b91af"&gt;ControllerContext &lt;/span&gt;controllerContext, &lt;span style="color: #2b91af"&gt;ActionDescriptor &lt;/span&gt;actionDescriptor)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;GetFilterAttributes(actionDescriptor).Select(x =&amp;gt; CreateFilter(x, _filterFactory));
    }

    &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata&lt;/span&gt;&amp;gt; GetFilterAttributes(&lt;span style="color: #2b91af"&gt;ActionDescriptor &lt;/span&gt;actionDescriptor)
    {
        &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata&lt;/span&gt;&amp;gt; attributes;
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(!AttributeCache.TryGetValue(actionDescriptor.UniqueId, &lt;span style="color: blue"&gt;out &lt;/span&gt;attributes))
        {
            attributes = LoadFilterAttributes(actionDescriptor);
            &lt;span style="color: blue"&gt;lock &lt;/span&gt;(AttributeCache) AttributeCache.Add(actionDescriptor.UniqueId, attributes);
        }
        &lt;span style="color: blue"&gt;return &lt;/span&gt;attributes;
    }

    &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata&lt;/span&gt;&amp;gt; LoadFilterAttributes(&lt;span style="color: #2b91af"&gt;ActionDescriptor &lt;/span&gt;actionDescriptor)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;actionDescriptor.GetCustomAttributes(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IFilterAttribute&lt;/span&gt;), &lt;span style="color: blue"&gt;true&lt;/span&gt;).Cast&amp;lt;&lt;span style="color: #2b91af"&gt;IFilterAttribute&lt;/span&gt;&amp;gt;().
                                Select(x =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata&lt;/span&gt;(x, &lt;span style="color: #2b91af"&gt;FilterScope&lt;/span&gt;.Action)).
                   Union(actionDescriptor.ControllerDescriptor.GetCustomAttributes(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IFilterAttribute&lt;/span&gt;), &lt;span style="color: blue"&gt;true&lt;/span&gt;).
                                Cast&amp;lt;&lt;span style="color: #2b91af"&gt;IFilterAttribute&lt;/span&gt;&amp;gt;().
                                Select(x =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata&lt;/span&gt;(x, &lt;span style="color: #2b91af"&gt;FilterScope&lt;/span&gt;.Controller)));
    }

    &lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Filter &lt;/span&gt;CreateFilter(&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata &lt;/span&gt;filterMetadata, 
                                       &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; filterFactory)
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;filter = filterFactory(filterMetadata.Attribute.FilterType);
        filterMetadata.Attribute.InitializeFilter(filter);
        &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Filter&lt;/span&gt;(filter, filterMetadata.Scope, GetFilterOrder(filter));
    }

    &lt;span style="color: blue"&gt;private static int&lt;/span&gt;? GetFilterOrder(&lt;span style="color: blue"&gt;object &lt;/span&gt;filter)
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;mvcFilter = filter &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IMvcFilter&lt;/span&gt;;
        &lt;span style="color: blue"&gt;return &lt;/span&gt;mvcFilter != &lt;span style="color: blue"&gt;null &lt;/span&gt;? mvcFilter.Order : (&lt;span style="color: blue"&gt;int&lt;/span&gt;?)&lt;span style="color: blue"&gt;null&lt;/span&gt;;
    }

    &lt;span style="color: blue"&gt;private class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FilterAttributeMetadata
    &lt;/span&gt;{
        &lt;span style="color: blue"&gt;public &lt;/span&gt;FilterAttributeMetadata(&lt;span style="color: #2b91af"&gt;IFilterAttribute &lt;/span&gt;attribute, &lt;span style="color: #2b91af"&gt;FilterScope &lt;/span&gt;scope)
        { Attribute = attribute; Scope = scope; }

        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IFilterAttribute &lt;/span&gt;Attribute { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FilterScope &lt;/span&gt;Scope { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }
    }

    &lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IFilterAttribute
    &lt;/span&gt;{
        &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;FilterType { &lt;span style="color: blue"&gt;get&lt;/span&gt;; }
        &lt;span style="color: blue"&gt;void &lt;/span&gt;InitializeFilter(&lt;span style="color: blue"&gt;object &lt;/span&gt;filter);
    }
}&lt;/pre&gt;

&lt;p&gt;Using the type on the attribute, the provider can create the filter with a factory passed into the constructor (Which would be our IoC container). Once the filter is created it is initialized by the InitializeFilter() method on the attribute. This completely decouples attributes from filters and allows us to constructor inject filters even when they are applied via attributes. So as an example lets say that we want to globally apply security but in some actions or controllers we want to turn it off so they are public. We can register the secure filter we defined above and our authentication service in our IoC container (In this example StructureMap):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MvcApplication &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;HttpApplication
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;protected void &lt;/span&gt;Application_Start()
    {
        &lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.Configure(x =&amp;gt;
                                    {
                                        x.For&amp;lt;&lt;span style="color: #2b91af"&gt;IAuthenticationService&lt;/span&gt;&amp;gt;().Use&amp;lt;&lt;span style="color: #2b91af"&gt;AuthenticationService&lt;/span&gt;&amp;gt;();
                                        x.For&amp;lt;&lt;span style="color: #2b91af"&gt;IAuthorizationFilter&lt;/span&gt;&amp;gt;().Use&amp;lt;&lt;span style="color: #2b91af"&gt;SecureFilter&lt;/span&gt;&amp;gt;();
                                    });

        &lt;span style="color: #2b91af"&gt;DependencyResolver&lt;/span&gt;.SetResolver(x =&amp;gt; (x.IsAbstract || x.IsInterface) ?
                                                &lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.TryGetInstance(x) :
                                                &lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.GetInstance(x), 
                                       x =&amp;gt; &lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.GetAllInstances(x).Cast&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;());

        &lt;span style="color: #2b91af"&gt;FilterProviders&lt;/span&gt;.Providers.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FilterAttributeProvider&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.GetInstance));
    }
}&lt;/pre&gt;


&lt;p&gt;Now our entire site requires authentication. For an action that we want public we can turn authentication off:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HomeController &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Controller
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;Secure&lt;/span&gt;(&lt;span style="color: blue"&gt;false&lt;/span&gt;)]
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ActionResult &lt;/span&gt;Index()
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;View();
    }
}&lt;/pre&gt;


&lt;p&gt;We only allow one SecureFilter to be applied, so since the filter associated with the attribute has an “Action” scope it will take priority over the global filter which has a “Global” scope.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-6749811842623789645?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/rizBhYVGSFg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/6749811842623789645/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/04/building-better-mvc-filter-attribute.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/6749811842623789645?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/6749811842623789645?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/rizBhYVGSFg/building-better-mvc-filter-attribute.html" title="Building a Better MVC Filter Attribute Provider" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>1</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/04/building-better-mvc-filter-attribute.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEUMR3c5fip7ImA9WhZQE0g.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-5459007266781147002</id><published>2011-04-18T16:37:00.001-07:00</published><updated>2011-04-20T19:18:06.926-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-20T19:18:06.926-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC3" /><title>Creating an Overridable MVC3 Global Filter</title><content type="html">&lt;p&gt;The new global filters in MVC3 are a nice way to apply behavior globally. Sometimes though you’ll want to opt out an action or controller from that behavior. Lets take authentication as an example; we want everything to be secure by default but we may have individual controllers or actions that will be public. The following authorize attribute enables us to create a global filter and then override the global behavior where we need to:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;// Make sure we specify that this attribute should only be specified once.
&lt;/span&gt;[&lt;span style="color: #2b91af"&gt;AttributeUsage&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;AttributeTargets&lt;/span&gt;.Method, AllowMultiple = &lt;span style="color: blue"&gt;false&lt;/span&gt;)]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SecureAttribute &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;AuthorizeAttribute
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private readonly bool &lt;/span&gt;_enabled;

    &lt;span style="color: green"&gt;// Default constructor with security enabled, used as a global filter
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;SecureAttribute()
    {
        _enabled = &lt;span style="color: blue"&gt;true&lt;/span&gt;;
    }

    &lt;span style="color: green"&gt;// Attribute constructor
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;SecureAttribute(&lt;span style="color: blue"&gt;bool &lt;/span&gt;enabled)
    {
        _enabled = enabled;
    }

    &lt;span style="color: blue"&gt;protected override bool &lt;/span&gt;AuthorizeCore(System.Web.&lt;span style="color: #2b91af"&gt;HttpContextBase &lt;/span&gt;httpContext)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;!_enabled || &lt;span style="color: #2b91af"&gt;AuthenticationService&lt;/span&gt;.Authenticate();
    }
}&lt;/pre&gt;

&lt;p&gt;A few things to note. First we need to specify that this attribute should only be applied once with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx"&gt;AttributeUsageAttribute&lt;/a&gt;. Under the covers the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattribute.aspx"&gt;FilterAttribute&lt;/a&gt; class looks for this attribute to determine if multiple instances of the filter can exist. In the default constructor we specify the default behavior; in this case security being enabled. In our authorization method we can check to see if authentication is enabled and proceed accordingly. &lt;/p&gt;

&lt;p&gt;Here we can register the global default. This filter will have a scope of “Global” which is a lower priority scope:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MvcApplication &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;HttpApplication
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;protected void &lt;/span&gt;Application_Start()
    {
        &lt;span style="color: #2b91af"&gt;GlobalFilters&lt;/span&gt;.Filters.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SecureAttribute&lt;/span&gt;());
        &lt;span style="color: green"&gt;// ...
&lt;/span&gt;    }
}&lt;/pre&gt;

&lt;p&gt;We can then override the default behavior where appropriate. The action attribute has a scope of “Action” which is a higher priority scope. So this filter will take precedence over the globally added one:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SomeController &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Controller
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;Secure&lt;/span&gt;(&lt;span style="color: blue"&gt;false&lt;/span&gt;)]
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ActionResult &lt;/span&gt;SomePublicAction()
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;View();
    }
}&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-5459007266781147002?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/ffBOZAHcH-w" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/5459007266781147002/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/04/creating-overridable-mvc3-global-action.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/5459007266781147002?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/5459007266781147002?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/ffBOZAHcH-w/creating-overridable-mvc3-global-action.html" title="Creating an Overridable MVC3 Global Filter" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/04/creating-overridable-mvc3-global-action.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkMDSXo_eyp7ImA9WhZQEUo.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-3335367349920331437</id><published>2011-04-15T16:41:00.001-07:00</published><updated>2011-04-18T16:47:58.443-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-18T16:47:58.443-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC3" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET 4.0" /><title>On Filters in MVC3</title><content type="html">&lt;p&gt;I’ve been digging into the new filter functionality in MVC3 and this is a brain dump of what I’ve learned and some thoughts.&lt;/p&gt;  &lt;p&gt;Filters currently must implement one or more of these interfaces: &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.iactionfilter.aspx"&gt;IActionFilter&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.iresultfilter.aspx"&gt;IResultFilter, &lt;/a&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.iexceptionfilter.aspx"&gt;IExceptionFilter&lt;/a&gt;, and &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.iauthorizationfilter.aspx"&gt;IAuthorizationFilter&lt;/font&gt;&lt;/a&gt;. The &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.imvcfilter.aspx"&gt;IMvcFilter&lt;/a&gt; interface allows you to tack on some additional metadata like the sort order and multiplicity. There are a couple of convenience classes that make creating filter &lt;strong&gt;&lt;em&gt;attributes&lt;/em&gt;&lt;/strong&gt; easier: &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattribute.aspx"&gt;FilterAttribute&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute.aspx"&gt;ActionFilterAttribute&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx"&gt;AuthorizeAttribute&lt;/a&gt;, &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx"&gt;HandleErrorAttribute&lt;/a&gt; and a few more. FilterAttribute implements &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.imvcfilter.allowmultiple.aspx"&gt;IMvcFilter&lt;/a&gt; and so provides that metadata out of the box. When deriving from any of these attribute classes its important to understand that disallowing multiple instances of a filter is accomplished by applying the &lt;span style="widows: 2; text-transform: none; text-indent: 0px; letter-spacing: normal; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="text-align: left; line-height: 18px; font-family: &amp;#39;Trebuchet MS&amp;#39;, arial; font-size: small; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class="Apple-style-span"&gt;&lt;a style="background-color: transparent; color: rgb(0,50,77); font-weight: bold; text-decoration: underline; background-origin: initial; background-clip: initial" href="http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx"&gt;AttributeUsageAttribute&lt;/a&gt; to your derived class and setting &lt;a href="http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.allowmultiple.aspx"&gt;AllowMultiple&lt;/a&gt; to false. The &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattribute.aspx"&gt;FilterAttribute&lt;/a&gt; class looks for that attribute to determine multiplicity and exposes it through the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.imvcfilter.allowmultiple.aspx"&gt;IMvcFilter.AllowMultiple property&lt;/a&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;Originally filters (Which is a terrible name BTW. What do they filter??) were applied as .NET attributes to actions or controllers. At the beginning of the request, the controller and action filter attributes were pulled from the action method and controller class and run. One thing to note is that .NET attributes are only created by the CLR once (they are essentially singletons) so every time you reflect them in a particular app domain you will get the same object instances. I think the whole attribute approach for filters is badly implemented. One of the problems is that if you want to inject dependencies into an filter you have to rig up property injection. Additionally since they are attributes, and essentially singletons, they cannot hold any request specific state. This may be fine for something like authentication but difficult for transaction management as you will not be able to use IoC to inject request specific dependencies. I think a better design would have been to make the attributes more like WCF behaviors where the attribute itself is not the filter but simply acts as a way to create the filter. But that’s neither here nor there.&lt;/p&gt;  &lt;p&gt;Now moving on to MVC3; things have changed a bit. We now have this concept of the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.ifilterprovider.aspx"&gt;IFilterProvider&lt;/a&gt; interface. This interface has one method, GetFilters() which as you can probably guess returns filters. Filter providers are now the source of all filters. There are three filter providers &lt;a href="http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html"&gt;registered out of the box&lt;/a&gt;: &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.globalfilters.filters.aspx"&gt;GlobalFilters.Filters&lt;/a&gt;:&lt;/strong&gt; This is simply a place where you can ad-hoc register individual filters that will be applied globally. These should be stateless as they will be reused for each request. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattributefilterprovider.aspx"&gt;FilterAttributeFilterProvider&lt;/a&gt;:&lt;/strong&gt; This provider uses reflection to pull filters applied as attributes to controller classes and action methods; it basically does the job that the pre MVC3 code did. So the filters that this provider returns are not global, they are scoped to the controller and action. Since they are attributes they are essentially singletons and will be reused for each request so they should be stateless. Also keep in mind that the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattributefilterprovider.aspx"&gt;FilterAttributeFilterProvider&lt;/a&gt; will only discover attributes that derive from &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattribute.aspx"&gt;FilterAttribute&lt;/a&gt;. So deriving from &lt;a href="http://msdn.microsoft.com/en-us/library/system.attribute.aspx"&gt;Attribute&lt;/a&gt; and then implementing a filter interface will not get your attribute picked up by this provider. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerinstancefilterprovider(v=vs.98).aspx"&gt;ControllerInstanceFilterProvider&lt;/a&gt;:&lt;/strong&gt; This provider allows the controller servicing the request to act as a filter (ee gads!). &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;MVC calls the FilterProviders.Providers.GetFilters() method, to aggregate all the filters from all the filter providers, for each individual request. You can also register your own filter providers by adding an instance of a type that implements IFilterProvider to the FilterProviders.Providers collection. FilterProviders wrap filters in the Filter class which adds some additional information such as the scope and order. You can override the order in the Filter class constructor if you are writing your own filter provider or extending an existing one. If you don’t pass in an order it will check and see if the filter implements IMvcFilter and get the order from there otherwise it defaults to -1. One interesting thing about the code that aggregates all these is that it removes duplicate filters for those filters that do not allow duplicates (Exposed by the IMvcFilter.AllowMultiple property). The way it determines which ones to remove is by sorting the filters by the order and then by the scope and preserving the first instance it encounters and rejecting the subsequent instances. This means that higher orders win and then within an order, narrower scopes will win (Which is actually a higher scope number).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blog.mikeobrien.net/2011/04/building-better-mvc-dependency-resolver.html"&gt;This post&lt;/a&gt; demonstrates how to pull global filters from your favorite IoC container. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-3335367349920331437?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/JXu-9l9xXT0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/3335367349920331437/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/04/on-filters-in-mvc3.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3335367349920331437?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3335367349920331437?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/JXu-9l9xXT0/on-filters-in-mvc3.html" title="On Filters in MVC3" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/04/on-filters-in-mvc3.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkIHSHw4fip7ImA9WhZQE0k.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-994344980882693362</id><published>2011-04-08T13:24:00.001-07:00</published><updated>2011-04-20T16:02:19.236-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-20T16:02:19.236-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Dependency Injection" /><category scheme="http://www.blogger.com/atom/ns#" term="StructureMap" /><category scheme="http://www.blogger.com/atom/ns#" term="ASP.NET MVC" /><title>Building a Better MVC Dependency Resolver</title><content type="html">&lt;p&gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;&lt;em&gt;Updated to include global filters.&lt;/em&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Unfortunately the latest effort to add DI to MVC falls short in a number of ways. There are 3 areas in particular that are causing me pain:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;No distinction between infrastructure and a request. &lt;/li&gt;    &lt;li&gt;No container per request. &lt;/li&gt;    &lt;li&gt;No request container create and release mechanism. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;In order to accommodate functionality specific to a request there would need to be a distinction between building up infrastructure related objects (created once at startup, e.g. IControllerFactory, IControllerActivator, etc.) and request related objects (created for each request, e.g. controllers, views, etc.) Unfortunately, all objects are built up from the same two methods regardless of if they’re infrastructure or request related. I use a StructureMap nested container per request. I want all dependencies for an individual request to be managed by that nested container. Also once the request completes I want to “clean up” the nested container (Like commit a transaction or whatever). Currently the dependency resolver offers no way to do this. Here is a better interface IMO:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDependencyResolver
&lt;/span&gt;{
    &lt;span style="color: green"&gt;// Infrastructure
    &lt;/span&gt;&lt;span style="color: blue"&gt;object &lt;/span&gt;GetInfrastructureService(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType);
    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetInfrastructureervices(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType);

    &lt;span style="color: green"&gt;// Per-request
    &lt;/span&gt;&lt;span style="color: blue"&gt;object &lt;/span&gt;CreateActionContainer();
    &lt;span style="color: blue"&gt;object &lt;/span&gt;GetActionService(&lt;span style="color: blue"&gt;object &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType);
    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetActionServices(&lt;span style="color: blue"&gt;object &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType);;
    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetActionFilters(&lt;span style="color: #2b91af"&gt;&lt;span style="color: #2b91af"&gt;&lt;span style="color: #2b91af"&gt;IContainer &lt;/span&gt;&lt;font color="#000000"&gt;container, &lt;/font&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&lt;font color="#000000"&gt;&amp;lt;&lt;/font&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;&lt;font color="#000000"&gt;&amp;gt; filterTypes&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;);
    &lt;span style="color: blue"&gt;void &lt;/span&gt;ReleaseActionContainer(&lt;span style="color: blue"&gt;object &lt;/span&gt;container);
}&lt;/pre&gt;

&lt;p&gt;I’m sure its unlikely that we’ll ever see anything like this from the MVC team but we can put together something on top of the current interface that gets us there:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public abstract class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DependencyResolverBase&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;IDependencyResolver&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;IFilterProvider &lt;/span&gt;&lt;span style="color: blue"&gt;where &lt;/span&gt;T : &lt;span style="color: blue"&gt;class
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private const string &lt;/span&gt;MetadataKey = &lt;span style="color: #a31515"&gt;&amp;quot;___DependencyResolverContainer___&amp;quot;&lt;/span&gt;;
    &lt;span style="color: blue"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;[] ActionFilterTypes = &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IAuthorizationFilter&lt;/span&gt;), &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IActionFilter&lt;/span&gt;), 
                                                               &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IResultFilter&lt;/span&gt;), &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IExceptionFilter&lt;/span&gt;) };
    &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IDictionary&lt;/span&gt;&amp;gt; _getRequestMetadata ;

    &lt;span style="color: blue"&gt;protected &lt;/span&gt;DependencyResolverBase()
    {
        _getRequestMetadata = () =&amp;gt; &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.Items;
    }

    &lt;span style="color: blue"&gt;protected &lt;/span&gt;DependencyResolverBase(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IDictionary&lt;/span&gt;&amp;gt; getRequestMetadata)
    {
        _getRequestMetadata = getRequestMetadata;
    }

    &lt;span style="color: green"&gt;// Infrastructure Services
    &lt;/span&gt;&lt;span style="color: blue"&gt;public abstract object &lt;/span&gt;GetInfrastructureService(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType);
    &lt;span style="color: blue"&gt;public abstract &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetInfrastructureServices(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType); 

    &lt;span style="color: green"&gt;// Action Services
    &lt;/span&gt;&lt;span style="color: blue"&gt;public abstract &lt;/span&gt;T CreateActionContainer();
    &lt;span style="color: blue"&gt;public abstract object &lt;/span&gt;GetActionService(T container, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType);
    &lt;span style="color: blue"&gt;public abstract &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetActionServices(T container, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType);
    &lt;span style="color: blue"&gt;public abstract &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetActionFilters(T container, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;&amp;gt; filterTypes);
    &lt;span style="color: blue"&gt;public abstract void &lt;/span&gt;ReleaseActionContainer(T container);
 
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;ReleaseActionContainer()
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;container = CurrentContainer;
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(container != &lt;span style="color: blue"&gt;null&lt;/span&gt;) ReleaseActionContainer(container);            
    }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;RegisterHttpApplication(&lt;span style="color: #2b91af"&gt;HttpApplication &lt;/span&gt;application)
    {
        application.EndRequest += (s, e) =&amp;gt; ReleaseActionContainer();
    }

    &lt;span style="color: blue"&gt;object &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IDependencyResolver&lt;/span&gt;.GetService(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;IsInfrastructureService(serviceType) ?
            GetInfrastructureService(serviceType) : 
            GetActionService(GetActionContainer(), serviceType);
    }

    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; &lt;span style="color: #2b91af"&gt;IDependencyResolver&lt;/span&gt;.GetServices(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;IsInfrastructureService(serviceType) ?
            GetInfrastructureServices(serviceType).
                Union(GetBuiltInInfrastructureServices(serviceType)) :
            GetActionServices(GetActionContainer(), serviceType);
    }

    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Filter&lt;/span&gt;&amp;gt; &lt;span style="color: #2b91af"&gt;IFilterProvider&lt;/span&gt;.GetFilters(&lt;span style="color: #2b91af"&gt;ControllerContext &lt;/span&gt;controllerContext, 
                                                   &lt;span style="color: #2b91af"&gt;ActionDescriptor &lt;/span&gt;actionDescriptor)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;GetActionFilters(GetActionContainer(), ActionFilterTypes, 
                                controllerContext, actionDescriptor).
                    Select(x =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Filter&lt;/span&gt;(x, &lt;span style="color: #2b91af"&gt;FilterScope&lt;/span&gt;.Global, &lt;span style="color: blue"&gt;null&lt;/span&gt;));
    }

    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetBuiltInInfrastructureServices(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;serviceType == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IFilterProvider&lt;/span&gt;) ? 
                        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; {&lt;span style="color: blue"&gt;this&lt;/span&gt;}:
                        &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Empty&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();
    }

    &lt;span style="color: blue"&gt;private &lt;/span&gt;T GetActionContainer()
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;container = CurrentContainer;
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(container == &lt;span style="color: blue"&gt;null&lt;/span&gt;) CurrentContainer = container = CreateActionContainer();
        &lt;span style="color: blue"&gt;return &lt;/span&gt;container;
    }

    &lt;span style="color: blue"&gt;private &lt;/span&gt;T CurrentContainer
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;(T)_getRequestMetadata()[MetadataKey]; }
        &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _getRequestMetadata()[MetadataKey] = &lt;span style="color: blue"&gt;value&lt;/span&gt;; }
    }       

    &lt;span style="color: blue"&gt;private static bool &lt;/span&gt;IsInfrastructureService(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;type)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;type.Namespace != &lt;span style="color: blue"&gt;null &lt;/span&gt;&amp;amp;&amp;amp; type.Namespace.StartsWith(&lt;span style="color: #a31515"&gt;&amp;quot;System.Web.Mvc&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;

&lt;p&gt;This class uses the template method so you can fill in the details in a derived class. Here is an example with StructureMap:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StructureMapDependencyResolver &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;DependencyResolverBase&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IContainer&lt;/span&gt;&amp;gt;
{
    &lt;span style="color: blue"&gt;public override object &lt;/span&gt;GetInfrastructureService(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;Resolve(&lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.Container, serviceType);
    }

    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetInfrastructureServices(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.Container.GetAllInstances(serviceType).Cast&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();
    }

    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IContainer &lt;/span&gt;CreateActionContainer()
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.Container.GetNestedContainer();
    }

    &lt;span style="color: blue"&gt;public override object &lt;/span&gt;GetActionService(&lt;span style="color: #2b91af"&gt;IContainer &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;Resolve(container, serviceType);
    }

    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetActionServices(&lt;span style="color: #2b91af"&gt;IContainer &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;container.GetAllInstances(serviceType).Cast&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;();
    }

    &lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetActionFilters(&lt;span style="color: #2b91af"&gt;IContainer &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;&amp;gt; filterTypes)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;container.Model.AllInstances.
            Join(filterTypes, x =&amp;gt; x.PluginType, y =&amp;gt; y, (x, y) =&amp;gt; x).
            Select(x =&amp;gt; x.ConcreteType).
            Distinct().
            Select(container.GetInstance);
    }

    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;ReleaseActionContainer(&lt;span style="color: #2b91af"&gt;IContainer &lt;/span&gt;container)
    {
        container.Dispose();
    }

    &lt;span style="color: blue"&gt;private static object &lt;/span&gt;Resolve(&lt;span style="color: #2b91af"&gt;IContainer &lt;/span&gt;container, &lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;(serviceType.IsAbstract || serviceType.IsInterface) ?
            container.TryGetInstance(serviceType) :
            container.GetInstance(serviceType);
    }
}&lt;/pre&gt;

&lt;p&gt;Registration is pretty straight forward. The DependencyResolverBase class needs to subscribe to the request end event in order to release the container. This event needs to be subscribed to for each http application (And there can be many) thus the override of Init(). The dependency resolver automatically registers itself as a filter provider so no need to manually register it.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MvcApplication &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;HttpApplication
&lt;/span&gt;{
    /...

    &lt;span style="color: blue"&gt;protected void &lt;/span&gt;Application_Start()
    {
        &lt;span style="color: #2b91af"&gt;ObjectFactory&lt;/span&gt;.Configure(x =&amp;gt;
                                    {
                                        x.For&amp;lt;&lt;span style="color: #2b91af"&gt;IDatabase&lt;/span&gt;&amp;gt;().Use&amp;lt;&lt;span style="color: #2b91af"&gt;MongoDB&lt;/span&gt;&amp;gt;();
                                        x.For&amp;lt;&lt;span style="color: #2b91af"&gt;IActionFilter&lt;/span&gt;&amp;gt;().Use&amp;lt;&lt;span style="color: #2b91af"&gt;SomeFilter&lt;/span&gt;&amp;gt;();
                                        x.For&amp;lt;&lt;span style="color: #2b91af"&gt;IResultFilter&lt;/span&gt;&amp;gt;().Use&amp;lt;&lt;span style="color: #2b91af"&gt;SomeFilter&lt;/span&gt;&amp;gt;();
                                    });

        &lt;span style="color: blue"&gt;var &lt;/span&gt;dependencyProvider = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StructureMapDependencyResolver&lt;/span&gt;();
        &lt;span style="color: #2b91af"&gt;DependencyResolver&lt;/span&gt;.SetResolver(dependencyProvider);
        &lt;span style="color: green"&gt;// ...
    &lt;/span&gt;}

    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Init()
    {
        ((&lt;span style="color: #2b91af"&gt;StructureMapDependencyResolver&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;DependencyResolver&lt;/span&gt;.Current).RegisterHttpApplication(&lt;span style="color: blue"&gt;this&lt;/span&gt;);
    }
}&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-994344980882693362?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/A6awhBzBRAo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/994344980882693362/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/04/building-better-mvc-dependency-resolver.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/994344980882693362?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/994344980882693362?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/A6awhBzBRAo/building-better-mvc-dependency-resolver.html" title="Building a Better MVC Dependency Resolver" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>3</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/04/building-better-mvc-dependency-resolver.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0MBQHo9cCp7ImA9WhZSFko.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-6741894068831275867</id><published>2011-04-01T10:37:00.001-07:00</published><updated>2011-04-01T10:37:31.468-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-01T10:37:31.468-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Git" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows" /><title>Adding the Current Time to the Git Bash Prompt on Windows</title><content type="html">&lt;p&gt;Many times I’ve wanted to look back a my git bash shell to see *when* I did something. Turns out its pretty trivial to modify the prompt to your liking. &lt;a href="http://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html"&gt;This page covers all sorts of things you can do&lt;/a&gt;. In order to make the prompt format permanent you’ll have to modify it in the profile here: [Program Files]\Git\etc\profile. Scroll down to about line 160 and you’ll see the prompt format. I wanted to just replace the current user with the current time, \@ (See the highlighted portion):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_DOioD_zaWYc/TZYNRclh7DI/AAAAAAAAAiA/7DuyZjVdbIQ/s1600-h/image%5B5%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_DOioD_zaWYc/TZYNS1r6odI/AAAAAAAAAiE/9ayd0qoGvm8/image_thumb%5B3%5D.png?imgmax=800" width="659" height="374" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_DOioD_zaWYc/TZYNTl9z37I/AAAAAAAAAiI/YfwPxz1SSmA/s1600-h/image%5B11%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_DOioD_zaWYc/TZYNVNEO-2I/AAAAAAAAAiM/2VcxSRQNU3E/image_thumb%5B7%5D.png?imgmax=800" width="659" height="374" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Save these changes and open a new git bash shell:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_DOioD_zaWYc/TZYNVWNt3vI/AAAAAAAAAiQ/cr4MyM9KCe8/s1600-h/image%5B18%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_DOioD_zaWYc/TZYNVhOt6qI/AAAAAAAAAiU/V4yc6R_xvHg/image_thumb%5B12%5D.png?imgmax=800" width="597" height="227" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-6741894068831275867?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/tAox7gBLZ68" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/6741894068831275867/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/04/adding-current-time-to-git-bash-prompt.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/6741894068831275867?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/6741894068831275867?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/tAox7gBLZ68/adding-current-time-to-git-bash-prompt.html" title="Adding the Current Time to the Git Bash Prompt on Windows" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_DOioD_zaWYc/TZYNS1r6odI/AAAAAAAAAiE/9ayd0qoGvm8/s72-c/image_thumb%5B3%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/04/adding-current-time-to-git-bash-prompt.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0UDQnk8eSp7ImA9WhZTFkg.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-9136462858178339019</id><published>2011-03-09T16:18:00.001-08:00</published><updated>2011-03-20T15:14:33.771-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-20T15:14:33.771-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET 4.0" /><category scheme="http://www.blogger.com/atom/ns#" term="WCF" /><title>Setting up a Secure WCF Service that uses a UserNamePasswordValidator and runs on Cassini</title><content type="html">&lt;p&gt;I spent the better part of today trying to figure this out so here is the brain dump. Basically I needed to do some prototyping that required a web service that used a custom UserNamePasswordValidator. Normally I have SSL setup in IIS and use credentials over a secured transport layer but I just wanted to whip up something simple for my prototype that would run in Cassini (Yes I know about IIS Express, haven't had time to try it out so it may have been a better route). Although WCF supposedly supports a &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.securitybindingelement.allowinsecuretransport.aspx"&gt;new option to enable credentials over any transport&lt;/a&gt;, unsecured or not, it’s the usual &lt;a href="http://blogs.msdn.com/b/distributedservices/archive/2010/05/13/wcf-and-intermediate-devices.aspx"&gt;half baked Microsoft “feature” that doesn't really get you anywhere&lt;/a&gt; and wont generate the WSDL (I’m lazy and I didn't want to wire up the client config by hand). In any event you can just use message security and forgo the transport level security with this configuration:&lt;/p&gt;  &lt;pre class="code"&gt;  &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;wsHttpBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;binding &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;UsernameAuthentication&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
          &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;security &lt;/span&gt;&lt;span style="color: red"&gt;mode&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Message&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;message &lt;/span&gt;&lt;span style="color: red"&gt;clientCredentialType&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;UserName&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
          &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;security&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;binding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;wsHttpBinding&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; 
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;bindings&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;services&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;service &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;WebServices.ContactService&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;behaviorConfiguration&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&lt;span style="color: blue"&gt;UsernameAuthentication&lt;/span&gt;&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;endpoint &lt;/span&gt;&lt;span style="color: red"&gt;address&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&amp;quot; &lt;span style="color: red"&gt;binding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;wsHttpBinding&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;bindingConfiguration&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&lt;span style="color: blue"&gt;&lt;span style="color: blue"&gt;UsernameAuthentication&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;quot; 
                  &lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;WebServices.ContactService&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;bindingNamespace&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;urn:Contacts&lt;/span&gt;&amp;quot; 
                  &lt;span style="color: red"&gt;contract&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;WebServices.IContactService&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;service&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;services&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;behaviors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;behavior &lt;/span&gt;&lt;span style="color: red"&gt;name&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&lt;span style="color: blue"&gt;UsernameAuthentication&lt;/span&gt;&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;
          &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;serviceMetadata &lt;/span&gt;&lt;span style="color: red"&gt;httpGetEnabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;true&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;serviceCredentials&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;userNameAuthentication &lt;/span&gt;&lt;span style="color: red"&gt;userNamePasswordValidationMode&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;Custom&lt;/span&gt;&amp;quot; &lt;br /&gt;&lt;span style="color: red"&gt;                customUserNamePasswordValidatorType&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;WebServices.UserNamePasswordValidator, WebServices&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;serviceCertificate &lt;/span&gt;&lt;span style="color: red"&gt;storeName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;My&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;storeLocation&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;LocalMachine&lt;/span&gt;&amp;quot; &lt;br /&gt;&lt;span style="color: red"&gt;                x509FindType&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;FindBySubjectName&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;findValue&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;MyPrototype&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
            &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;clientCertificate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
              &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;authentication &lt;/span&gt;&lt;span style="color: red"&gt;certificateValidationMode&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;None&lt;/span&gt;&amp;quot; &lt;span style="color: red"&gt;revocationMode&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;NoCheck&lt;/span&gt;&amp;quot; &lt;span style="color: blue"&gt;/&amp;gt;
            &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;clientCertificate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
          &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;serviceCredentials&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;span style="color: blue"&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;behavior&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
      &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;serviceBehaviors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;behaviors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;system.serviceModel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now the only other step is that you have to generate an X509 cert. I created a batch file to do this automatically (Requires the Windows/.NET SDK):&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;certutil.exe -delstore My MyPrototype&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;&amp;quot;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\makecert.exe&amp;quot; -sr LocalMachine -ss My -a sha1 -n CN=MyPrototype -sky exchange –pe&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;The last thing I needed was for the client not to try and validate the cert (As the CA cannot be validated):&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Contacts_Test()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;contacts = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ContactServiceClient&lt;/span&gt;();
    &lt;font color="#ff0000"&gt;contacts.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;&lt;/font&gt;
    contacts.ClientCredentials.UserName.UserName = &lt;span style="color: #a31515"&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;;
    contacts.ClientCredentials.UserName.Password = &lt;span style="color: #a31515"&gt;&amp;quot;admin&amp;quot;&lt;/span&gt;;
    // ...
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;At this point everything works like a champeen!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-9136462858178339019?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/XBwKx1wjdbE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/9136462858178339019/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/03/setting-up-secure-wcf-service-that-uses.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/9136462858178339019?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/9136462858178339019?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/XBwKx1wjdbE/setting-up-secure-wcf-service-that-uses.html" title="Setting up a Secure WCF Service that uses a UserNamePasswordValidator and runs on Cassini" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/03/setting-up-secure-wcf-service-that-uses.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUEEQXw4fip7ImA9Wx9aEkQ.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-7703107863133867627</id><published>2011-03-04T18:33:00.001-08:00</published><updated>2011-03-04T18:53:20.236-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-04T18:53:20.236-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Albacore" /><category scheme="http://www.blogger.com/atom/ns#" term="Ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="Nuget" /><category scheme="http://www.blogger.com/atom/ns#" term="Rake" /><title>Using Ruby/Rake/Albacore to Create and Deploy Nuget Packages</title><content type="html">&lt;p&gt;&lt;a href="http://www.ruby-lang.org/en/"&gt;Ruby&lt;/a&gt;/&lt;a href="http://rake.rubyforge.org/"&gt;Rake&lt;/a&gt;/&lt;a href="http://albacorebuild.net/"&gt;Albacore&lt;/a&gt; is a top notch build and deployment stack. If you aren't familiar with it take a look at this &lt;a href="http://blog.mikeobrien.net/2010/07/rubyrakealbacore-quickstart-guide-for.html"&gt;quick start guide&lt;/a&gt;. As of version 0.2.3 Albacore offers a couple of tasks to aid in the creation of &lt;a href="http://nuget.codeplex.com"&gt;Nuget&lt;/a&gt; packages. We’ll cover how to create and deploy Nuget packages using these tasks. The following assumes that the build script will be run on a build server but you can easily adapt it to run locally. It also assumes that the build server establishes a working folder for the source code and scripts when the build runs.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Installing Ruby/Rake/Albacore&lt;/strong&gt;&lt;/p&gt; &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="text-align: left; line-height: 18px; font-family: &amp;#39;Trebuchet MS&amp;#39;, arial; font-size: small; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class="Apple-style-span"&gt;     &lt;p style="line-height: 18px; margin-top: 5px; margin-bottom: 1em"&gt;If you haven't already, you will need to install either Ruby or IronRuby. After running the installers you should be good to go.&lt;/p&gt;      &lt;p style="line-height: 18px; margin-top: 5px; margin-bottom: 1em"&gt;&lt;a style="background-color: transparent; color: rgb(0,50,77); font-weight: bold; text-decoration: none; background-origin: initial; background-clip: initial" href="http://www.ironruby.net/"&gt;http://www.ironruby.net&lt;/a&gt;&lt;/p&gt;      &lt;p style="line-height: 18px; margin-top: 5px; margin-bottom: 1em"&gt;&lt;a style="background-color: transparent; color: rgb(0,50,77); font-weight: bold; text-decoration: none; background-origin: initial; background-clip: initial" href="http://rubyforge.org"&gt;http://rubyforge.org&lt;/a&gt;&lt;/p&gt;     &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="text-align: left; line-height: 18px; font-family: &amp;#39;Trebuchet MS&amp;#39;, arial; font-size: small; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class="Apple-style-span"&gt;         &lt;p style="line-height: 18px; margin-top: 5px; margin-bottom: 1em"&gt;Installing Rake is simply a matter of running&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font color="#0000ff" face="Courier New"&gt;(i)gem install rake&lt;/font&gt;:&lt;/p&gt;          &lt;p style="line-height: 18px; margin-top: 5px; margin-bottom: 1em"&gt;&lt;a style="background-color: transparent; color: rgb(0,50,77); font-weight: bold; text-decoration: none; background-origin: initial; background-clip: initial" href="http://lh5.ggpht.com/_DOioD_zaWYc/TFM-lqfvb7I/AAAAAAAAAcA/w-7XGj5hXvw/s1600-h/image61.png"&gt;&lt;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://lh4.ggpht.com/_DOioD_zaWYc/TFM-mUHDxCI/AAAAAAAAAcE/pnB3Gwd6hHw/image_thumb3%5B1%5D.png?imgmax=800" width="437" height="143" /&gt;&lt;/a&gt;&lt;/p&gt;         &lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="text-align: left; line-height: 18px; font-family: &amp;#39;Trebuchet MS&amp;#39;, arial; font-size: small; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class="Apple-style-span"&gt;             &lt;p style="line-height: 18px; margin-top: 5px; margin-bottom: 1em"&gt;Installing Albacore is simply a matter of running&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;font color="#0000ff" face="Courier New"&gt;(i)gem install albacore&lt;/font&gt;:&lt;/p&gt;              &lt;p style="line-height: 18px; margin-top: 5px; margin-bottom: 1em"&gt;&lt;a style="background-color: transparent; color: rgb(0,50,77); font-weight: bold; text-decoration: underline; background-origin: initial; background-clip: initial" href="http://lh4.ggpht.com/_DOioD_zaWYc/TFM-u4r525I/AAAAAAAAAcg/xbUDIv2LAnY/s1600-h/image%5B18%5D.png"&gt;&lt;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://lh6.ggpht.com/_DOioD_zaWYc/TFM-v_gvNYI/AAAAAAAAAck/FGvuHVvP1TU/image_thumb%5B11%5D.png?imgmax=800" width="477" height="269" /&gt;&lt;/a&gt;&lt;/p&gt;           &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;  &lt;p&gt;&lt;strong&gt;Nuget Gallery&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;If you want to publish your package to the Nuget gallery you will need to follow the steps below:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;If you haven't already, setup a gallery account &lt;a href="http://nuget.org/Users/Account/Register"&gt;here&lt;/a&gt;. Once you verify your email you’ll be able to login and continue. &lt;/li&gt;    &lt;li&gt;Next you will want to reserve your package name. To do this click on the “Contribute” tab. Next click on the “Register Package Id” link. Here you can enter the id of the package you want to publish and it will be reserved for you. The documentation states the following about package ID’s: &lt;em&gt;“Package IDs may not contain any spaces or characters that are invalid in an URL. In general, they follow the same rules as .NET namespaces do. So “Foo.Bar” is a valid ID, “Foo!” and “Foo Bar” are not.”&lt;/em&gt; &lt;/li&gt;    &lt;li&gt;Now you will need to note your access key which you will use later on. This can be viewed by clicking the “MY ACCOUNT” link in the upper right corner. There you will see your access key which is just a guid. If it is compromised you can generate a new one here. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Package Prep&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The next thing we’ll need to do is setup a folder that will contain the contents of the package. Under the build server working folder we’ll have our script dynamically create the following structure:&lt;/p&gt;  &lt;p&gt;&lt;font face="Courier New"&gt;/deploy      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; /package       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /lib&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Everything under the “package” folder will be the contents of the Nuget package. Nuget has a number of conventions for the folder structure that you can read about &lt;a href="http://nuget.codeplex.com/documentation?title=Creating%20a%20Package"&gt;here&lt;/a&gt;. But to keep it simple we’ll only cover the convention for deploying an assembly that does not target a specific version of the .NET framework. In that simple case we can just place the binaries from our project under a folder called “lib” (the following code makes use of &lt;a href="https://gist.github.com/855973"&gt;this module&lt;/a&gt;, filesystem.rb, for file system operations):&lt;/p&gt;  &lt;pre class="code"&gt;require &lt;span style="color: #a31515"&gt;&amp;quot;albacore&amp;quot;
&lt;/span&gt;require &lt;span style="color: #a31515"&gt;&amp;quot;release/filesystem&amp;quot;
&lt;/span&gt;&lt;span style="color: #a31515"&gt;&lt;font color="#000000"&gt;&lt;/font&gt;
&lt;/span&gt;&lt;span style="color: green"&gt;# ... Other tasks ...

&lt;/span&gt;desc &lt;span style="color: #a31515"&gt;&amp;quot;Prep the package folder&amp;quot;
&lt;/span&gt;task :prepPackage &lt;span style="color: teal"&gt;=&amp;gt; &lt;/span&gt;:unitTests &lt;span style="color: blue"&gt;do
    &lt;/span&gt;FileSystem&lt;span style="color: teal"&gt;.&lt;/span&gt;DeleteDirectory&lt;span style="color: teal"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;)
    &lt;/span&gt;FileSystem&lt;span style="color: teal"&gt;.&lt;/span&gt;EnsurePath&lt;span style="color: teal"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy/package/lib&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;)
    &lt;/span&gt;FileSystem&lt;span style="color: teal"&gt;.&lt;/span&gt;CopyFiles&lt;span style="color: teal"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;src/Gribble/bin/Release/Gribble.dll&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;, &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy/package/lib&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;)
    &lt;/span&gt;FileSystem&lt;span style="color: teal"&gt;.&lt;/span&gt;CopyFiles&lt;span style="color: teal"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;src/Gribble/bin/Release/Gribble.pdb&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;, &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy/package/lib&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;end&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here we make sure the “deploy” folder is cleared out and a fresh folder structure is recreated each build. Then we copy the assembly and pdb from our project “release” folder to the “lib” folder. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating the Nuspec File&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now we need to create the Nuspec file. You can find more information on that &lt;a href="http://nuget.codeplex.com/documentation?title=Nuspec%20Format"&gt;here&lt;/a&gt;. This file contains a description of the Nuget package and is bundled with it. For that we will use the Albacore &lt;a href="https://github.com/derickbailey/Albacore/wiki/Nuspec-Task"&gt;nuspec task&lt;/a&gt;:&lt;/p&gt;

&lt;pre class="code"&gt;desc &lt;span style="color: #a31515"&gt;&amp;quot;Create the nuspec&amp;quot;
&lt;/span&gt;nuspec :createSpec &lt;span style="color: teal"&gt;=&amp;gt; &lt;/span&gt;:prepPackage &lt;span style="color: blue"&gt;do &lt;/span&gt;&lt;span style="color: teal"&gt;|&lt;/span&gt;nuspec&lt;span style="color: teal"&gt;|
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;id &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;gribble&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;version &lt;span style="color: teal"&gt;= &lt;/span&gt;ENV&lt;span style="color: teal"&gt;[&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;GO_PIPELINE_LABEL&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;]
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;authors &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Mike O'Brien&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;owners &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Mike O'Brien&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;description &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Gribble is a simple, Linq enabled ORM designed to work with dynamically created tables.&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;summary &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;Gribble is a simple, Linq enabled ORM designed to work with dynamically created tables.&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;language &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;en-US&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;licenseUrl &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;https://github.com/mikeobrien/Gribble/blob/master/LICENSE&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;projectUrl &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;https://github.com/mikeobrien/Gribble&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;working_directory &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy/package&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;output_file &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;gribble.nuspec&amp;quot;
   &lt;/span&gt;nuspec&lt;span style="color: teal"&gt;.&lt;/span&gt;tags &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;orm sql&amp;quot;
&lt;/span&gt;&lt;span style="color: blue"&gt;end
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You’ll notice that this task depends on the prepPackage task to get our package folder initialized. You can look at the link noted earlier to get more information about these properties so I’ll just cover a couple of important ones. First the id property is going to be set to the package id we reserved earlier in the gallery. The version must be set so that this package can be differentiated from others that you push. In this case I’m using the version number created by the build server. The working directory and output file determine where the spec will be created. We want it to be created in the root of the package folder. This produces the following nuspec file:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515"&gt;xml &lt;/span&gt;&lt;span style="color: red"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;'&lt;span style="color: blue"&gt;1.0&lt;/span&gt;'&lt;span style="color: blue"&gt;?&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;package &lt;/span&gt;&lt;span style="color: red"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;'&lt;span style="color: blue"&gt;http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd&lt;/span&gt;'&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;metadata&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;gribble&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;1.0.28&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;authors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Mike O&lt;span style="color: red"&gt;&amp;amp;apos;&lt;/span&gt;Brien&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;authors&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;description&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Gribble is a simple, Linq enabled ORM designed to work with dynamically created tables.&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;description&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;language&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;en-US&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;language&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;licenseUrl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;https://github.com/mikeobrien/Gribble/blob/master/LICENSE&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;licenseUrl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;projectUrl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;https://github.com/mikeobrien/Gribble&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;projectUrl&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;owners&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Mike O&lt;span style="color: red"&gt;&amp;amp;apos;&lt;/span&gt;Brien&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;owners&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;summary&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;Gribble is a simple, Linq enabled ORM designed to work with dynamically created tables.&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;summary&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;tags&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;orm sql&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;tags&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
  &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;metadata&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;package&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;Creating the Nuget Package&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a package folder and nuspec file we can package all this up. To do this we can use the nugetpack task (No documentation on that yet):&lt;/p&gt;

&lt;pre class="code"&gt;desc &lt;span style="color: #a31515"&gt;&amp;quot;Create the nuget package&amp;quot;
&lt;/span&gt;nugetpack :createPackage &lt;span style="color: teal"&gt;=&amp;gt; &lt;/span&gt;:createSpec &lt;span style="color: blue"&gt;do &lt;/span&gt;&lt;span style="color: teal"&gt;|&lt;/span&gt;nugetpack&lt;span style="color: teal"&gt;|
   &lt;/span&gt;nugetpack&lt;span style="color: teal"&gt;.&lt;/span&gt;nuspec &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy/package/gribble.nuspec&amp;quot;
   &lt;/span&gt;nugetpack&lt;span style="color: teal"&gt;.&lt;/span&gt;base_folder &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy/package&amp;quot;
   &lt;/span&gt;nugetpack&lt;span style="color: teal"&gt;.&lt;/span&gt;output &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy&amp;quot;
&lt;/span&gt;&lt;span style="color: blue"&gt;end&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;This task is pretty simple; we need to provide the path to the nuspec file (nuspec), the path to the package folder (base_folder) and the folder to put the generated package into (output). The filename for the package is [Id].[Version].nupkg (i.e. gribble.1.0.28.nupkg). The actual package is just a zip file. You can open it with your favorite zip utility and view the contents. It’s basically the same as your package folder, save a few additions:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lh6.ggpht.com/_DOioD_zaWYc/TXGhBc47yeI/AAAAAAAAAh0/ms3JaeZPBGU/s1600-h/image%5B5%5D.png"&gt;&lt;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://lh4.ggpht.com/_DOioD_zaWYc/TXGhC8yXrDI/AAAAAAAAAh4/CVGfp1--gsg/image_thumb%5B3%5D.png?imgmax=800" width="672" height="276" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Push the Nuget Package to the Gallery&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now that we have the package we can push it to the gallery. To do this you will need to download the Nuget command line utility onto your build server. You can download it &lt;a href="http://nuget.codeplex.com/releases"&gt;here&lt;/a&gt; (“NuGet.exe Command Line”). You can place it wherever you want but you will need to include the parent folder in your system path so that your build server can find it (This may require you to restart your build server service after you modify the system path). If you don't know how to modify the system path just follow the steps &lt;a href="http://www.windows7hacker.com/index.php/2010/05/how-to-addedit-environment-variables-in-windows-7/"&gt;here&lt;/a&gt;. Now Albacore doesn't have a task to handle the pushing of the package so we’ll create our own (nuget.rb):&lt;/p&gt;

&lt;pre class="code"&gt;require &lt;span style="color: #a31515"&gt;&amp;quot;yaml&amp;quot;

&lt;/span&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;NugetPush
    attr_accessor :apiKey&lt;span style="color: teal"&gt;, &lt;/span&gt;:package

    &lt;span style="color: blue"&gt;def &lt;/span&gt;run&lt;span style="color: teal"&gt;()
        &lt;/span&gt;@apiKey &lt;span style="color: teal"&gt;= &lt;/span&gt;YAML&lt;span style="color: teal"&gt;::&lt;/span&gt;load&lt;span style="color: teal"&gt;(&lt;/span&gt;File&lt;span style="color: teal"&gt;.&lt;/span&gt;open&lt;span style="color: teal"&gt;(&lt;/span&gt;ENV&lt;span style="color: teal"&gt;[&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;USERPROFILE&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;] + &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;/.nuget/credentials&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;))[&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;api_key&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;] &lt;/span&gt;&lt;span style="color: blue"&gt;unless &lt;/span&gt;&lt;span style="color: teal"&gt;!&lt;/span&gt;@apiKey&lt;span style="color: teal"&gt;.&lt;/span&gt;nil?
        system&lt;span style="color: teal"&gt;(&lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;nuget&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;, &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;push&amp;quot;&lt;/span&gt;&lt;span style="color: teal"&gt;, &lt;/span&gt;@package&lt;span style="color: teal"&gt;, &lt;/span&gt;@apiKey&lt;span style="color: teal"&gt;)
    &lt;/span&gt;&lt;span style="color: blue"&gt;end
end

def &lt;/span&gt;nugetpush&lt;span style="color: teal"&gt;(*&lt;/span&gt;args&lt;span style="color: teal"&gt;, &amp;amp;&lt;/span&gt;block&lt;span style="color: teal"&gt;)
    &lt;/span&gt;body &lt;span style="color: teal"&gt;= &lt;/span&gt;lambda &lt;span style="color: teal"&gt;{ |*&lt;/span&gt;args&lt;span style="color: teal"&gt;|
        &lt;/span&gt;rc &lt;span style="color: teal"&gt;= &lt;/span&gt;NugetPush&lt;span style="color: teal"&gt;.&lt;/span&gt;new
        block&lt;span style="color: teal"&gt;.&lt;/span&gt;call&lt;span style="color: teal"&gt;(&lt;/span&gt;rc&lt;span style="color: teal"&gt;)
        &lt;/span&gt;rc&lt;span style="color: teal"&gt;.&lt;/span&gt;run
    &lt;span style="color: teal"&gt;}
    &lt;/span&gt;Rake&lt;span style="color: teal"&gt;::&lt;/span&gt;Task&lt;span style="color: teal"&gt;.&lt;/span&gt;define_task&lt;span style="color: teal"&gt;(*&lt;/span&gt;args&lt;span style="color: teal"&gt;, &amp;amp;&lt;/span&gt;body&lt;span style="color: teal"&gt;)
&lt;/span&gt;&lt;span style="color: blue"&gt;end&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And here is how we would use it:&lt;/p&gt;

&lt;pre class="code"&gt;require &lt;span style="color: #a31515"&gt;&amp;quot;albacore&amp;quot;
&lt;/span&gt;require &lt;span style="color: #a31515"&gt;&amp;quot;release/filesystem&amp;quot;
&lt;/span&gt;require &lt;span style="color: #a31515"&gt;&amp;quot;release/nuget&amp;quot;

&lt;/span&gt;&lt;span style="color: green"&gt;# ... Other tasks ...

&lt;/span&gt;desc &lt;span style="color: #a31515"&gt;&amp;quot;Push the nuget package&amp;quot;
&lt;/span&gt;nugetpush :pushPackage &lt;span style="color: teal"&gt;=&amp;gt; &lt;/span&gt;:createPackage &lt;span style="color: blue"&gt;do &lt;/span&gt;&lt;span style="color: teal"&gt;|&lt;/span&gt;nugetpush&lt;span style="color: teal"&gt;|
   &lt;/span&gt;nugetpush&lt;span style="color: teal"&gt;.&lt;/span&gt;package &lt;span style="color: teal"&gt;= &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;deploy/gribble.&lt;/span&gt;&lt;span style="color: teal"&gt;#{&lt;/span&gt;ENV&lt;span style="color: teal"&gt;[&lt;/span&gt;&lt;span style="color: #a31515"&gt;'GO_PIPELINE_LABEL'&lt;/span&gt;&lt;span style="color: teal"&gt;]}&lt;/span&gt;&lt;span style="color: #a31515"&gt;.nupkg&amp;quot;
&lt;/span&gt;&lt;span style="color: blue"&gt;end&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Not too much to it. All you have to do is provide the path to the package we created in the last step. But there is the matter of the api key which is needed to authenticate you. The task we created can take this directly in the build script but that is generally not a good idea as it will be visible to everyone who has access to the script (If your project is open source then that means everybody). So we take a cue from ruby gems and allow you to cache the key in the user profile (Which is &lt;a href="http://nuget.codeplex.com/workitem/656"&gt;evidently on its way in the next version of Nuget&lt;/a&gt;). You’ll notice that it looks for a yaml file called “[UserProfile]\.nuget\credentials” with an item called “api_key”. If your build server service is running under a user account this path will probably be something like “C:\Users\ServiceAccount\.nuget\credentials”. If its running as LocalSystem it gets a bit more tricky. If your build server is running on an x64 operating system the path will be “C:\Windows\SysWOW64\config\systemprofile\.nuget\credentials” and if it is an x86 operating system it will be “C:\Windows\System32\config\systemprofile\.nuget\credentials”. To create the “.nuget” folder you will have to do it from the command line as Windows Explorer doesn't like the period prefix. After you create the folder, create a file called “credentials” and enter the following:&lt;/p&gt;

&lt;p&gt;&lt;font face="Courier New"&gt;api_key: [Your API Key]&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;Once that's set the script will pull the key from there. &lt;/p&gt;

&lt;p&gt;At this point you should be ready to run your build script and have it create your nuget package and push it to the gallery. If you’re interested in doing this with ThoughtWorks Go check out &lt;a href="http://blog.mikeobrien.net/2010/09/setting-up-thoughtworks-go-build-on.html"&gt;this post&lt;/a&gt; and substitute the ruby gems related steps with the steps above. The code samples above were taken from the Gribble project. You can see full build script &lt;a href="https://github.com/mikeobrien/Gribble/tree/master/release"&gt;here&lt;/a&gt;. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-7703107863133867627?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/o9WX1Ps-csk" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/7703107863133867627/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/03/using-rubyrakealbacore-to-create-and.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7703107863133867627?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7703107863133867627?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/o9WX1Ps-csk/using-rubyrakealbacore-to-create-and.html" title="Using Ruby/Rake/Albacore to Create and Deploy Nuget Packages" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_DOioD_zaWYc/TFM-mUHDxCI/AAAAAAAAAcE/pnB3Gwd6hHw/s72-c/image_thumb3%5B1%5D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/03/using-rubyrakealbacore-to-create-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0MGR3k7fSp7ImA9Wx9aEUw.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-248773966444057573</id><published>2011-02-25T15:21:00.001-08:00</published><updated>2011-03-02T16:17:06.705-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-02T16:17:06.705-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Linq" /><title>Creating Custom Linq Query Operators that Support both IQuerable&lt;T&gt; and IEnumerable&lt;T&gt;</title><content type="html">&lt;p&gt;If your creating a custom query provider you may end up creating some custom query operators. I’ve found this to be a very powerful way to add additional functionality. If your doing this I would suggest also adding versions of your operators that work with IEnumerable&amp;lt;T&amp;gt;. The benefit of this is you can swap out your query provider for a List&amp;lt;T&amp;gt; (Or some other in memory collection that implements IEnumerable&amp;lt;T&amp;gt;) for unit testing. &lt;/p&gt;  &lt;p&gt;Lets start with an sample query operator that represents a bubble sort:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Queryable
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt; BubbleSort&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt; source)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(source == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;source&amp;quot;&lt;/span&gt;);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;source.Provider.CreateQuery&amp;lt;TSource&amp;gt;(
            &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Call(&lt;span style="color: blue"&gt;null&lt;/span&gt;, ((&lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;MethodBase&lt;/span&gt;.GetCurrentMethod()).MakeGenericMethod(&lt;span style="color: blue"&gt;new&lt;/span&gt;[] { &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(TSource) }), 
                            &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { source.Expression }));
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Here we have some code that calls this custom query operator:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; Sort(&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; items)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;items.BubbleSort();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;This query operator likely maps to an out of process call somewhere (Like on a database server) but in our unit tests we want to keep it in memory. Instead of passing in our query provider we can pass in an in memory collection like List&amp;lt;T&amp;gt;:&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: red"&gt;&lt;font color="#0000ff"&gt;Test&lt;/font&gt;&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Test_Sort()
{
    &lt;span style="color: blue"&gt;var &lt;/span&gt;items = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; { &lt;span style="color: #a31515"&gt;&amp;quot;Pauli&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Bohr&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Dirac&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Heisenberg&amp;quot; &lt;/span&gt;};
    &lt;span style="color: blue"&gt;var &lt;/span&gt;sorted = Sort(items.AsQueryable());
    &lt;span style="color: green"&gt;// ... Asserts
&lt;/span&gt;}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now if you were to do this it would fail because List&amp;lt;T&amp;gt; has no idea what BubbleSort() is. Although List&amp;lt;T&amp;gt; implements IQuerable&amp;lt;T&amp;gt; everything it does happens in memory as expected. When you treat an IEnumerable&amp;lt;T&amp;gt; as an IQueryable&amp;lt;T&amp;gt; it actually gets wrapped by a class called EnumerableQuery&amp;lt;T&amp;gt; which, when it executes, rewrites and compiles the expression tree. The interesting thing about this class (Technically delegated to the internal EnumerableRewriter class) is that it rewrites the expression tree so that arguments and return types of type IQueryable&amp;lt;T&amp;gt; get rewritten to be IEnumerable&amp;lt;T&amp;gt;. Same is true of Expression&amp;lt;Func&amp;lt;&amp;gt;&amp;gt; and Expression&amp;lt;Action&amp;lt;&amp;gt;&amp;gt;; they get rewritten to Func&amp;lt;&amp;gt; and Action&amp;lt;&amp;gt;. So if you want to be able to use an IQueryable&amp;lt;T&amp;gt; operator in memory, simply create a corresponding operator in the same type (This is required as far as I can tell) that operates on IEnumerable&amp;lt;T&amp;gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Queryable
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt; BubbleSort&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt; source)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(source == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;source&amp;quot;&lt;/span&gt;);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;source.Provider.CreateQuery&amp;lt;TSource&amp;gt;(
            &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Call(&lt;span style="color: blue"&gt;null&lt;/span&gt;, ((&lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;MethodBase&lt;/span&gt;.GetCurrentMethod()).MakeGenericMethod(&lt;span style="color: blue"&gt;new&lt;/span&gt;[] { &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(TSource) }), 
                            &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { source.Expression }));
    }

    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; BubbleSort&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source)
    {
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(source == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;source&amp;quot;&lt;/span&gt;);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SortLibrary&lt;/span&gt;.BubbleSort(source);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Now your custom query operator will work against your custom query provider or an in memory collection. Obviously, the code that works against your in memory collection should behave the same as your out of process call or this is all for naught. There is one caveat however for query operators that execute the expression but still return an IQueryable&amp;lt;T&amp;gt;, for example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt; Clone&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt; source)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(source == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;source&amp;quot;&lt;/span&gt;);
    &lt;span style="color: blue"&gt;return &lt;/span&gt;source.Provider.Execute&amp;lt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt;&amp;gt;(
        &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Call(&lt;span style="color: blue"&gt;null&lt;/span&gt;, ((&lt;span style="color: #2b91af"&gt;MethodInfo&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;MethodBase&lt;/span&gt;.GetCurrentMethod()).MakeGenericMethod(&lt;span style="color: blue"&gt;new&lt;/span&gt;[] { &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(TSource) }),
                        &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { source.Expression }));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In this case you will need to have the corresponding IEnumerable&amp;lt;T&amp;gt; operator convert to and return an IQueryable&amp;lt;T&amp;gt;:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TSource&amp;gt; Clone&amp;lt;TSource&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(source == &lt;span style="color: blue"&gt;null&lt;/span&gt;) &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;source&amp;quot;&lt;/span&gt;);
    &lt;span style="color: blue"&gt;return &lt;/span&gt;Enumerable.Clone(source).AsQueryable();
}&lt;/pre&gt;

&lt;p&gt;If you don’t do this the query provider will throw a type mismatch exception on the IEnumerable&amp;lt;T&amp;gt; your returning.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-248773966444057573?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/38z5awTJQMI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/248773966444057573/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/02/creating-custom-linq-query-operators.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/248773966444057573?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/248773966444057573?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/38z5awTJQMI/creating-custom-linq-query-operators.html" title="Creating Custom Linq Query Operators that Support both IQuerable&amp;lt;T&amp;gt; and IEnumerable&amp;lt;T&amp;gt;" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/02/creating-custom-linq-query-operators.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0MAQ3Y_fip7ImA9Wx9UFkQ.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-7719673546837677407</id><published>2011-01-24T14:02:00.001-08:00</published><updated>2011-02-14T08:04:02.846-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-14T08:04:02.846-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="TSQL" /><category scheme="http://www.blogger.com/atom/ns#" term="SQL" /><title>Getting Distinct Results on a Finite Set of Columns or Projections</title><content type="html">&lt;p&gt;I have a situation where I need to return a result set that is distinct on only a few columns or projections. The DISTINCT keyword does not fit the bill as it applies to the entire projection. Before we explore this lets start with some sample data in a table we'll call Contacts.&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;id&amp;#160; email&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; name&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;--&amp;#160; -------------------&amp;#160; -------&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;1&amp;#160;&amp;#160; foxydog@yahoo.com&amp;#160;&amp;#160;&amp;#160; Bob&lt;/font&gt;    &lt;br /&gt;&lt;font face="Consolas"&gt;2&amp;#160;&amp;#160; foxydog@yahoo.com&amp;#160;&amp;#160;&amp;#160; Robert&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;3&amp;#160;&amp;#160; foxydog@yahoo.com&amp;#160;&amp;#160;&amp;#160; Dick&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;4&amp;#160;&amp;#160; william@hotmail.com&amp;#160; Bill&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;In this example we want to get distinct records by email address, discarding duplicate records (As in this context they are not important). There are a couple of ways to approach this. You could do a GROUP BY on email and an aggregate the other fields (Like MAX) but this could mess things up if the other columns are related somehow (Like an address for example, you could get mismatched street, city, state and zip). You could also do a WHERE id IN on a sub query that does group by on email and a max on id which would solve the problem of mismatched columns:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;SELECT * FROM Contacts&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;WHERE id IN (SELECT MAX(id) FROM Contacts GROUP BY email)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;I don't like this however because it requires two table scans (BTW I have indexes on id and email):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_DOioD_zaWYc/TT328jsrD2I/AAAAAAAAAhc/DMVSZIl17Yw/s1600-h/image%5B5%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_DOioD_zaWYc/TT32-L74R8I/AAAAAAAAAhg/4r8lzFELYAw/image_thumb%5B3%5D.png?imgmax=800" width="796" height="182" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Another option is to use the ROW_NUMBER aggregate and PARTITION BY. You can partition by the columns or projections you choose (And I say &amp;quot;projections&amp;quot; because it doesn't strictly have to be a column name, it could be LEN(email) for example). So we could say the following:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;SELECT *&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;FROM (SELECT *, &lt;/font&gt;    &lt;br /&gt;&lt;font face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ROW_NUMBER() OVER(PARTITION BY email ORDER BY email) PartitionId &lt;/font&gt;    &lt;br /&gt;&lt;font face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM Contacts) T&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This returns the following:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;id&amp;#160; email&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; name&amp;#160;&amp;#160;&amp;#160;&amp;#160; PartitionId&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;--&amp;#160; -------------------&amp;#160; -------&amp;#160; -----------&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;1&amp;#160;&amp;#160; foxydog@yahoo.com&amp;#160;&amp;#160;&amp;#160; Bob&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;2&amp;#160;&amp;#160; foxydog@yahoo.com&amp;#160;&amp;#160;&amp;#160; Robert&amp;#160;&amp;#160; 2&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;3&amp;#160;&amp;#160; foxydog@yahoo.com&amp;#160;&amp;#160;&amp;#160; Dick&amp;#160;&amp;#160;&amp;#160;&amp;#160; 3&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;4&amp;#160;&amp;#160; william@hotmail.com&amp;#160; Bill&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Notice how the row numbers restart (PartitionId) when the email address changes? We can then take this one step further and only return records with a partition id of 1 giving us a distinct result set:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;SELECT *&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;FROM (SELECT *, &lt;/font&gt;    &lt;br /&gt;&lt;font face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ROW_NUMBER() OVER(PARTITION BY email ORDER BY email) PartitionId &lt;/font&gt;    &lt;br /&gt;&lt;font face="Consolas"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM Contacts) T&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;WHERE PartitionId = 1&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;This returns the following:&lt;/p&gt;  &lt;p&gt;&lt;font face="Consolas"&gt;id&amp;#160; email&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; name&amp;#160;&amp;#160;&amp;#160;&amp;#160; PartitionId&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;--&amp;#160; -------------------&amp;#160; -------&amp;#160; -----------&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;1&amp;#160;&amp;#160; foxydog@yahoo.com&amp;#160;&amp;#160;&amp;#160; Bob&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&lt;/font&gt;     &lt;br /&gt;&lt;font face="Consolas"&gt;3&amp;#160;&amp;#160; william@hotmail.com&amp;#160; Bill&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;And voila! Distinct on only one column. Even though this solution requires a bit more TSQL, the execution plan looks much nicer as we are only doing one table scan:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_DOioD_zaWYc/TT32-pEea8I/AAAAAAAAAhk/XW6Lr29qCZA/s1600-h/image%5B11%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_DOioD_zaWYc/TT32__hj1kI/AAAAAAAAAho/9ntqSWfq8wE/image_thumb%5B7%5D.png?imgmax=800" width="838" height="88" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-7719673546837677407?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/2QiMfy9puQ4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/7719673546837677407/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/01/getting-distinct-results-on-finite-set.html#comment-form" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7719673546837677407?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7719673546837677407?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/2QiMfy9puQ4/getting-distinct-results-on-finite-set.html" title="Getting Distinct Results on a Finite Set of Columns or Projections" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_DOioD_zaWYc/TT32-L74R8I/AAAAAAAAAhg/4r8lzFELYAw/s72-c/image_thumb%5B3%5D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/01/getting-distinct-results-on-finite-set.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQNQnk7fSp7ImA9Wx9WEkw.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-8974828291293306946</id><published>2011-01-16T14:19:00.001-08:00</published><updated>2011-01-16T14:19:53.705-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-16T14:19:53.705-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Yada Yada Yada" /><title>Three Layer Tea (AKA Sarawak Tea)</title><content type="html">&lt;p&gt;Aside from &lt;a href="http://en.wikipedia.org/wiki/Roti_canai"&gt;roti boom&lt;/a&gt; (roti canai with butter and sugar) my favorite treat from Malaysia was the three layer tea (Or Sarawak tea). Its made up of three layers, in the following order from top to bottom: black tea, evaporated milk and gula melaka (Brown coconut sugar in syrup form). Its is served chilled (I mix it up) and not only does it taste awesome its unbelievably refreshing in the stifling Malaysian heat. Most America grocers wont carry brown coconut sugar syrup (Sometimes called palm sugar) so you'll have to shop around at your local Asian grocers. I'd definitely give it a try!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;img style="display: block; float: none; margin-left: auto; margin-right: auto" src="http://2.bp.blogspot.com/_GM0kTqLQ9eI/S_plEwEugnI/AAAAAAAAG5I/P8NAstOw7mw/s1600/2010April+3+layer+tea+(2).JPG" width="360" height="480" /&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-8974828291293306946?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/Modme6GGUHc" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/8974828291293306946/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/01/three-layer-tea-aka-sarawak-tea.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/8974828291293306946?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/8974828291293306946?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/Modme6GGUHc/three-layer-tea-aka-sarawak-tea.html" title="Three Layer Tea (AKA Sarawak Tea)" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/_GM0kTqLQ9eI/S_plEwEugnI/AAAAAAAAG5I/P8NAstOw7mw/s72-c/2010April+3+layer+tea+(2).JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/01/three-layer-tea-aka-sarawak-tea.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEYAQ3wzcCp7ImA9Wx9WEEg.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-2337690403625537574</id><published>2011-01-14T11:25:00.001-08:00</published><updated>2011-01-14T16:42:22.288-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-01-14T16:42:22.288-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Expressions" /><category scheme="http://www.blogger.com/atom/ns#" term=".NET 4.0" /><category scheme="http://www.blogger.com/atom/ns#" term="Linq" /><title>Building a Better Expression Visitor</title><content type="html">&lt;p&gt;After trying to hack the new &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.expressionvisitor.aspx"&gt;expression visitor class in .NET 4.0&lt;/a&gt; to fit my needs I finally just gave up and wrote a new one from scratch. I did not need the rewriting capabilities of the FCL implementation as I am going directly from an expression tree to an object model. Here are the key features I needed to make this happen:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Ability to control which nodes are visited next.&lt;/strong&gt; I 'm using the approach of having different visitors for different parts of the expression. For example I have a select visitor that just handles the IQueryable methods, a where clause visitor that handles the where expression (Unary and binary expressions only) and a visitor to handle expressions that stem from constants or from properties on an entity. This composition greatly simplifies things and makes the visitors reusable. When I hit a point where I want to pass the torch to a different visitor I want to be able to specify that I don't want the current visitor to continue visiting a particular child node. You can do this manually with the current implementation but why not just tell the visitor which nodes you want it to skip and let it handle that? &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Need to know what the parent was.&lt;/strong&gt; Sometimes the parent node can effect how the child is processed. For example if the parent of an expression is a binary expression I need to handle it a little differently. Currently you have no information about the parent expression. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Ability to pass along state to visitors of children.&lt;/strong&gt; I needed to be able to pass along state or a lambda from parent to child in order to build up my object model. I tried a dictionary approach with the current implementation but this was hacky. Not to mention that I didn't want to be doing a ton of dictionary lookups. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So I put together an expression visitor that enables all these features and its made things &lt;strong&gt;&lt;em&gt;much&lt;/em&gt;&lt;/strong&gt; easier:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://gist.github.com/780050"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;https://gist.github.com/780050&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Here are a couple of examples:&lt;/p&gt;  &lt;p&gt;Each virtual method has a counterpart that allows you to control what child expressions are visited. For example the VisitMethodCall method has the following overload (In addition to the default):&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected void &lt;/span&gt;VisitMethodCall(&lt;span style="color: #2b91af"&gt;MethodCallExpression &lt;/span&gt;node, TState objectState, TState argumentsState, &lt;br /&gt;&lt;span style="color: blue"&gt;                               bool &lt;/span&gt;visitObject, &lt;span style="color: blue"&gt;bool &lt;/span&gt;visitArguments, &lt;span style="color: blue"&gt;params &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;[] argumentsNotToVisit)&lt;/pre&gt;

&lt;p&gt;Here you can control what is visited as demonstrated by the following implementation:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected override void &lt;/span&gt;VisitMethodCall(&lt;span style="color: #2b91af"&gt;Context &lt;/span&gt;context, &lt;span style="color: #2b91af"&gt;MethodCallExpression &lt;/span&gt;node)
{
    &lt;span style="color: green"&gt;// ...
&lt;/span&gt;    VisitMethodCall(node, context.State, context.State, &lt;span style="color: blue"&gt;true&lt;/span&gt;, &lt;span style="color: blue"&gt;true&lt;/span&gt;, node.Arguments.Skip(1).ToArray());
}&lt;/pre&gt;

&lt;p&gt;Here we're visiting all the children but skipping every parameter except the first one as we are handing those (Not shown). &lt;/p&gt;

&lt;p&gt;We may also need to know what the parent was, as in this example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected override void &lt;/span&gt;VisitMember(&lt;span style="color: #2b91af"&gt;Context &lt;/span&gt;context, &lt;span style="color: #2b91af"&gt;MemberExpression &lt;/span&gt;node)
{
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(!context.HasParent || context.Parent.IsBinaryLogicalOperator()) &lt;span style="color: green"&gt;//...;
    &lt;/span&gt;&lt;span style="color: blue"&gt;else &lt;/span&gt;&lt;span style="color: green"&gt;//...;
&lt;/span&gt;}&lt;/pre&gt;

&lt;p&gt;Here we need to know if the member expression itself is a predicate (i.e. x =&amp;gt; x.Active) or part of a binary logical operation (i.e. x =&amp;gt; x.Active == true). In my implementation these two scenarios are handled differently so I needed a way to check this. &lt;/p&gt;

&lt;p&gt;The last thing is passing of state. Here I have a visitor for the where clause as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WhereVisitor&lt;/span&gt;&amp;lt;TItem&amp;gt; : &lt;span style="color: #2b91af"&gt;ExpressionVisitorBase&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Operand&lt;/span&gt;&amp;gt;&amp;gt; {...}&lt;/pre&gt;

&lt;p&gt;The generic type argument is the visitor state. The state in this instance is an action that has one parameter, an Operand (The Operand type is irrelevant, just an example). This allows us to pass a lambda to a child expression visit method which in turn can pass in some state. This doesn't have to be a lambda, it can be anything that you want passed around from visit to visit.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;protected override void &lt;/span&gt;VisitBinary(&lt;span style="color: #2b91af"&gt;Context &lt;/span&gt;context, &lt;span style="color: #2b91af"&gt;BinaryExpression &lt;/span&gt;node)
{
&lt;span style="color: green"&gt;    // ...
    &lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;operand = &lt;span style="color: #2b91af"&gt;&lt;font color="#008000"&gt;// ...&lt;/font&gt;&lt;/span&gt;;
    context.State(operand);
    
    VisitBinary(node, 
        x =&amp;gt; operand.Operator.LeftOperand = x,
        &lt;span style="color: blue"&gt;null&lt;/span&gt;,
        x =&amp;gt; operand.Operator.RightOperand = x,
        &lt;span style="color: blue"&gt;true&lt;/span&gt;,
        &lt;span style="color: blue"&gt;false&lt;/span&gt;,
        &lt;span style="color: blue"&gt;true&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt;In this example we are creating an operand and passing it into the parent lambda. We are also specifying lambdas that will be passed to the visitor methods that evaluate the children. As you can see they are building up an object model.&lt;/p&gt;

&lt;p&gt;In conclusion there are two major things I've learned so far:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;If there is a lot of friction using the built in expression visitor, write a custom one from scratch that fits your needs. You can look at the one in the FCL with Reflector or the one I have linked above as an example. It's a bit of work to do but I've found it's a much simpler and cleaner approach and will save you headaches in the long run. &lt;/li&gt;

  &lt;li&gt;Go from an expression tree to an object model then from the object model to the domain language (i.e. TSQL). I've found that going directly from an expression tree to a domain language gets complicated very fast, especially when you want to add optimizations. Splitting it up into two steps greatly simplifies things. NHibernate does this as well.&lt;/li&gt;
&lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-2337690403625537574?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/4w62-H0M-rw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/2337690403625537574/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2011/01/building-better-expression-visitor.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/2337690403625537574?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/2337690403625537574?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/4w62-H0M-rw/building-better-expression-visitor.html" title="Building a Better Expression Visitor" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2011/01/building-better-expression-visitor.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkQAQXoyfyp7ImA9Wx9QEUk.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-3792038345248026364</id><published>2010-12-22T19:42:00.001-08:00</published><updated>2010-12-23T13:39:00.497-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-23T13:39:00.497-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term=".NET" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="Linq" /><title>Linq Providers Made Easy (Well, sorta)</title><content type="html">&lt;p&gt;Currently I'm implementing a simple Linq enabled ORM to handle an edge case in our system (For everything else we use NHibernate). I've found that the hard part about writing a Linq provider is parsing expression trees; the provider is actually the easy part. In the course of writing it I've created a simple base class that makes it even easier to create the provider:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public abstract class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;QueryableBase&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;IOrderedQueryable&lt;/span&gt;&amp;lt;T&amp;gt;, &lt;span style="color: #2b91af"&gt;IQueryProvider
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;QueryableBase&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; _createQuery;

    &lt;span style="color: blue"&gt;protected &lt;/span&gt;QueryableBase(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;QueryableBase&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; createQuery)
    {
        _createQuery = createQuery;
        Expression = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Constant(&lt;span style="color: blue"&gt;this&lt;/span&gt;);
    }

    &lt;span style="color: blue"&gt;public abstract object &lt;/span&gt;Query(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression);

    &lt;span style="color: green"&gt;// ---------------------- IOrderedQueryable Implementation -----------------

    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;Expression { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;ElementType { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return typeof&lt;/span&gt;(T); } }
    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryProvider &lt;/span&gt;Provider { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return this&lt;/span&gt;; } }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt; GetEnumerator()
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt;)((&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;)&lt;span style="color: blue"&gt;this&lt;/span&gt;).GetEnumerator();
    }

    &lt;span style="color: #2b91af"&gt;IEnumerator IEnumerable&lt;/span&gt;.GetEnumerator()
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;result = (&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;)Provider.Execute(Expression);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;result == &lt;span style="color: blue"&gt;null &lt;/span&gt;? &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Empty&amp;lt;T&amp;gt;().GetEnumerator() : result.GetEnumerator();
    }

    &lt;span style="color: green"&gt;// ---------------------- IQueryProvider Implementation -----------------

    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TElement&amp;gt; CreateQuery&amp;lt;TElement&amp;gt;(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;TElement&amp;gt;)CreateQuery(expression);
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IQueryable &lt;/span&gt;CreateQuery(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression)
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;query = _createQuery();
        query.Expression = expression;
        &lt;span style="color: blue"&gt;return &lt;/span&gt;query;
    }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;TResult Execute&amp;lt;TResult&amp;gt;(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression)
    {
        &lt;span style="color: blue"&gt;var &lt;/span&gt;value = Execute(expression);
        &lt;span style="color: blue"&gt;return &lt;/span&gt;value == &lt;span style="color: blue"&gt;null &lt;/span&gt;? &lt;span style="color: blue"&gt;default&lt;/span&gt;(TResult) : (TResult)value;
    }

    &lt;span style="color: blue"&gt;public object &lt;/span&gt;Execute(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;Query(expression);
    }
}&lt;/pre&gt;


&lt;p&gt;This base class provides everything you need, save the expression tree parsing, for a Linq provider. All you have to do is inherit from this class and implement the Query method as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlTable &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;QueryableBase&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;DataRow&lt;/span&gt;&amp;gt;
{
    &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlConnection &lt;/span&gt;_connection;
    &lt;span style="color: blue"&gt;private string &lt;/span&gt;_tableName;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;SqlTable(&lt;span style="color: #2b91af"&gt;SqlConnection &lt;/span&gt;connection, &lt;span style="color: blue"&gt;string &lt;/span&gt;tableName) : 
        &lt;span style="color: blue"&gt;base&lt;/span&gt;(() =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlTable&lt;/span&gt;(connection, tableName))
    { 
        _tableName = tableName;
        _connection = connection;
    }

    &lt;span style="color: blue"&gt;public override object &lt;/span&gt;Query(&lt;span style="color: #2b91af"&gt;Expression &lt;/span&gt;expression)
    {
        &lt;span style="color: green"&gt;// &lt;/span&gt;&lt;span style="color: #00008b"&gt;TODO: Parse the expression tree, generate TSQL, execute it and return the results.
&lt;/span&gt;    }
}&lt;/pre&gt;

&lt;p&gt;You'll notice that the QueryableBase constructor requires a lambda that creates a new instance of your provider (In this case SqlTable). This is used by the base class in the implementation of IQueryProvider to construct new queries based on this one but with a different expression (So that's why we are creating the new provider with the exact same parameters that were passed into the constructor of this one). Now all the magic happens in the Query method. Here is where you would parse the expression tree and translate it into something useful; in this case a TSQL statement that will be executed. Parsing expression trees is a subject all its own (And covered pretty well on the interwebs) so we'll gloss over that. Here is how we could use our new table query provider:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;connection = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlConnection&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;...&amp;quot;&lt;/span&gt;))
{
    connection.Open();

    &lt;span style="color: blue"&gt;var &lt;/span&gt;table = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SqlTable&lt;/span&gt;(connection, &lt;span style="color: #a31515"&gt;&amp;quot;Products&amp;quot;&lt;/span&gt;);

    &lt;span style="color: blue"&gt;var &lt;/span&gt;cheapProducts = table.Where(x =&amp;gt; (&lt;span style="color: blue"&gt;double&lt;/span&gt;)x[&lt;span style="color: #a31515"&gt;&amp;quot;Price&amp;quot;&lt;/span&gt;] &amp;lt; 5.0).
                              OrderBy(x =&amp;gt; x[&lt;span style="color: #a31515"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;]).
                              Take(50).
                              Select(x =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;{ Name = (&lt;span style="color: blue"&gt;string&lt;/span&gt;)x[&lt;span style="color: #a31515"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;], 
                                                Price = (&lt;span style="color: blue"&gt;double&lt;/span&gt;)x[&lt;span style="color: #a31515"&gt;&amp;quot;Price&amp;quot;&lt;/span&gt;]}).
                              ToList();

    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;product &lt;span style="color: blue"&gt;in &lt;/span&gt;cheapProducts) 
        &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;{0} = ${1:0.00}&amp;quot;&lt;/span&gt;, product.Name, product.Price);
}&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-3792038345248026364?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/yaf5gchI21g" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/3792038345248026364/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2010/12/linq-providers-made-easy-well-sorta.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3792038345248026364?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3792038345248026364?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/yaf5gchI21g/linq-providers-made-easy-well-sorta.html" title="Linq Providers Made Easy (Well, sorta)" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2010/12/linq-providers-made-easy-well-sorta.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A08BRX0_cCp7ImA9Wx9RGEU.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-5788757631362296175</id><published>2010-12-20T16:04:00.001-08:00</published><updated>2010-12-20T16:04:14.348-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-20T16:04:14.348-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="F#" /><category scheme="http://www.blogger.com/atom/ns#" term="Windows" /><title>Setting up MonoDevelop and F# on Windows</title><content type="html">&lt;p&gt;Based in part on Tomas Petricek's instructions &lt;a href="http://functional-variations.net/monodevelop/"&gt;here&lt;/a&gt; and &lt;a href="http://functional-variations.net/media/screencasts/linux-monodevelop/"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Either      &lt;ol&gt;       &lt;li&gt;Install Visual Studio 2008+ (Which you may already have installed).&lt;!--EndFragment--&gt; &lt;/li&gt;        &lt;li&gt;Or download and install the .NET framework 3.5+ here: &lt;a href="http://msdn.microsoft.com/en-us/netframework/aa569263"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://msdn.microsoft.com/en-us/netframework/aa569263&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Download and install Gtk# for .NET here: &lt;a href="http://monodevelop.com/Download"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://monodevelop.com/Download&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Download and install MonoDevelop for Windows here: &lt;a href="http://monodevelop.com/download"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://monodevelop.com/download&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Either      &lt;ol&gt;       &lt;li&gt;Install Visual Studio 2010+ (Which ships with F#) and select the F# option during setup or modify your existing installation to include F# (via Control Panel | Programs and Features | Microsoft Visual Studio...) &lt;/li&gt;        &lt;li&gt;Or install the F# stand alone package here: &lt;a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f8c623ae-aef6-4a06-a185-05f59be47d67"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f8c623ae-aef6-4a06-a185-05f59be47d67&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Install the F# MonoDevelop language bindings      &lt;ol&gt;       &lt;li&gt;In MonoDevelop, click Tools | Add-in Manager and click the &amp;quot;Install Add-ins...&amp;quot; button. &lt;/li&gt;        &lt;li&gt;Next click the &amp;quot;Repositories...&amp;quot; button, then the &amp;quot;Add&amp;quot; button. &lt;/li&gt;        &lt;li&gt;Make sure the &amp;quot;Register and on-line repository&amp;quot; radio button is selected and enter &lt;a href="http://functional-variations.net/addin/"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://functional-variations.net/addin/&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; and click &amp;quot;OK&amp;quot;.           &lt;br /&gt;          &lt;br /&gt;&lt;a href="http://lh5.ggpht.com/_DOioD_zaWYc/TQ_u3xb2ezI/AAAAAAAAAgw/Gv8aGvWorP4/s1600-h/image_thumb232.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image_thumb[23]" border="0" alt="image_thumb[23]" src="http://lh6.ggpht.com/_DOioD_zaWYc/TQ_u7MReVuI/AAAAAAAAAg0/bZmIQcZa8vE/image_thumb23_thumb.png?imgmax=800" width="807" height="942" /&gt;&lt;/a&gt;           &lt;br /&gt;&lt;/li&gt;        &lt;li&gt;Close out of the &amp;quot;Add-in Repository Management&amp;quot; dialog, expand the &amp;quot;Language bindings&amp;quot; node, check the &amp;quot;F# Language Binding...&amp;quot; and click &amp;quot;Forward&amp;quot;.&amp;#160; &lt;br /&gt;          &lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_DOioD_zaWYc/TQ_u7t7oGDI/AAAAAAAAAg4/4tHIMTeYCAY/s1600-h/image5.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_DOioD_zaWYc/TQ_u9N5xv8I/AAAAAAAAAg8/YYZEwmev5Yg/image_thumb3.png?imgmax=800" width="741" height="382" /&gt;&lt;/a&gt;           &lt;br /&gt;&lt;/li&gt;        &lt;li&gt;Click through default options in the add-in installation and restart MonoDevelop after it completes. &lt;/li&gt;        &lt;li&gt;Now you should see the F# project and file templates when creating new file or project.&amp;#160; &lt;br /&gt;          &lt;br /&gt;&lt;a href="http://lh6.ggpht.com/_DOioD_zaWYc/TQ_u9iGEc-I/AAAAAAAAAhA/sY3TLIRivlQ/s1600-h/image%5B13%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_DOioD_zaWYc/TQ_u-YCP25I/AAAAAAAAAhE/7PVnW8oP_3c/image_thumb%5B8%5D.png?imgmax=800" width="800" height="280" /&gt;&lt;/a&gt;&amp;#160; &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-5788757631362296175?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/02v4Tteg2JE" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/5788757631362296175/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2010/12/setting-up-monodevelop-and-f-on-windows.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/5788757631362296175?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/5788757631362296175?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/02v4Tteg2JE/setting-up-monodevelop-and-f-on-windows.html" title="Setting up MonoDevelop and F# on Windows" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_DOioD_zaWYc/TQ_u7MReVuI/AAAAAAAAAg0/bZmIQcZa8vE/s72-c/image_thumb23_thumb.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2010/12/setting-up-monodevelop-and-f-on-windows.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkUMSXw4fCp7ImA9Wx9RGEQ.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-5338832311588767921</id><published>2010-12-20T08:26:00.001-08:00</published><updated>2010-12-20T16:11:28.234-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-20T16:11:28.234-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Mac" /><category scheme="http://www.blogger.com/atom/ns#" term="F#" /><title>Setting up MonoDevelop and F# on Mac OS</title><content type="html">&lt;p&gt;Based on Tomas Petricek's instructions &lt;a href="http://functional-variations.net/monodevelop/"&gt;here&lt;/a&gt; and &lt;a href="http://functional-variations.net/media/screencasts/linux-monodevelop/"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Download and install Mono for Mac here (Open the .dmg and run the .pkg): &lt;a href="http://www.go-mono.com/mono-downloads/download.html"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://www.go-mono.com/mono-downloads/download.html&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;       &lt;br /&gt;&lt;a href="http://lh5.ggpht.com/_DOioD_zaWYc/TQ-DjEYxuVI/AAAAAAAAAgQ/QUlciR_V6Iw/s1600-h/image%5B5%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/_DOioD_zaWYc/TQ-DlM2CDrI/AAAAAAAAAgU/aRyE90-VMac/image_thumb%5B3%5D.png?imgmax=800" width="580" height="480" /&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Download and install MonoDevelop for Mac here: &lt;a href="http://monodevelop.com/download"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://monodevelop.com/download&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Download, unzip and install F# for Mac here: &lt;a href="http://fsxplat.codeplex.com/releases"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://fsxplat.codeplex.com/releases&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Install the F# MonoDevelop language bindings      &lt;ol&gt;       &lt;li&gt;In MonoDevelop, click Tools | Add-in Manager and click the &amp;quot;Install Add-ins...&amp;quot; button. &lt;/li&gt;        &lt;li&gt;Next click the &amp;quot;Repositories...&amp;quot; button, then the &amp;quot;Add&amp;quot; button. &lt;/li&gt;        &lt;li&gt;Make sure the &amp;quot;Register and on-line repository&amp;quot; radio button is selected and enter &lt;a href="http://functional-variations.net/addin/"&gt;&lt;u&gt;&lt;font color="#0066cc"&gt;http://functional-variations.net/addin/&lt;/font&gt;&lt;/u&gt;&lt;/a&gt; and click &amp;quot;OK&amp;quot;.           &lt;br /&gt;          &lt;br /&gt;&lt;a href="http://lh3.ggpht.com/_DOioD_zaWYc/TQ-DmI6v8jI/AAAAAAAAAgY/2PFEvYch6GA/s1600-h/image%5B12%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh5.ggpht.com/_DOioD_zaWYc/TQ-DntRPNdI/AAAAAAAAAgc/3dH0wcFKu60/image_thumb%5B8%5D.png?imgmax=800" width="702" height="768" /&gt;&lt;/a&gt;           &lt;br /&gt;&lt;/li&gt;        &lt;li&gt;Close out of the &amp;quot;Add-in Repository Management&amp;quot; dialog, expand the &amp;quot;Language bindings&amp;quot; node, check the &amp;quot;F# Language Binding...&amp;quot; and click &amp;quot;Forward&amp;quot;.          &lt;br /&gt;          &lt;br /&gt;&lt;a href="http://lh3.ggpht.com/_DOioD_zaWYc/TQ-Dn1hA8zI/AAAAAAAAAgg/fIdA5JiorPU/s1600-h/image%5B19%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_DOioD_zaWYc/TQ-Dobq3ffI/AAAAAAAAAgk/b-_lR68sjDI/image_thumb%5B13%5D.png?imgmax=800" width="727" height="360" /&gt;&lt;/a&gt;           &lt;br /&gt;&lt;/li&gt;        &lt;li&gt;Click through default options in the add-in installation and restart MonoDevelop after it completes. &lt;/li&gt;        &lt;li&gt;Now you should see the F# project and file templates when creating new file or project.&amp;#160; &lt;br /&gt;          &lt;br /&gt;&lt;a href="http://lh5.ggpht.com/_DOioD_zaWYc/TQ_wpWcQqTI/AAAAAAAAAhI/QAckZqD9eeQ/s1600-h/image%5B6%5D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_DOioD_zaWYc/TQ_wq9ot0DI/AAAAAAAAAhM/HRtu2GpBz70/image_thumb%5B3%5D.png?imgmax=800" width="800" height="300" /&gt;&lt;/a&gt;&amp;#160; &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-5338832311588767921?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/LD33vGuLBJQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/5338832311588767921/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2010/12/setting-up-monodevelop-to-run-f-on-mac.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/5338832311588767921?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/5338832311588767921?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/LD33vGuLBJQ/setting-up-monodevelop-to-run-f-on-mac.html" title="Setting up MonoDevelop and F# on Mac OS" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_DOioD_zaWYc/TQ-DlM2CDrI/AAAAAAAAAgU/aRyE90-VMac/s72-c/image_thumb%5B3%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2010/12/setting-up-monodevelop-to-run-f-on-mac.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUMDQHgyfyp7ImA9Wx9SFUU.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-8970574193170717986</id><published>2010-12-05T14:13:00.001-08:00</published><updated>2010-12-05T14:17:51.697-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-05T14:17:51.697-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="F#" /><title>Exploring The F# do Binding</title><content type="html">&lt;p&gt;I've often wondered how the F# compiler compiles the do binding. Here is the skinny on that (Using v4.0.30319.1 of the compiler). &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/dd393786.aspx"&gt;The docs give the following definition of the do binding&lt;/a&gt;:&lt;/p&gt;  &lt;p&gt;&lt;em&gt;&amp;quot;A do binding is used to execute code without defining a function or value. Also, do bindings can be used in classes...&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Use a do binding when you want to execute code independently of a function or value definition. The expression in a do binding must return unit. Code in a top-level do binding is executed when the module is initialized. The keyword do is optional.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Attributes can be applied to a top-level do binding. For example, if your program uses COM interop, you might want to apply the STAThread attribute to your program. You can do this by using an attribute on a do binding, as shown in the following code.&amp;quot;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;In a Module&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;First off lets talk about using the do binding directly in a module. To set the stage, lets say we have 2 modules with exactly the same code except one says hello and one says goodbye. Notice that the Hello module is the last module in the project (Remember, in F# file order matters).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/_DOioD_zaWYc/TPwOg-xzHuI/AAAAAAAAAgI/9c1jQLEVDIs/s1600-h/image%5B22%5D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_DOioD_zaWYc/TPwOiARPn6I/AAAAAAAAAgM/Rx1a_qOnx8g/image_thumb%5B18%5D.png?imgmax=800" width="270" height="154" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Goodbye.fs:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;module &lt;/span&gt;Goodbye

    &lt;span style="color: blue"&gt;open &lt;/span&gt;System

    &lt;span style="color: blue"&gt;let &lt;/span&gt;getTime () = DateTime.Now.ToString()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;message = &lt;span style="color: maroon"&gt;&amp;quot;goodbye there: &amp;quot;

    &lt;/span&gt;Console.WriteLine(message + getTime ())

    &lt;span style="color: blue"&gt;do
        &lt;/span&gt;Console.WriteLine(&lt;span style="color: maroon"&gt;&amp;quot;hasta luego&amp;quot;&lt;/span&gt;)

    &lt;span style="color: blue"&gt;do
        &lt;/span&gt;Console.WriteLine(&lt;span style="color: maroon"&gt;&amp;quot;oh bai&amp;quot;&lt;/span&gt;)
        Console.ReadKey() |&amp;gt; ignore&lt;/pre&gt;


&lt;p&gt;Hello.fs:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;module &lt;/span&gt;Hello

    &lt;span style="color: blue"&gt;open &lt;/span&gt;System

    &lt;span style="color: blue"&gt;let &lt;/span&gt;getTime () = DateTime.Now.ToString()
    &lt;span style="color: blue"&gt;let &lt;/span&gt;message = &lt;span style="color: maroon"&gt;&amp;quot;hello there: &amp;quot;

    &lt;/span&gt;Console.WriteLine(message + getTime ())

    &lt;span style="color: blue"&gt;do
        &lt;/span&gt;Console.WriteLine(&lt;span style="color: maroon"&gt;&amp;quot;hola&amp;quot;&lt;/span&gt;)

    &lt;span style="color: blue"&gt;do
        &lt;/span&gt;Console.WriteLine(&lt;span style="color: maroon"&gt;&amp;quot;oh hai&amp;quot;&lt;/span&gt;)
        Console.ReadKey() |&amp;gt; ignore&lt;/pre&gt;


&lt;p&gt;The previous two files are compiled as follows (Represented in C#, with some of the compiler noise removed).&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Goodbye
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;getTime() { &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.ToString(); }
    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;message { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;goodbye there: &amp;quot;&lt;/span&gt;; } }
}

&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Hello
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;getTime() { &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.ToString(); }
    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;message { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #a31515"&gt;&amp;quot;hello there: &amp;quot;&lt;/span&gt;; } }
}

&lt;span style="color: blue"&gt;internal static class &lt;/span&gt;$Goodbye
{
    &lt;span style="color: blue"&gt;static &lt;/span&gt;$Goodbye()
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;message = Goodbye.message;
        Console.WriteLine(Goodbye.message + Goodbye.getTime());
        Console.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;hasta luego&amp;quot;&lt;/span&gt;);
        Console.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;oh bai&amp;quot;&lt;/span&gt;);
        ConsoleKeyInfo info2 = Console.ReadKey();
    }
}

&lt;span style="color: blue"&gt;internal static class &lt;/span&gt;$Hello
{
    &lt;span style="color: blue"&gt;public static void &lt;/span&gt;main@()
    {
        &lt;span style="color: blue"&gt;string &lt;/span&gt;message = Hello.message;
        Console.WriteLine(Hello.message + Hello.getTime());
        Console.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;hola&amp;quot;&lt;/span&gt;);
        Console.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;oh hai&amp;quot;&lt;/span&gt;);
        ConsoleKeyInfo info2 = Console.ReadKey();
    }
}&lt;/pre&gt;


&lt;p&gt;A couple of things to note here. First of all, all the code (Whether defined in a do binding or directly on the module) gets rolled up into either a static constructor (If not the entry point) or the entry point method. The module's let bindings are placed in another class where value bindings are defined as properties and function bindings are defined as, well, methods. So you can see, as mentioned in the docs, that a do binding is not required when writing top level code in a module (The exception to this is if your writing a GUI app. You will need to use a do binding so that you can apply the STAThread attribute to it). The second thing to note is that it's the code in the &lt;strong&gt;&lt;em&gt;last&lt;/em&gt;&lt;/strong&gt; module that is compiled as the entry point for the application (If its not a library). And the entry point code does not need to be in a do binding (Unless it's a GUI app as mentioned above).&lt;/p&gt;

&lt;p&gt;If it is a GUI app you can use the do binding to define the STAThread attribute as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;open &lt;/span&gt;System
&lt;span style="color: blue"&gt;open &lt;/span&gt;System.Windows.Forms

[&amp;lt;STAThread&amp;gt;]
&lt;span style="color: blue"&gt;do
    let &lt;/span&gt;form = &lt;span style="color: blue"&gt;new &lt;/span&gt;Form()
    Application.Run(form)&lt;/pre&gt;

&lt;p&gt;Which gets compiled to:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;internal static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Startup
&lt;/span&gt;{
&lt;span style="color: green"&gt;    &lt;/span&gt;[&lt;span style="color: #2b91af"&gt;STAThread&lt;/span&gt;]
    &lt;span style="color: blue"&gt;public static void &lt;a href="mailto:$main@()"&gt;$&lt;/span&gt;main@()
&lt;/a&gt;    {
        &lt;span style="color: #2b91af"&gt;Form &lt;/span&gt;form = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Form&lt;/span&gt;();
        &lt;span style="color: #2b91af"&gt;Application&lt;/span&gt;.&lt;span style="color: red"&gt;&lt;font color="#000000"&gt;Run&lt;/font&gt;&lt;/span&gt;(form);
    }
}&lt;/pre&gt;

&lt;p&gt;All in all I don't see any point in using a do binding directly in a module unless you need to apply an attribute. If you, dear reader, have any insights into where a do binding in a module could be useful (Aside from applying attributes) I'd love to hear about it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In a Type&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As the docs mention, you can &lt;a href="http://msdn.microsoft.com/en-us/library/dd483473.aspx"&gt;use the do binding in a type&lt;/a&gt;. Using a do binding in a type is more interesting as it forms the type constructor (Either static or instance) and, unlike a within module, is required if you want to define any initialization logic (Other than let bindings which form the private fields). Here is an example (And yes, I know this is a contrived example as we could have just done the assignment in the let binding):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;namespace &lt;/span&gt;LawnCare
    &lt;span style="color: blue"&gt;open &lt;/span&gt;System

    &lt;span style="color: blue"&gt;type &lt;/span&gt;WeedWacker (manufacturer : string) =

        &lt;span style="color: blue"&gt;static let mutable &lt;/span&gt;random = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;maker = manufacturer
        &lt;span style="color: blue"&gt;let mutable &lt;/span&gt;sn = &lt;span style="color: blue"&gt;null

        static do
            &lt;/span&gt;random &amp;lt;- &lt;span style="color: blue"&gt;new &lt;/span&gt;Random()

        &lt;span style="color: blue"&gt;do
            &lt;/span&gt;sn &amp;lt;- maker.ToUpper() + &lt;span style="color: maroon"&gt;&amp;quot;-&amp;quot; &lt;/span&gt;+ random.Next().ToString()

        &lt;span style="color: blue"&gt;do
            &lt;/span&gt;maker &amp;lt;- maker.ToLower()

        &lt;span style="color: blue"&gt;member &lt;/span&gt;x.SerialNumber = sn
        &lt;span style="color: blue"&gt;member &lt;/span&gt;x.Maker = maker&lt;/pre&gt;


&lt;p&gt;This code gets compiled to the following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;internal static class &lt;/span&gt;$LawnCare
{
    &lt;span style="color: blue"&gt;static &lt;/span&gt;$LawnCare()
    {
        WeedWacker.random = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        WeedWacker.random = &lt;span style="color: blue"&gt;new &lt;/span&gt;Random();
    }
}
 
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WeedWacker
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;internal string &lt;/span&gt;sn;
    &lt;span style="color: blue"&gt;internal string &lt;/span&gt;maker;
    &lt;span style="color: blue"&gt;internal static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Random &lt;/span&gt;random;

    &lt;span style="color: blue"&gt;public &lt;/span&gt;WeedWacker(&lt;span style="color: blue"&gt;string &lt;/span&gt;manufacturer)
    {
        maker = manufacturer;
        sn = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        sn = maker.ToUpper() + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot; &lt;/span&gt;+ random.Next();;
        maker = maker.ToLower();
    }

    &lt;span style="color: blue"&gt;public string &lt;/span&gt;SerialNumber { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;sn; } }
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;Maker { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;maker; } }
}&lt;/pre&gt;

&lt;p&gt;Interesting to note that the type and its static constructor are separated into two different types. Also, just as with a module, you can define multiple do bindings and they will get rolled up into one instance and/or static constructor.&lt;/p&gt;

&lt;p&gt;Not the most exciting topic I must admit, but good to know none the less.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-8970574193170717986?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/4qs6rPX7n9U" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/8970574193170717986/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2010/12/exploring-f-do-binding.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/8970574193170717986?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/8970574193170717986?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/4qs6rPX7n9U/exploring-f-do-binding.html" title="Exploring The F# do Binding" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_DOioD_zaWYc/TPwOiARPn6I/AAAAAAAAAgM/Rx1a_qOnx8g/s72-c/image_thumb%5B18%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2010/12/exploring-f-do-binding.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUAMSXYzfip7ImA9Wx5aFkQ.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-4657222033166131116</id><published>2010-11-13T16:15:00.001-08:00</published><updated>2010-11-13T16:16:28.886-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-13T16:16:28.886-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="NHibernate" /><title>Why Updating a Clean Property with the NHibernate Pre Update Event Listener, with Dynamic Update Set on the Entity, Doesn't Work</title><content type="html">&lt;p&gt;Wow, that's a mouthful. Anyways, if your trying to update a property in an NHibernate (2.1.2.4000) pre update event listener that is &amp;quot;clean&amp;quot; (In other words wasn't updated in the calling code) and your entity is set to dynamic update the update will not make it to the database. Here is why that is the case. The pre update event gets called in the NHibernate.Action.EntityUpdateAction class. Problem is that by the time the event is called the dirty field list has already been set in this class. So no matter what you do the dirty list is already set in stone as an invariant:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EntityUpdateAction &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;EntityAction
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private &lt;font color="#ff0000"&gt;&lt;strong&gt;readonly&lt;/strong&gt;&lt;/font&gt; int&lt;/span&gt;[] &lt;strong&gt;&lt;font color="#ff0000"&gt;dirtyFields&lt;/font&gt;&lt;/strong&gt;;

    &lt;span style="color: green"&gt;// ...

    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;EntityUpdateAction(&lt;span style="color: blue"&gt;object &lt;/span&gt;id, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] state, &lt;span style="color: blue"&gt;int&lt;/span&gt;[] &lt;font color="#ff0000"&gt;&lt;strong&gt;dirtyProperties&lt;/strong&gt;&lt;/font&gt;, &lt;span style="color: blue"&gt;bool &lt;/span&gt;hasDirtyCollection, 
                              &lt;span style="color: blue"&gt;object&lt;/span&gt;[] previousState, &lt;span style="color: blue"&gt;object &lt;/span&gt;previousVersion, &lt;span style="color: blue"&gt;object &lt;/span&gt;nextVersion, &lt;span style="color: blue"&gt;object &lt;/span&gt;instance, 
                              &lt;span style="color: #2b91af"&gt;IEntityPersister &lt;/span&gt;persister, &lt;span style="color: #2b91af"&gt;ISessionImplementor &lt;/span&gt;session)
        : &lt;span style="color: blue"&gt;base&lt;/span&gt;(session, id, instance, persister)
    {
        &lt;font color="#ff0000"&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;.dirtyFields = dirtyProperties&lt;/strong&gt;&lt;/font&gt;;
        &lt;span style="color: green"&gt;// ...
    &lt;/span&gt;}

    &lt;span style="color: blue"&gt;public override void &lt;/span&gt;Execute()
    {
        &lt;span style="color: green"&gt;// ...
        &lt;/span&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;flag2 = &lt;span style="color: blue"&gt;this&lt;/span&gt;.&lt;span style="color: red"&gt;PreUpdate&lt;/span&gt;();
        &lt;span style="color: green"&gt;// ...
        &lt;/span&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(!flag2)
        {
            persister.Update(id, &lt;span style="color: blue"&gt;this&lt;/span&gt;.&lt;span style="color: red"&gt;&lt;font color="#333333"&gt;state&lt;/font&gt;&lt;/span&gt;, &lt;span style="color: blue"&gt;this&lt;/span&gt;.&lt;font color="#ff0000"&gt;&lt;strong&gt;dirtyFields&lt;/strong&gt;&lt;/font&gt;, &lt;span style="color: blue"&gt;this&lt;/span&gt;.&lt;span style="color: red"&gt;&lt;font color="#000000"&gt;hasDirtyCollection&lt;/font&gt;&lt;/span&gt;, &lt;span style="color: blue"&gt;this&lt;/span&gt;.&lt;span style="color: red"&gt;&lt;font color="#333333"&gt;previousState&lt;/font&gt;&lt;/span&gt;,
                             &lt;span style="color: blue"&gt;this&lt;/span&gt;.&lt;span style="color: red"&gt;&lt;font color="#000000"&gt;previousVersion&lt;/font&gt;&lt;/span&gt;, instance, &lt;span style="color: blue"&gt;null&lt;/span&gt;, session);
        }
        &lt;span style="color: green"&gt;// ...
    &lt;/span&gt;}
}&lt;/pre&gt;


&lt;p&gt;So as you can see, there is no way the pre update event can effect the dirty fields value that is passed into the update call. What's the solution? Create an interceptor and implement the OnFlushDirty method. The easiest way to do this is to inherit from EmptyInterceptor and override the method as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimestampInterceptor &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;EmptyInterceptor
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public override bool &lt;/span&gt;OnFlushDirty(&lt;span style="color: blue"&gt;object &lt;/span&gt;entity, &lt;span style="color: blue"&gt;object &lt;/span&gt;id, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] currentState, &lt;span style="color: blue"&gt;object&lt;/span&gt;[] previousState, 
                                      &lt;span style="color: blue"&gt;string&lt;/span&gt;[] propertyNames, &lt;span style="color: blue"&gt;global&lt;/span&gt;::NHibernate.Type.&lt;span style="color: #2b91af"&gt;IType&lt;/span&gt;[] types)
    {
        &lt;span style="color: blue"&gt;return &lt;/span&gt;SetTimestamp(entity, propertyNames, currentState, x =&amp;gt; x.Modified);
    }
}&lt;/pre&gt;


&lt;p&gt;Then register your interceptor and your good to go. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-4657222033166131116?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/SM7A5JTiqSI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/4657222033166131116/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2010/11/why-updating-clean-property-with.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/4657222033166131116?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/4657222033166131116?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/SM7A5JTiqSI/why-updating-clean-property-with.html" title="Why Updating a Clean Property with the NHibernate Pre Update Event Listener, with Dynamic Update Set on the Entity, Doesn&amp;#39;t Work" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2010/11/why-updating-clean-property-with.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkECQ3gyfip7ImA9Wx5VGEk.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-7263983584277287540</id><published>2010-10-01T16:52:00.001-07:00</published><updated>2010-10-11T16:44:22.696-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-10-11T16:44:22.696-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="SQL" /><title>The Great Guid vs Int PK Debate</title><content type="html">&lt;p&gt;&lt;em&gt;&lt;font color="#008000"&gt;Updated 10/11/2010&lt;/font&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;This has come up so often that I figured its time to do a blog post and just point people to it.&lt;/p&gt;  &lt;p&gt;First, like Voltaire said, lets define our terms. We're talking about a primary key which &lt;a href="http://en.wikipedia.org/wiki/Unique_key"&gt;according to Wikipedia&lt;/a&gt; is a value that &amp;quot;can uniquely identify each row in a table&amp;quot;. When I'm talking about ints I'm implying that it is an integer key that is incremented by the database (IE &lt;a href="http://msdn.microsoft.com/en-us/library/ms186775.aspx"&gt;&amp;quot;identity&amp;quot;&lt;/a&gt; in SQL Server). When I'm talking about guids I'm talking about &lt;a href="http://www.informit.com/articles/article.aspx?p=25862"&gt;sequential guids&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Reasons for using guids:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;They are managed by the application not the database.&lt;/strong&gt; I think this is my biggest reason for preferring them. I can have the application (domain or ORM) manage them which allows the application to be in control. Having the database manage key creation (Whether int or guid) buys me absolutely nothing and constrains the application for no good reason. I really think the application is where key management belongs, not the backing store. In relation to NHibernate, Tuna has an interesting post &lt;a href="http://nhforge.org/blogs/nhibernate/archive/2009/03/20/nhibernate-poid-generators-revealed.aspx"&gt;here&lt;/a&gt; and Fabio &lt;a href="http://fabiomaulo.blogspot.com/2009/02/nh210-generators-behavior-explained.html"&gt;here&lt;/a&gt; on this subject. I summarize: &lt;/li&gt;    &lt;ol&gt;     &lt;li&gt;&lt;strong&gt;Unit of work&lt;/strong&gt;. NHibernate has the concept of &lt;a href="http://nhforge.org/wikis/patternsandpractices/nhibernate-and-the-unit-of-work-pattern.aspx"&gt;unit of work&lt;/a&gt;. Objects are tracked and when you are ready to commit the changes it will figure out what needs to be done &lt;em&gt;&lt;strong&gt;at that time&lt;/strong&gt;&lt;/em&gt; and then do it. Using an int identity hijacks this ability when doing inserts because NH must &lt;strong&gt;&lt;em&gt;immediately&lt;/em&gt;&lt;/strong&gt; perform an insert (On Save()) in order to get the PK of the entity. If your doing a transaction and you roll it back then prior inserts have to be rolled back &lt;em&gt;&lt;strong&gt;in the database&lt;/strong&gt;&lt;/em&gt;, instead of simply having been in memory.&lt;/li&gt;      &lt;li&gt;&lt;strong&gt;Batching on insert&lt;/strong&gt;. For example creating the trite order and line items, committing and then having the line items be inserted in one shot instead of individually. If you're using an int identity you will not be able to take advantage of this type of batching because NHibernate needs to return the PK of each line item, which is generated by the database. Note that this &lt;em&gt;&lt;strong&gt;only&lt;/strong&gt;&lt;/em&gt; applies to inserts; as other operations do not generate a PK that needs to be returned (Like when doing an update).&lt;/li&gt;   &lt;/ol&gt;    &lt;li&gt;&lt;strong&gt;Easy to merge data.&lt;/strong&gt; This may or may not be a good reason depending on your application. Recently this has not been an issue for me but earlier in my career I worked for a payroll company that moved data around the system like crazy (It was a SAAS app that that had one DB per client). Some of the tables had int PKs and others had guid PK's. For obvious reasons it was much easier to move data from one db to another when you had guid PK's. Int PK's required regeneration so were a nightmare to deal with. And again, int PK's didn't buy us anything at all. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;They are not &lt;em&gt;as&lt;/em&gt; guessable as integers.&lt;/strong&gt; Notice I didn't say that weren't guessable period. Some things to think about:       &lt;ol&gt;       &lt;li&gt;Guids are not necessarily guaranteed to be truly random, as the UUID spec notes, they are &lt;a href="http://tools.ietf.org/html/rfc4122#section-6"&gt;guaranteed to be &lt;em&gt;unique&lt;/em&gt;&lt;/a&gt; not random (Some interesting discussion &lt;a href="http://stackoverflow.com/questions/2621563/how-random-is-system-guid-newguid-take-two"&gt;here&lt;/a&gt; on this subject). So the randomness, if any, depends on the algorithm used to generate them. If your developing on .NET 4.0 (Haven't checked older versions) your using Guid.NewGuid() which uses &lt;a href="http://msdn.microsoft.com/en-us/library/ms688568(VS.85).aspx"&gt;CoCreateGuid&lt;/a&gt; (You can see this in &lt;a href="http://www.red-gate.com/products/reflector/"&gt;Reflector&lt;/a&gt;). This in turn calls the function &lt;a href="http://msdn.microsoft.com/en-us/library/aa379205(VS.85).aspx"&gt;UuidCreate&lt;/a&gt; (So its creating a &lt;a href="http://tools.ietf.org/html/rfc4122"&gt;UUID&lt;/a&gt;). BTW, this means that the algorithm is OS dependent if your using .NET 4.0 (and probably older versions I'd imagine) as the guid generation does not happen in managed code. CoCreateGuid on both Windows Server 2003 SP2 x86 and Windows Server 2008 R2 yields a v4 UUID (See &lt;a href="http://tools.ietf.org/html/rfc4122#section-4.1.2"&gt;here&lt;/a&gt; for the layout). The &lt;a href="http://tools.ietf.org/html/rfc4122#section-4.4"&gt;spec states&lt;/a&gt; that this version &amp;quot;is meant for generating UUIDs from truly-random or pseudo-random number&amp;quot;. What does this mean exactly? All depends on the algorithm Microsoft is using and AFIAK there's no official information on it. I've seen a lot of talk about it on the web but it seems like its all anecdotal, nothing official. In any event it's probably safe to say that its &lt;a href="http://en.wikipedia.org/wiki/Pseudorandomness#Cryptography"&gt;pseudo random&lt;/a&gt;. So the bottom line is its not as guessable as say, incrementing an int, but if someone really wanted to guess prior and subsequent values they could do it. Maybe its like having a 10 foot high wooden fence around your yard as apposed to a 10 foot high, razor wire fence and guys with M4's? The wooden fence will stop the casual intruder but someone could get past it if they really really wanted to. Now what about the guid comb (&lt;a href="http://www.informit.com/articles/article.aspx?p=25862"&gt;Because this is what your going to want to use as its partially sequential and wont cause index fragmentation&lt;/a&gt;)? The &lt;a href="http://nhforge.org/blogs/nhibernate/archive/2009/05/21/using-the-guid-comb-identifier-strategy.aspx"&gt;NHibernate implementation&lt;/a&gt; (&lt;a href="http://gist.github.com/606878"&gt;NHibernate.Id.GuidCombGenerator&lt;/a&gt;) actually uses Guid.NewId() to get the base guid but then modifies part of it so that its sequential. According to the &lt;a href="http://tools.ietf.org/html/rfc4122#section-4.1.2"&gt;UUID spec&lt;/a&gt; the first 10 bytes are the temporally unique portion, and the last 6 bytes contain the spatially unique portion aka &amp;quot;node&amp;quot;. For version 4 of the UUID, the random one, everything is random, including the temporally unique portion &lt;em&gt;&lt;strong&gt;except for 6 bits &lt;/strong&gt;&lt;/em&gt;(Which specify the version and a reserved portion). The NHibernate guid comb implementation replaces the last 6 bytes with a temporally unique value (Making it sequential), while leaving the rest (74 bits) alone. So you go from having 122 bits of psuedo random data to having 74 bits of pseudo random data. Now your 10 foot tall wooden fence gets cut down to 6 feet. So that's why I say its not as guessable as an int identity. &lt;/li&gt;        &lt;li&gt;If your app is secure this really shouldn't matter. If being able to predict PK's allows an attacker to compromise your app then you have a problem and relying on security by obscurity is bad. And as we just discussed, guids are not truly random, or at least you shouldn't count on them being so. On the other hand you could say that its not possible to make sure there are absolutely no security holes in your application so using guid PK's raises the bar by making less information available to an attacker or at least making it harder to obtain. So I guess it all comes down to personal philosophy; I can see compelling arguments for both sides. I've sided on trying to make the app as secure as possible while giving attackers as little information as possible and making what information is there harder to obtain. &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Bunk reasons for using guids:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Ints are limited and we want a PK that will not run out of id's&lt;/strong&gt;. Yeah, ints are limited.....to &lt;em&gt;&lt;strong&gt;2.1 billion&lt;/strong&gt;&lt;/em&gt;!!! I don't buy this argument in the least. How many people doing line of business apps are running into that limit? Not saying it cant happen but we have a 400+ GB database and were not even scratching the surface of the the int upper limit. And you can always go to a big int if you want... &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Required for replication&lt;/strong&gt;. Ok this may not be a bunk reason, it may actually be a good reason for your application. But I see this reason thrown around a lot and I get the feeling that many people will not do replication down the road and just use this as yet another reason (Totally an anecdotal statement, no hard data on that one). &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;We want to be able to change our DBMS if we need to so we want to be completely decoupled from it&lt;/strong&gt;. I like the decoupling part, but the &amp;quot;change our DBMS part&amp;quot;? Does anyone actually swap out their DBMS on a regular basis, if ever? This is a bunk argument IMO because this does not happen in the real world. Plus I believe that most modern DBMS's support some sort of int identity so in the off chance this happens this will be the least of your worries. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Reasons for not using guids:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;They are slower and 4 times bigger: &lt;/strong&gt;There are a number of posts out there demonstrating that sequential guids are &lt;em&gt;&lt;strong&gt;marginally&lt;/strong&gt;&lt;/em&gt; slower than ints. Also they are 128 bits as opposed to 32 bits. In particular it will increase the size of your indexes (I don't include the size of the tables because 16 bytes for a column is relatively small which probably pales in comparison to the size of other columns in the table). Does that marginal difference in performance and increased index size mean you should definitely use ints? That really depends on your application and your development philosophy. Obviously if there is some concrete reason to go either way then the decision is already made (For example if you will do replication or if you've tested guid PK's and the performance hit is really that critical). If that isn't the case, which is probably where most LOB app developers are, then it may be less clear. Since the performance hit is marginal it may not be critical or even noticeable for your application. Be careful not to &amp;quot;strain the gnat&amp;quot; (Perceived guid vs int PK performance penalty) while &amp;quot;gulping down the camel&amp;quot; (All those real performance issues in your application). If the tables are big then the index size pales in comparison, so is that really an issue? I think it really comes down to philosophy. If your using a data centric approach then guids will seem pointless. So you might as well go the int route as guids aren't going to buy you anything and you will have the slight performance boost and smaller indexes. If your using an ORM and/or a DDD approach then the guid will be an attractive option as it puts the control where you want it; the application. In that case its definitely worth the slight performance hit or increased index size because there is a payoff.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Guid's are not human readable.&lt;/strong&gt; This is a personal preference thing and again depends on development philosophy as mentioned above. As someone who prefers a DDD approach I could care less if its human readable because I'd rather be spending my time in the domain, not mucking around in the db where I would be oft encountering guids. If your taking a data centric approach then you will be looking at them all the time and will probably want the simplicity and elegance of an integer. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Bunk reason for not using guids:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;They cause page splits in a clustered index and cause index fragmentation in non clustered indexes.&lt;/strong&gt; Guids yes, sequential guids no. Guid's are all psuedo random whereas guid combs have a sequential portion which avoids this. Sequential guids are on par with int identities performance-wise, check out Jimmy Nillson's &lt;a href="http://www.informit.com/articles/article.aspx?p=25862"&gt;guid comb article&lt;/a&gt; for some stats and further explanation. NHibernate also ships with a guid comb identity strategy so you can take advantage of it there.&amp;#160; &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;As mentioned earlier, I see the lines being drawn according to development philosophy. If you're using an ORM (And possibly doing some flavour of DDD) then there will be advantages to using a sequential guid, both concrete and philosophical, and no advantages to using an int. If your approach is data centric then guids will seem distasteful and int will seem like a trusted old friend. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-7263983584277287540?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/JKpj69gWn8E" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/7263983584277287540/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2010/10/great-guid-vs-int-pk-debate.html#comment-form" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7263983584277287540?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/7263983584277287540?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/JKpj69gWn8E/great-guid-vs-int-pk-debate.html" title="The Great Guid vs Int PK Debate" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>3</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2010/10/great-guid-vs-int-pk-debate.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU8DQHY7fCp7ImA9Wx5WF0U.&quot;"><id>tag:blogger.com,1999:blog-1785616503956962921.post-3153952253266208456</id><published>2010-09-29T11:08:00.001-07:00</published><updated>2010-09-29T11:11:11.804-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-29T11:11:11.804-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Open Source Projects" /><title>Novacut - Distributed Video Editing</title><content type="html">&lt;p&gt;&lt;a href="http://novacut.com/"&gt;&lt;img style="margin: 0px; display: inline" alt="Novacut" align="right" src="http://cdn.novacut.com/b2ebf0ebf1fa287976a20fc6f30229d916f2ebf9.png" width="296" height="46" /&gt;&lt;/a&gt;A buddy of mine is developing an OSS collaborative video editor inspired by the distributed nature of existing OSS tools like git. This seems like a very innovative approach as a team scattered around the world could shoot and post process video in a collaborative way. I just recently watched &lt;a href="http://www.imdb.com/title/tt0476240/"&gt;La gran final&lt;/a&gt; (Great movie BTW) and I bet Novacut would have been perfect for a project like that. The project primarily aims to facilitate indy film and TV production which lately, IMO, outshines the big production houses. Anyways, check it out &lt;a href="http://novacut.com/"&gt;here&lt;/a&gt; and back it financially &lt;a href="http://www.kickstarter.com/projects/novacut/novacut-entertainments-next-step"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1785616503956962921-3153952253266208456?l=blog.mikeobrien.net' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/D0hkhTknVkM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://blog.mikeobrien.net/feeds/3153952253266208456/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://blog.mikeobrien.net/2010/09/novacut-distributed-video-editing.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3153952253266208456?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1785616503956962921/posts/default/3153952253266208456?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/MobsDevBlog/~3/D0hkhTknVkM/novacut-distributed-video-editing.html" title="Novacut - Distributed Video Editing" /><author><name>Mike O'Brien</name><uri>http://www.blogger.com/profile/03869766123622193305</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://2.bp.blogspot.com/_DOioD_zaWYc/TGGOYEROe8I/AAAAAAAAAco/TjpdOotMmgI/S220/Igor.JPG" /></author><thr:total>0</thr:total><feedburner:origLink>http://blog.mikeobrien.net/2010/09/novacut-distributed-video-editing.html</feedburner:origLink></entry></feed>

