<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Philip Mateescu</title>
 <link href="http://philipm.at/atom.xml" rel="self"/>
 <link href="http://philipm.at/"/>
 <updated>2013-01-14T00:48:49-06:00</updated>
 <id>http://philipm.at/</id>
 <author>
   <name>Philip Mateescu</name>
   <email>hello@philipm.at</email>
 </author>

 
 <entry>
   <title>django-permissivecsrf</title>
   <link href="http://philipm.at/2013/django-permissivecsrf.html"/>
   <updated>2013-01-14T00:00:00-06:00</updated>
   <id>http://philipm.at/2013/django-permissivecsrf</id>
   <content type="html">&lt;p&gt;I bet you're here because you've run into the puzzling Django error&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;CSRF verification failed. Request aborted.

Reason given for failure:
    Referer checking failed - http://&amp;lt;domain&amp;gt;/ does not match https://&amp;lt;domain&amp;gt;/.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I'll double down that this happens when you try to make your login form post to HTTPS,
perhaps to &lt;code&gt;https://&amp;lt;domain&amp;gt;/accounts/login/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If I'm mostly right, you've run into the problem I've solved. If I'm not right and
you somehow ended up on this page, I'd love &lt;a href=&quot;/contact.html&quot;&gt;to hear about it&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Short version&lt;/h2&gt;

&lt;p&gt;In the CSRF middleware, Django does an extra check when a request comes over HTTPS to ensure it comes
from the same site (same origin check).&lt;/p&gt;

&lt;p&gt;If any portion of your site benefits from HTTPS, you probably should run your entire site over HTTPS.
You can use &lt;a href=&quot;https://github.com/rdegges/django-sslify&quot;&gt;django-sslify&lt;/a&gt; to force your website to operate in HTTPS mode all the time.&lt;/p&gt;

&lt;p&gt;Furthermore, if you want to disable CSRF checking for your own views, there are &lt;a href=&quot;https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#edge-cases&quot;&gt;other methods
you can use&lt;/a&gt;, for example the &lt;code&gt;@csrf_exempt&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;However, if the views are not under your control and you are comfortable n trading some security
for not really that much convenience, you can use &lt;a href=&quot;https://github.com/philipmat/django-permissivecsrf&quot;&gt;django-permissivecsrf&lt;/a&gt;
to work around this error.&lt;/p&gt;

&lt;p&gt;Use the instructions in the README file on &lt;a href=&quot;https://github.com/philipmat/django-permissivecsrf&quot;&gt;GitHub&lt;/a&gt; or on &lt;a href=&quot;http://pypi.python.org/pypi/django-permissivecsrf/&quot;&gt;PyPI&lt;/a&gt; to get this project up an running.
Mostly it consists of installing django-permissivecsrf and adding &lt;code&gt;'permissivecsrf.middleware.PermissiveCSRFMiddleware'&lt;/code&gt;
to your &lt;code&gt;MIDDLEWARE_CLASSES&lt;/code&gt; entry.&lt;/p&gt;

&lt;p&gt;Enjoy.&lt;/p&gt;

&lt;h2&gt;Long version&lt;/h2&gt;

&lt;p&gt;The gist of why this happens is explained in point #4 of the &lt;a href=&quot;https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-it-works&quot;&gt;How it works&lt;/a&gt; section of the Django documentation on
Cross Site Request Forgery (emphasis mine):&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;4) In addition, for HTTPS requests, strict referer checking is done by CsrfViewMiddleware.
This is necessary to address a Man-In-The-Middle attack that is possible under HTTPS
when using a session independent nonce, due to the fact that HTTP 'Set-Cookie' headers
are (unfortunately) accepted by clients that are talking to a site under HTTPS.
&lt;strong&gt;(Referer checking is not done for HTTP requests because the presence of the Referer header is not reliable enough under HTTP.)&lt;/strong&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;In other words, because the HTTPS headers are encrypted, the &lt;em&gt;HTTP-Referer&lt;/em&gt; header is resilient
against MITM attacks, so it can be safely used to check and make sure the CSRF cookie
is originated by the same site that served the page &lt;em&gt;and&lt;/em&gt; that the referring page has also been
served over HTTPS, which means that page has also been protected against header injections.&lt;/p&gt;

&lt;p&gt;The same check could be made on HTTP calls as well, but since HTTP headers are not encrypted, they
could be easily faked and thus the check would be a useless placebo.&lt;/p&gt;

&lt;p&gt;This explanation is also present, in comment form, in this &lt;a href=&quot;https://github.com/django/django/commit/f92a21daa7&quot;&gt;f92a21daa7&lt;/a&gt; commit by spookylukey aka Luke Plant,
and further detailed by him in a &lt;a href=&quot;https://groups.google.com/d/msg/django-developers/IgWK2vEePtY/R1r3Im4x3UMJ&quot;&gt;reply&lt;/a&gt; to a complaint about the strictness of CSRF Referer check
on the django-developers maillist.&lt;/p&gt;

&lt;h3&gt;How django-permissivecsrf works&lt;/h3&gt;

&lt;p&gt;The &lt;a href=&quot;https://github.com/django/django/blob/master/django/middleware/csrf.py&quot;&gt;Django CSRF middleware&lt;/a&gt; performs an extra-check if the request is over HTTPS to
ensure that the request came from the same site, i.e. that
the referrer (HTTP-Referer header) matches the current site,
and that the schema of the referrer is also HTTPS.&lt;/p&gt;

&lt;p&gt;In other words, in ensures that the call to https://example.com/account/login
came from another page of https://example.com/. As such, if you put your login
form on your non-secure homepage, http://example.com/, but use a secure target
for your form's &lt;em&gt;action&lt;/em&gt; attribute, &lt;code&gt;&amp;lt;form action=&quot;https://example.com/account/login&quot; method=&quot;POST&quot;&amp;gt;&lt;/code&gt;,
Django's check will fail because::&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;'http://example.com/' != ('https://%s/' % request.get_host())
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, Django will not perform the CSRF check &lt;em&gt;at all&lt;/em&gt; if the &lt;code&gt;request&lt;/code&gt; object has
an attribute &lt;code&gt;_dont_enforce_csrf_checks&lt;/code&gt; set to &lt;strong&gt;True&lt;/strong&gt;. That's what PermissiveCSRF relies on:
if the request came from the same site, regardless the schema, it sets &lt;code&gt;_dont_enforce_csrf_checks&lt;/code&gt;
to True, thus telling the Django CSRF middleware to skip the CSRF check for that request.&lt;/p&gt;

&lt;p&gt;This only happens if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;DEBUG == True&lt;/code&gt;. Your production server should always be HTTPS;&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;HTTP-Referer&lt;/code&gt; header is present;&lt;/li&gt;
&lt;li&gt;The request is for an HTTPS URL (i.e. &lt;code&gt;request.is_secure() == True&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;and the referrer uses HTTP.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;In all other cases it defers to Django for normal processing.&lt;/p&gt;

&lt;h3&gt;Bottom line&lt;/h3&gt;

&lt;p&gt;There's only one thing to take away from all this: &lt;strong&gt;in production use HTTPS (see &lt;a href=&quot;https://github.com/rdegges/django-sslify&quot;&gt;django-sslify&lt;/a&gt;)&lt;/strong&gt;. Period.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Miniature Minifier Makefile</title>
   <link href="http://philipm.at/2012/miniature_minifier_makefile.html"/>
   <updated>2012-09-30T00:00:00-05:00</updated>
   <id>http://philipm.at/2012/miniature_minifier_makefile</id>
   <content type="html">&lt;p&gt;A solution that uses only make and curl:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;make&quot;&gt;&lt;span class=&quot;c&quot;&gt;# c/o: http://wonko.com/post/simple-makefile-to-minify-css-and-js&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# Patterns matching CSS files that should be minified. Files with a -min.css&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# suffix will be ignored.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;CSS_FILES&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;filter-out %.min.css,&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;wildcard &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    css/*.css &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    css/**/*.css &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Patterns matching JS files that should be minified. Files with a -min.js&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# suffix will be ignored.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;JS_FILES&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;filter-out %.min.js,&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;wildcard &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    js/*.js &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    js/**/*.js &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Commands&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;CSS_MINIFIER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; curl -X POST -s &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    --data-urlencode &lt;span class=&quot;s2&quot;&gt;&amp;quot;input@CSS_TMP&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    http://www.cssminifier.com/raw

&lt;span class=&quot;nv&quot;&gt;JS_MINIFIER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; curl -s -X POST &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    --data-urlencode &lt;span class=&quot;s2&quot;&gt;&amp;quot;js_code@JS_TMP&amp;quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    http://marijnhaverbeke.nl//uglifyjs 

&lt;span class=&quot;nv&quot;&gt;CSS_MINIFIED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;CSS_FILES:.css&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;.min.css&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;JS_MINIFIED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;JS_FILES:.js&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;.min.js&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# target: minify - Minifies CSS and JS.&lt;/span&gt;
minify: minify-css minify-js

&lt;span class=&quot;c&quot;&gt;# target: minify-css - Minifies CSS.&lt;/span&gt;
minify-css: &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;CSS_FILES&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;CSS_MINIFIED&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# target: minify-js - Minifies JS.&lt;/span&gt;
minify-js: &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;JS_FILES&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;JS_MINIFIED&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;

%.min.css: %.css
    @echo &lt;span class=&quot;s1&quot;&gt;&amp;#39;  Minifying $&amp;lt; ==&amp;gt; $@&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;subst CSS_TMP,&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;&amp;lt;&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;,&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;CSS_MINIFIER&lt;span class=&quot;k&quot;&gt;))&lt;/span&gt; &amp;gt; &lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;
    @echo

%.min.js: %.js
    @echo &lt;span class=&quot;s1&quot;&gt;&amp;#39;  Minifying $&amp;lt; ==&amp;gt; $@&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;subst JS_TMP,&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;&amp;lt;&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;,&lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;JS_MINIFIER&lt;span class=&quot;k&quot;&gt;))&lt;/span&gt; &amp;gt; &lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;
    @echo

&lt;span class=&quot;c&quot;&gt;# target: clean - Removes minified CSS and JS files.&lt;/span&gt;
clean:
    rm -f &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;CSS_MINIFIED&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;JS_MINIFIED&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;c&quot;&gt;# target: help - Displays help.&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;help&lt;/span&gt;:
    @egrep &lt;span class=&quot;s2&quot;&gt;&amp;quot;^# target:&amp;quot;&lt;/span&gt; Makefile
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;You can also find the latest version &lt;a href=&quot;https://github.com/philipmat/LeaseMilesTracker/blob/master/Makefile&quot;&gt;on GitHub&lt;/a&gt; part of my &lt;a href=&quot;https://github.com/philipmat/LeaseMilesTracker/&quot;&gt;LeaseMilesTracker&lt;/a&gt; project.&lt;/p&gt;

&lt;h2&gt;How Does It Work?&lt;/h2&gt;

&lt;p&gt;Surprisingly simple.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We make a list of all CSS files, filtering out the &lt;em&gt;*.min.css&lt;/em&gt; files;&lt;/li&gt;
&lt;li&gt;we ask &lt;a href=&quot;http://www.cssminifier.com/&quot;&gt;cssminifier.com&lt;/a&gt; to make us a minified version;&lt;/li&gt;
&lt;li&gt;then we save the latter to a file ending in &lt;em&gt;min.css&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Same with the JavaScript files, but using the service provided by &lt;a href=&quot;http://marijnhaverbeke.nl/uglifyjs&quot;&gt;UglifyJS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The obvious targets are &lt;em&gt;minify-css&lt;/em&gt;, &lt;em&gt;minify-js&lt;/em&gt;, and &lt;em&gt;minify&lt;/em&gt; which is dependent on both.
The less obvious targets are &lt;em&gt;%.min.css&lt;/em&gt; and &lt;em&gt;%.min.js&lt;/em&gt; whose purpose is to cause
the re-minification of the CSS or JS files, if the normal files are newer than the minified version.&lt;/p&gt;

&lt;p&gt;Finally, &lt;em&gt;clean&lt;/em&gt; will remove all the minified files, that is all &lt;em&gt;*.min.css&lt;/em&gt; and &lt;em&gt;*.min.js&lt;/em&gt; files.&lt;/p&gt;

&lt;h2&gt;Why Would I Use This?&lt;/h2&gt;

&lt;p&gt;Ryan Grove's &lt;a href=&quot;http://wonko.com/post/simple-makefile-to-minify-css-and-js&quot;&gt;&quot;Simple makefile to minify CSS and JS&quot;&lt;/a&gt;,
the original inspiration for my Makefile,
is an excellent solution if you're using the &lt;a href=&quot;https://github.com/yui/yuicompressor/&quot;&gt;YUI compressor&lt;/a&gt;,
but YUIc requires Java (and so does Google's closure-compiler) and I was interesting
in having this not only work without Java,
but more important work on a system that would require no special tools
beyond what you'd find standard on most *nix distros.&lt;br/&gt;
After all this is 2012 and we're supposed to be using RESTful APIs and what not.&lt;/p&gt;

&lt;p&gt;Oh, and also Mountain Lion doesn't come with Java installed.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Kendo Grid Select Editor</title>
   <link href="http://philipm.at/2012/kendo_select_editor.html"/>
   <updated>2012-09-11T00:00:00-05:00</updated>
   <id>http://philipm.at/2012/kendo_select_editor</id>
   <content type="html">&lt;p&gt;I'm going to assume you're reading this post because you'd like to find out
how to use a regular HTML SELECT as an editor in a &lt;a href=&quot;http://demos.kendoui.com/web/grid/index.html&quot;&gt;Kendo grid&lt;/a&gt;,
and that you already know why you want to do that - you just had a
hard time figuring out how to.&lt;/p&gt;

&lt;p&gt;If you just want to see the code and don't care about the explanation,
&lt;a href=&quot;http://jsfiddle.net/8CWcF/10/&quot;&gt;here is a jsFiddle&lt;/a&gt; to play with,
also a &lt;a href=&quot;https://gist.github.com/3695638&quot;&gt;gist&lt;/a&gt; for your forking pleasure.&lt;/p&gt;

&lt;p&gt;If you wonder why you'd want to do it, read the &lt;a href=&quot;#why&quot;&gt;Why would I want to do that?&lt;/a&gt;
section that come back for the how.&lt;/p&gt;

&lt;p&gt;I see two primary scenarios for using a SELECT aka drop-down list.&lt;/p&gt;

&lt;h2&gt;Simple Field - Extra Info&lt;/h2&gt;

&lt;p&gt;This would be the case where, for example, your model contains a field to store
the user login (e.g. &lt;em&gt;foo&lt;/em&gt;), but when displaying the drop-down to select the user
you'd like to also list their name (e.g. &lt;em&gt;foo - Foo Frye&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Let us assume you have the mapping of login to full name in an array of objects,
and for genericality's sake let's assume it has a format similar to the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var nameList = [
  { Login: 'foo', Description: 'foo - Foo Frye' },
  ...
]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When you create your grid and specify the columns, you have the chance to pass in a function
that would create a custom editor instead of the text box the grid gives you by default:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#grid&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;kendoGrid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;dataSource&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Login&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Login&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;editor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;lt;select &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
                  &lt;span class=&quot;s1&quot;&gt;&amp;#39; data-bind=&amp;quot;source: listSource.list, &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
                    &lt;span class=&quot;s1&quot;&gt;&amp;#39;value: &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;field&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;quot; &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                  &lt;span class=&quot;s1&quot;&gt;&amp;#39; data-text-field=&amp;quot;Description&amp;quot;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                  &lt;span class=&quot;s1&quot;&gt;&amp;#39; data-value-field=&amp;quot;Login&amp;quot;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                  &lt;span class=&quot;s1&quot;&gt;&amp;#39;/&amp;gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;listSource&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;kendo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;observable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;nameList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;What's going on here?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We create a drop-down &lt;code&gt;$('&amp;lt;select/&amp;gt;')&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Using the &lt;code&gt;data-bind&lt;/code&gt; attribute we tell Kendo to use a specific &lt;code&gt;source&lt;/code&gt;
for the drop-down and to write the selected &lt;code&gt;value&lt;/code&gt; to the underlying
model's field (here &lt;code&gt;options.field == 'Login'&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;We tell it to use the &lt;em&gt;Description&lt;/em&gt; property of each element in the
&lt;code&gt;source&lt;/code&gt; for the text portion, and the &lt;em&gt;Login&lt;/em&gt; field for the &lt;code&gt;value&lt;/code&gt;.
This would be as if we had an &lt;code&gt;&amp;lt;option value=&quot;foo&quot;&amp;gt;foo - Foo Frye&amp;lt;/option&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We create a &lt;code&gt;kendo.observable&lt;/code&gt; containing the list of names.&lt;/li&gt;
&lt;li&gt;We append the HTML select to the container, which, if you're curious,
is the actual grid cell (&lt;code&gt;TD&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;There are only two tricky parts here and they both gravitate towards the
use of the &lt;code&gt;source&lt;/code&gt; directive: &lt;code&gt;options.model&lt;/code&gt; is the object from your data source that correspond to the
grid row; the &lt;code&gt;source: listSource.list&lt;/code&gt; will look for a property called &lt;em&gt;listSource&lt;/em&gt;
on the grid row/object/model and expect it to  &lt;br/&gt;
a) be a kendo.observable and  &lt;br/&gt;
b) contain a sub-object named &lt;em&gt;list&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;Complex Field&lt;/h2&gt;

&lt;p&gt;What if your &lt;em&gt;Login&lt;/em&gt; field was a complex object, maybe similar to one
of the entries in &lt;code&gt;nameList&lt;/code&gt;? If this was the case, how would you go
about displaying it in a SELECT, but have it write back to your field
the same complex object, not just a simple string.&lt;/p&gt;

&lt;p&gt;The code looks similar, in a way even simpler:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#grid&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;kendoGrid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;dataSource&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Complex&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Login&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Description&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;foo - Foo Frye&amp;#39;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;columns&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Complex&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;editor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;lt;select &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
                  &lt;span class=&quot;s1&quot;&gt;&amp;#39; data-bind=&amp;quot;source: listSource.list, &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
                    &lt;span class=&quot;s1&quot;&gt;&amp;#39;value: &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;field&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;quot; &amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                  &lt;span class=&quot;s1&quot;&gt;&amp;#39; data-text-field=&amp;quot;Description&amp;quot;&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
                  &lt;span class=&quot;s1&quot;&gt;&amp;#39;/&amp;gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;listSource&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;kendo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;observable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;nameList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The most significant difference from the simple example is the
absence of the &lt;code&gt;data-text-value&lt;/code&gt; attribute. This causes the entire
underlying record to be written back into the &lt;em&gt;Complex&lt;/em&gt; model field.&lt;/p&gt;

&lt;p&gt;That's it. If there was a simpler way to do this, I couldn't find it.&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;why&quot;&gt; &lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Why Would I Want to Do This?&lt;/h2&gt;

&lt;p&gt;If you use Telerik's excellent Kendo stack, you already have access to a nice
drop-down editor that is part of the suite. The Kendo UI demos even show you
&lt;a href=&quot;http://demos.kendoui.com/web/grid/editing-custom.html&quot;&gt;how to use the Kendo DropDownList&lt;/a&gt;
as a custom editor, and use it you should for it has a plethora of features.&lt;/p&gt;

&lt;p&gt;For all its niceness, the DropDownList is built upon styled UL/LI elements,
and that means it is missing a few features that a normal drop-down would have,
chiefly the ability to use keyboard navigation and the two
significant advantages that come with it: using keys to trigger and navigate the
list (Alt-Down on Windows and Spacebar on Mac), and type-to-select when focused
(for example typing &lt;strong&gt;Tex&lt;/strong&gt; or &lt;strong&gt;TT&lt;/strong&gt; to select &lt;strong&gt;Texas&lt;/strong&gt; in a list of states).&lt;/p&gt;

&lt;p&gt;That and the fact that it doesn't play 100% nice with &lt;a href=&quot;http://twitter.github.com/bootstrap/&quot;&gt;Twitter Bootstrap&lt;/a&gt;,
in the sense that the styles don't quite match. Oh, and on a mobile browser you don't
get the native picker.&lt;/p&gt;

&lt;p&gt;Fortunately, the Kendo grid allows you to specify a custom editor for a column;
unfortunately, the Kendo stack tends to make use of and wrap your SELECT in a
&lt;code&gt;kendo.observable&lt;/code&gt; type of object. I suspect that this is the main reason
why examples of using normal HTML inputs with the Kendo grid tend to be scarce on the web.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Templates for GoodNotes</title>
   <link href="http://philipm.at/2012/goodnotes_templates.html"/>
   <updated>2012-04-28T00:00:00-05:00</updated>
   <id>http://philipm.at/2012/goodnotes_templates</id>
   <content type="html">&lt;p&gt;Recently I've been testing a few apps for taking notes
and I have settled on &lt;a href=&quot;http://goodnotesapp.com/&quot;&gt;GoodNotes&lt;/a&gt;.&lt;br/&gt;
It's a really good looking app and the developers are dedicated to it.
How dedicated? They had a retina version out within days after the iPad 3 release.&lt;/p&gt;

&lt;!--


This is a point worth accounting for because it's almost two months later 
and *Penultimate*, probably the best known notes app,
still doesn't have a retina version. Neither does *Notes+* (the most expensive 
of the popular apps).

It took *Notability* more than a month, to release a version. 
*UPAD*, another popular app, was not even working on 
the new iPad; the developer came up with a fix about a week later, but still 
no retina-optimized graphics.


*GoodNotes* comes with a few built-in &quot;paper&quot; templates for notes: ruled, squared, and music sheet, 
but it also supports importing new templates.

--&gt;


&lt;p&gt;After a few weeks of giving up paper and exclusively using the app,
I've created a few templates for the type of notes I find myself taking most frequently.&lt;/p&gt;

&lt;p&gt;While these templates have been build with GoodNotes in mind, you can
&lt;a href=&quot;#tech_notes&quot;&gt;take the SVG files&lt;/a&gt; and with very few tweaks target them towards other
note taking apps.&lt;/p&gt;

&lt;p&gt;Read on for an explanation of the design and thoughts that went into
each template, how to import them into GoodNotes, and how you can
help improve them, or &lt;a href=&quot;#tldr&quot;&gt;jump to the end&lt;/a&gt; to see where you can get them.&lt;/p&gt;

