<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Richard Dingwall</title>
	
	<link>http://richarddingwall.name</link>
	<description>The adventures of a young kiwi software developer in London</description>
	<lastBuildDate>Wed, 09 May 2012 12:32:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/RichardDingwall" /><feedburner:info uri="richarddingwall" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Deferring starts with Castle’s StartableFacility</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/rbd0BR4srGY/</link>
		<comments>http://richarddingwall.name/2012/05/09/deferring-starts-with-castles-startablefacility/#comments</comments>
		<pubDate>Wed, 09 May 2012 12:32:10 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Castle Windsor]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4490</guid>
		<description><![CDATA[If you&#8217;re using Castle Windsor as your IoC container, the StartableFacility is great simple way to start up services like timers, socket listeners, etc that run for the duration of your application and require two-step initialization &#8212; i.e., constructing them then starting them. All you have to do is implement an IStartable interface, with a [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using Castle Windsor as your IoC container, the <a href="http://docs.castleproject.org/Windsor.Startable-Facility.ashx">StartableFacility</a> is great simple way to start up services like timers, socket listeners, etc that run for the duration of your application and require two-step initialization &#8212; i.e., constructing them <strong>then</strong> starting them. All you have to do is implement an IStartable interface, with a Start() and Stop() method and Castle will do the rest for you.</p>
<p>The StartableFacility will start each service eagerly by default, as soon as all of its dependencies have been registered in the container. But what if your services take a long time to start up, or you want more control over the time at which they start?</p>
<p>One option is to use the facility&#8217;s <a href="http://docs.castleproject.org/Windsor.Startable-Facility.ashx#Deferred_mode_-_Optimized_for_single_call_to_Install__10">built-in Deferred Mode</a> that waits until <em>all</em> components have been registered before starting any services.</p>
<p>If that is too early however, you can register all your IStartable components, but not drop the StartableFacility into the container until you want to start them. It will immediately start them all.</p>
<pre class="brush: csharp; title: ; notranslate">public void RegisterServices()
{
    // register startable services
    container.Register(Component.For...));
}

public void StartServices()
{
    // drop this in when you're ready to start them
    container.AddFacility&lt;StartableFacility&gt;();
}</pre>
<p>If you need even <em>more</em> fine-grain control, don&#8217;t register the facility at all &#8212; just loop through all your IStartable components and call Start() on them manually.</p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/rbd0BR4srGY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/05/09/deferring-starts-with-castles-startablefacility/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/05/09/deferring-starts-with-castles-startablefacility/</feedburner:origLink></item>
		<item>
		<title>Recording the git commit hash in your assembly with TeamCity</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/SBGxhMyQFZo/</link>
		<comments>http://richarddingwall.name/2012/04/30/recording-the-git-commit-hash-in-your-assembly-with-teamcity/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 09:21:52 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[TeamCity]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4897</guid>
		<description><![CDATA[If you&#8217;re using SVN, you can very easily configure TeamCity to insert the commit revision number into the assembly by using the built-in AssemblyInfo Patcher with a pattern like 1.0.0.%build.vcs.number%. This works because SVN and .NET both use integers for their version numbers. Git, on the other hand, uses a 40-character SHA-1 string as a [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using SVN, you can very easily configure TeamCity to insert the commit revision number into the assembly by using the built-in <a href="http://confluence.jetbrains.net/display/TCD7/AssemblyInfo+Patcher">AssemblyInfo Patcher</a> with a pattern like <code>1.0.0.%build.vcs.number%</code>. This works because SVN and .NET both use integers for their version numbers.</p>
<p>Git, on the other hand, uses a <a href="http://book.git-scm.com/1_the_git_object_model.html">40-character SHA-1 string</a> as a unique ID for commits. As you can see, this will <em>not</em> fit in a <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx">.NET assembly version</a>:</p>
<pre class="brush: plain; title: ; notranslate">
Example git commit: c9d183c8570143142ca61c555360e7f0732efc09
Example git commit (short version): c9d183c857
Example .NET assembly version: 1.2.3.4
</pre>
<p>One place it will fit is the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.assemblyinformationalversionattribute.aspx">AssemblyInformationalVersion</a> attribute, which accepts any string (thanks Joshka for the tip!). Here&#8217;s a quick MSBuild script to do it (using the <strong>AssemblyInfo</strong> task from <a href="http://msbuildtasks.tigris.org/">MSBuild Community Tasks</a>).</p>
<pre class="brush: xml; title: ; notranslate">
&lt;Project ToolsVersion=&quot;4.0&quot; xmlns=&quot;http://schemas.microsoft.com/developer/msbuild/2003&quot;&gt;
  &lt;PropertyGroup&gt;
    &lt;MSBuildCommunityTasksPath&gt;packages\MSBuildTasks.1.3.0.528\lib\net20&lt;/MSBuildCommunityTasksPath&gt;

    &lt;!-- Use the short version of the git hash. --&gt;
    &lt;GitCommitShortHash&gt;$(build_vcs_number.Substring(0, 7))&lt;/GitCommitShortHash&gt;
  &lt;/PropertyGroup&gt;

  &lt;Import Project=&quot;$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets&quot;/&gt;

  &lt;Target Name=&quot;GenerateAssemblyInfo&quot;&gt;
    &lt;AssemblyInfo CodeLanguage=&quot;CS&quot;
      OutputFile=&quot;MyApp\Properties\AssemblyInfo.cs&quot;
      AssemblyInformationalVersion=&quot;$(GitCommitShortHash)&quot; /&gt;
  &lt;/Target&gt;
&lt;/Project&gt;
</pre>
<p>Then in C# you can use something like the following to display it on startup or your About page:</p>
<pre class="brush: csharp; title: ; notranslate">public void Main()
{
    var gitCommit = Assembly.GetExecutingAssembly()
        .GetCustomAttributes(inherit: false)
        .OfType&lt;AssemblyInformationalVersionAttribute&gt;()
        .Single().Configuration;

    Console.WriteLine(&quot;My App starting up (git commit {0})&quot;, gitCommit);

    ...
}</pre>
<p>(Note Josh Flanagan has an alternative approach where he finds a way to <a href="http://lostechies.com/joshuaflanagan/2010/04/08/adding-git-commit-information-to-your-assemblies/">convert the git commit into an int</a> so you can use it in the AssemblyVersion).</p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/SBGxhMyQFZo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/04/30/recording-the-git-commit-hash-in-your-assembly-with-teamcity/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/04/30/recording-the-git-commit-hash-in-your-assembly-with-teamcity/</feedburner:origLink></item>
		<item>
		<title>Parallel vs serial javascript async tests</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/mb4Jt-JX_9A/</link>
		<comments>http://richarddingwall.name/2012/04/28/parallel-vs-serial-javascript-async-tests/#comments</comments>
		<pubDate>Sat, 28 Apr 2012 17:05:41 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4904</guid>
		<description><![CDATA[If you&#8217;re writing tests for a javascript web application, sooner or later you&#8217;ll need to be aware of whether you are using a parallel or serial test runner. Parallel Serial How it works Start all async tests at once and wait until they have all completed. Wait for each test to complete before starting the [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re writing tests for a javascript web application, sooner or later you&#8217;ll need to be aware of whether you are using a <strong>parallel</strong> or <strong>serial</strong> test runner.</p>
<table class="robustness-examples">
<thead style="text-align: center; font-weight: bold">
<tr>
<td width="33%"></td>
<td width="33%">Parallel</td>
<td width="33%">Serial</td>
</tr>
</thead>
<tbody>
<tr>
<td>How it works</td>
<td>Start all async tests at once and wait until they have all completed.</td>
<td>Wait for each test to complete before starting the next one.</td>
</tr>
<tr>
<td>Implications</td>
<td>Callbacks execute in the order they return so race conditions are possible.</td>
<td>Tests are isolated, but take much longer to run.</td>
</tr>
<tr>
<td>Examples</td>
<td><a href="http://pivotal.github.com/jasmine/">Jasmine</a>, <a href="http://vowsjs.org/">Vows</a></td>
<td><a href="http://docs.jquery.com/QUnit">QUnit</a>, <a href="http://visionmedia.github.com/mocha/">Mocha</a></td>
</tr>
</tbody>
</table>
<p>I discovered this last week while porting all our tests to a different framework. Under QUnit, everything was green, but under Jasmine, most of the end-to-end tests (that exercise our whole app, simulating user actions via the DOM down to a live API and back) were failing with all sorts of weird errors.</p>
<p>The problem? Unlike our regular tests, the end-to-end tests run the whole application at once, using the <em>entire</em> DOM, plus a globally-scoped event aggregator.</p>
<p>In QUnit all the tests ran fine, because they were completely isolated from one another. But in Jasmine they were running concurrently, all racing to update the DOM at the same time, and publishing all sorts of events that were being picked up by other tests. We fixed the problem by switching to Mocha instead&#8230; but definitely something to watch out for!</p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/mb4Jt-JX_9A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/04/28/parallel-vs-serial-javascript-async-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/04/28/parallel-vs-serial-javascript-async-tests/</feedburner:origLink></item>
		<item>
		<title>Turning a case sensitive string into a non-case sensitive string</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/9LWvah4ZXS4/</link>
		<comments>http://richarddingwall.name/2012/04/02/turning-a-case-sensitive-string-into-a-non-case-sensitive-string/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 22:55:55 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4846</guid>
		<description><![CDATA[Here&#8217;s a trick I recently picked up when dealing with document databases. Say you need to save objects that have IDs that only differ by case, but you&#8217;re using a document DB like Raven where keys are not case sensitive. In Google Books for example, oT7wAAAAIAAJ is an article in Spanish from a Brazilian journal, [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a trick I recently picked up when dealing with document databases. Say you need to save objects that have IDs that only differ by case, but you&#8217;re using a document DB like Raven where keys are not case sensitive. In Google Books for example, <a href="https://play.google.com/store/books/details?id=oT7wAAAAIAAJ">oT7wAAAAIAAJ</a> is an article in Spanish from a Brazilian journal, but <a href="https://play.google.com/store/books/details?id=OT7WAAAAIAAJ">OT7WAAAAIAAJ</a> is a book about ghosts. RavenDB would not be able to recognize that these are two different IDs &#8212; so attempting to store them would result in a single document that gets overwritten each time. What can you do?</p>
<p>If it were the other way around &#8212; database is case sensitive, app is not &#8212; simply discarding the case information by converting everything to a common lowercase representation (a lossy transformation) would do the trick.</p>
<p>Our situation is a bit harder, however. We somehow need to represent the key as a string including each letter <em>and</em> also store whether it was uppercase or not. You could write a custom converter for this (maybe using special escape characters to indicate uppercase letters)&#8230; but a much easier way would be simply to convert it to <a href="http://en.wikipedia.org/wiki/Base32">Base32</a>.</p>
<p>Why Base32? Using Base64 would produce shorter strings (more efficient encoding), but but it encodes data using both upper and lower case characters, so you are still at risk of collisions. Base32 on the other hand only uses uppercase, so it is safe to use for a case-insensitive key-value store.</p>
<p>Hex would work too (only uppercase characters from A-F), but it would need even more space to do so.</p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/9LWvah4ZXS4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/04/02/turning-a-case-sensitive-string-into-a-non-case-sensitive-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/04/02/turning-a-case-sensitive-string-into-a-non-case-sensitive-string/</feedburner:origLink></item>
		<item>
		<title>Mini book review: Recipes With Backbone</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/2rHW4UvmqAU/</link>
		<comments>http://richarddingwall.name/2012/03/15/mini-book-review-recipes-with-backbone/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 15:30:17 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Backbone.js]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4785</guid>
		<description><![CDATA[Disclaimer: this is my first-ever book review on this blog! I&#8217;m not a javascript/HTML developer by trade so it&#8217;s not going to be a critical review or anything &#8212; just how I found reading it. I&#8217;ve worked on a few web applications over the years, and I&#8217;m familiar with at least a couple of other [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://recipeswithbackbone.com/"><img src="http://richarddingwall.name/wp-content/uploads/2012/03/recipes-with-backbone.jpg" alt="" title="Recipes With Backbone" width="200" height="257" class="alignright size-full wp-image-4813" /></a></p>
<p><em><strong>Disclaimer:</strong> this is my first-ever book review on this blog! I&#8217;m not a javascript/HTML developer by trade so it&#8217;s not going to be a critical review or anything &#8212; just how I found reading it.</em></p>
<p>I&#8217;ve worked on a few web applications over the years, and I&#8217;m familiar with at least a couple of other MVC-style frameworks, but it&#8217;s all been .NET, and recently, all on the desktop. Only in the past few months have I attempted my first full-on browser-based javascript application, using the fantastic <a href="http://documentcloud.github.com/backbone/">Backbone.js</a>. The journey so far has been pretty bumpy, but I&#8217;m finally getting to the point where I&#8217;m comfortable enough with Backbone (and the javascript language itself) to really start being productive. But recently I&#8217;ve been starting to hit some complex situations that I&#8217;m not sure how to implement, and am struggling to find online. As well, I&#8217;m always on the look out for tips confirming the code I&#8217;ve already written is on the right track.</p>
<p>I stumbled across <a href="http://recipeswithbackbone.com/"><strong>Recipes With Backbone</strong></a> book by chance from a blog post by one of <a href="http://japhr.blogspot.com/">the authors</a>, where he was discussing how to implement default routes. I was a bit hesitant at first when I couldn&#8217;t find many reviews of it, but &#8216;advanced Backbone&#8217; is a pretty new subject area (even to find on blogs) and it was cheap so I bought a copy. I am glad to say I was very satisfied with my purchase.</p>
<p>The book has 152 pages, and it took me a day to read on my Kindle. It covers:</p>
<ol>
<li>Writing Client Side Apps (Without Backbone)</li>
<li>Writing Backbone Applications</li>
<li>Namespacing</li>
<li>Organizing with Require.js</li>
<li>View Templates with Underscore.js</li>
<li>Instantiated View</li>
<li>Collection View (Excerpt)</li>
<li>View Signature</li>
<li>Fill-In Rendering</li>
<li>Actions and Animations</li>
<li>Reduced Models and Collections</li>
<li>Non-REST Models</li>
<li>Changes Feed (Excerpt)</li>
<li>Pagination and Search</li>
<li>Constructor Route</li>
<li>Router Redirection</li>
<li>Evented Routers</li>
<li>Object References in Backbone</li>
<li>Custom Events</li>
<li>Testing with Jasmine</li>
</ol>
<p>The book is based around a hypothetical online calendar application, analogous to Google Calendar. I use GCal a lot so it was nice to have familiar examples &#8212; some books use made-up applications that are unfamiliar or too vague (even a blog or a todo list could be designed in many different ways). But when examples are based based off a specific real-world application like GCal or Twitter then you already <em>know</em> how it&#8217;s supposed to behave and it simply becomes a matter of mapping that behaviour to the code examples on the page.</p>
<p>The book is structured as a series of enhancements to the calendar &#8212; starting with a plain jQuery $.getJSON() on a server-generated HTML page, just like we used to write in 2008. This was a good familiar starting point for me, before leaping into a basic Backbone structure and then refactoring it and adding more advanced behaviour.</p>
<p>The twenty chapters are each presented in a problem: solution format. They are very brief but I really liked this &#8212; they are clear, succinct, get straight to the point, and are very readable. Code is explained in 3-4 line chunks at a time, which fit well on my Kindle (which has a small screen and no colors). Overall I thought it was very good value for time spent reading.</p>
<p>For me, the book was valuable because:
<ul>
<li>It corrected some things I thought I already knew :) like $(el) injection</li>
<li>It showed patterns for dealing with things like view <strong>deactivation</strong> and dangling event references (as a WPF developer this stuff gives me nightmares)</li>
<li>It showed how simply and elegantly difficult things like paging could be implemented</li>
<li>It suggested some neat things I hadn&#8217;t even thought about yet e.g. constructor routing</li>
</ul>
<p>The book also includes a chapter on Require.js, which it presents early on as a foundation &#8212; chapter four &#8212; right after namespacing. I&#8217;m a fan of require.js and have no doubt it will soon become a standard requirement for any serious JS development in future. But right now it&#8217;s not widely supported and &#8212; in my experience &#8212; can cause a lot more problems than it solves. Unless you&#8217;re totally comfortable accepting the fact you&#8217;ll probably have to hack a lot of third party jQuery plugins just to make them work, I would not recommend require.js to beginners until the rest of the community catches up.</p>
<p>Regardless, Recipes With Backbone is still the single best text I&#8217;ve read on Backbone (aside from the Backbone docs themselves), and I&#8217;m looking forward to seeing what else these guys write. I would definitely recommend it to anyone wanting to take their Backbone skills to the next level. Go check it out and read the sample chapters here: <a href="http://recipeswithbackbone.com/">http://recipeswithbackbone.com</a></p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/2rHW4UvmqAU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/03/15/mini-book-review-recipes-with-backbone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/03/15/mini-book-review-recipes-with-backbone/</feedburner:origLink></item>
		<item>
		<title>Returning camelCase JSON from ASP.NET Web API</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/Zs79DRogmSE/</link>
		<comments>http://richarddingwall.name/2012/03/10/returning-camelcase-json-from-asp-net-web-api/#comments</comments>
		<pubDate>Sat, 10 Mar 2012 20:09:13 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[ASP.NET Web API]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4768</guid>
		<description><![CDATA[Loving ASP.NET Web API but not loving the .NET-centric PascalCase JSON it produces? Luckily this is quite easy to fix with a custom formatter. I started with Henrik Nielsen&#8217;s custom Json.NET formatter and changed it slightly to use a camelCase resolver and also indent the JSON output (just for developers; we would turn this off [...]]]></description>
			<content:encoded><![CDATA[<p>Loving ASP.NET Web API but not loving the .NET-centric PascalCase JSON it produces?</p>
<pre class="brush: csharp; title: ; notranslate">
// a .NET class like this...
public class Book
{
    public int NumberOfPages { get; set; }
    public string Author { get; set; }
}</pre>
<pre class="brush: jscript; title: ; notranslate">
// ... should be serialized into JSON like this
{
    &quot;numberOfPages&quot;: 42,
    &quot;author&quot;: &quot;JK Rowling&quot;
}</pre>
<p>Luckily this is quite easy to fix with a custom formatter. I started with <a href="http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx">Henrik Nielsen&#8217;s custom Json.NET formatter</a> and changed it slightly to use a camelCase resolver and also indent the JSON output (just for developers; we would turn this off once the app was deployed). You can grab the code here: <strong><a href="https://gist.github.com/2012642">https://gist.github.com/2012642</a></strong></p>
<p>Then just swap out the default JSON formatter in your global.asax.cs:</p>
<pre class="brush: csharp; title: ; notranslate">var config = GlobalConfiguration.Configuration;

// Replace the default JsonFormatter with our custom one
var index = config.Formatters.IndexOf(config.Formatters.JsonFormatter);
config.Formatters[index] = new JsonCamelCaseFormatter();</pre>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/Zs79DRogmSE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/03/10/returning-camelcase-json-from-asp-net-web-api/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/03/10/returning-camelcase-json-from-asp-net-web-api/</feedburner:origLink></item>
		<item>
		<title>RavenDB Includes – much simpler than you think</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/-Fq9mtaBZmo/</link>
		<comments>http://richarddingwall.name/2012/03/08/ravendb-includes-much-simpler-than-you-think/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 15:41:28 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4714</guid>
		<description><![CDATA[Here&#8217;s something I&#8217;ve been struggling to get my head around over the past few days as I&#8217;ve been getting deeper into RavenDB. The example usage from their help page on pre-fetching related documents: That doesn&#8217;t look too difficult at first glance &#8211; it looks pretty similar to futures in NHibernate, which I&#8217;ve used plenty of [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s something I&#8217;ve been struggling to get my head around over the past few days as I&#8217;ve been getting deeper into RavenDB. The example usage from their help page on <a href="http://ravendb.net/docs/client-api/querying/handling-document-relationships">pre-fetching related documents</a>:</p>
<pre class="brush: csharp; title: ; notranslate">var order = session.Include&lt;Order&gt;(x =&gt; x.CustomerId)
    .Load(&quot;orders/1234&quot;);

// this will not require querying the server!
var cust = session.Load&lt;Customer&gt;(order.CustomerId);</pre>
<p>That doesn&#8217;t look too difficult at first glance &#8211; it looks pretty similar to futures in NHibernate, which I&#8217;ve used plenty of times before. But hang on. The first line instructs RavenDB to preload a second object behind the scenes, but how does it know it specifically to be a <code>Customer</code>?</p>
<pre class="brush: csharp; title: ; notranslate">session.Include&lt;Order&gt;(x =&gt; x.CustomerId)</pre>
<p>At first I thought there should be a <code>Customer</code> type argument somewhere. Something like this:</p>
<pre class="brush: csharp; title: ; notranslate">
// WRONG
var order = session.Include&lt;Customer&gt;(...)
    .Load&lt;Order&gt;(...)
</pre>
<p>Otherwise how can RavenDB know I want a <code>Customer</code> and not some other type? Maybe the <code>Order.CustomerID</code> property is actually stored in RavenDB as sort of strongly-typed weak reference to another object? Maybe the <code>order</code> returned is some sort of runtime proxy object with referencing metadata built-in?</p>
<p>No no no. It is <em>much</em> simpler than that. Let&#8217;s take a step back.</p>
<p>In a traditional SQL database, you need both the type (table name) and ID to locate an object. The ID alone is not enough, because two differently typed objects may have the same ID (but in different tables). So you need the type as well.</p>
<p>In RavenDB, you only need the ID. This is possible because RavenDB does not have the concept of types &#8211; under the covers it&#8217;s effectively just one big key-value store of JSON strings which get deserialized <strong>by the client</strong> into different CLR types. Even though the RavenDB .NET client is strongly typed (Customers and Orders), the server has no awareness of the different types stored within it.*</p>
<p>This is what makes includes work. Raven doesn&#8217;t <em>need</em> to know the type of the document being included, it&#8217;s just another chunk of JSON (which could be deserialized as anything). And the ID can only point to exactly one document because all documents are stored in the same single key-value store. So, back to the original example:</p>
<pre class="brush: csharp; title: ; notranslate">// This line instructs RavenDB to preload the JSON document
// that has an ID == x.CustomerId
var order = session.Include&lt;Order&gt;(x =&gt; x.CustomerId)
    .Load(&quot;orders/1234&quot;);

// This line accesses the previously-loaded JSON document and
// deserializes it as a Customer.
var cust = session.Load&lt;Customer&gt;(order.CustomerId);</pre>
<p>That makes <em>much</em> more sense now.</p>
<p><em><span style="font-size: smaller">* actually RavenDB does keep metadata about the CLR type, but for unrelated purposes.</style>
<p></em></p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/-Fq9mtaBZmo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/03/08/ravendb-includes-much-simpler-than-you-think/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/03/08/ravendb-includes-much-simpler-than-you-think/</feedburner:origLink></item>
		<item>
		<title>Yet another reason to love REST</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/Hvmc3lpu4S8/</link>
		<comments>http://richarddingwall.name/2012/03/07/the-real-reason-you-should-love-rest/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 12:20:39 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4683</guid>
		<description><![CDATA[There are a lot of reasons why you should love REST. It&#8217;s fast, simple, stateless, and easy to debug. This makes it absolutely fantastic to test against. REST APIs get you great end-to-end test coverage Line for line, an end-to-end system test covers a lot more code than a deep down class-level unit test. They [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of reasons why you should love REST. It&#8217;s fast, simple, stateless, and easy to debug. This makes it <strong>absolutely fantastic to test against</strong>.</p>
<h4>REST APIs get you great end-to-end test coverage</h4>
<p>Line for line, an end-to-end system test covers a lot more code than a deep down class-level unit test. They also more closely simulate a user&#8217;s actions, providing a realistic path of possible scenarios that need to be verified. So for the most bang for your buck, you want to be testing at the outermost level possible.</p>
<p>The ultimate level here is testing through UI automation &#8212; simulating clicks and looking for responses on screen, as a real user would do. However, although UI automation libraries are improving, UI tests still tend to be very complex to write and are often brittle to things like positioning/layout changes. A public API provides a fantastic alternative &#8216;hooking&#8217; point of entry where application behaviour can be invoked from external code, without involving the UI but still typically mirroring fields and actions on screen pretty closely (if well-designed in order to keep your clients fast and lightweight).</p>
<p>So testing against an API avoids the complexity of UI testing but still give the system a thorough end-to-end workout.</p>
<h4>Tests written against REST APIs aren&#8217;t brittle</h4>
<p>Although the back-end behind your API may change considerably, a public API must continue to work in the exact same way or you&#8217;ll break all your clients and have a lot of very angry users. So as well as covering more lines of code for less effort, tests written against a public API won&#8217;t be as brittle and need updating as often as &#8216;subcutaneous&#8217; ones testing against internal code structures.</p>
<h4>Everyone can connect to a REST API</h4>
<p>If you had to choose a protocol for interacting with your application, you couldn&#8217;t find a much simpler one than REST. Stateless HTTP, verbs, status codes and JSON &#8212; 99% of the time you need nothing but a browser to debug it, and your tests can be written in a completely different language than the API back-end and still be completely understandable.</p>
<p>Compare this to previous &#8216;standard&#8217; HTTP protocols that were often anything-but understandable &#8211; even with things like service discovery and WSDLs (designed to help developers), debugging SOAP XML mismatches between a Java Metro client and a .NET WCF service is one personal hell I hope never to have to relive.</p>
<h4>Not just for big APIs</h4>
<p>Remember you don&#8217;t need a formal public API like Twitter or Facebook to realise the testing benefits mentioned here. Pretty much any sort of REST endpoint will do. The JSON handlers powering your single-page web app would be a great entry point to get started testing underlying behaviour, for example.</p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/Hvmc3lpu4S8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/03/07/the-real-reason-you-should-love-rest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/03/07/the-real-reason-you-should-love-rest/</feedburner:origLink></item>
		<item>
		<title>Quick-and-dirty unique constraints in Raven DB</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/KNudNOnMgFg/</link>
		<comments>http://richarddingwall.name/2012/03/06/quick-and-dirty-unique-constraints-in-raven-db/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 12:17:28 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4636</guid>
		<description><![CDATA[Raven DB, like most NoSQL databases designed to be fast and scale out, does not readily support enforcing uniqueness of fields between documents. If two users must not share the same email address or Facebook ID, you cannot simply add a unique constraint for it. However, Raven DB does guarantee uniqueness in one place: each [...]]]></description>
			<content:encoded><![CDATA[<p>Raven DB, like most NoSQL databases designed to be fast and scale out, does not readily support enforcing uniqueness of fields between documents. If two users must not share the same email address or Facebook ID, you cannot simply add a unique constraint for it.</p>
<p>However, Raven DB <em>does</em> guarantee uniqueness in one place: each document must have a unique ID, or a <code>ConcurrencyException</code> will be thrown. By creating dummy documents with say, an email address for an ID, this feature can be effectively exploited to achieve <a href="http://old.ravendb.net/faq/unique-constraints">unique constraints in Raven DB</a>.</p>
<p>You can easily add this behaviour to your database if you install the Raven DB <a href="http://ravendb.net/docs/server/bundles/unique-constraints">UniqueConstraints Bundle</a>, which will enforce uniqueness on updates as well as inserts. However&#8230; if the field is immutable and you just want something quick and dirty you can use this: <a href="https://gist.github.com/1950991">RavenUniqueInserter.cs</a> :)</p>
<pre class="brush: csharp; title: ; notranslate">using (var session = documentStore.OpenSession())
{
    var user = new User
                   {
                       EmailAddress = &quot;rdingwall@gmail.com&quot;,
                       Name = &quot;Richard Dingwall&quot;
                   };
    try
    {
        new RavenUniqueInserter()
            .StoreUnique(session, user, p =&gt; p.EmailAddress);
    }
    catch (ConcurrencyException)
    {
        // email address already in use
    }
}</pre>
<p>It works by simply wrapping the call to <code>DocumentSession.Store()</code> with another document &#8211; in this case, it would also create a document with ID <code>UniqueConstraints/MyApp.User/rdingwall@gmail.com</code>, guaranteed to be unique.</p>
<p>You can grab it here: <strong><a href="https://gist.github.com/1950991">https://gist.github.com/1950991</a></strong></p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/KNudNOnMgFg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/03/06/quick-and-dirty-unique-constraints-in-raven-db/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/03/06/quick-and-dirty-unique-constraints-in-raven-db/</feedburner:origLink></item>
		<item>
		<title>Fast empty Raven DB sandbox databases for unit tests</title>
		<link>http://feedproxy.google.com/~r/RichardDingwall/~3/UD7xxxNPSbM/</link>
		<comments>http://richarddingwall.name/2012/02/29/fast-empty-raven-db-sandbox-databases-for-unit-tests/#comments</comments>
		<pubDate>Wed, 29 Feb 2012 14:49:05 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[RavenDB]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://richarddingwall.name/?p=4605</guid>
		<description><![CDATA[Say you you have some NUnit/xUnit/Mspec tests that require a live Raven DB instance. Specifically: You do not want your test to be affected by any existing documents, so ideally the Raven DB database would be completely empty. Your test may span multiple document sessions, so doing it all within a single transaction and rolling [...]]]></description>
			<content:encoded><![CDATA[<p>Say you you have some NUnit/xUnit/Mspec tests that require a live Raven DB instance. Specifically:</p>
<ul>
<li>You do not want your test to be affected by any existing documents, so ideally the Raven DB database would be completely empty.</li>
<li>Your test may span multiple document sessions, so doing it all within a single transaction and rolling it back is not an option.</li>
<li>You want your tests to run <strong>fast</strong>.</li>
</ul>
<p>What are your options?</p>
<p>Raven DB currently has no <code>DROP DATABASE</code> or equivalent command. The recommended method is simply to delete Raven DB&#8217;s <code>Server\Data</code> or <code>Server\Tenants</code> directories, but this requires restarting the Raven DB service (expensive). Also any live document stores may throw an exception at this point.</p>
<h4>Multi-tenanting</h4>
<p>One option that Raven DB makes very cheap, however, is spinning up new database instances (aka <a href="http://ayende.com/blog/4707/ravendb-multi-tenancy">tenants</a>). In fact all you need to do is specify a new <code>DefaultDatabase</code> and the document store will spin a new database up for you. For example:</p>
<pre class="brush: csharp; title: ; notranslate">
var store = new DocumentStore
    {
        Url = &quot;http://localhost:8080&quot;,
        DefaultDatabase = &quot;MyAppTests-&quot; + DateTime.Now.Ticks
    };
store.Initialize();

// now you have an empty database!
</pre>
<p>Pretty easy huh? <a href="https://gist.github.com/1940759">Here&#8217;s a little test helper</a> I wrote to help manage these sandbox databases, stores and sessions. Here&#8217;s how you would use it in a tenant-per-fixture test:</p>
<pre class="brush: csharp; title: ; notranslate">[TestFixture]
public class When_doing_something
{
    [TestFixtureSetUp]
    public void SetUp()
    {
        RavenDB.SpinUpNewDatabase();

        using (var session = RavenDB.OpenSession())
        {
            // insert test data
        }
    }

    [Test]
    public void It_should_foo()
    {
        using (var session = RavenDB.OpenSession())
        {
            // run tests
        }
    }
}</pre>
<p>You can grab it here as a gist on Github: <strong><a href="https://gist.github.com/1940759">https://gist.github.com/1940759</a></strong>.</p>
<p>Note that if you use this method, a number of sandbox databases will (of course) build up over time. You can clean these up you by simply deleting the Raven DB data directories. (See gist for an example batch file you can throw in your source control to do this.)</p>
<img src="http://feeds.feedburner.com/~r/RichardDingwall/~4/UD7xxxNPSbM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://richarddingwall.name/2012/02/29/fast-empty-raven-db-sandbox-databases-for-unit-tests/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://richarddingwall.name/2012/02/29/fast-empty-raven-db-sandbox-databases-for-unit-tests/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 1.630 seconds -->