&lt;h2&gt;The Templates&lt;/h2&gt;

&lt;p&gt;The template are geared towards writing and designing rather than viewing (read-only)
so &lt;a href=&quot;#tech_notes&quot;&gt;they account&lt;/a&gt; for the space occupied by UI elements. They
also favor unobtrusive guiding elements so that they provide support for
writing but not distract from what is being written or drawn.&lt;/p&gt;

&lt;h3&gt;960 Grid System&lt;/h3&gt;

&lt;p&gt;Two templates support the &lt;a href=&quot;http://960.gs/&quot;&gt;960 Grid&lt;/a&gt; system (also employed by my favorite: &lt;a href=&quot;http://twitter.github.com/bootstrap/&quot;&gt;Twitter Bootstrap&lt;/a&gt;)
in 12 column format:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;960px Portrait&lt;/em&gt; - has longer columns for designing in portrait mode;&lt;br/&gt;
&lt;img src=&quot;/media/images/gn-t-1.jpg&quot; alt=&quot;960px portrait&quot; /&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;960px Landscape&lt;/em&gt;  - has wider columns (33% wider), better suited for drawing in landscape.&lt;br/&gt;
&lt;img src=&quot;/media/images/gn-t-2.jpg&quot; alt=&quot;960px landscape&quot; /&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;These two has been inspired by &lt;a href=&quot;http://blog.hellopanos.co.uk/post/20405921067/penultimate-templates-for-960&quot;&gt;hellopanos's templates&lt;/a&gt; for &lt;em&gt;Penultimate&lt;/em&gt;,
which I found too dark and thus distracting. Since the iPad has a great screen
a lighter color is very visible and let's the design take center stage while still
providing alignment guidance. Space between columns is proportional to the width of the columns.&lt;/p&gt;

&lt;p&gt;Since the design is performed within the constraint of the columns,
I have chosen to indicate the outer/side border of the 960px grid
with two faint lines. This way they are not distracting yet
provide the visual indication of what the boundaries are.&lt;/p&gt;

&lt;h3&gt;iPad&lt;/h3&gt;

&lt;p&gt;Three iPad templates present an iPad that is scaled about 73% from the actual size (in pixels).
FWIW, I even got the corner radius accurate and then I realized it doesn't really matter that much.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/gn-t-6.jpg&quot; alt=&quot;iPad template&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The screen portion displays the status bar (20px scaled by 0.73) and if you squint hard enough you'll see marker
lines for the 44pt navigation and 49pt tab bars. They are very faint so that they don't distract
from designing your interface, but still present if you want to make use of them.&lt;/p&gt;

&lt;h3&gt;Meeting Notes&lt;/h3&gt;

&lt;p&gt;Inspired by a Penultimate template, it contains a top section to record the date/time and place
of the meeting, the subject/topic/reason for meeting, and the participants. This section doesn't
offer a great deal of space, but if you need more than that you're probably doing something wrong
or at least ineffectual.&lt;/p&gt;

&lt;p&gt;The template devotes most of its body to the meeting notes and is concluded by a checklist-like
section to record action items.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/gn-t-3.jpg&quot; alt=&quot;Meeting Notes template&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I gave each action item what I thought to be enough space, but you might want to consider
writing in those sections using the GoodNotes magnifier mode.&lt;/p&gt;

&lt;p&gt;Each section contains almost transparent text to indicate its purpose.
I've thought it would be nice to provide such a hint, but I made it light
enough so that it allows to write on top of it.&lt;/p&gt;

&lt;h3&gt;Task List&lt;/h3&gt;

&lt;p&gt;The task list template is geared toward being able to capture tasks quickly. That means
that the space for each of the 14 lines is oversized so that you don't have to use the magnifier mode.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/gn-t-4.jpg&quot; alt=&quot;Task List template&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The lines are a bit darker than the other templates as they intend to provide more guidance
for writing. There is white space at the top of the templates, enough to write a
title for the task list, but not so much that it's distracting it if you don't.&lt;/p&gt;

&lt;p&gt;If you look at the image by itself you might think there's too much space at the bottom,
but remember that GoodNotes has an area at the bottom where it displays the toolbar
for bringing up the palm rest or magnifier. The template accounts for that.&lt;/p&gt;

&lt;h3&gt;ToDo List&lt;/h3&gt;

&lt;p&gt;The to-do template also has 14 rows of checkboxes but in two-column format as it's
designed for shorter entries. You can use the magnifier mode if you need to
capture more details.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/gn-t-5.jpg&quot; alt=&quot;ToDo template&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Since to-do lists are more likely to have a title,
the template provides more emphasis by separating the title area from the checklist
through a darker line, while at the same time the individual to-do entries have
lighter supporting lines to allow more focus on the written text.&lt;/p&gt;

&lt;h2&gt;How to Import the Templates&lt;/h2&gt;

&lt;p&gt;Using the images in GoodNotes is simple. Get the templates
onto the device, then from within a notebook click the &lt;strong&gt;+&lt;/strong&gt; button
in the toolbar, click &lt;em&gt;Import&lt;/em&gt;, select your image and go to town on it.&lt;/p&gt;

&lt;p&gt;However, if you find yourself using one or more of the templates with some frequency,
it might be worth going through the following five-step process to make them
part of the template gallery.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Click on the &lt;strong&gt;+&lt;/strong&gt; button in the toolbar and choose &lt;strong&gt;Import&lt;/strong&gt;&lt;br/&gt;
&lt;img src=&quot;/media/images/gn-import-1.jpg&quot; alt=&quot;Import image into GoodNotes&quot; /&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select your image from one of the image sources, depending on how you made available
the templates to GoodNotes (in this case, I copied them to the app using iTunes).&lt;br/&gt;
&lt;img src=&quot;/media/images/gn-import-2.jpg&quot; alt=&quot;Select the template to import&quot; /&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Repeat step 1: touch the &lt;strong&gt;+&lt;/strong&gt; in the toolbar, but select &lt;strong&gt;Other Templates&lt;/strong&gt; this time.
You will be presented with the template selection dialog.&lt;br/&gt;
&lt;img src=&quot;/media/images/gn-import-3.jpg&quot; alt=&quot;Go to Template dialog&quot; /&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click the &lt;strong&gt;+&lt;/strong&gt; button in the top left corner of this dialog, then select &lt;strong&gt;Current Template&lt;/strong&gt;.&lt;br/&gt;
&lt;img src=&quot;/media/images/gn-import-4.jpg&quot; alt=&quot;Add current template to template list&quot; /&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Give your template a recognizable name. Optionally, touch &lt;strong&gt;Edit&lt;/strong&gt; then use
the sizing chevron to move your template(s) in a better position.&lt;br/&gt;
&lt;img src=&quot;/media/images/gn-import-5.jpg&quot; alt=&quot;Name template and choose position&quot; /&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&lt;a name=&quot;tech_notes&quot;&gt; &lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Tech Notes&lt;/h2&gt;

&lt;p&gt;One of the decisions I made early on was to optimize these templates towards
input (writing/drawing), which meant having to account for GoodNotes's toolbar
and palm-rest/magnifier. I could've started with a &lt;em&gt;768 x 1024&lt;/em&gt; image to mimic
the iPad's screen, but I've noticed that GoodNotes moves the template down so
that its top is right below the toolbar, effectively clipping its bottom part
and thus reducing the effective area.&lt;/p&gt;

&lt;p&gt;For this reason, the templates are actually &lt;em&gt;768 x 972&lt;/em&gt;, given that GoodNotes's
toolbar is 52 pixels high. The chevron that triggers the palm-rest/magnifier
is 35 pixels high, so I designed all templates so that their bottom 40 pixels
remains clear. I could've cropped the image and target 768 x 937, but I
thought it looked better if the &quot;paper&quot; continued all the way to the bottom of the screen.&lt;/p&gt;

&lt;p&gt;To create each template, I have started with a handcrafted SVG file and
tweaked it for hours (mostly because I had no familiarity with SVG)
until I got the desired result.&lt;/p&gt;

&lt;p&gt;I then exported the images into PNG using OS X's &lt;code&gt;qlmanage&lt;/code&gt; and
ImageMagick's &lt;code&gt;convert&lt;/code&gt; and loaded them into GoodNotes to check out
the result and make sure the templates were comfortable to use.&lt;/p&gt;

&lt;p&gt;Converting to PNG could've (should've) been a simple process because ImageMagick
can convert SVG images, alas the feature-set support is limited, and occasionally,
incorrectly implemented.&lt;/p&gt;

&lt;p&gt;It would've been a great deal easier to create them in an image editor and,
indeed, the first attempt was in Pixelmator and it took about 1/3 to half
of the time it took to create the SVGs.&lt;/p&gt;

&lt;p&gt;However, since the SVG files are text they are effectively the &lt;em&gt;source code&lt;/em&gt;
for the templates, something I can certainly appreciate as a software engineer.&lt;/p&gt;

&lt;p&gt;As a bonus, you need but a text editor to quickly change their dimensions
and create templates for another note taking app (e.g. Penultimate).&lt;/p&gt;

&lt;p&gt;&lt;a name=&quot;tldr&quot;&gt; &lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Where to Get Them&lt;/h2&gt;

&lt;p&gt;The templates &lt;a href=&quot;https://github.com/philipmat/TemplatesForGoodNotes&quot;&gt;are on github&lt;/a&gt; and the SVG files are within the &lt;em&gt;source&lt;/em&gt;
folder of that repository. If you're interested only in the PNG files, I've
uploaded a zip file that &lt;a href=&quot;https://github.com/philipmat/TemplatesForGoodNotes/downloads&quot;&gt;contains them all&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The templates, both PNG and SVG files, are released under a &lt;a href=&quot;http://creativecommons.org/licenses/by-nc-sa/3.0/&quot;&gt;Creative Commons BY-NC-SA&lt;/a&gt; license,
which means you are free to share them and change (remix) them, but you cannot sell them,
you must give proper attribution, and should you alter them you may distribute the
resulting work only under the same license (or similar).&lt;/p&gt;

&lt;p&gt;That being said, &lt;a href=&quot;https://github.com/philipmat/TemplatesForGoodNotes&quot;&gt;have fun hacking them&lt;/a&gt;. Looking forward to it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Asymmetric Sparkbars</title>
   <link href="http://philipm.at/2012/asymmetric_sparkbars.html"/>
   <updated>2012-03-22T00:00:00-05:00</updated>
   <id>http://philipm.at/2012/asymmetric_sparkbars</id>
   <content type="html">&lt;p&gt;I am creating some sparkbars, &lt;a href=&quot;http://en.wikipedia.org/wiki/Sparkline&quot;&gt;sparkline's&lt;/a&gt; poorer cousin, for a project I'll talk about soon,
and I wanted to represent in a single chart both additions and subtractions.&lt;/p&gt;

&lt;p&gt;As it happens, there's an entire range of Unicode characters devoted to blocks: &lt;a href=&quot;http://en.wikipedia.org/wiki/List_of_Unicode_characters#Block_elements&quot;&gt;U+2580 to U+259F&lt;/a&gt;,
of which U+2581, &quot;Lower one eighth block&quot;, through U+2588, &quot;Full block&quot;, seem perfect for rendering
text sparklines.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;smart way&lt;/em&gt; to handle this task would've been to use &lt;a href=&quot;http://omnipotent.net/jquery.sparkline/&quot;&gt;jQuery.sparkline&lt;/a&gt;, which makes use of the
&lt;strong&gt;canvas&lt;/strong&gt; element to draw some pretty impressive charts, but I stubbornly decided to stick with text.&lt;/p&gt;

&lt;p&gt;I am starting with two data sets, one representing tasks &lt;em&gt;completed&lt;/em&gt; per day - progress, the other
being tasks &lt;em&gt;created&lt;/em&gt; per day, for the same interval, and I will use a &lt;code&gt;spark()&lt;/code&gt; function provided
by &lt;a href=&quot;https://github.com/msiebuhr/node-textspark&quot;&gt;node-textspark&lt;/a&gt; to convert each set into discrete values,
returned to me as Unicode characters.&lt;/p&gt;

&lt;p&gt;My starting point is these two sparklines:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/sparkbars-01.png&quot; alt=&quot;Progress and regress&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I'd love for the top of the second set to be glued to the bottom of the first set and thus
give a good visual of what happened for each day. For that to happen I'll be using a different color
and I will make use of negative space to represent the data points.&lt;/p&gt;

&lt;p&gt;First I'll set the background color of the second series to red and the
foreground color to white, effectively transforming the negative space, previously
white - now red, into sparkbars that seem to hang from the top.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/sparkbars-02.png&quot; alt=&quot;Progress and regress - in complimenting colors&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Unfortunately, the block characters stop at the &lt;a href=&quot;http://en.wikipedia.org/wiki/Baseline_(typography)&quot;&gt;baseline&lt;/a&gt; and thus the space
between the baseline and the beard line (&lt;a href=&quot;http://en.wikipedia.org/wiki/Descender&quot;&gt;not making this up&lt;/a&gt;)
is visible and colored red (from the background).&lt;/p&gt;

&lt;p&gt;To make that line go away, I'll set the height on the &lt;code&gt;div&lt;/code&gt; containing the second series
to the same height as the font, then set the &lt;code&gt;overflow&lt;/code&gt; property to &lt;code&gt;hidden&lt;/code&gt; so that it clips
the bottom part, below the baseline.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/sparkbars-03.png&quot; alt=&quot;Progress and regress - clipping below the baseline&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Lastly, I'll set a negative top margin for the second &lt;code&gt;div&lt;/code&gt; so that
it gets closer to top series:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/sparkbars-04.png&quot; alt=&quot;Progress and regress - in complimenting colors&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I chose to leave a pixel between the series as I think it looks just a tad better
than if they had no space between them:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/sparkbars-05.png&quot; alt=&quot;Progress and regress - in complimenting colors&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The ending HTML is deceptively simple and scales well when the browser zooms in.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;style &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;.inverse&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.inner&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;background-color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;color&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;white&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;.sparkline-group&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;font&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Courier New&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;font-size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;18px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;nc&quot;&gt;.sparkline-group&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;.sparkline.inverse&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;margin-top&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;-7px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;height&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;18px&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;overflow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;hidden&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;sparkline-group&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;sparkline&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;spark1&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;inner&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;sparkline inverse&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;span&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;spark2&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;inner&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt; 
        
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;src=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;spark.js&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;script &lt;/span&gt;&lt;span class=&quot;na&quot;&gt;type=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;spark1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
                &lt;span class=&quot;nx&quot;&gt;spark2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;spark1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerHTML&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;spark1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;document&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;getElementById&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;spark2&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;innerText&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;spark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;spark2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;That's it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Json.NET and NHibernate Serialization Blues</title>
   <link href="http://philipm.at/2012/json_net_nhibernate.html"/>
   <updated>2012-02-27T00:00:00-06:00</updated>
   <id>http://philipm.at/2012/json_net_nhibernate</id>
   <content type="html">&lt;p&gt;After spending some irritating hours with Microsoft's &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx&quot;&gt;JavaScriptSerializer&lt;/a&gt;,
I've switched over to &lt;a href=&quot;http://json.codeplex.com/&quot;&gt;Json.NET&lt;/a&gt; and I've been happier. That matters. A lot.&lt;/p&gt;

&lt;p&gt;I have better control over serialization, for example I can specify how to handle circular references
(&lt;a href=&quot;http://james.newtonking.com/projects/json/help/SerializationSettings.html&quot;&gt;ReferenceLoopHandling&lt;/a&gt;), or whether to include nulls (&lt;a href=&quot;http://james.newtonking.com/projects/json/help/SerializationSettings.html&quot;&gt;NullValueHandling&lt;/a&gt;) and/or
default values (&lt;a href=&quot;http://james.newtonking.com/projects/json/help/SerializationSettings.html&quot;&gt;DefaultValueHandling&lt;/a&gt;); it also gives me more injection points.&lt;/p&gt;

&lt;p&gt;Yet it exhibits one frustrating behavior when one of the objects is masked by an NHibernate proxy,
as it starts to go off the rails and soon the JSON code is riddled with &lt;code&gt;__interceptors&lt;/code&gt;, and &lt;code&gt;EventListeners&lt;/code&gt;,
and various other critters that have no purpose being sent across the wire.&lt;/p&gt;

&lt;p&gt;(Yes, arguably I shouldn't be directly serializing these NHibernate objects, but the DTOs I would use
would look remarkably similar to the NH objects they would attempt to mock.)&lt;/p&gt;

&lt;p&gt;The problem comes with the chaining of strategies a &lt;code&gt;JsonSerializer&lt;/code&gt; uses to determine how to serialize a particular strategies.
This chain is captured by the &lt;a href=&quot;http://james.newtonking.com/projects/json/help/html/T_Newtonsoft_Json_Serialization_DefaultContractResolver.htm&quot;&gt;DefaultContractResolver&lt;/a&gt; in the &lt;code&gt;CreateContract(Type)&lt;/code&gt; method (which thankfully and
wisely is also &lt;code&gt;virtual&lt;/code&gt;): it tries in succession to figure out if the type is a primitive, if it's decorated with &lt;code&gt;Json*Attributes&lt;/code&gt;,
if it's a dictionary, etc, and eventually it just gives up and creates a plain &lt;code&gt;JsonObjectContract&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, in the middle of that chain is a test for whether the type is &lt;code&gt;ISerializable&lt;/code&gt;, and unfortunately the NHibernate proxies
happen to have that attribute, and that is what causes the serializer to go down a rabbit hole.&lt;/p&gt;

&lt;p&gt;My solution is to force it to use a &lt;code&gt;JsonObjectContract&lt;/code&gt; when it encounters one of these proxies.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IgnoreSerializableJsonContractResolver&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DefaultContractResolver&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JsonContract&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CreateContract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;cm&quot;&gt;/* Behavior in base we&amp;#39;re overriding:&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;        if (typeof(ISerializable).IsAssignableFrom(objectType))&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;            return CreateISerializableContract(objectType);&lt;/span&gt;
&lt;span class=&quot;cm&quot;&gt;        //*/&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAutoClass&lt;/span&gt; 
              &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;objectType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Namespace&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; 
              &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ISerializable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAssignableFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreateObjectContract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreateContract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objectType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JsonSerializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ContractResolver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IgnoreSerializableJsonContractResolver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I am not sure whether the condition is too restrictive or may generate false positives, but all the NH proxies I've encountered
seem to exhibit the same behavior: they are auto-generated classes, without a namespace.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>JSON Entity Converter</title>
   <link href="http://philipm.at/2012/json_entity_serializer.html"/>
   <updated>2012-02-20T00:00:00-06:00</updated>
   <id>http://philipm.at/2012/json_entity_serializer</id>
   <content type="html">&lt;p&gt;Whether in the context of my friend Curtis's &lt;a href=&quot;http://curtis.schlak.com/2012/01/24/borax.html&quot;&gt;BORAX&lt;/a&gt; proposal, or &lt;a href=&quot;http://philipm.at/2012/0121/&quot;&gt;my own&lt;/a&gt;,
we will eventually need to serialize our domain objects and ship them to the browser.&lt;/p&gt;

&lt;p&gt;Our best solutions is probably some sort of &lt;a href=&quot;http://martinfowler.com/eaaCatalog/dataTransferObject.html&quot;&gt;Domain Transfer Object (DTO)&lt;/a&gt;,
but for a second let's assume we take the quick route and turn our domain objects
directly into JSON mush.&lt;/p&gt;

&lt;p&gt;Is ASP.NET MVC 3 we can use the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult(v=vs.100).aspx&quot;&gt;JsonResult&lt;/a&gt; class, but with it comes a problem:
the &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx&quot;&gt;JavaScriptSerializer&lt;/a&gt;, which &lt;code&gt;JsonResult&lt;/code&gt; uses,
cannot handle circular references (and rightfully so since JSON doesn't have references).&lt;/p&gt;

&lt;p&gt;Let me illustrate using the following simplified domain classes (you can find the full code in &lt;a href=&quot;https://gist.github.com/1868672&quot;&gt;this Gist&lt;/a&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;// Layer Supertype&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Company&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Contact&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MainContact&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Contact&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Company&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WorksFor&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;When serializing an object of type &lt;code&gt;Company&lt;/code&gt;, the &lt;code&gt;JavaScriptSerializer&lt;/code&gt; will choke on &lt;code&gt;Contact.WorksFor&lt;/code&gt;
since it points back to the &lt;code&gt;Company&lt;/code&gt; that the contact is associated with.&lt;/p&gt;

&lt;p&gt;Before we talk about how we'll fix it, let's talk about the output.
What I think I'd like to have when serialize a company to JSON is:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;js&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Moof&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;MainContact&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Clarus&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;WorksFor&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;TypeName&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Company&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;In other words, if one of the object we try to serialize has
a property of type &lt;code&gt;Entity&lt;/code&gt;, we'll just output its &lt;code&gt;Id&lt;/code&gt; and assume the consumer of the JSON will
know to request &lt;code&gt;/refdata/{entitytypes}/{id}&lt;/code&gt; to resolve it, if needed.&lt;/p&gt;

&lt;p&gt;Of course, this will not avoid circular references entirely, but merely provide an example
on how to work with the class that will solve our particular problem: &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptconverter.aspx&quot;&gt;JavaScriptConverter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JavaScriptConverter&lt;/code&gt;, of the &lt;code&gt;System.Web.Script.Serialization&lt;/code&gt; clan, allows us to inject specific
type handlers into &lt;code&gt;JavaScriptSerializer&lt;/code&gt; and
have it defer to our converter when encountering those types in the serialization process.
And it gets better: our converter won't have to provide &lt;em&gt;actual JSON&lt;/em&gt;, it merely has to
return a dictionary of name-value pairs and the &lt;code&gt;JavaScriptSerializer&lt;/code&gt; will take care
of the actual transformation.&lt;/p&gt;

&lt;p&gt;Here's how we could implement the required &lt;code&gt;Serialize&lt;/code&gt; method:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Serialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JavaScriptSerializer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;props&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetProperties&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Public&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetProperty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PropertyInfo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAssignableFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PropertyType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;// real gist code: ret.Add(name, _reducer((Entity) value)); //&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;TypeName&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Entity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;(Of course the &lt;a href=&quot;https://gist.github.com/1868672&quot;&gt;actual implementation&lt;/a&gt; is a bit more elegant, I hope)&lt;/p&gt;

&lt;p&gt;We can now serialize objects such as these:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;co&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Company&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Moof Inc.&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clarus&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Contact&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Clarus&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mark&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Contact&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Mark&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddContact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clarus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddContact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mark&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MainContact&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clarus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;converter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EntityConverter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Contact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;JavaScriptSerializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterConverters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;converter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;serializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Serialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;co&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;And get the following JSON representation:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;js&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Moof Inc.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;Contacts&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Clarus&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;WorksFor&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                &lt;span class=&quot;s2&quot;&gt;&amp;quot;TypeName&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Company&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; 
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Mark&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;WorksFor&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                &lt;span class=&quot;s2&quot;&gt;&amp;quot;TypeName&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Company&amp;quot;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; 
    &lt;span class=&quot;s2&quot;&gt;&amp;quot;MainContact&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Clarus&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
        &lt;span class=&quot;s2&quot;&gt;&amp;quot;WorksFor&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;Id&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
            &lt;span class=&quot;s2&quot;&gt;&amp;quot;TypeName&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Company&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The &lt;a href=&quot;https://gist.github.com/1868672&quot;&gt;full code&lt;/a&gt; - designed to work with the most excellect &lt;a href=&quot;http://www.linqpad.net/&quot;&gt;LINQPad&lt;/a&gt;,
shows three attempts (four if we include the failed circular reference error)
and with them the various ways the &lt;code&gt;EntityConverter&lt;/code&gt; can control the JSON output.&lt;/p&gt;

&lt;p&gt;If you want to use the code in Visual Studio, add a reference to &lt;code&gt;System.Web.Extensions&lt;/code&gt;
and import the &lt;code&gt;System.Web.Script.Serialization&lt;/code&gt; namespace.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Setting up FxCop under CruiseControl.NET</title>
   <link href="http://philipm.at/2012/setting_up_fxcop_under_ccnet.html"/>
   <updated>2012-02-14T00:00:00-06:00</updated>
   <id>http://philipm.at/2012/setting_up_fxcop_under_ccnet</id>
   <content type="html">&lt;p&gt;After spending up half a day trying to get &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/bb429476(v=vs.80).aspx&quot;&gt;FxCop&lt;/a&gt; v1.36 to play nice with &lt;a href=&quot;http://www.cruisecontrolnet.org/&quot;&gt;CruiseControl.NET&lt;/a&gt; and MSBuild,
I thought it was worth spending an extra hour or two documenting it.&lt;/p&gt;

&lt;p&gt;There are two ways we can get FxCop running under CCNet: the easy way and (what I think it's) the right way.&lt;/p&gt;

&lt;p&gt;Either way, we start by creating an FxCop project using the FxCop GUI; it makes it easy to add our assemblies
and to select the rules we'd like enforced.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The easy way&lt;/strong&gt; has us using an &lt;code&gt;&amp;lt;Exec/&amp;gt;&lt;/code&gt; task within the MSBuild file:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;Target&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FxCop&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;DependsOnTargets=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Build;RunFastTests&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;Delete&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Files=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;$(ProjectRoot)\fxcop.xml&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;Exec&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Command=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;amp;quot;$(FxCopPath)\FxCopCmd.exe&amp;amp;quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;    /dic:&amp;amp;quot;$(ProjectRoot)\src\CodeNameDictionary.xml&amp;amp;quot;&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;    /project:$(ProjectRoot)\src\Default.FxCop /out:$(ProjectRoot)\fxcop.xml&amp;quot;&lt;/span&gt; 
    &lt;span class=&quot;na&quot;&gt;ContinueOnError=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Exec&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Target&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;In this version, we're telling FxCop command line tool to use a custom dictionary - more on this later,
the project file we created earlier, and to output the results to an XML file.&lt;/p&gt;

&lt;p&gt;When being run, FxCop tends to load the previous results, which would cause load errors if we renamed classes that previously had errors,
so we'll &lt;code&gt;&amp;lt;Delete/&amp;gt;&lt;/code&gt; the output file before running the task.&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;The nicer, more explicit way&lt;/strong&gt;, has us use the supplemental tasks provided by the &lt;a href=&quot;http://msbuildtasks.tigris.org/&quot;&gt;MSBuild Community Tasks Project&lt;/a&gt;,
in particular the &lt;code&gt;&amp;lt;FxCop/&amp;gt;&lt;/code&gt; task (grab at least version 1.3.0.528, part of the &lt;em&gt;nightly builds&lt;/em&gt; - v1.2 doesn't
properly support this task).&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;Target&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FxCop&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;DependsOnTargets=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Build;RunFastTests&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;Delete&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Files=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;$(ProjectRoot)\fxcop.xml&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;FxCop&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ToolPath=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;$(FxCopPath)\FxCopCmd.exe&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ProjectFile=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;$(ProjectRoot)\src\Default.FxCop&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;CustomDictionary=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;$(ProjectRoot)\src\CodeNameDictionary.xml&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;FailOnError=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;false&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;AnalysisReportFileName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;$(ProjectRoot)\fxcop.xml&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Target&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;What you won't find in the MSBuild Community Tasks documentation is the requirement to use the &lt;code&gt;ToolPath&lt;/code&gt; to point to the right version
of FxCop. As of now, the &lt;code&gt;&amp;lt;FxCop/&amp;gt;&lt;/code&gt; task looks explicitly for FxCop v1.32 and for a registry key that no longer exists
when it tries to build the full command line:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToolPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fxCopPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetFolderPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Environment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SpecialFolder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProgramFiles&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;fxCopPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Combine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fxCopPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;@&amp;quot;Microsoft FxCop 1.32&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegistryKey&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buildKey&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Registry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ClassesRoot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OpenSubKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&amp;quot;FxCopProject\shell\Open\command&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buildKey&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LogError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Could not find the FxCopProject File command in the registry. Please make sure FxCop is installed.&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fxCopPath&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buildKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetValue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fxCopPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The problem with the registry look up is that FxCop v1.36 uses a key called &lt;code&gt;FxCop.Project.9.0&lt;/code&gt;,
all which means that the &lt;code&gt;&amp;lt;FxCop/&amp;gt;&lt;/code&gt; task fails to find the executable. That is why we have to use the
&lt;code&gt;ToolPath&lt;/code&gt;, inherited from its &lt;code&gt;Task&lt;/code&gt; parent, to help it find it.&lt;/p&gt;

&lt;p&gt;Alright, now the promised dictionary clarification.&lt;/p&gt;

&lt;p&gt;If our namespaces, classes, etc. include non-lexical words, for example &lt;code&gt;Flickr.Configurator&lt;/code&gt;, FxCop will announce&lt;br/&gt;
that our code breaks a naming rule - &lt;code&gt;CA1704:IdentifiersShouldBeSpelledCorrectly&lt;/code&gt; - and will tell us to:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Correct the spelling of 'Flickr' in assembly ExternalDependencies.dll'.&lt;/p&gt;

&lt;p&gt;Correct the spelling of 'Configurator' in assembly ExternalDependencies.dll'.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;(Yes, &lt;em&gt;Configurator&lt;/em&gt; is not an English word either - us developers have a lot of those.)&lt;/p&gt;

&lt;p&gt;We'll fix this problem by adding a &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/bb429472(v=vs.80).aspx&quot;&gt;custom FxCop dictionary&lt;/a&gt; containing the words we want to mark as correct:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;Dictionary&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;Words&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;Recognized&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Word&amp;gt;&lt;/span&gt;Flickr&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Word&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;Word&amp;gt;&lt;/span&gt;Configurator&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Word&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Recognized&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/Words&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/Dictionary&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;We'll then spell  this dictionary using the &lt;code&gt;CustomDictionary&lt;/code&gt; property of the &lt;code&gt;&amp;lt;FxCop/&amp;gt;&lt;/code&gt; task.&lt;/p&gt;

&lt;p&gt;We could and should also add the dictionary into the FxCop project file to make sure the GUI doesn't complain
about those naming rules being broken either. Find the &lt;code&gt;&amp;lt;CustomDictionaries/&amp;gt;&lt;/code&gt; node within the project file,
&lt;code&gt;Default.FxCop&lt;/code&gt; in the examples above, and add a &lt;code&gt;&amp;lt;CustomDictionary/&amp;gt;&lt;/code&gt; node:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;CustomDictionaries&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;SearchFxCopDir=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;True&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;SearchUserProfile=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;True&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;SearchProjectDir=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;True&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- Tells FxCop to find it in the same location as the Default.FxCop file --&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;CustomDictionary&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;Path=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;CodeAnalysisDictionary.xml&amp;quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/CustomDictionaries&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;




&lt;p&gt;&amp;nbsp;&lt;/p&gt;


&lt;p&gt;&lt;/p&gt;

&lt;p&gt;CCNet captures all the output of a build into a giant result file using &lt;code&gt;&amp;lt;merge&amp;gt;&lt;/code&gt; tasks.
We'll follow the &lt;a href=&quot;http://confluence.public.thoughtworks.org/display/CCNET/Using+CruiseControl.NET+with+FxCop&quot;&gt;instructions on the CCNet website&lt;/a&gt; to merge in the generated &lt;code&gt;fxcop.xml&lt;/code&gt; file:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;tasks&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;merge&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;files&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;file&amp;gt;&lt;/span&gt;fxcop.xml&lt;span class=&quot;nt&quot;&gt;&amp;lt;/file&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- Other files to merge for your build would also be included here --&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/files&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/merge&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/tasks&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Finally, to be able view the results of the FxCop analysis in CCNet's web dashboard, we'll need to include
the XSLT files, which extract and pretty up the entries merged in from &lt;code&gt;fxcop.xml&lt;/code&gt;, into
the &lt;code&gt;dashboard.config&lt;/code&gt; file, typically located within &lt;code&gt;C:\Program Files\CruiseControl.NET\webdashboard\&lt;/code&gt; folder:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;xml&quot;&gt;...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;buildPlugins&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;buildReportBuildPlugin&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;xslFileNames&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- other xsl file, such nunit and msbuild --&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;xslFile&amp;gt;&lt;/span&gt;xsl\fxcop-summary_1_36.xsl&lt;span class=&quot;nt&quot;&gt;&amp;lt;/xslFile&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nt&quot;&gt;&amp;lt;xslFile&amp;gt;&lt;/span&gt;xsl\fxcop-report_1_36.xsl&lt;span class=&quot;nt&quot;&gt;&amp;lt;/xslFile&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/xslFileNames&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/buildReportBuildPlugin&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;buildLogBuildPlugin&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- These create two menu entries on the left, when viewing a project&amp;#39;s build page --&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xslReportBuildPlugin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;description=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FxCop Summary&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;actionName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FxCopSummary&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xslFileName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;xsl\fxcop-summary_1_36.xsl&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/xslReportBuildPlugin&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;xslReportBuildPlugin&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;description=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FxCop Report&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;actionName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;FxCopReport&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;xslFileName=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;xsl\fxcop-report_1_36.xsl&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&amp;lt;/xslReportBuildPlugin&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/buildPlugins&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;That's it. Next time you build you should see the FxCop details when you click the &quot;FxCop Report&quot; link.&lt;/p&gt;

&lt;p&gt;The only one thing to note is that violating FxCop rules, no matter their level or the value of &lt;code&gt;ContinueOnError&lt;/code&gt;,
will not mark the build a broken. If you want FxCop failures to break the build, there is
&lt;a href=&quot;http://sharpfellows.com/post/Getting-FxCop-to-break-the-build.aspx&quot;&gt;one further step&lt;/a&gt; you need to take.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>New (Old) Web App Architecture</title>
   <link href="http://philipm.at/2012/new_webapp_architecture.html"/>
   <updated>2012-01-21T00:00:00-06:00</updated>
   <id>http://philipm.at/2012/new_webapp_architecture</id>
   <content type="html">&lt;p&gt;We've been doing this for a while, more or less, in various shapes and forms, so it's nothing new.
We've been doing it as mash-ups and we've been doing it with AJAX and we've done it with web services,
but we've only done mash-ups when we couldn't control the source of data, and we've done AJAX only to speed things up,
and web services - well, they're traditionally XML based, and by now we've mostly agreed that is somewhat ugly.&lt;/p&gt;

&lt;p&gt;When we could control the source of data, we've reverted back to some server-side framework producing the UI code,
and we worked hard to make that combination good, just like we worked hard before to make integrating web services easy.&lt;/p&gt;

&lt;h2&gt;Why a New Way?&lt;/h2&gt;

&lt;p&gt;What we have right now - with Rails, and Django, and ASP.NET MVC, and PHP, and you name it - is better that everything we
ever had before, but there are a few things that irk me about the current state of affairs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If I want to change your UI (and I don't mean cosmetically), it's almost guaranteed that I have to &lt;strong&gt;learn your framework&lt;/strong&gt;.
At a minimum, I'll have to learn your templating language, but my point is that I cannot do much if I only know HTML and
JavaScript. Conversely you will not be able to make use of my hypothetical UI skills unless I also happen to know your framework.&lt;/li&gt;
&lt;li&gt;It's &lt;strong&gt;laborious to get your project to run&lt;/strong&gt;. Sometimes it's easy, most often it's not; I'll run into dependency issues,
versioning issues, versioning of dependencies issues, and project setup issues when all I wanted to do was to make a minor
UI change.&lt;/li&gt;
&lt;li&gt;I need to &lt;strong&gt;run your whole stack&lt;/strong&gt; to be able to see and change your UI: database, modules (gems, eggs, dlls), services, etc.
If any of your dependencies is truly external (say that SAP system you get your list of clients from) you most likely can
kiss your off-lining goodbye, unless you were smart enough to provide a stub service that still serves relevant data.&lt;/li&gt;
&lt;li&gt;If your project provides a UI but doesn't provide a &lt;strong&gt;separate API&lt;/strong&gt;, it's very difficult or at least impractical for me to consume your data.
This is more difficult to accept in an enterprise where we're all supposed to be working towards the same goal. And building a separate API
slows you down, and increases your maintenance costs because now you have two pieces of software you need to maintain, unless you were
smart from the beginning and made &lt;em&gt;your&lt;/em&gt; app consume &lt;em&gt;your&lt;/em&gt; API.&lt;/li&gt;
&lt;li&gt;There's a clear boundary between the client - the web browser - and the server, with a good deal of data being sent
back and forth, yet you mostly never &lt;strong&gt;write tests for the communication between browser and server&lt;/strong&gt;. You don't do that
for the same reason: it's difficult or impractical to consume what you send (GET), slightly easier to consume what you receive (POST, PUT, DELETE).&lt;/li&gt;
&lt;li&gt;In some setups it &lt;strong&gt;takes time for a model change to propagate to the UI&lt;/strong&gt;, more so with compiled languages.
Thor forbid if you need to make a DB change - now we have to wait for the DBA(s).&lt;/li&gt;
&lt;li&gt;It's hard to &lt;strong&gt;scale your app&lt;/strong&gt; because building your UI often needs all the models it uses to have been loaded in
the same thread/process. Most of the time, your apps, or at least the layers building the UI, are not designed or cannot handle different models coming from
different sources.&lt;/li&gt;
&lt;li&gt;You are &lt;strong&gt;building your UI sequentially&lt;/strong&gt;. Slowest buffalo wins: you have to wait for your slowest model to load before you can send your page to the browser.
Not that async building would always make a difference - sometimes you just have to wait for that drop-down to populate before
you can successfully edit that record - but in a sync world it doesn't matter if the relevant data is fast and the irrelevant data
loads slowly, because it all boils down to everything having to finish loading before you produce your UI.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;As web developers we cringe at the thought of putting markup in the database, or having stored procedures generate HTML, but
we have no problems with code generating HTML.
I'm not saying it's hypocrisy, &lt;a href=&quot;http://www.amazon.com/Choke-Chuck-Palahniuk/dp/0385720920&quot;&gt;but it's the first word that comes to mind&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you're a desktop developer, imagine your services spewing UI controls instead of just typical web service XML.
Imagine your &lt;code&gt;UIToolbar&lt;/code&gt;, &lt;code&gt;NSButton&lt;/code&gt;, &lt;code&gt;ContextMenuStrip&lt;/code&gt;, or your &lt;code&gt;GridBagLayout&lt;/code&gt; being the result of a
web service call; I don't mean a UI control that you create in your UI layer based on a web service call,
but an actual object that you only deserialize and put on screen at (x,y).
I get a shiver just writing this, but in the world of web apps it's something perfectly natural.&lt;/p&gt;

&lt;p&gt;Why? Because we have a good templating language? Because we've always done it this way?
Because everything comes from a server anyway?&lt;/p&gt;

&lt;p&gt;Between mash-ups and web services, we've already solved a great deal of those issue.
We just need to apply them rigorously and consistently.&lt;/p&gt;

&lt;h2&gt;Proposal&lt;/h2&gt;

&lt;p&gt;I propose that we &lt;a href=&quot;http://github.com/philipmat/webmvc/&quot;&gt;build the app&lt;/a&gt; UI using static HTML (as in not produced by other code) and
that we use only JSON to exchange business data, by means of the four HTTP verbs (GET,POST,PUT,DELETE),
essentially turning your Rails/Django/ASP.NET MVC/PHP app into a lean-mean JSON producing/consuming machine.&lt;/p&gt;

&lt;p&gt;Loading the UI for displaying or editing an entity would take the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The browser requires from the web server &lt;code&gt;http://example.com/clients/1&lt;/code&gt;. It receives a
static HTML page representing the layout required to display the client and JavaScript code
that knows how to perform the next step.&lt;/li&gt;
&lt;li&gt;The JavaScript code (e.g. jQuery) loads, perhaps from another server, &lt;code&gt;http://data.example.com/clients/1&lt;/code&gt;,
the JSON representation of &lt;strong&gt;Client(1)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The JSON values are displayed into the static layout (for example, using Knockout.JS).&lt;/li&gt;
&lt;li&gt;Saving is performed by POSTing the updated JSON representation back to the originating data URL (&lt;code&gt;http://data.example.com/clients/1&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;If we consider that most pages fall into a few larger categories: display entity list, display one entity, edit one entity, etc.
the first step can be further generalized by having it serve a more generic static template for the category and having it load
more detailed template for the entity:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The browser navigates to &lt;code&gt;http://example.com/one_entity.html#clients/1&lt;/code&gt;. It receives a static
HTML page representing the generic layout for displaying one entity and JavaScript code
that knows how to load the detailed template, and how to load the entity data.&lt;/li&gt;
&lt;li&gt;The JavaScript code loads &lt;code&gt;http://example.com/templates/client.html&lt;/code&gt; and inserts it into a
known placeholder node.&lt;/li&gt;
&lt;li&gt;The JavaScript code then loads JSON from &lt;code&gt;http://data.example.com/clients/1&lt;/code&gt; and populates the
template received in step two.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&lt;img src=&quot;/media/images/new_webapp_architecture.png&quot; alt=&quot;Diagram of how all this works&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This latter formula allows us to focus on the details of what differs between various entity displays
and maintain the same layout.&lt;/p&gt;

&lt;h2&gt;Advantages&lt;/h2&gt;

&lt;p&gt;With this approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If I want to change your UI, all I have to learn is your API, which is as little as looking at
what &lt;strong&gt;JSON objects&lt;/strong&gt; you send me.&lt;/li&gt;
&lt;li&gt;I don't need to get your project to run: all I need are the &lt;strong&gt;static HTML files&lt;/strong&gt;.
I can &lt;strong&gt;serialize your JSON replies&lt;/strong&gt; to individual files and serve them as such.
Sure, I won't be able to send them back, but at least I can navigate most of your app.&lt;/li&gt;
&lt;li&gt;I don't need to run your whole stack: all I need is a &lt;strong&gt;web server&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You don't need to provide a separate API - your &lt;strong&gt;web services are now your API&lt;/strong&gt;. I can integrate with
your API just as easy as you can.&lt;/li&gt;
&lt;li&gt;We both can now &lt;strong&gt;write and run tests for that client-server boundary&lt;/strong&gt;. By preserving the JSON you send me at one
point in time I can make sure that any changes you made in your active code don't cause surprise
midway through my development cycle. You too can serialize known replies to make sure that code changes elsewhere have not
caused changes in the API.&lt;/li&gt;
&lt;li&gt;UI changes are in almost all cases as easy as a &lt;strong&gt;page refresh&lt;/strong&gt;. You can make your model and DB changes later -
as long as you send me the JSON structure we agreed on my UI will behave correctly.&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;scale your app through sharding at domain level&lt;/strong&gt;, different JSON sources coming from different servers,
and I can make sure I will configure my URL(s) to account for that. That also means that if I want to get clients from
that SAP server you don't have to write a bridge, I'll just use theirs (which hopefully provides a JSON interface).&lt;/li&gt;
&lt;li&gt;Because asynchronous tasks is something JavaScript is really good at, I can &lt;strong&gt;load my interface asynchronously&lt;/strong&gt;:
I can load the list of clients from server 1, the list of countries from server 2, and current market prices for lolcats
from server 3 without doing anything I wouldn't normally do in JavaScript. Actually making things synchronous in JS
is sometimes the more complicated approach (ask NodeJS programmers).&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;On top of that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What you cache (JSON) became considerably smaller and any web server can already deal incredibly efficiently
with static HTML.&lt;/li&gt;
&lt;li&gt;You can send an implementation team to a client site that doesn't have to know much beyond HTML and JavaScript,
and let's be honest - that skill set comes in considerably cheaper.&lt;/li&gt;
&lt;li&gt;You can finally get that HTML5 guru that loathes working with ASP.NET. Because he won't have to.
And he'll get to use his Mac (yay!).&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Between libraries like jQuery and Knockout.JS (or Backbone.JS) we already have all the tools to make it happen.
Chances are you already use them &lt;a href=&quot;http://weblogs.asp.net/shijuvarghese/archive/2011/08/21/building-javascript-mvvm-apps-in-asp-net-mvc-using-knockoutjs.aspx&quot;&gt;with&lt;/a&gt; &lt;a href=&quot;https://workshops.thoughtbot.com/backbone-js-on-rails&quot;&gt;your&lt;/a&gt; &lt;a href=&quot;http://geeks.aretotally.in/log4play-log4j-ui-mashed-up-with-play-framework-knockout-js-and-websockets&quot;&gt;app&lt;/a&gt; - why not go all the way.&lt;/p&gt;

&lt;p&gt;Notice that I was careful not to say the app is RESTful - that's because my proposal doesn't address the
&lt;a href=&quot;http://en.wikipedia.org/wiki/HATEOAS&quot; title=&quot;Hypermedia as the Engine of Application State&quot;&gt;fourth guiding principle&lt;/a&gt; of Fielding's
cannon. That's something for you to decide (and I think &lt;a href=&quot;http://curtis.schlak.com/2012/01/19/fieldings-rest.html&quot;&gt;you should do it&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Here's a simple example, using Knockout.JS, to see what I'm talking about: &lt;a href=&quot;http://github.com/philipmat/webmvc/&quot;&gt;http://github.com/philipmat/webmvc/&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>.Net Interfaces Are Not Classes</title>
   <link href="http://philipm.at/2011/interfaces_are_not_classes.html"/>
   <updated>2011-10-14T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/interfaces_are_not_classes</id>
   <content type="html">&lt;p&gt;Well, duh! Right?&lt;/p&gt;
&lt;p&gt;To be honest, if you know that already, and who doesn&amp;#8217;t, then this post does not have much to offer, except a tale of &lt;code&gt;DataBinding&lt;/code&gt; exceptions, &lt;code&gt;TypeConverters&lt;/code&gt;, and a few interesting tid-bits I picked up along the way, along with what I think it&amp;#8217;s a bug in a core .Net class.&lt;/p&gt;
&lt;!--
* &quot;data binding and interfaces&quot;:#databinding_and_interfaces
* &quot;the devil is in the code&quot;:#devil
* &quot;interface inheritance&quot;:#inheritance
* &quot;when in doubt&quot;:#when_in_doubt
* &quot;conclusion&quot;:#tldr
--&gt;

&lt;p&gt;Throughout this post I will use two interface and two classes implementing them to make my point:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IBase&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;BaseMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDerived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IBase&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DerivedMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IBase&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;BaseMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;BaseMethod&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDerived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DerivedMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;DerivedMethod&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Rocket surgery, innit?&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;databinding_and_interfaces&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;data binding and interfaces&lt;/h2&gt;
&lt;p&gt;Good &lt;acronym title=&quot;Object Oriented Programming&quot;&gt;&lt;span class=&quot;caps&quot;&gt;OOP&lt;/span&gt;&lt;/acronym&gt; code monkeys have designed and programmed against abstractions, e.g., &lt;strong&gt;interfaces&lt;/strong&gt;, before Uncle Bob articulated the &lt;a href=&quot;http://blog.objectmentor.com/articles/2009/02/12/getting-a-solid-start&quot;&gt;&lt;span class=&quot;caps&quot;&gt;SOLID&lt;/span&gt; principles&lt;/a&gt;, and I like to believe that even before the advent of the &lt;a href=&quot;http://en.wikipedia.org/wiki/Dependency_inversion_principle&quot;&gt;Dependency Inversion principle&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It only makes sense that we apply the same principle, programming against interfaces, to &lt;a href=&quot;http://en.wikipedia.org/wiki/MVVM&quot; title=&quot;Model View ViewModel&quot;&gt;&lt;span class=&quot;caps&quot;&gt;MVVM&lt;/span&gt;&lt;/a&gt; design, as it shows in this overly simplistic example:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyForm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Form&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IBase&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDerived&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;OnLoad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EventArgs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;:&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; 
                          &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Base is null&amp;quot;&lt;/span&gt; 
                          &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;:&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; 
                          &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Derived is null&amp;quot;&lt;/span&gt; 
                          &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DerivedMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MyViewModel&lt;/span&gt;  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IBase&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDerived&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;I&amp;#8217;m using &lt;code&gt;Windows.Forms&lt;/code&gt; because &lt;span class=&quot;caps&quot;&gt;WPF&lt;/span&gt; would&amp;#8217;ve been too verbose for such a tiny task, but the concept translates almost word for word.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;caps&quot;&gt;MVVM&lt;/span&gt; was designed to make use of binding, &lt;code&gt;DataBinding&lt;/code&gt; to be more specific:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;MyForm&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;viewBase&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyForm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Base&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; 
         &lt;span class=&quot;n&quot;&gt;viewDerived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyForm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Text&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Derived&amp;quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;viewModel&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MyViewModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;viewBase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataBindings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Base&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;viewModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Base&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                            &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                            &lt;span class=&quot;n&quot;&gt;DataSourceUpdateMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OnPropertyChanged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;viewDerived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DataBindings&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Derived&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;viewModel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;Derived&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                               &lt;span class=&quot;k&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
                               &lt;span class=&quot;n&quot;&gt;DataSourceUpdateMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OnPropertyChanged&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;viewBase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;viewDerived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Show&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Running that code (&lt;a href=&quot;/media/files/databinding.linq&quot;&gt;here is the script&lt;/a&gt; to use with &lt;a href=&quot;http://www.linqpad.net/&quot;&gt;LINQPad&lt;/a&gt;) we&amp;#8217;d expect to get two windows with titles &amp;#8220;Base:BaseMethod&amp;#8221;, and respectively &amp;#8220;Derived:DerivedMethod&amp;#8221;, yet instead we get two big, fat, &lt;code&gt;FormatExceptions&lt;/code&gt; that make as much sense as a fish with a bicycle (full stack trace &lt;a href=&quot;https://gist.github.com/1283578#file_gistfile1.txt&quot;&gt;here&lt;/a&gt; and also notice how early in the control&amp;#8217;s lifecyle this error happens):&lt;/p&gt;
&lt;pre&gt;System.FormatException: Cannot format the value to the desired type.
   at System.Windows.Forms.Binding.FormatObject(Object value)
   ...
   at System.Windows.Forms.Control.UpdateBindings()
   ...
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message&amp;amp; m)
   at System.Windows.Forms.Control.WndProc(Message&amp;amp; m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message&amp;amp; m)
   at System.Windows.Forms.Form.WmShowWindow(Message&amp;amp; m)
   at System.Windows.Forms.Form.WndProc(Message&amp;amp; m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp;amp; m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp;amp; m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)&lt;/pre&gt;
&lt;p&gt;Cannot &lt;em&gt;format&lt;/em&gt; ?! And format what &amp;#8211; a &lt;code&gt;Base&lt;/code&gt; into an &lt;code&gt;IBase&lt;/code&gt;? How is that possible?&lt;/p&gt;
&lt;h2&gt;the devil is in the code&lt;/h2&gt;
&lt;p&gt;Very puzzled, I pulled out a decompiler, &lt;a href=&quot;http://www.jetbrains.com/decompiler/&quot;&gt;JetBrains&amp;#8217; dotPeek&lt;/a&gt;, took a look at &lt;code&gt;Binding.FormatObject(Object)&lt;/code&gt;, and found these interesting lines:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;//-  propertyType is typeof(IBase)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//-  value is our new Base()&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//-  so is obj1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;propertyType&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsSubclassOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;propertyType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;||&lt;/span&gt; 
     &lt;span class=&quot;n&quot;&gt;obj1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;propertyType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;I&amp;#8217;m pretty sure this would&amp;#8217;ve jumped out at you, dear reader, because you&amp;#8217;re so much smarter, but in my head it didn&amp;#8217;t make any sense; I thought &amp;#8220;of course &lt;code&gt;Base&lt;/code&gt; is a child of &lt;code&gt;IBase&lt;/code&gt; &amp;#8211; what&amp;#8217;s your problem?&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Yes, &lt;code&gt;Base&lt;/code&gt; is a &lt;em&gt;child&lt;/em&gt; of &lt;code&gt;IBase&lt;/code&gt;, for very fuzzy values of &lt;em&gt;child&lt;/em&gt;, but it is not a &lt;em&gt;subclass&lt;/em&gt; of &lt;code&gt;IBase&lt;/code&gt;: &lt;code&gt;Base&lt;/code&gt; &lt;em&gt;implements&lt;/em&gt; &lt;code&gt;IBase&lt;/code&gt; but does not &lt;em&gt;inherit&lt;/em&gt; from &lt;code&gt;IBase&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Given the direction of the assignment, I believe this is a bug: &lt;code&gt;IsSubclassOf&lt;/code&gt; is an unnecessary restriction, they should have used &lt;code&gt;propertyType.IsAssignableFrom(obj1.GetType())&lt;/code&gt; instead, which would&amp;#8217;ve taken care of this mess and allow for binding onto an interface to happen as expected.&lt;/p&gt;
&lt;p&gt;Well, let&amp;#8217;s debate that later, how do we fix it?&lt;/p&gt;
&lt;p&gt;Obviously we could change &lt;code&gt;MyForm.Base&lt;/code&gt; to be of type &lt;code&gt;Base&lt;/code&gt; and &lt;code&gt;MyForm.Derived&lt;/code&gt; to be of either type &lt;code&gt;Derived&lt;/code&gt; or &lt;code&gt;Base&lt;/code&gt;. That would work, and it&amp;#8217;s a reasonable fix if you happen to dislike abstractions.&lt;/p&gt;
&lt;p&gt;You could also change both properties to be of type &lt;code&gt;object&lt;/code&gt;, but that just means you have &lt;span class=&quot;caps&quot;&gt;OOP&lt;/span&gt; in general.&lt;/p&gt;
&lt;p&gt;The next few instructions in &lt;code&gt;Binding.FormatObject&lt;/code&gt; contain another solution:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;TypeConverter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;converter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; 
    &lt;span class=&quot;n&quot;&gt;TypeDescriptor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetConverter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; 
                                &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
                                &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;converter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;converter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CanConvertTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;propertyType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;converter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConvertTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;propertyType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx&quot;&gt;read more&lt;/a&gt; about the &lt;code&gt;TypeConverter&lt;/code&gt;, while I create one for the &lt;code&gt;Derived&lt;/code&gt; class and then decorate it using the &lt;code&gt;TypeConverterAttribute&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DerivedConverter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TypeConverter&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typeD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CanConvertTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITypeDescriptorContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;destinationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;destinationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAssignableFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CanConvertFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;destinationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
   
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ConvertTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ITypeDescriptorContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CultureInfo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;culture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; 
         &lt;span class=&quot;kt&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;destinationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;  
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;destinationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAssignableFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ConvertTo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;culture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;destinationType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;na&quot;&gt;[TypeConverter(typeof(DerivedConverter))]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDerived&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;DerivedMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;DerivedMethod&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you rerun the script with the &lt;code&gt;TypeConverter&lt;/code&gt; in place, you&amp;#8217;ll only get one exception for &lt;code&gt;IBase&lt;/code&gt;, while the &lt;code&gt;IDerived&lt;/code&gt; example performs as expected.&lt;/p&gt;
&lt;p&gt;Kind of silly, isn&amp;#8217;t it, to use a separate class to convert an instance into the interface it implements anyway.&lt;/p&gt;
&lt;p&gt;It was at this point that I did what any sane developer would do when faced with this snafu: bitch on Twitter. Rory Primrose (&lt;a href=&quot;https://twitter.com/roryprimrose&quot;&gt;@roryprimrose&lt;/a&gt; &amp;#8211; you should follow him) &lt;a href=&quot;https://twitter.com/roryprimrose/status/123877774829158400&quot;&gt;replied&lt;/a&gt; with an example of his own, this one around interface inheritance.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;inheritance&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;interface inheritance&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://www.neovolve.com/post/2008/07/03/reflection-pop-quiz-does-interface-inheritance-exist.aspx&quot;&gt;His assumption&lt;/a&gt; was that calling &lt;code&gt;Type.GetMethods&lt;/code&gt; on a derived interface would also return the methods of its parent(s).&lt;/p&gt;
&lt;p&gt;I had to try and cook some of &lt;a href=&quot;/media/files/interfaces_are_not_classes.linq&quot;&gt;my own code&lt;/a&gt; and here are the surprising results:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt; 
  &lt;span class=&quot;n&quot;&gt;inst_pub&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Instance&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Public&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;flat_inst_pub&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FlattenHierarchy&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inst_pub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;decl_inst_pub&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BindingFlags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DeclaredOnly&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inst_pub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logm&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Derived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iB&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IBase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;iD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDerived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cB&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cD&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Instance | Public&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetMethods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inst_pub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 6&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Flatten | Instance | Public&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetMethods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flat_inst_pub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 6&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Declared | Instance | Public&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetMethods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;decl_inst_pub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 1&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;logm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Instance | Public&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetMethods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inst_pub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;                   &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;logm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Flatten | Instance | Public&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetMethods&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flat_inst_pub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; 1?!&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;cD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsSubclassOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Derived subclass of Base?&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; True&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Derived is Base&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                                  &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; True&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;cD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsSubclassOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Derived subclass of IDerived?&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; False&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDerived&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Derived is IDerived&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;                          &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; True&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;iD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAssignableFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;Derived assignable to IDerived?&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; True&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;iD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsSubclassOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;IDerived subclass of IBase?&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;               &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; False?!&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;iB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsAssignableFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;iD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Dump&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;IDerived assignable IBase?&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;//=&amp;gt; True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The classes behave as expected (2 methods + 4 from &lt;code&gt;Object&lt;/code&gt;), but I expected &lt;code&gt;FlattenHierarchy&lt;/code&gt; with &lt;code&gt;IDerived&lt;/code&gt; to return two methods: its own &lt;code&gt;DerivedMethod&lt;/code&gt; and its parent &lt;code&gt;BaseMethod&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Probing further, shows not only that &lt;strong&gt;classes are not subclasses of the interfaces they implement, but neither are interfaces subclasses of their parents&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Much sooner that I, Rory asked the question at the core of his post:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Does interface inheritance exist?&lt;/p&gt;
&lt;p&gt;Much to my amazement, the answer is no, at least according to reflection. This is completely not what I had assumed.&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;when_in_doubt&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;when in doubt&lt;/h2&gt;
&lt;p&gt;&amp;#8230; look up the specs.&lt;/p&gt;
&lt;p&gt;In highsight, it should all have been obvious. My mistake lies in thinking of interfaces as purely virtual/abstract classes (there&amp;#8217;s some C++ roots showing &amp;#8211; only pointing that out to get some street cred).&lt;/p&gt;
&lt;p&gt;However, that shows fault immediately because, even if it was so &amp;#8211; even if interfaces were purely virtual &lt;em&gt;classes&lt;/em&gt;, &lt;strong&gt;the &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt; does not support multiple class inheritance&lt;/strong&gt; (but you knew that already, didn&amp;#8217;t you), yet it supports multiple interface inheritance using the exact same syntax.&lt;/p&gt;
&lt;p&gt;The standard, &lt;a href=&quot;http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf&quot;&gt;&lt;span class=&quot;caps&quot;&gt;ECMA&lt;/span&gt;-335&lt;/a&gt;, explicitly states in &lt;em&gt;8.9.9 Object type inheritance&lt;/em&gt; that:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;[&amp;#8230;] all object types shall either explicitly or implicitly declare support for (i.e., inherit from) exactly one other object type.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If I were to nitpick though, I would point out that the C# standard, &lt;a href=&quot;http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf&quot;&gt;&lt;span class=&quot;caps&quot;&gt;ECMA&lt;/span&gt;-334&lt;/a&gt;, is a bit inconsistent in usage: within &lt;em&gt;8.9 Interfaces&lt;/em&gt; you will read &amp;#8220;Interfaces can employ multiple inheritance&amp;#8221;, which is contradicted by &lt;em&gt;&lt;span class=&quot;caps&quot;&gt;ECMA&lt;/span&gt;-335 8.10 Member inheritance&lt;/em&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;While interface types can be derived from other interface types, they only “inherit” the requirement to implement method contracts, never fields or method implementations.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is a &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt; restriction: while ordinary C++ does support multiple inheritance, Managed C++ doesn&amp;#8217;t.  &lt;a href=&quot;http://blogs.msdn.com/b/slippman/archive/2004/08/05/209606.aspx&quot;&gt;This blog post&lt;/a&gt; shares Managed C++ team&amp;#8217;s decision and includes this grain of insight about Eiffel&amp;#8217;s support of multiple inheritance on the &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;[&amp;#8230;] the &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt; does not, for example, support private inheritance, value inheritance (that is, the inheritance of implementation but not of type), or multiple inheritance (MI). While a language can choose to support these aspects of inheritance, that support requires a mapping onto the existing &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt; object model because there is no direct support.&lt;/p&gt;
&lt;p&gt;The Eiffel language under &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt;, for example, choose to provide an MI mapping [&amp;#8230;]&lt;/p&gt;
&lt;p&gt;&lt;a name=&quot;tldr&quot;&gt; &lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;conclusion&lt;/h2&gt;
&lt;p&gt;What did we learn?&lt;/p&gt;
&lt;p&gt;In truth, there&amp;#8217;s no true inheritance in the &lt;span class=&quot;caps&quot;&gt;CLI&lt;/span&gt; when it comes to interfaces &amp;#8211; that is reserved for classes; in practice, we can consider interface inheritance exists as long as we use those relationships for typing rather than reflection.&lt;/p&gt;
&lt;p&gt;If you perform runtime type inspection with a goal to see if casting is possible, use &lt;code&gt;Type.IsAssignableFrom&lt;/code&gt;, rather than &lt;code&gt;Type.IsSubclassOf&lt;/code&gt;. I cannot think of a good use case where you&amp;#8217;d explicitly want &lt;code&gt;IsSubclassOf&lt;/code&gt; and that would not betray your abstractions, but it&amp;#8217;s late and I cannot think well during normal hours, let alone late into the night.&lt;/p&gt;
&lt;p&gt;When you need to list the members an interface exposes, including the members of its parents, you have to perform recursive inspection using &lt;code&gt;GetInterfaces()&lt;/code&gt; and then query those results in turn for members.&lt;/p&gt;
&lt;p&gt;If &lt;code&gt;DataBinding&lt;/code&gt; throws wrenches in your spokes because the target type is an interface, either replace it with an abstract class or use a &lt;code&gt;TypeConverter&lt;/code&gt; in combination with a &lt;code&gt;TypeConverterAttribute&lt;/code&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Three Ways To Indent JSON</title>
   <link href="http://philipm.at/2011/indent_json.html"/>
   <updated>2011-09-01T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/indent_json</id>
   <content type="html">&lt;p&gt;Python comes with a &lt;a href=&quot;http://docs.python.org/library/json.html&quot;&gt;JSON encoder and decoder&lt;/a&gt; you can use to prettify messy JSON code.&lt;/p&gt;

&lt;h2&gt;On the Command Line&lt;/h2&gt;

&lt;p&gt;If you have an unindented JSON file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat ugly.json | python -mjson.tool &amp;gt; pretty.json&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(May I recommend the wonderful &lt;a href=&quot;http://itunes.apple.com/us/app/dterm/id415520058?mt=12&quot;&gt;DTerm&lt;/a&gt; to run commands from almost any app you might be in?)&lt;/p&gt;

&lt;h2&gt;From within VIM&lt;/h2&gt;

&lt;p&gt;Using filter commands, &lt;code&gt;!&lt;/code&gt;, replace the text with its formatted version, filtered through the same Python command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:%!python -mjson.tool&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since it's VIM, you can replace as little or as much as needed; see &lt;a href=&quot;http://vim.wikia.com/wiki/Use_filter_commands_to_process_text&quot;&gt;Use filter commands to process text&lt;/a&gt; for more information.&lt;/p&gt;

&lt;h2&gt;With a Service&lt;/h2&gt;

&lt;p&gt;Create an Automator service and use the &lt;em&gt;Run Shell Script&lt;/em&gt; action to process the input text (make sure to select the &lt;em&gt;Output replaces selected text&lt;/em&gt; checkbox), once again using Python:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/json_workflow.png&quot; alt=&quot;Automator service&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Since Automator automatically saves services to the &lt;code&gt;~/Library/Services/&lt;/code&gt; directory, it'll become available to you instantly, and it'll allow you to go from this: &lt;br/&gt;
&lt;img src=&quot;/media/images/json_0.png&quot; alt=&quot;Unindented JSON code&quot; /&gt;&lt;/p&gt;

&lt;p&gt;to this:&lt;br/&gt;
&lt;img src=&quot;/media/images/json_1.png&quot; alt=&quot;Indented JSON code&quot; /&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>.Net DI Container Speed Redux</title>
   <link href="http://philipm.at/2011/di_speed_redux.html"/>
   <updated>2011-08-19T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/di_speed_redux</id>
   <content type="html">&lt;p&gt;Following my &lt;a href=&quot;/2011/0808/&quot;&gt;previous&lt;/a&gt; .Net DI container speed test, a few people (thanks &lt;a href=&quot;http://twitter.com/#!/slaneyrw/status/101149491121504257&quot;&gt;@slaneyrw&lt;/a&gt;, &lt;a href=&quot;http://twitter.com/#!/roryprimrose/status/101150276437811200&quot;&gt;@roryprimrose&lt;/a&gt;, &lt;a href=&quot;http://twitter.com/#!/JamesChaldecott/status/101313241833144321&quot;&gt;@JamesChaldecott&lt;/a&gt;) pointed out that the abysmal performance of &lt;strong&gt;Unity&lt;/strong&gt; in both &lt;em&gt;&lt;a href=&quot;/2011/0808/#singletons_loaded&quot;&gt;singleton loaded&lt;/a&gt;&lt;/em&gt; and &lt;em&gt;&lt;a href=&quot;/2011/0808/#transient_loaded&quot;&gt;transient loaded&lt;/a&gt;&lt;/em&gt; scenarios was largely due to checking for object registration before requesting. For example &lt;a href=&quot;http://twitter.com/#!/roryprimrose/status/101150276437811200&quot;&gt;@roryprimrose&lt;/a&gt; detailed that :&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;@philipmatix The perf problem in Unity is
actually in the IsRegistered call (35,347ms),
not the Resolve (580ms).&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;A &lt;a href=&quot;http://unity.codeplex.com/discussions/268223&quot;&gt;discussion on CodePlex&lt;/a&gt; with Chris Tavares and Grigori Melnik further denoted that, from &lt;strong&gt;Unity&lt;/strong&gt;'s perspective, the use of &lt;q&gt;&lt;code&gt;IsRegistered&lt;/code&gt; is intended for debugging your apps only&lt;/q&gt;.&lt;/p&gt;

&lt;p&gt;With that in mind, I've decided to add tests that show the performance of containers when no registration checks were performed. Following the numbers and charts I'll also provide my thoughts on why I believe both cases have merit.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Spoiler alert&lt;/em&gt;: &lt;strong&gt;Unity&lt;/strong&gt; redeems itself, and although not quite the speediest container, it put out some pretty damn good numbers.&lt;/p&gt;

&lt;p&gt;The rest of this post is broken down as following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#noregcheck&quot;&gt;performance without registration checks&lt;/a&gt; - in which we look at the numbers the containers yielded out when no registration checks were performed;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#regcheckdebate&quot;&gt;the registration check debate&lt;/a&gt; - in which we talk about whether &lt;code&gt;IsRegistered&lt;/code&gt; should be performed at all;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#regcheckalt&quot;&gt;alternatives to registration checks&lt;/a&gt; - we run a few scenarios that deal with optional dependencies - eschewing registration checks, and see the performance of those alternatives;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#tldr&quot;&gt;conclusion&lt;/a&gt; - where I stop thinking.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;A name=&quot;noregcheck&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Performance without Registration Checks&lt;/h2&gt;

&lt;p&gt;The following table displays the improvements that took place once the registration check was removed. I expected more impressive gains; I guess all containers but &lt;strong&gt;Unity&lt;/strong&gt; have really efficient registration checks and as a result have only posted gains that hovered mostly between 1.1x to 1.3x, with a few weird numbers in between.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;               Sgl             Trans           Sgl - L           Trans - L
                10k    100k     10k   100k      10k     100k      10k     100k 
Autofac         1.3     5.7     1.0    1.0      1.0      1.1      1.0      1.1
CastleWindsor   1.1     4.2     1.0    1.1      1.2      1.1      1.0      1.0
Ninject         1.0     2.1     1.0    1.0      1.1      0.9      1.0      1.0
Spring.Net      1.3    22.0     1.0    1.0      1.2      1.3      1.0      1.0
StructureMap    1.0     1.2     1.2    1.1      1.1      1.3      1.0      1.1
Unity           3.3     3.7     2.7    2.9     78.2    169.9     68.0    148.2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Of course, the big news here is that &lt;strong&gt;Unity&lt;/strong&gt;, unencumbered by registration checks, posted speeds that were from 68 to 169 times faster.&lt;/p&gt;

&lt;p&gt;To make it clear, the change in code was:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Old and busted&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsRegistered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;())&lt;/span&gt; 
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// New hotness&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;container&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Another way to look at these numbers is to compare the contribution of each container to the total time it took each scenario to run. Of course, &lt;strong&gt;Unity&lt;/strong&gt;'s trimmed out since it caused the chart to go nuts (to give you an idea this portion you see here is approximately 7 inches - &lt;em&gt;Numbers'&lt;/em&gt; measurements - out of 50 inches that was the size of the entire chart).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/di_100k_total_runs.jpg&quot; alt=&quot;Contribution of each container to the average run time of the 100k scenarios&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If it wasn't obvious, &lt;em&gt;&quot;w/&quot;&lt;/em&gt; indicates &lt;em&gt;with registration checks&lt;/em&gt; and &lt;em&gt;&quot;w/o&quot;&lt;/em&gt; means &lt;em&gt;without registration checks&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;An interesting detour for me was to try and get at least a superficial understanding of why &lt;strong&gt;Unity&lt;/strong&gt;'s &lt;code&gt;IsRegistered&lt;/code&gt; performed so poorly. As an extension method it doesn't do much - it simply iterates over the &lt;code&gt;unityContainer.Registrations&lt;/code&gt; picking up the one whose type matches the requested type. This is where things differ between &lt;strong&gt;Unity&lt;/strong&gt; and almost all other containers: whereas those containers build their registries at the point where you register the types, &lt;strong&gt;Unity&lt;/strong&gt; builds its list &lt;em&gt;on each call to &lt;code&gt;Registrations&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Indeed, each call to &lt;code&gt;Registrations&lt;/code&gt; builds an &lt;code&gt;IDictionary&amp;lt;Type, List&amp;lt;string&amp;gt;&amp;gt;&lt;/code&gt;, which is then filled in with types and names retrieved from the &lt;code&gt;NamedTypesRegistry&lt;/code&gt;, here represented by &lt;code&gt;registeredNames&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;FillTypeRegistrationDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;typeRegistrations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;parent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FillTypeRegistrationDictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeRegistrations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;foreach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;registeredNames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisteredTypes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeRegistrations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ContainsKey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;typeRegistrations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;typeRegistrations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;typeRegistrations&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;registeredNames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetKeys&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Distinct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ToList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Finally, on return, this dictionary is then transformed by a LINQ query into an &lt;code&gt;IEnumerable&amp;lt;ContainerRegistration&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's obvious that having this code executed  100,000 times, more so when loaded with a significant number of classes, will not win any performance awards.&lt;/p&gt;

&lt;p&gt;If you are wondering, like I did, this was a conscious design decision, as indicated by Chris Tavares, who said:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Yeah, IsRegistered is not optimized at all - in fact, it's almost deliberately suboptimal [...]&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;(I left out a very important second part of his response, which I'd like to use in &lt;a href=&quot;#regcheckdebate&quot;&gt;the registration check debate&lt;/a&gt; section).&lt;/p&gt;

&lt;p&gt;Fair enough, it's a decision I respect, and my intent is not to provide criticism, but a mere &lt;em&gt;heads-up&lt;/em&gt; to fellow developers, given a great deal of people cheered the introduction of &lt;code&gt;IsRegistered&lt;/code&gt; in &lt;strong&gt;Unity 2.0&lt;/strong&gt;. Since I am quite fond of &lt;strong&gt;Unity&lt;/strong&gt;, I'd very much like to see it brought on to perform on parity with the other containers, in all aspects.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;regcheckdebate&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;The Registration Check Debate&lt;/h2&gt;

&lt;p&gt;In the same reply that I've extracted the quote above, Mr. Tavares asked a very good question: &lt;q&gt;Why do you call IsRegistered at all?&lt;/q&gt;, and expressed an opinion I share: &lt;q&gt;if you're using it a lot you're most likely doing DI wrong&lt;/q&gt;.&lt;/p&gt;

&lt;p&gt;In broader terms, all these containers are truly &lt;abbr title=&quot;Inversion of Control&quot;&gt;IoC&lt;/abbr&gt; containers. &lt;a href=&quot;http://martinfowler.com/bliki/InversionOfControl.html&quot;&gt;IoC&lt;/a&gt; is an approach to handling object dependencies, a &lt;em&gt;meta-pattern&lt;/em&gt; if you will, whereas &lt;em&gt;Dependency Injection&lt;/em&gt; - which itself comes in about three flavors - is an implementation pattern. The other common IoC pattern is &lt;em&gt;Service Locator&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I believe that even if Martin Fowler didn't coin the two terms, he gave them the definition that people usually link to. You should read &lt;a href=&quot;http://martinfowler.com/articles/injection.html#UsingAServiceLocator&quot;&gt;his article&lt;/a&gt;, but a very simplistic view would say that when you use &lt;em&gt;DI&lt;/em&gt; you end up with all your dependencies injected in your classes, ready for use, whereas with &lt;em&gt;SL&lt;/em&gt; you &lt;a href=&quot;http://martinfowler.com/articles/injection.html#UsingAServiceLocator&quot;&gt;use a dedicated class&lt;/a&gt; to construct your dependencies right in the places in code where you need them. There are various &lt;a href=&quot;http://martinfowler.com/articles/injection.html#ServiceLocatorVsDependencyInjection&quot;&gt;use cases&lt;/a&gt; for either, but the general feeling, at least my feeling, is that &lt;em&gt;DI&lt;/em&gt; presents a cleaner approach.&lt;/p&gt;

&lt;p&gt;For all its cleanliness, &lt;em&gt;Dependency Injection&lt;/em&gt; does require that you start without prior restraints. Not quite the case when dealing with a good mess of code where the &lt;em&gt;High Cohesion, Low Coupling&lt;/em&gt; principle wasn't even known to the developers, let alone employed in an effective way.&lt;/p&gt;

&lt;p&gt;It is from this direction I came and, as Michael Feathers points in his seminal book &lt;a href=&quot;http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052&quot;&gt;Working Effectively with Legacy Code&lt;/a&gt;, &lt;em&gt;Service Locator&lt;/em&gt; is an effective tool to help break tightly coupled classes (think of &lt;em&gt;SL&lt;/em&gt;, if you will, as the gateway drug to &lt;em&gt;DI&lt;/em&gt;). I think in part due to this type of situation, and I'm sure due in part to developers avoiding the &lt;em&gt;DI&lt;/em&gt; because it requires a good deal more discipline, the presence of a &lt;em&gt;SL&lt;/em&gt; in code is perceived as a good indication of &lt;em&gt;code smell&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I got similar comments from other people. @dot_NET_Junkie was stronger in his statement, &lt;a href=&quot;http://twitter.com/#!/dot_NET_Junkie/status/101522500739006464&quot;&gt;calling it&lt;/a&gt; an &lt;em&gt;anti-pattern&lt;/em&gt;. Although not in such intense terms, I do share his feeling, but sometimes it's not entirely fair. Given my use-case, decoupling classes so that I can eventually get to pure-DI, it would be akin to calling a crutch an anti-pattern when all you're trying to do is get a little mobility waiting for your broken leg to mend.&lt;/p&gt;

&lt;p&gt;At the end of the day, all that these containers provide is dependency resolution, how you employ them, whether pure &lt;em&gt;DI&lt;/em&gt; or most likely a combination of &lt;em&gt;DI&lt;/em&gt; and &lt;em&gt;SL&lt;/em&gt;, it's up to you. What didn't escape me is the irony of the &lt;em&gt;Service Locator&lt;/em&gt; being present even in the purest of &lt;em&gt;DI&lt;/em&gt; cases: after all, how do you get that very first object, the root of your entire running hierarchy, out of the &lt;em&gt;DI&lt;/em&gt; container? You ask for it, you get it, and there's a name for that: service location. Glad we've established that; &lt;a href=&quot;http://en.wikiquote.org/wiki/George_Bernard_Shaw#Anecdotal_dialogue&quot;&gt;now we are just haggling about the price.&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;regcheckalt&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Alternatives to Registration Checks&lt;/h2&gt;

&lt;p&gt;There's another interesting use case for a service locator, at least one in which it is considerably easier to employ it versus dependency injection: optional plugin points. What all these containers we looked at have in common is that when requesting an object that the containers cannot construct they will throw an exception: &lt;strong&gt;Unity&lt;/strong&gt; throws a &lt;code&gt;ResolutionFailedException&lt;/code&gt;, &lt;strong&gt;Ninject&lt;/strong&gt; throws an &lt;code&gt;ActivationException&lt;/code&gt;, etc. Given exceptions tend to be somewhat expensive in .Net, never mind the silliness of considering the absence of an &lt;em&gt;optional component&lt;/em&gt; an exceptional case, it sounds reasonable that we'd check whether a component is registered before requesting it from the container.&lt;/p&gt;

&lt;p&gt;But don't trust me when I say exceptions are expensive - let's look at some numbers. In the following benchmark I am trying to retrieve an object that is not registered and guard against it missing, first using &lt;code&gt;IsRegistered&lt;/code&gt; and then by handling the exception thrown by the container.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Run_IR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsRegistered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Run_Ex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ResolutionFailedException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The testing methodology differs from the previous cases, too. Instead of loading the containers with 222 interfaces (666 classes) from the start, I will load them progressively, starting with 1 registered interface (3 named classes) and going up increments of 20. Furthemore, as I do this because of my curiosity around &lt;strong&gt;Unity&lt;/strong&gt;, I will only test it against &lt;strong&gt;Autofac&lt;/strong&gt; (no other reason than it was the first one alphabetically). If you're interested in the source code, you'll find it in the &lt;a href=&quot;https://github.com/philipmat/di_speed/tree/ex_vs_isreg&quot;&gt;ex_vs_isreg branch&lt;/a&gt; on github; the only relevant classes are &lt;code&gt;VariableLoadAutofacRunner&lt;/code&gt; and &lt;code&gt;VariableLoadUnityRunner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What I've learned? &lt;code&gt;ResolutionFailedException&lt;/code&gt; is &lt;em&gt;CRAZY expensive&lt;/em&gt; in Unity. Took me about two days to tweak the tests because I thought I coded an infinite loop. Here's a useless chart:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/di_isreg_vs_ex.jpg&quot; alt=&quot;Comparison of IsRegistered vs handling exceptions&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Look at the values behind the chart, and yes, you're reading it right - it took &lt;em&gt;43 minutes&lt;/em&gt; to run a single loop of 10k queries:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Regs      Autofac               Unity
         5x10k  1x10k       5x10k       1x10k
         IsReg     Ex       IsReg          Ex       
1            4    508          70     198,241
21           2    378         803     205,340
41           2    376       1,571     205,370
61           2    375       2,296     210,353
81           2    378       3,061     212,034
101          2    378       3,876     212,431
121          2    372       4,712     219,162
141          2    373       5,277     219,637
161          2    565       6,397     223,136
181          2    375       6,774     223,365
201          2    375       8,012     226,826
221          2    374       8,720     232,349
Run time    26   4827      51,569   2,588,244
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see that although exceptions in &lt;strong&gt;Autofac&lt;/strong&gt; are about 150x more expensive than &lt;code&gt;IsRegistered&lt;/code&gt; they don't even hold a candle to &lt;strong&gt;Unity&lt;/strong&gt;'s.&lt;/p&gt;

&lt;p&gt;So exceptions are out, what else? &lt;strong&gt;Unity&lt;/strong&gt; provides a built-in way to handle optional dependencies by the way of an &lt;code&gt;OptionalDependencyAttribute&lt;/code&gt; that you decorate your injection points with. If &lt;strong&gt;Unity&lt;/strong&gt; can find a dependency it will inject it, otherwise it will pass in null. The attribute is not the only way, as Chris Tavares &lt;a href=&quot;http://unity.codeplex.com/discussions/268223&quot;&gt;wrote&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Optional dependencies can be configured the same way as any other dependency - through attributes, the API, or the config file. As such, you don't have to put attributes in your code anywhere. Just resolve an object with the optional dependencies, and that object will get them injected (or not) if the container has a registration for it.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I have no reason to doubt him, but I was hard pressed to find either examples or documentation on how to handle them without the attribute. I'm sure that if you ask, either him or Grigori Melnik will happily provide such examples.&lt;/p&gt;

&lt;p&gt;How does the &lt;code&gt;OptionalDependency&lt;/code&gt; attribute handle? Quite well.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/di_opt.jpg&quot; alt=&quot;Straight resolve vs Optional with none registered vs Optional with one registered&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the table below, the first column contains the progressive number of interfaces registered (x3 for objects), the second is the 10x10k run time for straight &lt;code&gt;Resolve&amp;lt;IDummy&amp;gt;&lt;/code&gt;, i.e. where the classes had no dependencies, the third column contains the numbers for the dummies having an external optional dependency but with none registered to satisfy it, whereas in the third column there is a dependency to satisfy the constructors of the dummies.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Reg     Straight  Optional,  Optional, 
                    None Reg   One Reg
1           33         36         34
21          30         32         32
41          33         39         38
61          37         45         45
81          40         54         53
101         46         64         62
121         51         75         73
141         52         87         85
161         58        100        103
181         62        114        110
201         78        131        124
221         76        162        142
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Very respectable numbers provided you are either ok with peppering your code with &lt;code&gt;[OptionalDependency]&lt;/code&gt; (&lt;em&gt;ugh&lt;/em&gt;), or use a config file (&lt;em&gt;ugh&lt;/em&gt;), or you figure out how you can configure it through the &lt;strong&gt;Unity&lt;/strong&gt; API (&lt;em&gt;yay!&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;There's one more way - of course I left the best for last: the most elegant approach to handling optional components is to employ the &lt;a href=&quot;http://en.wikipedia.org/wiki/Null_Object_pattern&quot;&gt;Null Object pattern&lt;/a&gt;, in other words to provide a default, no-op implementation for all optional components. That way, you can use the container with no registration checks and get the excellent performance all containers provide, and when you have a specific implementation you simply override the existing registration in the container. As a supplemental benefit, the &lt;em&gt;Null Object pattern&lt;/em&gt; saves you one of those ugly but required null checks.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;tldr&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The best use case for any of the six containers we looked at, now and &lt;a href=&quot;/2011/0808/&quot;&gt;a few weeks ago&lt;/a&gt;, &lt;a href=&quot;http://code.google.com/p/autofac/&quot;&gt;Autofac&lt;/a&gt;, &lt;a href=&quot;http://docs.castleproject.org/Windsor.MainPage.ashx&quot;&gt;Castle.Windsor&lt;/a&gt;, &lt;a href=&quot;http://ninject.org/&quot;&gt;Ninject&lt;/a&gt;, &lt;a href=&quot;http://www.springframework.net/&quot;&gt;Spring.Net&lt;/a&gt;, &lt;a href=&quot;http://structuremap.net/structuremap/&quot;&gt;StructureMap&lt;/a&gt;, and &lt;a href=&quot;http://unity.codeplex.com/&quot;&gt;Microsoft Unity&lt;/a&gt;, and probably the best use case for &lt;em&gt;Dependency Injection&lt;/em&gt; in general, is one where all your dependencies are satisfied. If you find yourself in this fortunate position, 4 out of the 6 containers produce results so close it's not worth giving it a second thought - go with what you have or know. Even in the &lt;strong&gt;Ninject&lt;/strong&gt; case, if you're familiar with it, stick with it. Best you use &lt;em&gt;any&lt;/em&gt; container than go without. If you're forced to use &lt;strong&gt;Spring.Net&lt;/strong&gt;, I'm sorry; I hope you reap the benefits of the rest of the framework.&lt;/p&gt;

&lt;p&gt;If you find yourself employing a &lt;em&gt;Service Locator pattern&lt;/em&gt; and &lt;strong&gt;Unity&lt;/strong&gt;, examine whether you have a great deal of optional dependencies or, for that matter, any reason to perform &lt;code&gt;IsRegistered&lt;/code&gt; checks. Best you don't, whether you use  &lt;strong&gt;Unity&lt;/strong&gt;, or any other container. If you do need to deal with optional dependencies, go over the &lt;a href=&quot;#regcheckalt&quot;&gt;alternatives to registration checks&lt;/a&gt; - your best bet is the &lt;em&gt;Null Object pattern&lt;/em&gt;, it's a technique that works with all containers. It's an awesome pattern in general, and one that applies greatly to almost all situations, not just &lt;em&gt;DI&lt;/em&gt;, and yet, sadly, I don't see employed enough.&lt;/p&gt;

&lt;p&gt;The source code for my tests is on github. I have now created several branches for the various experiments we have performed: the original, using &lt;code&gt;IsRegistered&lt;/code&gt; is in the &lt;a href=&quot;https://github.com/philipmat/di_speed/tree/with_isregistered&quot;&gt;with_isregistered branch&lt;/a&gt;; the direct &lt;code&gt;Resolve&lt;/code&gt; without &lt;code&gt;IsRegistered&lt;/code&gt; is in the &lt;a href=&quot;https://github.com/philipmat/di_speed/tree/without_isregistered&quot;&gt;without_isregistered branch&lt;/a&gt;; the attempts to measure the impact of exceptions in case of failed resolutions - for &lt;strong&gt;Autofac&lt;/strong&gt; and &lt;strong&gt;Unity&lt;/strong&gt; only - is in the &lt;a href=&quot;https://github.com/philipmat/di_speed/tree/ex_vs_isreg&quot;&gt;ex_vs_isreg branch&lt;/a&gt;; finally, the examination of &lt;strong&gt;Unity&lt;/strong&gt;'s performance with the &lt;code&gt;OptionalDependencyAttribute&lt;/code&gt; is in the &lt;a href=&quot;https://github.com/philipmat/di_speed/tree/ex_vs_isreg_vs_opt&quot;&gt;ex_vs_isreg_vs_opt branch&lt;/a&gt; (badly named because it doesn't try to measure the performance of exception handling).&lt;/p&gt;

&lt;!-- 
&quot;In fact, injecting dependencies on factories is just Service Locator in disguise&quot; 
David Chlimsky
http://butunclebob.com/ArticleS.DavidChelimsky.DependencyInjectionIsOnlyMostlyGood
--&gt;

</content>
 </entry>
 
 <entry>
   <title>.Net DI Container Speed Test</title>
   <link href="http://philipm.at/2011/di_speed.html"/>
   <updated>2011-08-08T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/di_speed</id>
   <content type="html">&lt;p&gt;You probably don't care why I've done this, but if you don't even care about most of the details of this test, let me lay it on you short and sweet: &lt;strong&gt;Autofac&lt;/strong&gt;, &lt;strong&gt;Castle.Windsor&lt;/strong&gt;, and &lt;strong&gt;StructureMap&lt;/strong&gt; put out some excellent, consistent numbers; &lt;strong&gt;Spring.Net&lt;/strong&gt; is middle of the road; &lt;strong&gt;Ninject&lt;/strong&gt; is consistently the slowest of the pack by several orders of magnitude; and finally &lt;strong&gt;Unity&lt;/strong&gt; showed such a weird behavior that made me question both the validity of my approach and the sanity of its developers (mostly kidding, &lt;em&gt;ctavares&lt;/em&gt; is awesome). &lt;em&gt;Note&lt;/em&gt;: there's an &lt;a href=&quot;/2011/0819/&quot;&gt;update&lt;/a&gt; which examines some of the problems with &lt;strong&gt;Unity&lt;/strong&gt;; you'll probably want to &lt;a href=&quot;/2011/0819/&quot;&gt;read it&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have elected to run against the latest versions of the six major containers of the .Net world: &lt;a href=&quot;http://code.google.com/p/autofac/&quot;&gt;Autofac v2.4.5.724&lt;/a&gt;, &lt;a href=&quot;http://docs.castleproject.org/Windsor.MainPage.ashx&quot;&gt;Castle.Windsor v2.5.3&lt;/a&gt;, &lt;a href=&quot;http://ninject.org/&quot;&gt;Ninject v2.2.0.0&lt;/a&gt;, &lt;a href=&quot;http://www.springframework.net/&quot;&gt;Spring.Net v1.3.1&lt;/a&gt;, &lt;a href=&quot;http://structuremap.net/structuremap/&quot;&gt;StructureMap v2.6.1&lt;/a&gt;, and &lt;a href=&quot;http://unity.codeplex.com/&quot;&gt;Microsoft Unity v2.1.0.0&lt;/a&gt; using the .Net 4.0 framework. I expect the results to be similar if you were to use 3.5; I could be wrong, but I don't think any of them uses exclusive 4.0 features in areas that would make an impact on these results.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#methodology&quot;&gt;methodology&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#tldr&quot;&gt;results&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#singletons&quot;&gt;retrieving singletons&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#transient&quot;&gt;transient objects&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#singletons_loaded&quot;&gt;retrieving singletons - loaded containers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#transient_loaded&quot;&gt;transient objects - loaded containers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#mytake&quot;&gt;my take&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;A name=&quot;methodology&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Methodology&lt;/h2&gt;

&lt;p&gt;My tests (&lt;a href=&quot;https://github.com/philipmat/di_speed&quot; title=&quot;DI Speed on github&quot;&gt;source code&lt;/a&gt; available on github) concern themselves almost exclusively with the speed with which objects are retrieved from the containers. Memory footprint was not even a secondary concern and, to be honest, I don't even know how to measure it with any kind of precision. The code contains &lt;a href=&quot;https://github.com/philipmat/di_speed/blob/master/dotnet/src/Main-4/PerfCounter.cs&quot;&gt;a modest attempt&lt;/a&gt; at memory measurements - if you believe it to be correct, there's a setting in &lt;code&gt;app.config&lt;/code&gt; to print it out as you run the tests.&lt;/p&gt;

&lt;p&gt;I believe there are two scenarios that are relevant to observing the performance of these containers: retrieving the same instance each &lt;code&gt;Resolve&amp;lt;T&amp;gt;()&lt;/code&gt; call, i.e., &lt;em&gt;singleton scope&lt;/em&gt;, and retrieving a new instance on each call a.k.a. &lt;em&gt;transient scope&lt;/em&gt;. I don't think either solution is what you would see in real life situations; most likely you'd encounter a mix of the two, with transient objects being injected with singletons and transients alike, but I couldn't come up with a recipe that wouldn't be easily dismissible on account of lacking balance in proportion of singleton to transient objects. At least taking extremes only leaves you to defend from one direction.&lt;/p&gt;

&lt;p&gt;Resolving singletons tests the container's raw retrieval speed and its registration internals. Creating new objects on each call tests the container's build plan and the strategies it uses to match up objects.&lt;/p&gt;

&lt;p&gt;All containers allow more advanced object lifestyles, e.g., single instance per thread or per HTTP request, but I don't have enough confidence in my abilities (I'm also lazy) to come up with a clear strategy to produce measurements that are a) accurate, b) relevant, and c) somewhat equivalent across containers. However, I think the &lt;em&gt;singleton&lt;/em&gt; and &lt;em&gt;transient&lt;/em&gt; stories are somewhat indicative of other usage scenarios.&lt;/p&gt;

&lt;p&gt;In both cases my program will load and configure all the containers then perform 10 runs of a loop requesting the same object a large number of times (10k and 100k) then average those 10 runs. I will refer to these two as the &lt;em&gt;10k run&lt;/em&gt; and respectively the &lt;em&gt;100k run&lt;/em&gt;. So that's four combinations so far: &lt;em&gt;singleton 10k&lt;/em&gt;, &lt;em&gt;singleton 100k&lt;/em&gt;, &lt;em&gt;transient 10k&lt;/em&gt;, &lt;em&gt;transient 100k&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Finally, to add a fake real world flavor to the test, I will repeat the four test scenarios with the containers loaded with some significant number classes in order to simulate a container under some pressure and make 10k and, respectively, 100k requests of a number of objects randomly selected. I don't think any container performs any kind of heuristic analysis of the requests with intent to provide a quicker resolution path for those objects requested more/most often, though I wish they did.&lt;/p&gt;

&lt;p&gt;I am running this in a virtual machine that I will restart before each of the eight scenarios and then I will run the test program three times. I will post the numbers from both the first and third run, but will favor the third run for figures and charts.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;tldr&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Results&lt;/h2&gt;

&lt;p&gt;Individual results are boring, but are presented in the next sections for your perusal, so let us take a look at pretty charts presenting the overall situation.&lt;/p&gt;

&lt;p&gt;I had to force a max value on the Y-axis (milliseconds) seeing how &lt;strong&gt;Unity&lt;/strong&gt;  completely blew the scales. As such, please pay attention to the scale on the left and occasionally to the number on top of the bars.&lt;/p&gt;

&lt;p&gt;The chart of timings by task gives us a focused view and paints the relative behavior of containers within those groups:
&lt;img src=&quot;/media/images/di_10k_task.jpg&quot; alt=&quot;10k by task&quot; /&gt;
&lt;img src=&quot;/media/images/di_100k_task.jpg&quot; alt=&quot;100k by task&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Notice how &lt;strong&gt;Autofac&lt;/strong&gt;, &lt;strong&gt;Windsor&lt;/strong&gt;, and &lt;strong&gt;StructureMap&lt;/strong&gt; behaved consistently well, &lt;strong&gt;Ninject&lt;/strong&gt; behaved consistently badly (although that's an exaggeration), whereas &lt;strong&gt;Spring.Net&lt;/strong&gt; and &lt;strong&gt;Unity&lt;/strong&gt; were highly irregular in their numbers.&lt;/p&gt;

&lt;p&gt;Plotting the same numbers by container shows the areas of strength and weakness of each participant, but also how the containers behaved overall relative to each other:
&lt;img src=&quot;/media/images/di_10k_container.jpg&quot; alt=&quot;10k by container&quot; /&gt;
&lt;img src=&quot;/media/images/di_100k_container.jpg&quot; alt=&quot;100k by container&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We also get to see quirks, such as four out of six containers showing a dip in the &lt;em&gt;100k singleton loaded&lt;/em&gt; test relative to their other numbers.&lt;/p&gt;

&lt;p&gt;All containers are roughly on parity syntactically and feature-wise - one can easily move from one to the next with little training, the only &lt;em&gt;significant&lt;/em&gt; differentiator that I observed was elegance of configuration, but that's hardly something to hang your project onto.&lt;/p&gt;

&lt;p&gt;Let us look at individual numbers in ugly tables next. Feel free to jump to &lt;a href=&quot;#mytake&quot;&gt;my take&lt;/a&gt; if this is not of interest to you.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;singletons&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Retrieving Singletons&lt;/h2&gt;

&lt;p&gt;I believe this is the most common case in construction of &lt;strong&gt;DI trees&lt;/strong&gt; and it's after all just an optimization of the &lt;em&gt;transient&lt;/em&gt; scenario. It's suitable for classes whose context remains unchanged through the execution of the program, be they complex or not. If you rely (solely? largely?) on the constructor flavor of dependency injection, chances are everything behaves as if it was in singleton mode anyway. However, if you use a &lt;em&gt;Service Locator&lt;/em&gt; approach, whether exclusively or not, the difference between singleton and transient becomes something you have to pay more attention to.&lt;/p&gt;

&lt;p&gt;Retrieving singletons is a case of configuring the containers to indicate that once an object is created, the same instance should be returned for each subsequent &lt;code&gt;Resolve&lt;/code&gt; call.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Castle.Windsor registration&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WindsorContainer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Component&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ImplementedBy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;LifeStyle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleton&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Retrieval&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Kernel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HasComponent&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// Do is void Do() {}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The results of the runs:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:               10k - 1st  10k - 3rd  100k - 1st  100k - 3rd
------------------------------------------------------------
Autofac                15         14         688         635
Castle.Windsor         22         23         807         790
Ninject               123        121       2,616       2,558
Spring.Net             17         16       1,982       1,935
StructureMap           21         21         212         205
Unity                  93         98         992         996

         Median        22         22         900         893
            Min        15         14         212         205
            Max       123        121       2,616       2,558
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The median across containers for &lt;code&gt;Resolve()&lt;/code&gt;-ing the same object ten thousand times is around 20 milliseconds, with four out of six containers being close around that number. Of the two offenders, &lt;strong&gt;Ninject&lt;/strong&gt; was 8.6 times slower than the fastest container, &lt;strong&gt;Autofac&lt;/strong&gt;, and &lt;strong&gt;Unity&lt;/strong&gt; close on its heels at 7x the speed. The &lt;em&gt;100k&lt;/em&gt; run only served to make the gap more obvious with &lt;strong&gt;Ninject&lt;/strong&gt; clocking in a whooping 12.5x slower. Surprisingly, the second fastest container in the &lt;em&gt;10k&lt;/em&gt; run, &lt;strong&gt;Spring.Net&lt;/strong&gt; was now the second slowest (9.4x slower).&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;transient&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Transient Objects&lt;/h2&gt;

&lt;p&gt;The transient scope is most common with classes that control access to limited resources: database connection, web service requests, etc., or whose context changes on every call. Or if you just want to be safer at the expense of a bit performance. My gut feeling is that in most cases these type of classes will have either creation or use costs that outweigh the container's, but nevertheless I thought it makes for an interesting test case because it tells us how efficient each container's build plan is. I don't expect any of the containers to use &lt;a href=&quot;http://ayende.com/blog/3167/creating-objects-perf-implications&quot;&gt;the very slow&lt;/a&gt; &lt;code&gt;Activator.CreateInstance&lt;/code&gt;, but there are various strategies that can be employed to create new objects and calculate their dependencies, all which can use different lifestyles.&lt;/p&gt;

&lt;p&gt;As the test focuses on object creation I chose the route of an object with no dependencies, rather than the more complex but more realistic case of a class that gets injected with other dependencies, which themselves might be dynamically resolved at that point.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Autofac configuration - defaults to transient lifetime&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ContainerBuilder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;RegisterType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;As&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;builder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Build&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// Retrieval&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IsRegistered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Resolve&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The results are, as expected, a good deal slower than the &lt;em&gt;singleton scope&lt;/em&gt; scenario, except for &lt;strong&gt;StructureMap&lt;/strong&gt;, which captured the crown once more:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:               10k - 1st  10k - 3rd  100k - 1st  100k - 3rd
------------------------------------------------------------
Autofac                64         64         710         634
Castle.Windsor         80         80         845         797
Ninject               255        253       2,575       2,678
Spring.Net            189        185       1,856       1,874
StructureMap           21         21         202         192
Unity                 109         99       1,008         956

         Median        95         90          927        877
            Min        21         21          202        192
            Max       255        253        2,575      2,678
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I expected the &lt;em&gt;100k&lt;/em&gt; test to repeat the story of the &lt;em&gt;singleton&lt;/em&gt; and to increase in the same proportion from the &lt;em&gt;10k&lt;/em&gt; figures, yet instead the numbers came close to those of the &lt;em&gt;100k singleton&lt;/em&gt; run, for all containers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ninject&lt;/strong&gt; sticked to its guns and managed again to be 12x slower than the fastest container in the &lt;em&gt;10k&lt;/em&gt; race - &lt;strong&gt;StructureMap&lt;/strong&gt;, and almost 14x slower in the &lt;em&gt;100k&lt;/em&gt; run. Worthy mention of suckage: &lt;strong&gt;Spring.Net&lt;/strong&gt;, which managed slowness factors of 8.8 and 9.8 relative to &lt;strong&gt;StructureMap&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;singletons_loaded&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Retrieving Singletons - Loaded Containers&lt;/h2&gt;

&lt;p&gt;Based on what I've seen of their respective source code, each container takes different approaches to storing class registrations and as such when a resolution is performed, each container will use a different strategy to either retrieve an existing instance or create a new one (&lt;strong&gt;Unity&lt;/strong&gt; calls them &lt;em&gt;build plans&lt;/em&gt;, which I like a lot). I believe this test will attempt to show how well each container's approach scales when the container has a good deal of objects registered and created.&lt;/p&gt;

&lt;p&gt;I didn't know what was supposed to be a realistic number of classes to saddle a container with, so I've picked pretty numbers. I've loaded each container with 666 classes registered for 222 interfaces (three name discriminators for each interface). I made then 10k and respectively 100k random requests, pairs of interface/discriminator, to resolve those registrations.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// StructureMap - registering named objects&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ObjectFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Named&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Named&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Named&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Named&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;0&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy221&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy664&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Named&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;For&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDummy221&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Singleton&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Use&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SimpleDummy665&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Named&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;  

&lt;span class=&quot;c1&quot;&gt;// Retrieval of named objects - SM doesn&amp;#39;t have an IsRegistered&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ObjectFactory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TryGetInstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDummy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Do&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;On one hand I expected the figures to be a bit slower, but not by much. The containers should have efficient ways to sift through the registrations to find the proper resolutions.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:               10k - 1st  10k - 3rd  100k - 1st  100k - 3rd
------------------------------------------------------------
Autofac                25         23         166         163
Castle.Windsor         35         39         342         328
Ninject               128        133       1,053       1,040
Spring.Net             59         36         223         229
StructureMap          114        118         289         348
Unity               6,012      5,945      62,278      59,292

         Median        87         79         316         338 
            Min        25         23         166         163
            Max     6,012      5,945      62,278      59,292
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What the!? What just happened? Are those &lt;strong&gt;Unity&lt;/strong&gt; numbers real? &lt;strong&gt;258.5&lt;/strong&gt; and respectively a whooping &lt;strong&gt;363.8&lt;/strong&gt; times slower than &lt;strong&gt;Autofac&lt;/strong&gt;. By contrast, it makes the usual suspect, &lt;strong&gt;Ninject&lt;/strong&gt;, look like Formula-1 car.&lt;/p&gt;

&lt;p&gt;Unfortunately it is true. I was so stupefied by the result that I had to try two different approaches on three different machines. They all posted similar numbers.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;transient_loaded&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Transient Objects - Loaded Containers&lt;/h2&gt;

&lt;p&gt;You can probably anticipate how quickly I moved on to the next test. &lt;strong&gt;Unity&lt;/strong&gt; has put an okay fight in the &lt;em&gt;transient&lt;/em&gt; test, recording numbers on par with the other two usually fast containers: &lt;strong&gt;Autofac&lt;/strong&gt; and &lt;strong&gt;Windsor&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Alas, even as the test was in progress (and it took forever and three cookies), I could tell there was no redemption. And sure enough:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:               10k - 1st  10k - 3rd  100k - 1st  100k - 3rd
------------------------------------------------------------
Autofac                73         74         806         645
Castle.Windsor         99         98       1,164         941
Ninject               248        247       2,683       2,338
Spring.Net            230        226       2,346       2,157
StructureMap          118        116         324         312
Unity               6,077      5,918      60,142      58,971

         Median       174        171       1,755       1,549 
            Min        73         74         324         312
            Max     6,077      5,918      60,142      58,971
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I'm not going to dwell on those numbers. I don't know how to explain them. I'll just quickly mention &lt;strong&gt;Ninject&lt;/strong&gt; posted only 3.3x in the &lt;em&gt;10k&lt;/em&gt;, but jumped back to 7.5x in the &lt;em&gt;100k&lt;/em&gt;. &lt;strong&gt;Spring.Net&lt;/strong&gt; was the second slowest in the &lt;em&gt;100k&lt;/em&gt;, being 6.9 times slower than &lt;strong&gt;StructureMap&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;mytake&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;My Take&lt;/h2&gt;

&lt;p&gt;From seeing these numbers, working with the containers (not just for the purpose of this test), and being somewhat aware of what tells them apart: my favorite is &lt;strong&gt;&lt;a href=&quot;http://code.google.com/p/autofac/&quot;&gt;Autofac&lt;/a&gt;&lt;/strong&gt;, which is small, incredibly powerful, and yet very elegant to use. In stark contrast to &lt;strong&gt;Spring.Net&lt;/strong&gt;, I think I spent the least amount of time trying to figure out how to use &lt;strong&gt;Autofac&lt;/strong&gt; - mostly everything seemed obvious. Even the more advanced features, some of which the other containers are either barely now getting them or still dreaming of them, are very straightforward and easy to pick up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;http://structuremap.net/structuremap/&quot;&gt;StructureMap&lt;/a&gt;&lt;/strong&gt; is &lt;em&gt;fast&lt;/em&gt; (see, I made it italic so it seems fast even as it stands still), but its documentation is a few versions behind, which makes it hard at times to figure out what's the right approach to a given problem. It might be friendlier to introduce in legacy code, given it provides from the get go a &lt;strong&gt;Service Locator&lt;/strong&gt; through its &lt;code&gt;ObjectFactory&lt;/code&gt; static root, but given your level of comfort or abstraction needs you might choose to roll your own &lt;strong&gt;Service Locator&lt;/strong&gt;. At any rate, thanks to the &lt;a href=&quot;http://commonservicelocator.codeplex.com/&quot;&gt;Common Service Locator&lt;/a&gt; project, you can abstract your DI container quite well. You might find &lt;strong&gt;StructureMap&lt;/strong&gt; suitable if you need a very fast container when loaded with a lot of classes, but if that was a very important criteria I would definitely devise more suitable tests before jumping in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;http://docs.castleproject.org/Windsor.MainPage.ashx&quot;&gt;Castle.Windsor&lt;/a&gt;&lt;/strong&gt; is pretty much the veteran, yet it managed to stay nimble. It has one of the smartest guys in the industry &lt;a href=&quot;http://ayende.com/blog&quot;&gt;behind it&lt;/a&gt;. It also has a few quirks here and there, and it's not the most elegant to work with (name registration and retrieval in particular). I honestly cannot conjure a good reason to pick &lt;strong&gt;Windsor&lt;/strong&gt; over &lt;strong&gt;Autofac&lt;/strong&gt;, unless you are using the rest of the &lt;strong&gt;&lt;a href=&quot;http://www.castleproject.org&quot;&gt;Castle&lt;/a&gt;&lt;/strong&gt; framework.&lt;/p&gt;

&lt;p&gt;At the opposite side of the spectrum: &lt;strong&gt;&lt;a href=&quot;http://www.springframework.net/&quot;&gt;Spring.Net&lt;/a&gt;&lt;/strong&gt;. A very powerful framework, especially if you travelled here from the land of Java, its DI/IoC container is just horrible to work with. I've always left it till the very last and punished myself for eating too many brownies by forcing me to complete the wrapper classes for the tests. I am aware of the &lt;a href=&quot;https://github.com/SpringSource/spring-net-codeconfig&quot;&gt;CodeConfig&lt;/a&gt; project and its attempt to bring some sanity and an alternative to the hideous XML configuration files that &lt;strong&gt;Spring.Net&lt;/strong&gt; otherwise employs (don't even try to configure generics). It not only has spotty and slow performance, it's actually quite unpleasant to work with.&lt;/p&gt;

&lt;p&gt;Microsoft's &lt;strong&gt;&lt;a href=&quot;http://unity.codeplex.com/&quot;&gt;Unity&lt;/a&gt;&lt;/strong&gt; is not a bad DI container; it used to be &lt;em&gt;meh...&lt;/em&gt; at version 1.2, but starting with 2.0 - which took a very long time to come to light - it's quite nice to work with. That is until you see these numbers and that's seriously off-putting, no matter how many injection and extension features it offers. If you think about picking it up because it integrates well with ASP.Net MVC/Prism/EF/etc, well, so does &lt;strong&gt;Autofac&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Finally, &lt;strong&gt;Ninject&lt;/strong&gt; - cool name, awesome website, bad performance. I would use it if the alternative was &lt;strong&gt;Spring.Net&lt;/strong&gt; - but frankly can't see why anyone would voluntarily choose it. Except if you want to be different, which sounds to me like a perfectly good reason.&lt;/p&gt;

&lt;p&gt;Alright, we're done. The &lt;a href=&quot;https://github.com/philipmat/di_speed&quot; title=&quot;DI Speed on github&quot;&gt;source code&lt;/a&gt; is on github and once you read it you'll probably want to &lt;a href=&quot;/contact.html&quot;&gt;contact me&lt;/a&gt; and laugh at something stupid I did in there. Or call me names. Or maybe just to talk about those hard to believe &lt;strong&gt;Unity&lt;/strong&gt; numbers.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Quickly Move Messages In Mail.app</title>
   <link href="http://philipm.at/2011/mail_quickly_file.html"/>
   <updated>2011-07-16T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/mail_quickly_file</id>
   <content type="html">&lt;p&gt;This trick uses OS X's superb &lt;em&gt;search in menus&lt;/em&gt; feature and since all your mail folders are available as menus entries under &lt;strong&gt;Message &gt; Move To&lt;/strong&gt;, it makes it really quick and easy to perform this operation.&lt;/p&gt;

&lt;p&gt;If you use Gmail, remember that labels are available as folder so think of this as a less elegant way to label your messages.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/media/images/mail_move_or_copy.jpg&quot; alt=&quot;Use Search in menus to find your email folder&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Press &lt;code&gt;Cmd-?&lt;/code&gt; or &lt;code&gt;Cmd-Shift-/&lt;/code&gt; to access the &lt;strong&gt;Help &gt; Search&lt;/strong&gt; menu. &lt;sup&gt;&lt;a href=&quot;#footnote_1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;Type in the name of your folder.&lt;/li&gt;
&lt;li&gt;Press arrow down once for &lt;strong&gt;Move To&lt;/strong&gt; or twice for &lt;strong&gt;Copy To&lt;/strong&gt; and hit return.&lt;/li&gt;
&lt;/ol&gt;


&lt;hr class=&quot;footnotes&quot; /&gt;


&lt;p&gt;&lt;a name=&quot;footnote_1&quot;&gt;1&lt;/a&gt;. You might need to &lt;a href=&quot;/media/images/keyboard_settings.jpg&quot;&gt;enable the shortcut&lt;/a&gt; in System Preferences &gt; Keyboard &gt; Keyboard Shortcuts &gt; Application Shortcuts &gt; All Applications &gt; Show Help menu.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Two Significant New Features In Lion</title>
   <link href="http://philipm.at/2011/two_new_features.html"/>
   <updated>2011-06-14T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/two_new_features</id>
   <content type="html">&lt;p&gt;A great deal of smart people blogged and &lt;a href=&quot;http://5by5.tv/&quot;&gt;spoke on podcasts&lt;/a&gt; about all the new features in OS X 10.7 aka Lion.&lt;/p&gt;

&lt;p&gt;But there are two features that albeit not life-saving improve the quality of life quite considerably and
have been at the forefront of Mac &lt;strike&gt;critique&lt;/strike&gt; switcher pain since... well, OS X.&lt;/p&gt;

&lt;h2&gt;#1: Resize from Any Edge&lt;/h2&gt;

&lt;p&gt;Instead of just from the bottom right corner, you can now &lt;a href=&quot;http://www.apple.com/macosx/whats-new/features.html#other&quot;&gt;resize from any edge&lt;/a&gt; and any corner too: &lt;br/&gt;
&lt;img src=&quot;/media/images/resize_lion.png&quot; alt=&quot;Resize from any edge&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The small resize chevron in the bottom right corner is also gone and when combined with
the disappearing scrollbars the effect is very pleasant. It's as if windows have finally
found their purpose: to display content, not widgets.&lt;/p&gt;

&lt;p&gt;I don't need to say more about this one. As an owner of a large LCD that&lt;br/&gt;
displays multiple windows at the same time, thank you! Thank you so &lt;em&gt;unpossibly&lt;/em&gt; very much.&lt;/p&gt;

&lt;h2&gt;#2: Merge Folder Content&lt;/h2&gt;

&lt;p&gt;When you copy or move a folder and the target already contains a folder
with the same name, the Finder gives you the option
to either replace the target folder or cancel the operation:    &lt;br/&gt;
&lt;img src=&quot;/media/images/folder_override_sl.jpg&quot; alt=&quot;Snow Leopard behavior&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Another way to think about it is to view folders being equivalent to files:
if you were to move a file and if the target folder already contained a file with the same name,
would you expect that the operating system merges the two files? &lt;strong&gt;#rationalization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It may make sense to geeks but to most people switching from other OS's, where they have learned
to click absently minded on a torrent of badly worded and poorly button-ed dialogs,
this was not just merely annoying but down-right harmful.
You learn fast though, as the saying goes.&lt;/p&gt;

&lt;p&gt;Lion &lt;a href=&quot;http://www.apple.com/macosx/whats-new/features.html#finder&quot;&gt;changes this behavior&lt;/a&gt; and offers to merge the folder rather than replace its content:&lt;br/&gt;
&lt;img src=&quot;/media/images/folder_override_lion.png&quot; alt=&quot;Lion behavior&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I like the &lt;strong&gt;Keep Both&lt;/strong&gt; button because &lt;strong&gt;merge&lt;/strong&gt; is such a geeky word, but on the other hand I
could argue that perhaps &lt;strong&gt;both&lt;/strong&gt; is more ambiguous since according to the dictionary:&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;both&lt;/strong&gt; |bōθ| &lt;br/&gt;
 adjective &amp;amp; pronoun  &lt;br/&gt;
 used to refer to two people or things, regarded and identified together&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;Using this definition one would expect that both folders would keep their identity, which is obviously not the case.
Perhaps it's just a matter of perspective and whether one sees &quot;the thing&quot; as being the folder's content
rather than folder itself.&lt;/p&gt;

&lt;h2&gt;Why Wait So Long?&lt;/h2&gt;

&lt;p&gt;This is &lt;strong&gt;the one question&lt;/strong&gt; that's been nagging me regarding these two changes, but I think I'll let smarter people answer.&lt;/p&gt;

&lt;p&gt;Apple could've done both a good while ago - as far as I can tell there's nothing earth-shattering in Lion without which
they couldn't have introduced these features; after all the radars have been filed, and complaints have been voiced
for quite a long time.&lt;/p&gt;

&lt;p&gt;Or, perhaps, a more interesting question would be &lt;em&gt;&quot;what can we read/infer/speculate from this change?&quot;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I cannot come up with anything that would tie the &lt;strong&gt;merge&lt;/strong&gt; feature into their
&lt;a href=&quot;http://programmista.com/post/6427972345/lion-ios-5-and-icloud-declare-war-against-the-file&quot;&gt;getting rid&lt;/a&gt; of the file system &lt;a href=&quot;http://gigaom.com/apple/apple-could-kill-the-finder-would-you-miss-it/&quot;&gt;as we know if&lt;/a&gt;, and
I'm even more puzzled by the &lt;strong&gt;resize&lt;/strong&gt; feature because it seems counter-intuitive to me that Apple would spend
time on a feature that's been, aside from having the menu at the top and the window control buttons to the left,
a seemingly core visual feature that people identified with OS X (and complained bitterly about).&lt;/p&gt;

&lt;p&gt;One more for the road:&lt;/p&gt;

&lt;h2&gt;256 Terminal Colors&lt;/h2&gt;

&lt;p&gt;If anything, a &lt;a href=&quot;http://www.apple.com/macosx/whats-new/features.html#unix&quot;&gt;256-color terminal&lt;/a&gt; means &lt;strong&gt;vim&lt;/strong&gt; can look as good in the console as &lt;strong&gt;MacVim&lt;/strong&gt;
looks.  &lt;br/&gt;
&lt;img src=&quot;/media/images/lion_256_terminal.png&quot; alt=&quot;Terminal enhancements&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The screenshot above shows the new terminal theme called &quot;Silver Aerogel&quot;. Can't say I'm crazy about it.
Black text on gray doesn't offer a very good contrast, but the blur on the window makes it stand out.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Word Count Service For OS X</title>
   <link href="http://philipm.at/2011/word_count_service.html"/>
   <updated>2011-05-24T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/word_count_service</id>
   <content type="html">&lt;p&gt;I recently needed a quick way to count the words in selected text on a web page. It seemed like an OS X service would be the perfect ticket, moreso since Snow Leopard shows in the Services menu only those services appropriate for the current context.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://daringfireball.net/2007/01/word_count_script_for_thisservice&quot;&gt;Smarter people&lt;/a&gt; have &lt;a href=&quot;http://equinox-of-insanity.com/2008/01/better-word-count-service/&quot;&gt;tackled&lt;/a&gt; this need before, but none of their solutions seemed to work for me, ironically, in Snow Leopard. I suspect that either something changed ever so slightly in AppleScript, or that those scripts were designed to work with a specific utility (&lt;a href=&quot;http://wafflesoftware.net/thisservice/&quot;&gt;ThisService&lt;/a&gt; - which by the way it seemed pretty awesome) rather than standalone.&lt;/p&gt;

&lt;p&gt;Either way, here's my solution:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;applescript&quot;&gt;&lt;span class=&quot;k&quot;&gt;on&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;run&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;display alert&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Counted &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;words&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;¬&lt;/span&gt;
    &lt;span class=&quot;s2&quot;&gt;&amp;quot; words.&amp;quot;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;The selected text had &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;characters&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as &lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot; characters.&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;input&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Here's a screenshot of it counting the words of this post (how self-referential):
&lt;img src=&quot;/media/images/word_count.jpg&quot; alt=&quot;Word Count Screenshot&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Two quick notes: both &lt;a href=&quot;http://daringfireball.net/2007/01/word_count_script_for_thisservice&quot;&gt;John Gruber&lt;/a&gt;'s and &lt;a href=&quot;http://equinox-of-insanity.com/2008/01/better-word-count-service/&quot;&gt;Nima Yousefi&lt;/a&gt;'s scripts use &lt;code&gt;on process(_str)&lt;/code&gt; - I had no luck getting that to work and instead I've decided to go with &lt;strong&gt;Automator&lt;/strong&gt;'s suggestion of &lt;code&gt;on run {input, parameters}&lt;/code&gt;. As a result of my choice, it seems I was also required to cast the input to a string (&lt;code&gt;(input as string)&lt;/code&gt;), otherwise &lt;code&gt;count words of&lt;/code&gt; would consistently and unwantedly return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>ipy - Completion For Python Interactive</title>
   <link href="http://philipm.at/2011/ipy.html"/>
   <updated>2011-05-12T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/ipy</id>
   <content type="html">&lt;p&gt;Bash has awesome command and file completion. I like that.  Ruby people are just spoiled with &lt;strong&gt;irb&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Python can do that too.&lt;/p&gt;

&lt;p&gt;Completion is handled by the &lt;a href=&quot;http://docs.python.org/library/rlcompleter.html&quot;&gt;&lt;code&gt;rlcompleter&lt;/code&gt; module&lt;/a&gt;, aided by the &lt;code&gt;readline&lt;/code&gt; library, and it is all set up with two lines of code.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;rlcompleter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;readline&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse_and_bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;tab: complete&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TAB&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PRESSED&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__doc__&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_line_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read_init_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__file__&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;insert_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_completer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse_and_bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Useful, but you want to have that set up automatically (if you can remember or can be bothered to type those liens every time you start &lt;code&gt;python&lt;/code&gt;, you have my deepest respects, yet I'm also slightly suspicious of your sanity). You have two options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;a href=&quot;http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP&quot;&gt;&lt;code&gt;PYTHONSTARTUP&lt;/code&gt;&lt;/a&gt; environment variable, which if pointing to the name of a readable file &lt;q&gt;the Python commands in that file are executed before the first prompt is displayed in interactive mode.&lt;/q&gt;&lt;/li&gt;
&lt;li&gt;Running &lt;code&gt;python -i [script]&lt;/code&gt;, which will cause the interpreter to remain in interactive mode after evaluating the script specified on the command line. You can use it in combination with an &lt;code&gt;alias&lt;/code&gt; command.&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;Start by saving those two lines to a file. I'll call mine &lt;code&gt;~/.python_init.py&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;rlcompleter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;readline&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;readline&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse_and_bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;tab: complete&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;The &lt;code&gt;PYTHONSTARTUP&lt;/code&gt; route&lt;/strong&gt; sets this environment variable in your &lt;strong&gt;personal initialization file&lt;/strong&gt;,  &lt;code&gt;.bashrc&lt;/code&gt; for the bashful of us (&lt;a href=&quot;http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html&quot;&gt;why you want &lt;code&gt;bashrc&lt;/code&gt; instead of &lt;code&gt;bash_profile&lt;/code&gt;&lt;/a&gt;):&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;PYTHONSTARTUP&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;~/.python_init.py
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;The &lt;code&gt;alias&lt;/code&gt; route&lt;/strong&gt; defines an alias in your &lt;strong&gt;personal initialization file&lt;/strong&gt; for &lt;code&gt;python -i ~/.python_init.py&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;alias &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ipy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;python -i ~/.python_init.py&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Now you can start your &lt;strong&gt;python&lt;/strong&gt; interpreter using &lt;em&gt;50% fewer characters!&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ipy
&amp;gt;&amp;gt;&amp;gt; import re
&amp;gt;&amp;gt;&amp;gt; re.&amp;lt;TAB&amp;gt;
re.DEBUG              re.__doc__            re._compile_repl&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
re.DOTALL             re.__file__           re._expand&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
re.I                  re.__format__&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;        re._pattern_type&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
re.IGNORECASE         re.__getattribute__&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;  re._pickle&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
re.L                  re.__hash__&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;          re._subx&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
re.LOCALE             re.__init__&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;          re.compile&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
...
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Either works the same, but I would think that &lt;code&gt;PYTHONSTARTUP&lt;/code&gt;, being a &lt;em&gt;set and forget&lt;/em&gt; option, would serve you better.&lt;/p&gt;

&lt;p&gt;Want to know more? Read the &lt;a href=&quot;http://docs.python.org/library/rlcompleter.html&quot;&gt;rlcompleter documentation&lt;/a&gt;. The &lt;a href=&quot;http://docs.python.org/library/readline.html&quot;&gt;readline docs&lt;/a&gt; are interesting, too.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Jekyll vs. Hyde - A Comparison Of Two Static Site Generators</title>
   <link href="http://philipm.at/2011/jekyll_vs_hyde.html"/>
   <updated>2011-05-07T00:00:00-05:00</updated>
   <id>http://philipm.at/2011/jekyll_vs_hyde</id>
   <content type="html">&lt;p&gt;It seemed fitting, when I first thought about it, that my first blog post should discuss the engine I chose to power my website. After spending days writing this for you, I'm reasonably confident we both would've been better served if I went drinking with my imaginary friends and you just pretended you liked reading it. Anyway...&lt;/p&gt;

&lt;p&gt;There are several static site generators/engines you can choose from and your choice is most likely bound by either what features your hosting provider offers or by language preference.
On the Ruby planet &lt;a href=&quot;https://github.com/mojombo/jekyll&quot;&gt;Jekyll&lt;/a&gt;, &lt;a href=&quot;http://webby.rubyforge.org/&quot;&gt;Webby&lt;/a&gt;, and &lt;a href=&quot;http://nanoc.stoneship.org/&quot;&gt;nanoc&lt;/a&gt; are the most popular choices.&lt;/p&gt;

&lt;p&gt;Robert Rees has &lt;a href=&quot;http://rrees.wordpress.com/2009/06/01/semi-static-cmss/&quot;&gt;an excellent comparison&lt;/a&gt; of the Ruby engines, which he calls &quot;Semi-static CMSs&quot;. His article was written in June 2009, but you'll find it very much pertinent as two years later these three engines are still firing on all pistons. Largely based on Robert's review, I have selected &lt;strong&gt;Jekyll&lt;/strong&gt; to represent the Ruby side.&lt;/p&gt;

&lt;p&gt;With Python you have &lt;a href=&quot;http://ringce.com/hyde&quot;&gt;Hyde&lt;/a&gt; and that's pretty much it.&lt;/p&gt;

&lt;p&gt;PHP fares about the same: &lt;a href=&quot;http://staceyapp.com/&quot;&gt;Stacey&lt;/a&gt; seems to power quite a good deal of small websites. But on the horizon, on account of his creator, a higher profile engine might end up being &lt;a href=&quot;http://marco.org&quot;&gt;Marco Arment&lt;/a&gt;'s &lt;a href=&quot;http://www.marco.org/secondcrack&quot;&gt;Second Crack&lt;/a&gt;, whenever he decides to open source it. I've decided not to include PHP in this comparison because I'm prejudiced to believe that PHP folks are more in the dynamic website camp, given how easy PHP is to get up and go with.&lt;/p&gt;

&lt;p&gt;With a static website generator you will spend most of your time producing the content and about the same fidgeting with the layout. Ironically, &lt;strong&gt;Jekyll&lt;/strong&gt; and &lt;strong&gt;Hyde&lt;/strong&gt; lend themselves quite well to a head to head comparison on both features. Not only do both support &lt;a href=&quot;http://daringfireball.net/projects/markdown/&quot;&gt;Markdown&lt;/a&gt; and &lt;a href=&quot;http://www.textism.com/tools/textile/&quot;&gt;Textile&lt;/a&gt; for writing the content, their templating languages are very similar too: Hyde uses the &lt;a href=&quot;http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs&quot;&gt;Django templating language&lt;/a&gt;, whereas Jekyll uses &lt;a href=&quot;http://www.liquidmarkup.org/&quot;&gt;Liquid&lt;/a&gt;, whose inspiration was, surprise!, the Django templating language.&lt;/p&gt;

&lt;p&gt;While researching these engines I have put together &lt;a href=&quot;https://github.com/philipmat/jekyll_vs_hyde&quot;&gt;an example website&lt;/a&gt; built using Jekyll and then Hyde &lt;a href=&quot;https://github.com/philipmat/jekyll_vs_hyde&quot;&gt;on github&lt;/a&gt;. While this post does make some references to this sample, you might find it a good counterpart as it covers some of the elements I struggled to figure out.&lt;br/&gt;
To get this sample site, execute &lt;code&gt;git clone https://github.com/philipmat/jekyll_vs_hyde&lt;/code&gt; in the directory of your choice or simply use github to view the code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;#installation&quot;&gt;Installation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#structure&quot;&gt;Structure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#configuration&quot;&gt;Advanced Configuration and Options&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#templates&quot;&gt;The Content and the Templating Language&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#misc&quot;&gt;Miscellaneous Features and Considerations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;#tldr&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;A name=&quot;installation&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Installation&lt;/h2&gt;

&lt;p&gt;The best way to install Jekyll is via RubyGems:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;gem install jekyll
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This will also install all required dependencies: &lt;strong&gt;directory_watcher&lt;/strong&gt;, &lt;strong&gt;liquid&lt;/strong&gt;, &lt;strong&gt;open4&lt;/strong&gt;, &lt;strong&gt;maruku&lt;/strong&gt; and &lt;strong&gt;classifier&lt;/strong&gt;. More information &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/install&quot;&gt;can be found here&lt;/a&gt;, but one interesting, if not ironic, bit is that Jekyll relies on &lt;a href=&quot;http://pygments.org/&quot;&gt;Pygments&lt;/a&gt;, a Python project, to provide code syntax highlighting. The installation instructions for Pygments, also detailed on the &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/install&quot;&gt;Jekyll install page&lt;/a&gt;, are dependent on your OS, but are usually a variant of:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;sudo easy_install Pygments
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;I recommend that you also generate at this point the stylesheet required by Pygments to style your code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;python -c &lt;span class=&quot;s2&quot;&gt;&amp;quot;from pygments.formatters import HtmlFormatter; print HtmlFormatter().get_style_defs(&amp;#39;.highlight&amp;#39;)&amp;quot;&lt;/span&gt; &amp;gt; highlight.css
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Hyde's setup, on the other hand, is more involved. You will start by getting the source code, followed by installing the requirements (&lt;strong&gt;Django&lt;/strong&gt;, &lt;strong&gt;pyYAML&lt;/strong&gt;, and &lt;strong&gt;markdown&lt;/strong&gt;), and probably complete by installing Pygments.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /opt/local/src
git clone https://github.com/lakshmivyas/hyde.git
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;hyde &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Since you're getting intimate with that terminal anyway, you might want to consider installing &lt;a href=&quot;http://code.google.com/p/python-markdown2/&quot;&gt;markdown2&lt;/a&gt;, a faster and more complete implementation of Markdown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A note on Hyde installation&lt;/strong&gt;: if you plan to follow along Hyde's &lt;a href=&quot;https://github.com/lakshmivyas/hyde&quot;&gt;own instructions&lt;/a&gt;, step #2 of the documentation offers to generate a &quot;basic website&quot; for you, and then processes this skeleton to generate your website. Unfortunately, this &quot;basic website&quot; turns out to be quite comprehensive, and its processing will fail because it requires a great deal of Python modules which were not listed in &lt;code&gt;requirements.txt&lt;/code&gt;. At this point you have several options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You follow along with me or use my &lt;em&gt;truly basic&lt;/em&gt; skeleton which I promise it'll work or your money back.&lt;/li&gt;
&lt;li&gt;You leave the &quot;basic website&quot; as is and prod it for structure and other tricks. Just be aware that what was generated is anything but &lt;em&gt;basic&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;You proceed to discover and install all the requirements and then generate and run the website (hats off to you, you man of genius).&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;&lt;A name=&quot;structure&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Structure&lt;/h2&gt;

&lt;p&gt;Both languages tend to follow a similar structure, with Jekyll favoring convention-over-configuration, whereas Hyde relies entirely on configuration. Both will read pretty much all files in the structure and either through process or straight copy will generate your static website in dedicated output folder.&lt;/p&gt;

&lt;h3&gt;Jekyll&lt;/h3&gt;

&lt;p&gt;The more prominent elements of a typical Jekyll structure include a &lt;code&gt;_layouts&lt;/code&gt; folder that stores the templates to build the site, a &lt;code&gt;_posts&lt;/code&gt; folder that contains the blog content, and a &lt;code&gt;_site&lt;/code&gt; folder where the generated website will be placed. Also typically present are a configuration file in YAML format, &lt;code&gt;_config.yml&lt;/code&gt;, a root &lt;code&gt;index.html&lt;/code&gt;, and most likely some sort of &lt;code&gt;media&lt;/code&gt; folder for your &lt;code&gt;.css&lt;/code&gt;, &lt;code&gt;.js&lt;/code&gt;, and images.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./
  _config.yml
  index.html
  _layouts/
      base.html
      post.html
  _posts/
      2011-04-11-lore_ipsum_sit_amet.markdown
      2011-04-21-consectetur_adipisicing_elit.markdown
  media/
      main.css
      main.js
  _site/
      ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This configuration is merely a preference. Between command line arguments and the &lt;code&gt;_config.yml&lt;/code&gt; file, Jekyll allows you to overwrite most of its options, which can be &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/configuration&quot;&gt;quite extensive&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Within this structure, Jekyll will attempt to process all &lt;code&gt;.html&lt;/code&gt;, &lt;code&gt;.markdown&lt;/code&gt;, and &lt;code&gt;.textile&lt;/code&gt; files that contain a specific header, called &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/YAML-Front-Matter&quot;&gt;YAML Front Matter&lt;/a&gt;; it will ignore those files that do not have this header or those that start with an underscore (including folders). All other files will be copied to the &lt;code&gt;_site&lt;/code&gt; folder, preserving the folder hierarchy in which they were located.&lt;/p&gt;

&lt;p&gt;Jekyll also mandates that the files within the &lt;code&gt;_posts&lt;/code&gt; folder are in a &lt;code&gt;YEAR-MONTH-DATE-blog_title.MARKUP&lt;/code&gt; format. You have control over the &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/Permalinks&quot;&gt;permalinks&lt;/a&gt; it will generate, but the names of the files are fixed.&lt;/p&gt;

&lt;h3&gt;Hyde&lt;/h3&gt;

&lt;p&gt;Hyde helpfully offers to generate a &quot;basic website&quot; for newcomers - which, as mentioned before, doesn't work, but gives you a basic idea of what Hyde looks like and a &lt;code&gt;settings.py&lt;/code&gt; file to work with. You need to comment out a good deal of (not initially useful) lines in &lt;code&gt;settings.py&lt;/code&gt; to get you started.&lt;/p&gt;

&lt;p&gt;Hyde compartmentalizes its files a bit more than Jekyll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;layout&lt;/code&gt; - files used as templates for the content (set using &lt;code&gt;LAYOUT_DIR&lt;/code&gt; in &lt;code&gt;settings.py&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;&lt;code&gt;content&lt;/code&gt; - where the content resides (set with &lt;code&gt;CONTENT_DIR&lt;/code&gt;). These files will be processed by Hyde and the templates in &lt;code&gt;layout&lt;/code&gt; will be applied according to each file's individual preference, similar but not exactly like Jekyll's &lt;strong&gt;YAML Front Matter&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;media&lt;/code&gt; - images, stylesheets, etc (&lt;code&gt;MEDIA_DIR&lt;/code&gt;). These are files that are copied (but may be pre-processed) to the output folder preserving their hierarchical structure;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;deploy&lt;/code&gt; - the folder to which Hyde outputs the static website (&lt;code&gt;DEPLOY_DIR&lt;/code&gt;). Its Jekyll equivalent is the &lt;code&gt;_site&lt;/code&gt; folder.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;&lt;A name=&quot;configuration&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Advanced Configuration and Options&lt;/h2&gt;

&lt;p&gt;Aside from the caveat regarding Hyde's generated &quot;basic website&quot; and &lt;code&gt;settings.py&lt;/code&gt;, neither engine requires any further configuration to start working on your website, but both offer more advanced options, and in both cases &lt;em&gt;most&lt;/em&gt; of these options are configurable through &lt;code&gt;settings.py&lt;/code&gt; in Hyde's case and &lt;code&gt;_config.yml&lt;/code&gt; in Jekyll's.&lt;/p&gt;

&lt;p&gt;You get common features such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;exclude files from processing (e.g. &lt;code&gt;.htaccess&lt;/code&gt;) or even entire folders (e.g. &lt;code&gt;drafts&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;specify what port to run the development server on;&lt;/li&gt;
&lt;li&gt;indicate a different output folder;&lt;/li&gt;
&lt;li&gt;alternative rendering engines (e.g. a different Markdown engine);&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://growl.info/&quot;&gt;Growl&lt;/a&gt; notifications;&lt;/li&gt;
&lt;li&gt;syntax highlighting for code snippets using Pygments.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Some of the other options come from a combination of usage and configuration. For example, Hyde allows you to use a different Markdown engine, &lt;a href=&quot;http://code.google.com/p/python-markdown2/&quot;&gt;markdown2&lt;/a&gt;, by using a &lt;code&gt;{% markdown2 %}&lt;/code&gt; tag in the content or templates, whereas Jekyll interprets all &lt;code&gt;.markdown&lt;/code&gt; files as Markdown syntax but the actual translation engine can be changed in &lt;code&gt;_config.yml&lt;/code&gt; using the &lt;code&gt;markdown: [engine]&lt;/code&gt; setting, allowing you to switch from the default &lt;strong&gt;maruku&lt;/strong&gt; to &lt;strong&gt;RDiscount&lt;/strong&gt; or another similar engine.&lt;/p&gt;

&lt;p&gt;This touches on the first and perhaps largest difference between the two engines: Hyde makes use of template tags for processing, while Jekyll relies on the file extension to determine the type of markup for the content present within it. As a result, Hyde allows you to use multiple engines within the same content file: you can freely mix Markdown and Textile within a single file, if that's something you'd want to do, but at the expense of having your content sprinkled with Django tags.&lt;/p&gt;

&lt;p&gt;Hyde also makes heavy use of &lt;a href=&quot;https://github.com/lakshmivyas/hyde&quot;&gt;pre-processors&lt;/a&gt; and &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Site-Post-Processors&quot;&gt;post-processors&lt;/a&gt; when transforming the &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Content-Processors&quot;&gt;content&lt;/a&gt; and &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Media-Processors&quot;&gt;support files&lt;/a&gt; and allows you, among others, to minify your JavaScript files (using &lt;strong&gt;JSMin&lt;/strong&gt;) or to optimize them using the &lt;strong&gt;Closure Compiler&lt;/strong&gt;, to code your CSS using &lt;strong&gt;SASS&lt;/strong&gt; or &lt;strong&gt;Less CSS&lt;/strong&gt; or &lt;strong&gt;Stylus&lt;/strong&gt;, or even generate thumbnails for your images.&lt;br/&gt;
Of course, you'd have to install the appropriate Python packages for these pre-processors, for example you would need the &lt;a href=&quot;http://www.pythonware.com/products/pil/&quot;&gt;PIL - Python Imaging Library&lt;/a&gt; to make the thumbnails pre-processor work. These pre-processors are enumerated in specific lists within &lt;code&gt;settings.py&lt;/code&gt;, e.g:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;MEDIA_PROCESSORS = {
    '.css':('hydeengine.media_processors.TemplateProcessor',
            'hydeengine.media_processors.CSSmin',),
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but they are then configured separately using &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Settings&quot;&gt;global options&lt;/a&gt; specific to each preprocessor (e.g. &lt;code&gt;THUMBNAIL_MAX_WIDTH&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;In contrast, Jekyll allows its options to be &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/configuration&quot;&gt;configured&lt;/a&gt; in a hierarchical, and easier to read, fashion:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;maruku:
  use_tex:    false
  use_divs:   false
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Jekyll's answer to Hyde's pre-processors, to some degree, are a quite extensive &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/Plugins&quot;&gt;collection of plugins&lt;/a&gt;, for example Jason Graham's &lt;a href=&quot;https://gist.github.com/639920&quot;&gt;Less CSS Converter&lt;/a&gt; or Michael Levin's &lt;a href=&quot;http://www.kinnetica.com/projects/jekyll-sitemap-generator/&quot;&gt;sitemap.xml generator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Oh, and cherry on the cake, both have support for &lt;a href=&quot;http://growl.info/&quot;&gt;Growl&lt;/a&gt; - built-in Hyde and through &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/Plugins&quot;&gt;a plugin&lt;/a&gt; in Jekyll, although they both generate the files so fast I don't see why you would need it.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;templates&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;The Content and the Templating Language&lt;/h2&gt;

&lt;p&gt;Hyde uses the &lt;a href=&quot;http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs&quot;&gt;Django templating language&lt;/a&gt; while Jekyll uses the similar, but less complex/powerful, &lt;a href=&quot;http://www.liquidmarkup.org/&quot;&gt;Liquid&lt;/a&gt;.&lt;br/&gt;
If you're familiar with &lt;strong&gt;Django&lt;/strong&gt;, you'll be at home with &lt;strong&gt;Liquid&lt;/strong&gt;. If you're very familiar with Django, you'll notice pretty fast that Liquid is missing one very significant feature: &lt;strong&gt;blocks&lt;/strong&gt;. We'll talk about this later - I just wanted to get it out of the way.&lt;/p&gt;

&lt;h3&gt;A Simple Page&lt;/h3&gt;

&lt;p&gt;In both cases, you will pair a content file with one or more template files.&lt;br/&gt;
&lt;strong&gt;Let's start with Jekyll&lt;/strong&gt; and a very simple &quot;about&quot; page, which will start out as an &lt;code&gt;./about.markdown&lt;/code&gt; file and end up in &lt;code&gt;./_site/about.html&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;---
layout: page
title: About
---

This sample shows how to build a very simple 
[Jekyll](https://github.com/mojombo/jekyll) powered website.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first section of the &lt;code&gt;about.markdown&lt;/code&gt; file, between the &lt;code&gt;---&lt;/code&gt; markers is the &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/YAML-Front-Matter&quot;&gt;YAML Front Matter&lt;/a&gt;. Jekyll requires it on all files you'd like to have processed, otherwise the file will be ignored and copied to the output as is. The &lt;code&gt;layout&lt;/code&gt; property tells Jekyll what template to use when processing this file, so in this case Jekyll expects there is a &lt;code&gt;./_layouts/page.html&lt;/code&gt; file, which &lt;a href=&quot;https://github.com/philipmat/jekyll_vs_hyde&quot;&gt;in my example&lt;/a&gt; looks something like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;---
layout: base
---
&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;content&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;{{ page.title }}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;

{{ content }}

&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Three quick things as this is a comparison more than an introduction to the templating language: the &lt;code&gt;{{ page.title }}&lt;/code&gt; gets replaced by the &lt;code&gt;title&lt;/code&gt; parameter from &lt;code&gt;about.markdown&lt;/code&gt;'s &lt;strong&gt;YAML Front Matter&lt;/strong&gt; section; the &lt;code&gt;{{ content }}&lt;/code&gt; will contain the processed body of the &lt;code&gt;about.markdown&lt;/code&gt;; and, finally, because &lt;code&gt;page.html&lt;/code&gt; contains it's own &lt;strong&gt;YAML Front Matter&lt;/strong&gt; section, it will be further processed using &lt;code&gt;./_layouts/base.html&lt;/code&gt;.&lt;br/&gt;
&lt;code&gt;base.html&lt;/code&gt; will then look very similar:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;{{ page.title }} - Jekyll Sample&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    {% include nav.html %}
    {{ content }}
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The interesting bit is the &lt;code&gt;{% include nav.html %}&lt;/code&gt; which does what you think it does, except it will require that the file is located at &lt;code&gt;./_includes/nav.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's look at the Hyde equivalent&lt;/strong&gt;, but we'll do so in reverse order. I promise you it'll make more sense.&lt;br/&gt;
&lt;code&gt;base.html&lt;/code&gt; looks just like the Jekyll equivalent:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Jekyll vs. Hyde - A Comparison Of Two Static Site Generators - Hyde Sample&lt;span class=&quot;nt&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    {% include &amp;quot;nav.html&amp;quot; %}
    {% block content %}page content goes here{% endblock%} %}
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The difference from the Jekyll version is the &lt;code&gt;{% block content %}{% endblock %}&lt;/code&gt;.&lt;br/&gt;
Next template in chain: &lt;code&gt;page.html&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;{% extends &amp;quot;base.html&amp;quot; %}
{% block content %}
&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;content&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;{{ page.title }}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
{% block article %}{% endblock %}
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
{% endblock %}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;In Django, blocks in child files overwrite blocks in the parent they inherit from (the &lt;code&gt;{% extends &quot;base.html%}&lt;/code&gt; specifies the parent of a template). As such, the &lt;code&gt;content&lt;/code&gt; block in the &lt;code&gt;page.html&lt;/code&gt; template will replace the similar block in the parent &lt;code&gt;base.html&lt;/code&gt;. Finally, let's examine Hyde's version of the &quot;about&quot; page, &lt;code&gt;./content/about.html&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;{% extends &amp;quot;page.html&amp;quot; %}
{% hyde
    title: &amp;quot;About&amp;quot;
    created: 2011-04-26 00:25:00
%}

{% block article %}
{% markdown %}
This sample shows how to build a very simple 
  [Hyde](https://github.com/lakshmivyas/hyde) powered website.
{% endmarkdown %}
{% endblock %}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Notice how the &lt;code&gt;content&lt;/code&gt; block of &lt;code&gt;page.html&lt;/code&gt; replaces the parent's, and the &lt;code&gt;article&lt;/code&gt; block of &lt;code&gt;about.html&lt;/code&gt; replaces the similar block defined in &lt;code&gt;page.html&lt;/code&gt;. With Hyde a child page can overwrite any of the blocks defined in any its ancestors; contrast with Jekyll where a child can only overwrite the parent's &lt;code&gt;{{ content }}&lt;/code&gt; declaration.&lt;br/&gt;
Your probably figured out that the &lt;code&gt;{% hyde %}&lt;/code&gt; block is the rough equivalent of &lt;strong&gt;YAML Front Matter&lt;/strong&gt; header.&lt;/p&gt;

&lt;h3&gt;Dynamic Page - The Blog&lt;/h3&gt;

&lt;p&gt;To create a blog page, that is to generate a dynamic page that essentially includes other pages, Jekyll and Hyde offer similar mechanisms, but they go a different way about it. Jekyll is more opinionated and requires that your blog posts are &lt;em&gt;a)&lt;/em&gt; stored within the &lt;code&gt;_posts&lt;/code&gt; folder and &lt;em&gt;b)&lt;/em&gt; that they follow a very specific naming convention: &lt;code&gt;YEAR-MONTH-DATE-blog_title.MARKUP&lt;/code&gt;, where &lt;code&gt;MARKUP&lt;/code&gt; would be either &lt;code&gt;markdown&lt;/code&gt; or &lt;code&gt;textile&lt;/code&gt;. If you do so, you will be able to use a construct in your templates that will iterate over the posts in reverse order by the date within the file name. For example, to output the list of the last 3 posts you would write:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;{% for post in site.posts limit:3  %}
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;{{ post.title }}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    {{ post.content }}
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;hr/&amp;gt;&lt;/span&gt;
{% endfor  %}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;site.posts&lt;/code&gt; (or &lt;code&gt;paginator.posts&lt;/code&gt; if you &lt;a href=&quot;https://gist.github.com/227621&quot;&gt;enable pagination&lt;/a&gt; in &lt;code&gt;_config.yml&lt;/code&gt;) contains all your posts regardless of the structure you have in place inside the &lt;code&gt;_posts&lt;/code&gt; folder. I think this is nice because it allows you to organize your posts however you think it's best for you. &lt;br/&gt;
Variables added to the &lt;strong&gt;YAML Front Matter&lt;/strong&gt; are available to you through the &lt;code&gt;post&lt;/code&gt; variable. I am making use of this feature in the example website to show how one could provide an abstract of a post through the &lt;code&gt;post.abstract&lt;/code&gt; atom.&lt;/p&gt;

&lt;p&gt;In contrast, Hyde doesn't really care about any particular structure. From within any file you can enumerate its siblings and children and even access its ancestor (parent folder). &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Templating-Guide&quot;&gt;The templating guide&lt;/a&gt; has a good deal more information, but you'll deal with the following variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;page&lt;/code&gt; variable, which refers to the current page; for example, within &lt;code&gt;index.html&lt;/code&gt;, &lt;code&gt;page&lt;/code&gt; refers to the &lt;code&gt;index.html&lt;/code&gt; file;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;page.node&lt;/code&gt; refers to the directory in which &lt;code&gt;page&lt;/code&gt; exists (&lt;code&gt;./content/&lt;/code&gt; in the case of &lt;code&gt;./content/index.html&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;&lt;code&gt;page.node.walk&lt;/code&gt; provides a list of all the children, depth first, of the &lt;code&gt;node&lt;/code&gt;, the &lt;code&gt;page&lt;/code&gt; itself included as the first node.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;page.node.walk_pages&lt;/code&gt; provides the same except it excludes the folders.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;In practice, you could use Hyde, with its multiple &quot;dynamic&quot; sources, to build any kind of websites (for example a documentation website), whereas Jekyll, with it's single &quot;dynamic&quot; source, seems heavily slanted towards blogging.&lt;br/&gt;
However, this comes at a price: for simple types of websites working with Hyde tends to require more effort. Examine the equivalent of the Jekyll &lt;code&gt;index.html&lt;/code&gt; posted above:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;{% extends &amp;quot;base.html&amp;quot; %}
{% block article %}
    {% for node in page.node.walk %}
    {% if node.name == &amp;quot;posts&amp;quot; %}
        {% for post in node.walk_pages %}
        {% if not post.listing and not post.exclude %}
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;{{ post.title }}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
            {% render_article post %}
        {% endif %}
        {% endfor %}
    {% endif %}
    {% endfor %}
{% endblock %}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Considerably more involved, I would think. Also one of the reasons I started writing this post and providing the sample website: the lack of Hyde documentation beyond laconic enumeration of the features. Some of the &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki&quot;&gt;documentation&lt;/a&gt; mentions inexistent examples, and overall it just makes it unnecessarily hard to figure out even some of the basic details on how you are supposed to be using the engine.&lt;/p&gt;

&lt;p&gt;Both engines support tags or categories for posts, and they define them the same way: in the &lt;strong&gt;YAML Front Matter&lt;/strong&gt; for Jekyll and the &lt;code&gt;{% hyde %}&lt;/code&gt; block for Hyde. You can access these categories in Jekyll through the &lt;code&gt;site.categories&lt;/code&gt; variable, which collects all the categories defined in all posts. Hyde requires that you first &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Site-Preprocessors&quot;&gt;configure a site pre-processor&lt;/a&gt; which will enable category access through the &lt;code&gt;categories&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Part of the templating language, both Jekyll and Hyde allow you to include other files (my example includes a &lt;code&gt;nav.html&lt;/code&gt; file) using the &lt;code&gt;{% include FILE %}&lt;/code&gt;; less obvious is that Jekyll looks for these files in the &lt;code&gt;_includes&lt;/code&gt; folder, whereas Hyde looks for it in the &lt;code&gt;content&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Read through Hyde's &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Templating-Guide&quot;&gt;templating guide&lt;/a&gt; to discover more about what the &lt;code&gt;page&lt;/code&gt;, &lt;code&gt;page.node&lt;/code&gt;, and &lt;code&gt;site&lt;/code&gt; variables offer. The Jekyll equivalent can be found on the &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/Template-Data&quot;&gt;template data&lt;/a&gt; wiki page.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;misc&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Miscellaneous Features and Considerations&lt;/h2&gt;

&lt;p&gt;Both engines come with a &lt;strong&gt;built-in web server&lt;/strong&gt; that allows you to quickly check how the generated content will look like. Jekyll's is based on WEBrick and Hyde's on CherryPy, so you will need to have the respective gem or egg installed. One of the features that makes Jekyll stand out is that it combines the server with a directory watcher which will continuously regenerate the files that have changed (run &lt;code&gt;jekyll --auto --server&lt;/code&gt;), whereas for Hyde you will need to run the regen command each time you change a file by running &lt;code&gt;python /path/to/hyde.py -g&lt;/code&gt;.  &lt;br/&gt;
In practice, this is an incredible time saver and Jekyll processes only the files that change so it is very, very quick. To give you an idea how fast this process is, consider that I have MacVim set to save the files whenever the window loses focus; I can &lt;code&gt;Cmd-Tab&lt;/code&gt; to the browser window and by the time I get to issue the refresh page command, the changed file has already been processed.&lt;/p&gt;

&lt;p&gt;To get the same functionality, as I was working through the sample website for Hyde, I ended up backgrounding the webserver (started with &lt;code&gt;python /path/to/hyde.py -w&lt;/code&gt;), and executing the regen command each time I would need to see the output. Unfortunately, Hyde seems to reprocess the entire website; it still is fast because the site is small, but it's something to consider when selecting your engine.&lt;/p&gt;

&lt;p&gt;When it comes to &lt;strong&gt;extensibility&lt;/strong&gt;, Hyde offers a good deal of built-in &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Site-Preprocessors&quot;&gt;site&lt;/a&gt;, &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Media-Processors&quot;&gt;media&lt;/a&gt;, and &lt;a href=&quot;https://github.com/lakshmivyas/hyde/wiki/Content-Processors&quot;&gt;content&lt;/a&gt; pre-processors and the vast library of &lt;a href=&quot;http://djangosnippets.org/&quot;&gt;Django snippets&lt;/a&gt;, whereas Jekyll relies on &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/Plugins&quot;&gt;plugins&lt;/a&gt; (a &lt;a href=&quot;https://github.com/josegonzalez/josediazgonzalez.com/tree/master/_plugins&quot;&gt;good collection&lt;/a&gt; of simple plugins written/compiled by Jose Gonzalez) to augment its functionality and to provide supplemental tags for Liquid.&lt;/p&gt;

&lt;p&gt;Another factor to consider is whether you have &lt;strong&gt;an existing blog&lt;/strong&gt;. If it runs on &lt;strong&gt;Wordpress&lt;/strong&gt;, you're in luck: both Jekyll and Hyde offer &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/Blog-Migrations&quot;&gt;migrators&lt;/a&gt; / &lt;a href=&quot;http://stevelosh.com/blog/2010/01/moving-from-django-to-hyde/&quot;&gt;importers&lt;/a&gt;. The edge goes to Jekyll whose &lt;a href=&quot;https://github.com/mojombo/jekyll/wiki/Blog-Migrations&quot;&gt;migrator list&lt;/a&gt;  is quite extensive: &lt;strong&gt;Drupal&lt;/strong&gt;, &lt;strong&gt;Movable Type&lt;/strong&gt;, &lt;strong&gt;Typo 4&lt;/strong&gt;, &lt;strong&gt;TextPattern&lt;/strong&gt;, &lt;a href=&quot;http://coolaj86.info/articles/migrate-from-blogger-to-jekyll.html&quot;&gt;Blogger/Blogspot&lt;/a&gt;, and all else failing, you have good ol' CSV.&lt;/p&gt;

&lt;p&gt;And finally, &lt;strong&gt;deploying your site&lt;/strong&gt; to its final location is an exercise in simply copying the output folder, perhaps using a tool like &lt;code&gt;rsync&lt;/code&gt; to speed it up. Hyde has an interesting project in &lt;a href=&quot;https://github.com/lakshmivyas/hyde/blob/master/clydeweb/README.markdown&quot;&gt;Clyde&lt;/a&gt; which is an web editor for your pages, which would allow you to edit your site untethered from the computer on which you normally edit your site. As of right now, &lt;strong&gt;Clyde&lt;/strong&gt; is not fully complete or stable, but that might be something that would sway your vote, so I thought you should know.&lt;/p&gt;

&lt;p&gt;&lt;A name=&quot;tldr&quot;&gt; &lt;/A&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Jekyll is considerably easier to get up and run with, has &lt;em&gt;plenty&lt;/em&gt; of examples, a strong presence in github behind it, and where it's lacking it can be extended with plugins. That Liquid is such a close implementation of the Django templating language is both a blessing and a curse: a blessing because you can look at existing Django examples, and a curse because not all those examples will work, and because you will not be able to take advantage of the large library of filters and tags available for Django.&lt;/p&gt;

&lt;p&gt;Hyde on the other hand benefits immediately from the power of the Django templating language, the considerate flexibility that comes with it, as well as most of the tags and filters available through the &lt;a href=&quot;http://djangosnippets.org/&quot;&gt;Django snippets&lt;/a&gt; library. It takes  more effort to get it up and develop your initial wireframe, and there is not much help if you get off the beaten path, but those costs might be offset if you require the more advanced features Hyde provides, including the one possibly killer features in the &lt;a href=&quot;https://github.com/lakshmivyas/hyde/blob/master/clydeweb/README.markdown&quot;&gt;Clyde&lt;/a&gt; web editor, once it's complete and stable.&lt;/p&gt;

&lt;p&gt;If you're considering running your website off github's infrastructure, choosing Jekyll is pretty much a no-brainer. If you run it on your own infrastructure, I hope this post and the &lt;a href=&quot;https://github.com/philipmat/jekyll_vs_hyde&quot;&gt;accompanying sample&lt;/a&gt; gave you enough information to make an informed guess about the direction you want to pursue.&lt;/p&gt;

&lt;p&gt;I the end I chose &lt;a href=&quot;https://github.com/mojombo/jekyll&quot;&gt;Jekyll&lt;/a&gt;. Either way, have fun.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>

