<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom"><id>urn:yobriefca-se:feed:index</id><updated>Thu Oct 19 00:00:00 UTC 2017</updated><title type="text">Yo! Briefcase: index</title><link rel="self" href="https://yobriefca.se/feed/index.xml"></link><entry><title>Bashing Ambiguity</title><updated>Thu Oct 19 00:00:00 UTC 2017</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2017/10/19/bashing-ambiguity/"></link><id>urn:yobriefca-se:feed:post:Bashing Ambiguity</id><content type="html">&lt;p&gt;I read a great tweet the other year,&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Was hoping to get some verbose output from pkill, ran &lt;code&gt;pkill -9 -v process_name&lt;/code&gt;. Ho boy.  — Maxime Chevalier (@Love2Code) &lt;a href="https://twitter.com/Love2Code/status/778387312002510848"&gt;September 21, 2016&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I can't say I've ever used pkill so had to do a tiny bit of digging through the man pages of &lt;code&gt;pkill&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;PKILL&lt;span class="o"&gt;(&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;                  BSD General Commands Manual                 PKILL&lt;span class="o"&gt;(&lt;/span&gt;1&lt;span class="o"&gt;)&lt;/span&gt;

NAME
     pgrep, pkill -- find or signal processes by name

SYNOPSIS
     pkill &lt;span class="o"&gt;[&lt;/span&gt;-signal&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-ILafilnovx&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-F pidfile&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-G gid&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-P ppid&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-U uid&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-g pgrp&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-t tty&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;-u euid&lt;span class="o"&gt;]&lt;/span&gt; pattern ...

DESCRIPTION

     The pkill &lt;span class="nb"&gt;command &lt;/span&gt;searches the process table on the running system and signals all processes that match the criteria given on the &lt;span class="nb"&gt;command &lt;/span&gt;line.

     The following options are available:

  	 ...

     -v          Reverse the sense of the matching; display processes that &lt;span class="k"&gt;do &lt;/span&gt;not match the given criteria.
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So heh, &lt;code&gt;pkill&lt;/code&gt; is based on &lt;code&gt;pgrep&lt;/code&gt; which is based on &lt;code&gt;grep&lt;/code&gt; and &lt;code&gt;-v&lt;/code&gt; is the shorthand notation of &lt;code&gt;--invert-match&lt;/code&gt; so the previous command becomes kill everything (with extreme prejudice) that isn't named &lt;code&gt;process_name&lt;/code&gt;. &lt;em&gt;Ho boy&lt;/em&gt; indeed. Of course it's an easy mistake to make, I'm certain I would have fallen prey of this eventually had I not seen this tweet. I'm known for simply throwing more than a few &lt;code&gt;-vvvvvvvvvvvvv&lt;/code&gt;'s onto commands that don't behave as I'd expect.&lt;/p&gt;&lt;p&gt;&lt;em&gt;But AHA! I have the solution - just be more explicit and use the &lt;code&gt;--&lt;/code&gt; notation&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Well.... not quite.&lt;/p&gt;&lt;p&gt;For some crazy bizarro reason &lt;code&gt;pkill&lt;/code&gt; and &lt;code&gt;pgrep&lt;/code&gt; don't have the double dash equivalent of &lt;code&gt;v/invert-match&lt;/code&gt;. I'm sure there is a sane and logical rationale for this but then again, given it's computers all the way down, maybe not. &lt;/p&gt;&lt;p&gt;It's shit isn't it? Even the most standard tools we use as developers take the &lt;a href="https://en.wikipedia.org/wiki/Principle_of_least_astonishment"&gt;principle of least astonishment&lt;/a&gt; and just throw it into the endlessly burning tyre fire of misery and terrible user experience.&lt;/p&gt;&lt;p&gt;So what can we do? Easy! &lt;strong&gt;TRUST&lt;/strong&gt;. &lt;strong&gt;NOTHING&lt;/strong&gt;. Never assume anything. Slow down. Think. Think even when it seems obvious. Think even when you've written the same command for the 100th time. If you can't think or don't want to think make sure everything you do isn't irrevocably destructive, make sure you can get back to the same point you where at before you went and destroyed the world in a raging hellfire of accidental typage.&lt;/p&gt;</content></entry><entry><title>How to do unnecessary things in long winded ways...</title><updated>Wed Jun 21 00:00:00 UTC 2017</updated><author><name>James Hughes</name></author><link href="/presentations/clojurescraping.pdf"></link><id>urn:yobriefca-se:feed:post:How to do unnecessary things in long winded ways...</id><content type="html"></content></entry><entry><title>Quick and Dirty JSON Builder for Java</title><updated>Fri Feb 10 00:00:00 UTC 2017</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2017/02/10/quick-and-dirty-json-builder-for-java/"></link><id>urn:yobriefca-se:feed:post:Quick and Dirty JSON Builder for Java</id><content type="html">&lt;p&gt;If you're writing tests involving JSON or trying to return some basic JSON structure in a simple Play or Spark application here is a simple shorthand notation that can be used to build said response.&lt;/p&gt;&lt;p&gt;Given the following JSON structure,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="json"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nt"&gt;"title"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Test Schema"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nt"&gt;"type"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"object"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nt"&gt;"properties"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nt"&gt;"firstName"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nt"&gt;"type"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nt"&gt;"lastName"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nt"&gt;"type"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nt"&gt;"age"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nt"&gt;"description"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Age in years"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nt"&gt;"type"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"integer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nt"&gt;"minimum"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nt"&gt;"required"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"firstName"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"lastName"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It is possible to represent this in Java like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;toJson&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"title"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Test Schema"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"type"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"object"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"properties"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"type"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"string"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
        &lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"type"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"string"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
        &lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"description"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Age in years"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"type"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"integer"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"minimum"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
        &lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;),&lt;/span&gt;
    &lt;span class="s"&gt;"required"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"lastName"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In order to achieve this then 2 static imports must be used,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;static&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;google&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;common&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ImmutableMap&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;of&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;static&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;libs&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toJson&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Obviously, this assume the use of Guava and Play but we can at least avoid the use of Play (i.e. if you're using some other web framework) by shimming the &lt;code&gt;toJson&lt;/code&gt; method using the Jackson &lt;code&gt;ObjectMapper&lt;/code&gt; directly e.g.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;JsonNode&lt;/span&gt; &lt;span class="nf"&gt;toJson&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// either provide or create an ObjectMapper&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ObjectMapper&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;valueToTree&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's nicer than using, and having to wrangle, a massive string blob because it helps avoid some of accidental syntactical JSON errors that you can make (extra commas, unbalanced braces etc.) when dealing with a raw string. It's also a bit terser than using the Jackson builder directly as that can make deep chaining a bit awkward from what I've experienced.&lt;/p&gt;&lt;p&gt;CAVEAT: I use this in tests and basic webapps were performance is not important in the slightest so I haven't attempted to understand the overhead of using this approach.&lt;/p&gt;</content></entry><entry><title>ansible-console: An Interactive REPL for Ansible</title><updated>Tue Jan 10 00:00:00 UTC 2017</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2017/01/10/ansible-console-an-interactive-repl-for-ansible/"></link><id>urn:yobriefca-se:feed:post:ansible-console: An Interactive REPL for Ansible</id><content type="html">&lt;p&gt;If you know about ansible-console you can ignore this.  If you don't this might be of use to you.&lt;/p&gt;&lt;p&gt;Something found out recently is that Ansible has an interactive REPL of sorts in &lt;code&gt;ansible-console&lt;/code&gt; for doing some adhoc things on a collection of hosts.&lt;/p&gt;&lt;p&gt;Using it is pretty simple&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;&amp;gt; ansible-console -i preproduction

Welcome to the ansible console.
Type &lt;span class="nb"&gt;help &lt;/span&gt;or ? to list commands.

jamhughes@all &lt;span class="o"&gt;(&lt;/span&gt;3&lt;span class="o"&gt;)[&lt;/span&gt;f:5&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This will bring you into the shell itself,&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;the &lt;code&gt;-i preproduction&lt;/code&gt; tells the console to use a certain hosts file (in my case preproduction).&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;From here you can use the normal ansible modules.  So for example if I wanted to get the date on all the hosts I can do this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;jamhughes@all &lt;span class="o"&gt;(&lt;/span&gt;3&lt;span class="o"&gt;)[&lt;/span&gt;f:5&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;shell date
frontend1.preproduction | SUCCESS | &lt;span class="nv"&gt;rc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 &amp;gt;&amp;gt;
Tue Jan 10 22:04:18 GMT 2017

frontend2.preproduction | SUCCESS | &lt;span class="nv"&gt;rc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 &amp;gt;&amp;gt;
Tue Jan 10 22:04:18 GMT 2017

backend1.preproduction | SUCCESS | &lt;span class="nv"&gt;rc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 &amp;gt;&amp;gt;
Tue Jan 10 22:04:18 GMT 2017
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you have specific groups you want to work in you can &lt;code&gt;cd&lt;/code&gt; into groups&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;jamhughes@all &lt;span class="o"&gt;(&lt;/span&gt;3&lt;span class="o"&gt;)[&lt;/span&gt;f:5&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;frontend
jamhughes@customer &lt;span class="o"&gt;(&lt;/span&gt;3&lt;span class="o"&gt;)[&lt;/span&gt;f:5&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;shell date
frontend1.preproduction | SUCCESS | &lt;span class="nv"&gt;rc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 &amp;gt;&amp;gt;
Tue Jan 10 22:04:18 GMT 2017

frontend2.preproduction | SUCCESS | &lt;span class="nv"&gt;rc&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 &amp;gt;&amp;gt;
Tue Jan 10 22:04:18 GMT 2017
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I guess it flies in the face of all this immutable infrastructure stuff but hey we are not all there yet and stuff like this can be handy.&lt;/p&gt;</content></entry><entry><title>Tabularizing output of shell scripts with column</title><updated>Fri Jan 06 00:00:00 UTC 2017</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2017/01/06/tabularizing-output-of-shell-scripts-with-column/"></link><id>urn:yobriefca-se:feed:post:Tabularizing output of shell scripts with column</id><content type="html">&lt;p&gt;I tend to live in my terminal and as such tend to write a bunch of shell scripts for various project specific things.  One script I have lists, form a project directory, the what branch each of my repositories is on.  It looks a bit like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;

&lt;span class="nv"&gt;projects&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;
  super-service 
  super-client 
  authentication-service 
  infrastructure    &lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;project in &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;projects&lt;/span&gt;&lt;span class="p"&gt;[*]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="nv"&gt;$project&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"$project $(git branch | sed -n -e 's/^\* \(.*\)/\1/p')"&lt;/span&gt;
  &lt;span class="nb"&gt;cd&lt;/span&gt; ..
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When I run this the output looks like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;super-service master
super-client master
authentication-service experimental
infrastructure master
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The list is longer and the names above aren't real but you see the problem.  It's hard to see at a glance what repos are on what branches.&lt;/p&gt;&lt;h2&gt;Enter &lt;code&gt;column&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;If you pipe the output of the script above in &lt;code&gt;column -t -s' '&lt;/code&gt; the result is a bit different.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;super-service           master
super-client            master
authentication-service  experimental
infrastructure          master
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can see from the output above the &lt;code&gt;column&lt;/code&gt; command pretty much does what is says on the tin.  It turns output into formatted table with discernible columns.  This makes it much easier to read.  The additional parameter I passed where,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;-t&lt;/code&gt; which tells column to read the whole input and determine the number of columns automatically to print the table&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;-s' '&lt;/code&gt; specifies the character to use to determine a column break when using the &lt;code&gt;-t&lt;/code&gt; command.  In this case, we specify whitespace.  In fact whitespace is the default character to break on so this is actually not needed in our case but if each column was separated by a | character we could use &lt;code&gt;-s'|'&lt;/code&gt; to change the default behavior.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Now go forth and prettify those scripts, they're worth it.&lt;/p&gt;</content></entry><entry><title>Functional Collections in Scala (Lightning Talk)</title><updated>Tue Nov 08 00:00:00 UTC 2016</updated><author><name>James Hughes</name></author><link href="/presentations/scalacollections.pdf"></link><id>urn:yobriefca-se:feed:post:Functional Collections in Scala (Lightning Talk)</id><content type="html"></content></entry><entry><title>Kotlin - 2 Years On</title><updated>Wed Feb 24 00:00:00 UTC 2016</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2016/02/24/kotlin-2-years-on/"></link><id>urn:yobriefca-se:feed:post:Kotlin - 2 Years On</id><content type="html">&lt;p&gt;I &lt;a href="https://yobriefca.se/blog/2012/07/14/kotlin-heres-what-i-think-for-now/"&gt;first played&lt;/a&gt; with Kotlin back in 2012, I've written some Android apps with and without it and generally played around with it. Since the &lt;a href="http://blog.jetbrains.com/kotlin/2016/02/kotlin-1-0-released-pragmatic-language-for-jvm-and-android/"&gt;release of Kotlin 1.0&lt;/a&gt; a few days ago I've decided to jump back into it and see if my original views still hold up. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Herein lies opinion of the personal kind. Proceed with an open mind.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Scala is my day job, not only Scala but &lt;strong&gt;legacy/old&lt;/strong&gt; Scala. Not only that but &lt;strong&gt;a lot&lt;/strong&gt; of legacy/old Scala. Worse still not only a lot of legacy/old Scala but a lot of &lt;strong&gt;other peoples&lt;/strong&gt; legacy/old Scala. Thanks to the feature rich and impressively flexible nature of Scala this means there is a lot of sharp edges, unpredictability and a fresh arduous learning curve for each functional area. It can seem like a language designed by &lt;a href="https://en.wikipedia.org/wiki/At%C3%AB"&gt;Atë&lt;/a&gt; at times.&lt;/p&gt;&lt;p&gt;Java is no better of course, it just resides at the other end of a spectrum, with its anaemic yet rigid feature set and almost pointless type system it's no wonder you see so many over-engineered and baklava-class layered solutions. Yeah Java 8 has improved the language a fair bit but the sacrifices made for backwards compatibility has meant those changes don't often go far enough.&lt;/p&gt;&lt;p&gt;Both Scala and Java require a lot of work to simplify so there has to be a middle ground and I (still) think that middle ground is Kotlin.&lt;/p&gt;&lt;p&gt;Think of Kotlin as C# for the JVM, Scala the Good Parts, Java++ or simply a decent general purpose language that won't require the blood of your first born. It's created by Jetbrains, it's about 5ish years old and just gone version 1.0. It is used extensively on Jetbrains products, being hailed as "Swift for Android" and works seamlessly within a mixed code base. It offers features similar to C# Extension methods, Scala implicits, Scala case classes, multiple inheritance and solid collection functionality like LINQ. It doesn't just ape Scala and C# features but introduces relatively original concepts like &lt;a href="http://kotlinlang.org/docs/reference/delegated-properties.html"&gt;delegated properties&lt;/a&gt;, &lt;a href="https://kotlinlang.org/docs/reference/delegation.html#class-delegation"&gt;class delegation&lt;/a&gt; and &lt;a href="https://kotlinlang.org/docs/reference/typecasts.html"&gt;typecasting&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;I'd also proffer that it has an easier transition from Java to Kotlin than it is for Scala though I have no evidence for this other than my own experience of all 3 languages (including leading mixed skill teams in Scala and Java).&lt;/p&gt;&lt;p&gt;You could ask "but why not one of the other many languages available on the JVM?" and that would be fair. Many JVM languages haven't seen a great deal of traction or remain relatively niche but I think Kotlin is much better placed. It's more "general purpose" than a lot of the alt languages which means it will map better to current practises. It's closer to Java and/or Scala than other languages, it strikes the right balance around the type system with features like type inference and enhanced generics and it doesn't hurt that Jetbrains; a major player in the JVM world; is heavily invested in Kotlins future.&lt;/p&gt;&lt;p&gt;In the two years since I started playing with Kotlin it has aged well. It resisted adding a plethora of features for the sake of it and instead created a core syntax and feature set that allows developers enough wiggle room for being creative without turning them into Wizards of Arcane DSLs. This is a sweet spot for me when it comes to building and supporting a service over many years as it avoids the unnecessary pain that comes from the extreme ends of spectrum that Java and Scala tend to reside. Of course this doesn't mean it's perfect, nothing is. You're still adding another language to a project and taking on the associated baggage that comes with it like tooling differences and additional upskilling. Its not suddenly going to make your organisation move the JVM if they aren't already there. It's also not going to fix all your problems - bad architecture will remain bad architecture and bad patterns will remain bad patterns. However, with that said, &lt;strong&gt;I believe Kotlin will reduce enough of the friction of general software development and maintenance on the JVM to make it worth the investment&lt;/strong&gt;.&lt;/p&gt;</content></entry><entry><title>alias, union and case in Crystal</title><updated>Thu Jan 14 00:00:00 UTC 2016</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2016/01/14/alias-union-and-case-in-crystal/"></link><id>urn:yobriefca-se:feed:post:alias, union and case in Crystal</id><content type="html">&lt;p&gt;So I've been diving into Crystal lately. It's nice. This isn't a post about why I think it's nice, it's a post about a few of the features of Crystal so let’s get straight down to it.&lt;/p&gt;&lt;h2&gt;&lt;code&gt;alias&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;&lt;code&gt;alias&lt;/code&gt; is a very common feature that we can see across many languages but it can be used to build upon other features in Crystal. As the name suggests you use &lt;code&gt;alias&lt;/code&gt; to give a type a different name.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="no"&gt;TableOfContents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Int32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the example above we give the type &lt;code&gt;Hash(String, Int32)&lt;/code&gt; a custom alias of &lt;code&gt;TableOfContents&lt;/code&gt; which allows us to refer to this type as &lt;code&gt;TableOfContents&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chapter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tableOfContents&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;TableOfContents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Int32&lt;/span&gt;
  &lt;span class="n"&gt;tableOfContents&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chapter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So instead of using &lt;code&gt;Hash(String, String)&lt;/code&gt; in the method signature above we can use &lt;code&gt;TableOfContents&lt;/code&gt;. So why is this useful? Well for one thing it allows us to reduce complex type names into smaller names. The example above isn't exactly verbose but longer types can be more readable.&lt;/p&gt;&lt;p&gt;Now of course size isn't everything so another advantage of &lt;code&gt;alias&lt;/code&gt; is that we can add meaning to types within our problem domain. You're less likely to wonder what this hash of string to string is or should contain when we give a name like &lt;code&gt;TableOfContents&lt;/code&gt;. In fact, we could enrich the signature above more by giving specific types an alias.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="no"&gt;ChapterTitle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
&lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="no"&gt;PageNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Int32&lt;/span&gt;
&lt;span class="k"&gt;alias&lt;/span&gt; &lt;span class="no"&gt;TableOfContents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ChapterTitle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PageNumber&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;From this we can infer that for a given chapter title the table of contents is capable of finding the page number that the chapter starts on.&lt;/p&gt;&lt;h2&gt;&lt;code&gt;union&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;Unlike many languages the return type of methods can actually be more than one type. Crystal uses type inference so declaring types is not mandatory. Take a look at this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isAThing&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;isAThing&lt;/span&gt;
    &lt;span class="s2"&gt;"I did a thing"&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="mi"&gt;42&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What type is this? It could be an &lt;code&gt;Int32&lt;/code&gt; it could be a &lt;code&gt;String&lt;/code&gt;. In other statically typed languages the type most likely resolves to some common base class like &lt;code&gt;Object&lt;/code&gt; or &lt;code&gt;Any&lt;/code&gt; which makes sense but is actually a tiny bit pants&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;In Crystal this type will be inferred as &lt;code&gt;String|Int32&lt;/code&gt;. This is a union and is essentially a type of its own that could be either a &lt;code&gt;String&lt;/code&gt; or a &lt;code&gt;Int32&lt;/code&gt;. We can explicitly give this method a return type which means the compiler can verify our assumption&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isAThing&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;Bool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You may have noticed I just wrote the wrong return type and attempting to compile the code results in a helpful compilation error message&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;Error in ./src/something.cr:14: instantiating &lt;span class="s1"&gt;'doAThing(Bool)'&lt;/span&gt;

doAThing&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
^

in ./src/something.cr:6: &lt;span class="nb"&gt;type &lt;/span&gt;must be &lt;span class="o"&gt;(&lt;/span&gt;String | Bool&lt;span class="o"&gt;)&lt;/span&gt;, not &lt;span class="o"&gt;(&lt;/span&gt;String | Int32&lt;span class="o"&gt;)&lt;/span&gt;

def doAThing&lt;span class="o"&gt;(&lt;/span&gt;t : Bool&lt;span class="o"&gt;)&lt;/span&gt; : String|Bool
    ^
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Union types give us the rigidity and predictability of types while, at the same time, allowing a certain amount of flexibility as if we were working in a purely dynamic language. &lt;/p&gt;&lt;h2&gt;&lt;code&gt;case&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;So we've got these nice &lt;code&gt;union&lt;/code&gt; types how do we deal with them in code? Well similar to other languages Crystal has methods like &lt;code&gt;is_a?(type_name)&lt;/code&gt; and &lt;code&gt;responds_to?(method_name)&lt;/code&gt; that allows us to inspect the current type of the variable but it also supports a kind of pattern matching for types using &lt;code&gt;case&lt;/code&gt;.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filename&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="no"&gt;Error&lt;/span&gt;
  &lt;span class="c1"&gt;# read file return a string of its contents&lt;/span&gt;
&lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="n"&gt;an_error&lt;/span&gt;
  &lt;span class="n"&gt;an_error&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;maybeContents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"somefile.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;maybeContents&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;maybeContents&lt;/span&gt;
&lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="no"&gt;Error&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"We had a wee error doing that"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In each &lt;code&gt;when&lt;/code&gt; of the &lt;code&gt;case&lt;/code&gt; the type of &lt;code&gt;maybeContents&lt;/code&gt; will be the specific type that was matched against. So &lt;code&gt;maybeContents&lt;/code&gt; will start as a &lt;code&gt;String|Error&lt;/code&gt; but be available as a &lt;code&gt;String&lt;/code&gt; in the first &lt;code&gt;when&lt;/code&gt; and an &lt;code&gt;Error&lt;/code&gt; in the second &lt;code&gt;when&lt;/code&gt;.&lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: "pants" means bad.&lt;/sup&gt;&lt;/p&gt;</content></entry><entry><title>entr</title><updated>Wed Jan 13 00:00:00 UTC 2016</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2016/01/13/entr/"></link><id>urn:yobriefca-se:feed:post:entr</id><content type="html">&lt;p&gt;&lt;a href="http://entrproject.org/"&gt;&lt;code&gt;entr&lt;/code&gt;&lt;/a&gt; is a command line utility for running arbitrary commands when files change.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Just for clarification - I did not contribute to this project. I just happen to find it useful.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Yes, yes you already have all this with guard, live-reload, guardian, watcher, watchy, watch, bouncy, bounce, bouncer, sir-loads-alot, loady, loadup, springy, trampoline, hot-rocket&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; etc. but &lt;a href="http://entrproject.org/"&gt;&lt;code&gt;entr&lt;/code&gt;&lt;/a&gt; is worth a look. Why? Well it has a few criteria that make it compelling,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;It's not locked into a particular use case, language or ecosystem&lt;/li&gt;
  &lt;li&gt;It has no external dependencies&lt;/li&gt;
  &lt;li&gt;It works on more than just Linux&lt;/li&gt;
  &lt;li&gt;It's command line friendly (works with pipes)&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;So lets just see it in action,&lt;/p&gt;&lt;h2&gt;Install it&lt;/h2&gt;&lt;p&gt;You can either build it from &lt;a href="https://bitbucket.org/eradman/entr/"&gt;source&lt;/a&gt; or if you're using a mac you an use brew&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;brew update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; brew install entr
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Use it&lt;/h2&gt;&lt;p&gt;What you want to do is get a list of files you are interested in i.e. ones that should trigger "something" when they change. This can be something as simple as &lt;code&gt;ls&lt;/code&gt; or as convoluted as &lt;code&gt;find . -iname '*.cr' -not -path './.crystal/*'&lt;/code&gt; something like. One way or another a command that spits out a file list is the first part.&lt;/p&gt;&lt;p&gt;Next you pipe this into &lt;code&gt;entr&lt;/code&gt; with the command you want to run.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;ls -d * | entr make
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this case when a file listed by &lt;code&gt;ls&lt;/code&gt; changes &lt;code&gt;make&lt;/code&gt; will be ran.&lt;/p&gt;&lt;h2&gt;Further betterness&lt;/h2&gt;&lt;p&gt;This is good enough for many use cases but there are always edge cases. One good example is live reloading a web server. In this case the process that you spawn typically blocks until killed which would cause issues with the above command. To cater for this use case &lt;code&gt;entr&lt;/code&gt; gives us the &lt;code&gt;-r&lt;/code&gt; flag.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;ls -d * | entr -r lein ring server-headless
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;With the &lt;code&gt;-r&lt;/code&gt; &lt;code&gt;entr&lt;/code&gt; will attempt to terminate then running process prior to starting it again.&lt;/p&gt;&lt;p&gt;We also have the &lt;code&gt;-c&lt;/code&gt; option. This does nothing more than clear the screen before running the command.&lt;/p&gt;&lt;p&gt;Finally, &lt;code&gt;entr&lt;/code&gt; provides the ability to pass a special argument, &lt;code&gt;/_&lt;/code&gt;, into the command it is configured to run. This argument will be replaced with the path to the first file that has changed. This is useful when you specifically need to execute the changed file, for example, when working with data loading scripts,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;ls ./data/*.js | entr mongo &amp;lt; /_
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href="http://entrproject.org/"&gt;&lt;code&gt;entr&lt;/code&gt;&lt;/a&gt; is nice and simple with no external dependencies and not specific to any particular technology stack. &lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: I may have made some or all of these up.&lt;/sup&gt;&lt;/p&gt;</content></entry><entry><title>An example of refactoring Scala code</title><updated>Sat Jan 02 00:00:00 UTC 2016</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2016/01/02/an-example-of-refactoring-scala-code/"></link><id>urn:yobriefca-se:feed:post:An example of refactoring Scala code</id><content type="html">&lt;p&gt;I recently had to add code to an old and critical area of a 2 year old, rather large, Scala project. The code which centred around a core function that accepted an object instance, performed various actions that ultimately updated/transformed some properties on the instance. The passed in instance, a case class, used immutable values for all its properties so in order to apply these additional transforms the function created additional internal values after each transformation. Finally, it would return the final updated instance. &lt;/p&gt;&lt;p&gt;My work involved introducing yet another step in this series of transformations so I decided to play with various ways of achieving the same thing to see if there was a better way of approaching the problem.&lt;/p&gt;&lt;h2&gt;Set the scene&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;The following code is contrived for the purposes of this article. It may seem overly simple but I didn't want to bog down the examples with more than necessary.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;So let's set the scene using a stripped down example.&lt;/p&gt;&lt;p&gt;First of all, we have a primary &lt;code&gt;Order&lt;/code&gt; model. An order instance is created and persisted once the customer has completed the order and This has a number of optional properties that will be set as the order is processed through various states (new order, order completed, order paid etc.)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;paymentMethod&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;requisitionRequest&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;RequisitionRequest&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; 
    &lt;span class="n"&gt;invoice&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Invoice&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;paymentId&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; 
    &lt;span class="n"&gt;fulfilled&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;processedTimestamp&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;PaymentMethod&lt;/code&gt; is a trait that a number of case objects extend,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethods&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Cash&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;CreditCard&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;RequisitionRequest&lt;/code&gt; and &lt;code&gt;Invoice&lt;/code&gt; are empty case classes that act as stubs. Their content isn't really important for these examples.&lt;/p&gt;&lt;p&gt;In our example we have a core function that will process an order after payment has been completed.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
	&lt;span class="o"&gt;???&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This function should perform a number of things&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;It should submit the order to the stock room and update the order with a &lt;code&gt;requisitionRequest&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;If the order is paid by cash it should generate a new invoice and add this to the order (customer has not yet actually paid but agreed to pay)&lt;/li&gt;
  &lt;li&gt;If the order is paid by anything other than cash then it should retrieve the payment ID from the payment system and update the order&lt;/li&gt;
  &lt;li&gt;Once these things have completed it should mark the order as fulfilled, and finally,&lt;/li&gt;
  &lt;li&gt;It should update the order with the processed date.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;We can set up some stub functions,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;submitToStockroom&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requisitionRequest&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RequisitionRequest&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;generateInvoice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;invoice&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Invoice&lt;/span&gt;&lt;span class="o"&gt;()))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paymentId&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"payment"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;markOrderProcessed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processedTimestamp&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1451468923165L&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let's assume that we are adding the additional functionality of updating the &lt;code&gt;processedTimestamp&lt;/code&gt; as this was, for some reason, not being done before.&lt;/p&gt;&lt;h2&gt;The current solution&lt;/h2&gt;&lt;p&gt;The existing solution looked a bit like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;submittedOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submitToStockroom&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;updatedOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submittedOrder&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="nd"&gt;@Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethods&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cash&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;generateInvoice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;updatedOrder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilled&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;After a first pass of adding the new functionality of adding the processed timestamp it looked like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;submittedOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submitToStockroom&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;updatedOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submittedOrder&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="nd"&gt;@Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethods&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cash&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; 
      &lt;span class="n"&gt;generateInvoice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; 
      &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;fulfilledOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updatedOrder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilled&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;markOrderProcessed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilledOrder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;My first pass was to follow a similar pattern of assigning an updated version of the order to a &lt;code&gt;val&lt;/code&gt; and subsequently pass that to the new &lt;code&gt;markOrderProcessed&lt;/code&gt; function.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I think it's worth pointing out again that, yes, there are ways to make even this style a bit terser but instead focus on the overall style rather than the specifics of this example&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;There are a few things that bugged me about this approach. While it was consistent and rather easy to update it did cause some issues.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The actual code was more complex and it took some understanding where to place the actual code. Subsequent changes would further increase this complexity.&lt;/li&gt;
  &lt;li&gt;Coming up with a meaningful name for the new value was frustrating and I simply refuse to just append a number to a values name.&lt;/li&gt;
  &lt;li&gt;I introduced a bug because at one point I referenced the wrong transient value.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Beside the bug that, thankfully, a unit test flagged up none of this is exactly show stopping but I was curious if we could improve this function.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;One more caveat. These examples are not necessarily &lt;strong&gt;better&lt;/strong&gt;. This is not an article about gradual refactoring to a better solution. Each example is an alternative way of expressing the same problem and each example has its flaws. Consistency of approach across code is a godsend when trying to dive into legacy code.&lt;/p&gt;
&lt;/blockquote&gt;&lt;h2&gt;Why not just use a &lt;code&gt;var&lt;/code&gt;?&lt;/h2&gt;&lt;p&gt;Instead of using new &lt;code&gt;val&lt;/code&gt; declarations for each step of the transformation why not use a &lt;code&gt;var&lt;/code&gt;? &lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;updatedOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;submitToStockroom&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;updatedOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updatedOrder&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="nd"&gt;@Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethods&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cash&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;generateInvoice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;updatedOrder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;updatedOrder&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;updatedOrder&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilled&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;markOrderProcessed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;updatedOrder&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I've interacted, both directly and indirectly, with many people that think having even a single &lt;code&gt;var&lt;/code&gt; in your code base is an affront to the Scala gods but more times than not this appears to be down to lack of rationale thinking. Yes, we've created mutable state but we are still dealing with immutable object and any mutation is confined to this single function - local mutable state can be acceptable. &lt;/p&gt;&lt;p&gt;What this approach does is reduce the number of variable/values used to two&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The &lt;code&gt;order&lt;/code&gt; that was passed in, and,&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;updatedOrder&lt;/code&gt; that we create and assign to multiple times&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;This tactic reduces the chances of using the wrong value in a function call compared to creating multiple values in the previous approach.&lt;/p&gt;&lt;h2&gt;Why do we even need the &lt;code&gt;var&lt;/code&gt;?&lt;/h2&gt;&lt;p&gt;So if reducing the amount of variables/values we create appears to have a positive effect - why not simply &lt;strong&gt;never&lt;/strong&gt; create one in this function?&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;markOrderProcessed&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paymentMethod&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethods&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cash&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;generateInvoice&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;submitToStockroom&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilled&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Yikes! So lets break this down. Based on the &lt;code&gt;paymentMethod&lt;/code&gt; we are creating a partial function based on either &lt;code&gt;generateInvoice&lt;/code&gt; or &lt;code&gt;retrievePaymentNotification&lt;/code&gt;. From here we are passing in the result of &lt;code&gt;submitToStockroom(order)&lt;/code&gt;, updating the object using the copy constructor to mark it fulfilled and finally marking the order as processed via &lt;code&gt;markOrderProcessed&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;If I had to deconstruct this as someone else's code I'd probably need to take the rest of the day off to recover. Please never do this.&lt;/p&gt;&lt;p&gt;Can we improve this approach though?&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;markOrderProcessed&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;submitToStockroom&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="nd"&gt;@Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethods&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cash&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;generateInvoice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}).&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilled&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we have dropped the currying of functions in favour of simply nesting invocations using a &lt;code&gt;match&lt;/code&gt;. Is it any better? Maybe. I think it's a &lt;strong&gt;tiny bit&lt;/strong&gt; easier to decipher but it is still a big ball of tangled code. The order of execution doesn't flow from top to bottom but instead from inside out, kind of. This makes it very hard to derive execution order and so updating this code, especially when order is important, would be a massive pain.&lt;/p&gt;&lt;h2&gt;Improving the flow&lt;/h2&gt;&lt;p&gt;I've written a fair bit of Clojure over the last 2 years and one of the feature I really like about it is the &lt;a href="https://clojuredocs.org/clojure.core/-%3E"&gt;threading macro&lt;/a&gt;. It's a tiny bit of syntactic sugar that would let you unroll calls like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="c1"&gt;// BTW this is not clojure it's scala-ish pseudocode&lt;/span&gt;
&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toUppercase&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"abcd"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"X"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Into something like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="c1"&gt;// again this is scala-ish pseudocode&lt;/span&gt;
&lt;span class="n"&gt;thread&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"abcd"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toUppercase&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;replace&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"X"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This more clearly defines the order of execution to the reader.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Starting with the string &lt;code&gt;"abcd"&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Make the string uppercase&lt;/li&gt;
  &lt;li&gt;Replace "A" with "X"&lt;/li&gt;
  &lt;li&gt;Split by character&lt;/li&gt;
  &lt;li&gt;Get the first character&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;With the first approach you are forced to work from inside out to understand what is happening and in what order.&lt;/p&gt;&lt;p&gt;Scala doesn't have a standard threading operator or macro but it does have some ways to accomplish something similar.&lt;/p&gt;&lt;h2&gt;Using &lt;code&gt;Option&lt;/code&gt; as a pipeline&lt;/h2&gt;&lt;p&gt;You can think of an &lt;code&gt;Option&lt;/code&gt; as a list of zero or one items that you can apply operations on. So if we make a &lt;code&gt;Some(Order)&lt;/code&gt; based on the order passed into the function we can apply operations on the contained order in a way that mimics the thread operator.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;submitToStockroom&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collect&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="nd"&gt;@Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethods&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cash&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;generateInvoice&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilled&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;markOrderProcessed&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we are using the power of &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;collect&lt;/code&gt; to transform the original order through a series of steps without having to create intermediate variables or values. Finally we call &lt;code&gt;get&lt;/code&gt; to unwrap the Order again. This approach only really works if we are always at least accepting and returning an &lt;code&gt;Order&lt;/code&gt;.&lt;/p&gt;&lt;h2&gt;Compose a pipeline&lt;/h2&gt;&lt;p&gt;Using &lt;code&gt;Some&lt;/code&gt; is alright I guess but for this type of problem Scala does have something we can use to create a pipeline - functional composition.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;processOrderPostPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;submitToStockroom&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;andThen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paymentMethod&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethods&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cash&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;generateInvoice&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;retrievePaymentNotification&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;andThen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fulfilled&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;andThen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;markOrderProcessed&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this example we are currying or partially applying the &lt;code&gt;submitToStockroom&lt;/code&gt; function which produces a new type &lt;code&gt;scala.Function1[Order, Order]&lt;/code&gt; which is a function that accepts a single &lt;code&gt;Order&lt;/code&gt; and returns an &lt;code&gt;Order&lt;/code&gt;. This gives us access to the compositional functions of the &lt;code&gt;Function1&lt;/code&gt; type - specifically &lt;code&gt;andThen&lt;/code&gt; in our case. Using &lt;code&gt;andThen&lt;/code&gt; we compose a single function that pipes its input and output through the series of functions in order. Finally we execute the function by invoking it with our input order &lt;code&gt;(order)&lt;/code&gt;.&lt;/p&gt;&lt;h2&gt;So which is best?&lt;/h2&gt;&lt;p&gt;This was never about one approach being better than another. There are a few examples that are clearly not reasonable solutions and would in fact make everything worse but there is no clear winner. &lt;/p&gt;&lt;p&gt;As you can see there are many ways to skin a cat. Depending on the context one approach may yield code that is easier to understand and maintain.&lt;/p&gt;</content></entry><entry><title>Refactoring Pattern Matching in Scala</title><updated>Mon Dec 21 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/12/21/refactoring-pattern-matching-in-scala/"></link><id>urn:yobriefca-se:feed:post:Refactoring Pattern Matching in Scala</id><content type="html">&lt;p&gt;I want to take a look at making effective use of Scala's pattern matching capabilities for a fairly trivial example. Don't expect any of this to be mind blowing but maybe you're venturing in Scala for the first time or have been tinkering for a while you might find this useful. &lt;/p&gt;&lt;p&gt;The example I give below has been taken from a real world example but the context has been changed and made bit more trivial and non-specific.&lt;/p&gt;&lt;h2&gt;Setting the stage&lt;/h2&gt;&lt;p&gt;We have a solution by which a customer can place an order through our system. They can pay by various means - Credit Card, Bitcoin and Direct Debit. Once they have placed their order they are taken to a payment selection screen where they can select how they want to pay. Finally, once they have selected their payment method they are redirected to the appropriate payment gateway.&lt;/p&gt;&lt;p&gt;First lets model the &lt;code&gt;PaymentMethod&lt;/code&gt; types&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Bitcoin&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;DirectDebit&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This gives us case objects that represent the payment types our system supports. Next lets create an &lt;code&gt;Order&lt;/code&gt; class to model our actual order.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;BigDecimal&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;selectedPaymentMethod&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This class has some properties relating to the order&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;An &lt;code&gt;id&lt;/code&gt; property, and&lt;/li&gt;
  &lt;li&gt;A &lt;code&gt;total&lt;/code&gt; property that holds the final price of the order.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;It also has the &lt;code&gt;selectedPaymentMethod&lt;/code&gt; property which is an option of our &lt;code&gt;PaymentMethod&lt;/code&gt; type. It is defined as an &lt;code&gt;Option&lt;/code&gt; because prior to payment selection we will have an order but no payment method selected.&lt;/p&gt;&lt;p&gt;Now lets assume our system is an event based system. Once payment select has been made the &lt;code&gt;Order&lt;/code&gt; instance is persisted to a data store with the chosen option and a system event is triggered to being processing payment and routing the customer.&lt;/p&gt;&lt;p&gt;Within whatever event handling strategy we use (Actor, Queue, Bus, Callback etc.) we then need to retrieve the customers most recent order and if there is a payment method defined route to the correct payment gateway. We have defined a method like for retrieving the mot recent customer order, &lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;mostRecentCustomerOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customerId&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It returns a &lt;code&gt;Option[Order]&lt;/code&gt; because there are situations where this is used and the customer doesn't yet have a recent order. Just go with it.&lt;/p&gt;&lt;h2&gt;Decisions using pattern matching&lt;/h2&gt;&lt;p&gt;Now to the juicy bit. Given we've got the most recent order we need to route accordingly. At this point we want to return yet another &lt;code&gt;Option&lt;/code&gt; this time of a potential &lt;code&gt;Route&lt;/code&gt; which is an abstraction that allows us to route to external systems (the details are really not important for this example).&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;If there is no order we return &lt;code&gt;None&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;If there is an order but no selected payment method we return &lt;code&gt;None&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;If there is an order and a selected method we return a &lt;code&gt;Some&lt;/code&gt; of a route depending on what the select method is.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Lets code this up.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;mostRecentCustomerOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customerId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selectedPaymentMethod&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirectToPaymentGateway&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selectedPaymentMethod&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bitcoin&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirectToBitcoinGateway&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;selectedPaymentMethod&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DirectDebit&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirectToBankGateway&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I see this code a lot. It's not necessarily wrong but it is rather long winded. We are doing some things unnecessarily. For example, we are extracting the &lt;code&gt;Order&lt;/code&gt; into an &lt;code&gt;order&lt;/code&gt; value and inspecting the contents. Not a major issue but we aren't actually using it inside the case statements body. We also run the risk of shadowing other values of the same name.&lt;/p&gt;&lt;h2&gt;Deeper matching&lt;/h2&gt;&lt;p&gt;Pattern matching in Scala isn't confined to the top level class. It is entirely possible to match all the way down the object graph. So lets revisit the same bit of code and remove the unnecessary extraction of a value and just use pure matching.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;mostRecentCustomerOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customerId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirectToPaymentGateway&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bitcoin&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirectToBitcoinGateway&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DirectDebit&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirectToBankGateway&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this case we avoid extracting the &lt;code&gt;order&lt;/code&gt; value and make each &lt;code&gt;case&lt;/code&gt; a bit more succinct by matching purely on the existence of a &lt;code&gt;PaymentMethod&lt;/code&gt; within the &lt;code&gt;Option&lt;/code&gt;. We also use &lt;code&gt;_&lt;/code&gt; to mark values we aren't interested in. Whether or not this is easier to read than the previous example is very subjective. I think it is but only really marginally so.&lt;/p&gt;&lt;h2&gt;Option.collect&lt;/h2&gt;&lt;p&gt;There is another option (pun intended). As &lt;code&gt;mostRecentCustomerOrder&lt;/code&gt; returns an &lt;code&gt;Option&lt;/code&gt; and we are expected to return an &lt;code&gt;Option&lt;/code&gt; we can use &lt;code&gt;Option.collect&lt;/code&gt;. &lt;/p&gt;
&lt;blockquote&gt;
  &lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;pf&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;PartialFunction&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;, &lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
  &lt;pre class="highlight"&gt;&lt;code&gt;Returns a scala.Some containing the result of applying pf 
to this scala.Option's contained value, if this option is 
nonempty and pf is defined for that value.
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;&lt;p&gt;This gives us the power of pattern matching so we can transform the input &lt;code&gt;Option&lt;/code&gt; in to what we want &lt;strong&gt;if and only if&lt;/strong&gt; the input satisfies our conditions, falling back to &lt;code&gt;None&lt;/code&gt; if it doesn't. You can think of &lt;code&gt;collect&lt;/code&gt; like a conditional &lt;code&gt;map&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;mostRecentCustomerOrder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customerId&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;collect&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;redirectToPaymentGateway&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bitcoin&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;redirectToBitcoinGateway&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DirectDebit&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
  	&lt;span class="n"&gt;redirectToBankGateway&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;By using &lt;code&gt;collect&lt;/code&gt; we don't need to provide the default fallback case of returning &lt;code&gt;None&lt;/code&gt; plus we get to unwrap all those &lt;code&gt;Some(Order(...))&lt;/code&gt; cases as well as not having to wrap each cases return value in a &lt;code&gt;Some&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;Again this is purely personal opinion but this reduction of noise makes this code much easier to read. We are letting the language deal with the default cases without having to repeat ourselves.&lt;/p&gt;&lt;h2&gt;Summary&lt;/h2&gt;&lt;p&gt;None (pun intended) of the options (pun intended) above are necessarily wrong and one approach may fit better in some scenarios than another but I feel that laying out the options (pun intended) helps us make more informed decisions while we try and build quality code.&lt;/p&gt;</content></entry><entry><title>On Popularity</title><updated>Fri Nov 27 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/11/27/on-popularity/"></link><id>urn:yobriefca-se:feed:post:On Popularity</id><content type="html">&lt;p&gt;Overhearing two developers lamenting someone’s choice of library it seemed that after bit of googling they deemed the library wasn't very good. It didn't have enough stars on GitHub. &lt;/p&gt;&lt;p&gt;Popularity is an awful metric to judge anything on and the data behind thumbs or stars or hearts is so skewed to be meaningless too.&lt;/p&gt;&lt;h2&gt;Birth of a Star&lt;/h2&gt;&lt;p&gt;Having released a few inconsequential open source projects on GitHub personally I noticed that stars arrived in for my projects shortly after the project was announced or when it got linked to by places like Twitter or Reddit. There was no way people actually dropped everything they where doing, used my code and immediately found benefit. So this begs the question - what does a "star" mean?&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;"Hey thanks this looks great and I may use it soon" - &lt;strong&gt;LIKELY&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;"I must remember to look at this when I have more time" - &lt;strong&gt;LIKELY&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;"Hey look at me I may or may not have used this popular library, accept me" - &lt;strong&gt;POSSIBLY&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;"What does this button actually do?" - &lt;strong&gt;I GUESS SO&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;"After 6, long, months I've just shipped a project using this library and it helped me avoid hours of pain" - &lt;strong&gt;UNLIKELY&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;The actions of "starring" means different things to different people but given the fact that they shoot up rapidly after the repo gets featured on an influential site suggests that there are certainly a lot of people using it as an acknowledgement that the project looks interesting. Now that’s not to say that early star adopters &lt;strong&gt;never&lt;/strong&gt; use the project - many popular projects clearly have a large active user base but it's not &lt;strong&gt;because&lt;/strong&gt; of the number of stars they have.&lt;/p&gt;&lt;h2&gt;Death of a Star&lt;/h2&gt;&lt;p&gt;So you visit a new project on GitHub, recommended to you by someone on TwitteRedditNews. You read the README.md and think - "cool". You hit the little star and download the project. You start tinkering with it, maybe write a blog post about it. Perhaps even use it in a small side project. Then you get distracted by life. You move on to other projects using different technologies and that, once cool, library is forgotten.&lt;/p&gt;&lt;p&gt;How many people actually actively go back and prune their "stars"? I'm willing to bet not many. Why bother, right? Staring is just a meaningless gesture yet people still insist on judging a project based on how many people performed these meaningless actions. &lt;/p&gt;&lt;p&gt;Heck I imagine even if you got badly burnt by a particular project after a few months of using it you'd probably still not remember to go back and unstar it. I presume it would take a public outcry and a good old internet pile-on to make a significant dent in a projects "star" popularity&lt;/p&gt;&lt;h2&gt;Starry Starry Night&lt;/h2&gt;&lt;p&gt;There are many ways to determine the viability of a project but "stars" isn't one of them. Sure you could use it to determine if one project is more popular than the other but what does that actually tell you? Without other (actually useful) data it means very little. Outside of comparing similar projects it means even less - some genuinely useful projects have little to no stars because they don't do "popular" things, just; you know, stuff that can really make a difference.&lt;/p&gt;&lt;h2&gt;Stop look and listen. Oh and think as well. Do lots of that.&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;Popularity is not a indicator of quality (though quality may build popularity).&lt;/li&gt;
  &lt;li&gt;Popularity is not an indicator that you'll get meaningful support or documentation (though good support and documentation may build popularity)&lt;/li&gt;
  &lt;li&gt;Not all good things are necessarily popular/shiny (accessibility, security etc. - all those important things that just aren't all that fun but absolutely essential)&lt;/li&gt;
  &lt;li&gt;Indicators for popularity don't necessarily mean the subject is &lt;strong&gt;still&lt;/strong&gt; popular&lt;/li&gt;
  &lt;li&gt;Relative popularity could be down to better marketing, shouting louder or simply luck.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Stars, hearts, thumbs up, pluses, smiles, artisanal breadsticks or whatever else we use to suggest the presence of the magical unicorn we call brand engagement - ignore them. Make informed decisions with real data specific to your needs. &lt;/p&gt;&lt;p&gt;Is it more work?&lt;/p&gt;&lt;p&gt;Yes.&lt;/p&gt;&lt;p&gt;All worthwhile things are. Thats why they are worthwhile.&lt;/p&gt;</content></entry><entry><title>The Twelve Factor App (Redux)</title><updated>Sun Sep 20 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="/presentations/twelve-factor-app-redux.pdf"></link><id>urn:yobriefca-se:feed:post:The Twelve Factor App (Redux)</id><content type="html"></content></entry><entry><title>More Betterness</title><updated>Wed Sep 09 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/09/09/more-betterness/"></link><id>urn:yobriefca-se:feed:post:More Betterness</id><content type="html">&lt;style&gt;
  body {
    background-color: #f5f7f1;
    color: #424242;
  }

  a {
    color: #93bdc2;
  }

  .row h1 {
    display:none;
  }

  .homer a {
    color: #424242;
  }
&lt;/style&gt;
&lt;blockquote&gt;&lt;p&gt;This article is adapted from a presentation I gave on team work and understanding&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.001.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Hi. This wont take very long so just sit back and enjoy yourself. I want to start with a quote.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.002.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Have a read of that, and let it sink in for a moment before we move on. We'll come back to it later.&lt;/p&gt;&lt;p&gt;OK? Good.&lt;/p&gt;&lt;p&gt;I'd like to introduce you to a few people.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.005.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;This is Alice. Typical hacker, Alice rarely writes tests. Never mind Test Driven Development there is literally no tests. Talk about pushing more broken garbage out in to the world. Boo Alice. Boo.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.008.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Here's Bob. Bob &lt;a href="http://shop.oreilly.com/product/9780596809492.do"&gt;bought a book&lt;/a&gt; recently a learned the "Boy Scout Rule". Since then he has been helping his team out by making old code more concise. Bob, you're awesome.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.011.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Hi Caitlin. Caitlin's team are a bit peeved at Caitlin because even though everyone has moved to a more functional style of writing JavaScript Caitlin still insists on writing old-style imperative &lt;code&gt;for&lt;/code&gt; loops which are super noisy. Get with the program Caitlin.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.014.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Dales team are on fire! Not literally but they did just knock out a really robust, fault tolerant data pipeline project in just shy of a week. Mind. Blown. Gold stars all round folks.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.017.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Finally we have Eda. Eda is like a magpie. She just loves kicking off projects in whatever shiny new technologies are at the top of Hacker News. The problem is she gets bored and distracted by other new things. Eventually she just moves on to another project. For pity sake Eda, sort it out.&lt;/p&gt;&lt;p&gt;Now these are interesting stories and I'm sure you can identify various people in these personas but there's a problem here. A few problems in fact which we will get to later. For now lets focus on one problem. All these stories are missing something. Context.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.019.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Context is to understanding like oxygen is to a fire. Without context it is impossible to fully understand any situation. Without context we are forced to make assumptions and judgements, we are more easily coerced into believing things about the situation and ultimately make wrong decisions.&lt;/p&gt;&lt;p&gt;So let me provide you a bit of context for the previous case studies.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.022.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Back to Alice - the dirty little hacker. Turns out Alice is working on a series of really rapid prototypes to help a client understand some of their internal requirements and discover what actually needs to be built. These prototypes are all throwaway and Alice knows that codifying assumptions through tests would be a pointless goal.&lt;/p&gt;&lt;p&gt;We're sorry Alice we were wrong about you.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.025.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Now Bob. Bob. Bob. Bob. Bobs nobel application of the "Boy Scout Rule" has really mucked things up for the support team. You see Bob had a goal of making code more concise but he ignored the fact that someone needs to maintain this code and so Bob set about code golfing perfectly readable, nicely structured code into a tangle of chained functional style calls. Bob made the support team cry.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.028.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Ah yes Caitlin. Going against the grain with coding style, using old style for loops in JavaScript when there is a perfectly good functional approach available. Well... Caitlin is right. You see Caitlin is working in a very performance sensitive part of the application and, well, old-style for loops are often much faster than their functional cousins. She used data to measure performance and came to this conclusion using her findings. Caitlin you used evidence based decision making instead of just falling in line. You rock.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.031.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Oh look it's Dales elite team of rockstar ninjas. Thanks for the robust data pipeline folks but I only wanted you to scrape a couple of web pages and extra some figures. In fact while I waited for you I wrote a little shell script using &lt;code&gt;curl&lt;/code&gt;, &lt;code&gt;sed&lt;/code&gt; and &lt;code&gt;awk&lt;/code&gt; to do it for me. Your entire team just burnt nearly a week of funding due to over-engineering. Thanks for nothing Dales Team.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.034.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Finally Eda. Eda likes to remain current as it helps her make more informed decisions. So in her down time she works on hobby projects and code katas. She rarely finishes them because there is so much churn in the world of technology that she get distracted. Thats cool, she learnt stuff, she was under no obligation to actually release anything. Eda makes better decisions in work because of her tinkering and is well respected. Have a big fat pay rise Eda.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.035.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;So you can see with more context we can actually alter the entire narrative of a story. What was once a bad thing becomes a good thing and vice-versa. Of course it may not. Eda could well have been one of those people that likes to start projects in work, complicate matters and then leave. But she wasn't.&lt;/p&gt;&lt;p&gt;And that is the point of this entire talk. With enough context we can make better decisions, with better decisions we can be more successful and start moving toward more betterness.&lt;/p&gt;&lt;p&gt;But it's not just context other things where at play such as bias and influence. So in order to achieve more betterness we need to understand these things as well.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.036.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;First and foremost establish as much context as possible. Context is king after all. You can see how without context I was able to alter the narrative of a story. Know that there may always be more to it. Use discovery to improve understanding of the context. There is a reason why a great discovery session starts with as many diverse stakeholders as possible - everyone has a slightly different view of the overall context.&lt;/p&gt;&lt;p&gt;Don't walk into any situation with rigid assumptions, don't “solutionize" before you know enough. Listen.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.037.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;I'm sure as I was reading out the case studies some of you had some reaction to the stories I was telling. Perhaps Alice and her lack of tests made you roll your eyes. Thats bias at play. Bias actually reinforced the narrative irrespective of the situational context.&lt;/p&gt;&lt;p&gt;We all have bias and bias exists without context. Bias affects your understanding of the context and so can adversely affect decision making. While we can't necessarily change our bias we can be aware of it. This helps us better understand our decision making.&lt;/p&gt;&lt;p&gt;In fact not being aware of a bias you may hold can lead to unintended steering of decision making.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.038.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;One thing I did during the original case studies was, at least attempt to, influence your feelings for the person. I intentionally used negative words and tone to make you think the worst.&lt;/p&gt;&lt;p&gt;Influence may know about situational context but doesn't care. The loudest voice in the room may get their way but they may not be right. Influence can be either internal or external. That echo chamber of a Twitter stream that tells you how "OO is bad and functional programming is the one true way" can affect the decisions you make because of the influence it has over you. The thing is no-one wants to blog about daily maven usage when you get more social engagement and karma from writing about SBT internals.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.039.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;And this brings me on to the team.&lt;/p&gt;&lt;p&gt;When I say team I mean a proper team not a group of people thrown together because the resourcing pipeline spat out a few available candidates. Some may have started out that way of course. I mean a group of people that have come together under a common cause. A team of people, not resources.&lt;/p&gt;&lt;p&gt;The great thing about a good team is that it normalises everything. Biases balance out, influences are moderated and everyone works together to form the truest view of the given problem context. The team establish context, the team make decisions.&lt;/p&gt;&lt;p&gt;A good team will do wonderful things. A bad team can lay the groundwork for years of pain. A good team will coalesce on an ideal. A bad team will bicker. A good team will make informed decisions quickly. A bad team will flounder. A bad team will cost you reputation and money. A good team will address problems upfront and openly. A bad team will let things fester until peer review time. A good team is diverse as it naturally allows for a broader range of thinking. A bad team is a monoculture.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.041.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;So remember this.&lt;/p&gt;&lt;p&gt;Your team is your (work) family and as such you should love your team. Give a damn about the people and leave your ego at the door.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/morebetterness/morebetterness.043.png" alt="More Betterness" _="_" /&gt;&lt;/p&gt;&lt;p&gt;And of course I wasn't quoting some horrible Harry Potter erotic fanfic describing some carnal act between Harry Potter characters. Snape needed medical attention. Shame really.&lt;/p&gt;&lt;p&gt;The right context really changes the way a mind thinks.&lt;/p&gt;</content></entry><entry><title>More Betterness</title><updated>Wed Sep 09 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="/presentations/more-betterness.pdf"></link><id>urn:yobriefca-se:feed:post:More Betterness</id><content type="html"></content></entry><entry><title>Refactoring Towards Transducers</title><updated>Tue Aug 04 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/08/04/refactoring-towards-transducers/"></link><id>urn:yobriefca-se:feed:post:Refactoring Towards Transducers</id><content type="html">&lt;blockquote&gt;&lt;p&gt;This is a quick post but hey at least I'm not &lt;a href="https://yobriefca.se/blog/2015/07/28/window-dot-prompt-in-elm/"&gt;talking&lt;/a&gt; &lt;a href="https://yobriefca.se/blog/2015/07/29/zombie-dice-score-card-in-elm/"&gt;about&lt;/a&gt; &lt;a href="https://yobriefca.se/blog/2015/08/02/deconstructing-your-first-elm-app/"&gt;Elm&lt;/a&gt;. COMBOBREAKER!&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Clojure 1.7 introduced a new, fairly large concept - &lt;a href="http://clojure.org/transducers"&gt;transducers&lt;/a&gt;. Transducers are "composable algorithmic transformations" which means they are little isolated transformer functions. They aren't bound to any particular input or output and as such can be stacked or composed ad-hoc to make neat little pipelines for data processing. &lt;/p&gt;&lt;p&gt;This required a fairly large internal change in the way many collection functions in Clojure behaved. Now most of these functions now accept an arity of one less than they normally do. For example,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;incrementalizer&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map &lt;/span&gt;&lt;span class="nv"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this case &lt;code&gt;incrementalizer&lt;/code&gt; is defined as a &lt;code&gt;map&lt;/code&gt; function that will perform &lt;code&gt;inc&lt;/code&gt; over whatever it gets passed. We didn't pass &lt;code&gt;map&lt;/code&gt; a data structure to map over and so it returns a transducer. We can use &lt;code&gt;comp&lt;/code&gt; to compose a bunch of these transducers together and run them across a collection using forms such as &lt;code&gt;into&lt;/code&gt;, &lt;code&gt;transduce&lt;/code&gt;, &lt;code&gt;eduction&lt;/code&gt; and &lt;code&gt;sequence&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;As I was working on a new project recently I had just moved to Clojure 1.7 and so decided to take a stab at refactoring a few utility functions I had into a series of transducers. This post covers how I approached this refactor. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;It's worth noting that this is purely an exercise in refactoring towards transducers. I am not suggesting that this increases performance, is easier to maintain or makes code any more readable. I'm not even suggesting this &lt;strong&gt;should&lt;/strong&gt; be done at all as that depends on the context of your work.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;The original approach looked like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;user&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;clojure.java.io&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="nv"&gt;io&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;clojure.string&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="nv"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
            
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;list-files&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file-seq &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;io/file&lt;/span&gt; &lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;is-xml?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&amp;gt; &lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.getName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str/split&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="s"&gt;"\."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;= &lt;/span&gt;&lt;span class="s"&gt;"xml"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
                         
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;list-xml-files&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;files&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;and &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.isFile&lt;/span&gt; &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is-xml?&lt;/span&gt; &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
          &lt;span class="nv"&gt;files&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;list-xml-paths&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.getAbsolutePath&lt;/span&gt; &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list-xml-files&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list-files&lt;/span&gt; &lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
       
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;xml-paths&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list-xml-paths&lt;/span&gt; &lt;span class="s"&gt;"./workspace/xml"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A bit of scrappy code that I wrote interactively while doing some ad-hoc file processing. What we end up with is a list of absolute file paths to some XML files contained within a specified folder. In order to transform a folder path to a list of XML files it does a few things,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;List the contents of the folder using the built-in &lt;code&gt;file-seq&lt;/code&gt; function in Clojure&lt;/li&gt;
  &lt;li&gt;Remove everything in the list that isn't a file (such as pipes and directories)&lt;/li&gt;
  &lt;li&gt;Remove everything in the list without the &lt;code&gt;.xml&lt;/code&gt; extension&lt;/li&gt;
  &lt;li&gt;Call &lt;code&gt;.getAbsolutePath&lt;/code&gt; on the remainder of the list to get the full path as a string&lt;/li&gt;
&lt;/ol&gt;&lt;h2&gt;Towards Transducers&lt;/h2&gt;&lt;p&gt;Any place that we apply some sort of filtering, mapping, reduction, take etc. over a collection is a candidate for being converted to a transducer. In the previous examples we can identify a few points that can be rewritten as transducers,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The &lt;code&gt;filter&lt;/code&gt; which actually has 2 checks
  &lt;ul&gt;
    &lt;li&gt;is it a file?&lt;/li&gt;
    &lt;li&gt;is it an xml file?&lt;/li&gt;
  &lt;/ul&gt;&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;map&lt;/code&gt; that converts a list of XML files into their absolute paths&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;If we rewrite these areas as transducers we come out with the following code,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;xml-extension-only&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&amp;gt; &lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.getName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str/split&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="s"&gt;"\."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;= &lt;/span&gt;&lt;span class="s"&gt;"xml"&lt;/span&gt;&lt;span class="p"&gt;)))))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;files-only&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.isFile&lt;/span&gt; &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;as-path&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;item&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.getAbsolutePath&lt;/span&gt; &lt;span class="nv"&gt;item&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;you should be able to see these transducer definitions are just restructured, single arity versions of our original &lt;code&gt;filter&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt; calls.&lt;/p&gt;&lt;p&gt;Now we need to turn these into a pipeline. We do this using the &lt;code&gt;comp&lt;/code&gt; function,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;xform&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;comp &lt;/span&gt;&lt;span class="nv"&gt;files-only&lt;/span&gt; &lt;span class="nv"&gt;xml-extension-only&lt;/span&gt; &lt;span class="nv"&gt;as-path&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The use of &lt;code&gt;xform&lt;/code&gt; as a name is common in transducer examples (short for transform) so I have retained the use here for consistency. &lt;/p&gt;&lt;p&gt;With our transducers composed into a transform pipeline we can apply it to our input,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;list-xml-paths&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sequence&lt;/span&gt; &lt;span class="nv"&gt;xform&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list-files&lt;/span&gt; &lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I redefined the &lt;code&gt;list-xml-paths&lt;/code&gt; function to call &lt;code&gt;sequence&lt;/code&gt; over the list of files and apply our transducer pipeline.&lt;/p&gt;&lt;p&gt;The complete code for this example looks like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;user&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;clojure.java.io&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="nv"&gt;io&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;clojure.string&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="nv"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
            
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;list-files&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file-seq &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;io/file&lt;/span&gt; &lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;xml-extension-only&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&amp;gt; &lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.getName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str/split&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="s"&gt;"\."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;= &lt;/span&gt;&lt;span class="s"&gt;"xml"&lt;/span&gt;&lt;span class="p"&gt;)))))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;files-only&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.isFile&lt;/span&gt; &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;as-path&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;item&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.getAbsolutePath&lt;/span&gt; &lt;span class="nv"&gt;item&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;xform&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;comp &lt;/span&gt;&lt;span class="nv"&gt;files-only&lt;/span&gt; &lt;span class="nv"&gt;xml-extension-only&lt;/span&gt; &lt;span class="nv"&gt;as-path&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;list-xml-paths&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sequence&lt;/span&gt; &lt;span class="nv"&gt;xform&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list-files&lt;/span&gt; &lt;span class="nv"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
  
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;xml-paths&lt;/span&gt; 
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;list-xml-paths&lt;/span&gt; &lt;span class="s"&gt;"./workspace/xml"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;</content></entry><entry><title>Deconstructing Your First Elm App</title><updated>Sun Aug 02 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/08/02/deconstructing-your-first-elm-app/"></link><id>urn:yobriefca-se:feed:post:Deconstructing Your First Elm App</id><content type="html">&lt;blockquote&gt;&lt;p&gt;This article is similar to the &lt;a href="https://github.com/evancz/elm-architecture-tutorial#the-elm-architecture"&gt;Elm Architecture Tutorial&lt;/a&gt; so if you fancy a more official explanation you should check that out too.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;One of things that has really impressed me in my research of &lt;a href="http://elm-lang.org"&gt;Elm&lt;/a&gt; has been the simplicity of the architecture of an application. An Elm app is essentially just a reactive stream of actions that transition the model into various states and in turn these state changes cause the view to redraw. You don't mutate anything you just create a new state and the entire application redraws itself. Oh and it's fast too. This is thanks to the &lt;a href="http://elm-lang.org/blog/blazing-fast-html"&gt;virtual DOM&lt;/a&gt; approach that has risen to fame through the likes of Elm and &lt;a href="http://facebook.github.io/react/"&gt;React&lt;/a&gt;. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Essentially Elm allows you to treat the UI as an &lt;a href="http://iki.fi/sol/imgui/"&gt;Immediate Mode UI&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;&lt;h2&gt;Deconstruction&lt;/h2&gt;&lt;p&gt;What I want to do is demonstrate this simplicity by creating a very simple application firstly using the &lt;code&gt;start-app&lt;/code&gt; package and then without using the &lt;code&gt;start-app&lt;/code&gt; package. By using the &lt;code&gt;start-app&lt;/code&gt; package initially we can see how an Elm app hangs together at a high level then, by removing the use of the package, we can dive a bit deeper into how our application is made reactive with some really simple functional patterns.&lt;/p&gt;&lt;h2&gt;Using start-app&lt;/h2&gt;&lt;p&gt;The &lt;a href="https://github.com/evancz/start-app"&gt;start-app&lt;/a&gt; package abstracts away most of the boilerplate required to wire up a typical Elm application. It is designed to help people new to Elm get their first application up and running without to much upfront cognitive load. You just supply the &lt;code&gt;StartApp.start&lt;/code&gt; a type containing&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Your &lt;code&gt;model&lt;/code&gt; that is used to store state&lt;/li&gt;
  &lt;li&gt;Your &lt;code&gt;view&lt;/code&gt; that provides a visual representation of the current state, and,&lt;/li&gt;
  &lt;li&gt;Your &lt;code&gt;update&lt;/code&gt; method that, through action signals generated from inside or outside your application, transitions your model from one state to another&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;If you want to follow along you need to have Elm installed. Have a look at the &lt;a href="http://elm-lang.org/install"&gt;official site for instructions&lt;/a&gt; and various editor plugins for Elm.&lt;/p&gt;&lt;p&gt;So lets create our first application. &lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sh"&gt;&amp;gt; mkdir elm-demo-start-app &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;elm-demo-start-app

&amp;gt; elm package install

Some new packages are needed. Here is the upgrade plan.

  Install:
    elm-lang/core 2.1.0

Do you approve of this plan? &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y
Downloading elm-lang/core
Packages configured successfully!
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This gives us the necessary manifests and caches that you get with any package managers in Elms case we get,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;elm-package.json&lt;/code&gt; which declares all the metadata and dependencies our application needs&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;elm-stuff/&lt;/code&gt; which acts as our package cache. You'll see the source of the dependencies we downloaded in the previous steps.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Then we want to add the extra dependencies that allow us to quickly build our Elm app,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sh"&gt;&amp;gt; mkdir elm-demo-start-app &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;elm-demo-start-app

&amp;gt; elm package install

Some new packages are needed. Here is the upgrade plan.

  Install:
    elm-lang/core 2.1.0

Do you approve of this plan? &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y
Downloading elm-lang/core
Packages configured successfully!

&amp;gt; elm package install evancz/elm-html 3.0.0

To install evancz/elm-html I would like to add the following
dependency to elm-package.json:

    &lt;span class="s2"&gt;"evancz/elm-html"&lt;/span&gt;: &lt;span class="s2"&gt;"3.0.0 &amp;lt;= v &amp;lt; 4.0.0"&lt;/span&gt;

May I add that to elm-package.json &lt;span class="k"&gt;for &lt;/span&gt;you? &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y

Some new packages are needed. Here is the upgrade plan.

  Install:
    evancz/elm-html 3.0.0
    evancz/virtual-dom 1.2.3

Do you approve of this plan? &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y
Downloading evancz/elm-html
Downloading evancz/virtual-dom
Packages configured successfully!

&amp;gt; elm package install evancz/start-app 1.0.0

To install evancz/start-app I would like to add the following
dependency to elm-package.json:

    &lt;span class="s2"&gt;"evancz/start-app"&lt;/span&gt;: &lt;span class="s2"&gt;"1.0.0 &amp;lt;= v &amp;lt; 2.0.0"&lt;/span&gt;

May I add that to elm-package.json &lt;span class="k"&gt;for &lt;/span&gt;you? &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y

Some new packages are needed. Here is the upgrade plan.

  Install:
    evancz/start-app 1.0.1

Do you approve of this plan? &lt;span class="o"&gt;(&lt;/span&gt;y/n&lt;span class="o"&gt;)&lt;/span&gt; y
Downloading evancz/start-app
Packages configured successfully!
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So far, via the most polite package manager I've ever encountered, we have installed&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;evancz/elm-html&lt;/code&gt; which allows us to work and build HTML and associated elements attributes, events and CSS styling&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;evancz/virtual-dom&lt;/code&gt; is installed as dependency of &lt;code&gt;elm-html&lt;/code&gt; and provides the virtual DOM implementation that Elm uses.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;evancz/start-app&lt;/code&gt; a small library for making writing standard Elm apps with less upfront wiring.&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;TO THE CODE!!&lt;/h2&gt;&lt;p&gt;The app is going to be really basic, and I really do mean basic. Just enough to demonstrate the core components of the Elm architecture - the &lt;strong&gt;model&lt;/strong&gt; for holding state, the &lt;strong&gt;view&lt;/strong&gt; for visualising app state and the &lt;strong&gt;update&lt;/strong&gt; function for transitioning our state.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/elm-demo.png" alt="App States" _="_" /&gt;&lt;/p&gt;&lt;p&gt;The app will have 2 buttons - "Mark" and "Reset". When you click "Mark" the app will append a &lt;strong&gt;block&lt;/strong&gt; (█) character to represent that event to the event log and increment the number of events logged. When you click "Reset" the app will clear the event log and zero the number of events logged. Contrived? Yes of course. But acceptably so.&lt;/p&gt;&lt;h2&gt;Starting Point&lt;/h2&gt;&lt;p&gt;So lets create an &lt;code&gt;EventLog.elm&lt;/code&gt; file. This will be our one and only code file for the application. We'll start by declaring the module and importing the necessary packages,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;EventLog&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html&lt;/span&gt;            &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html.Events&lt;/span&gt;     &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html.Attributes&lt;/span&gt; &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Signal&lt;/span&gt;          &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;StartApp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I've just imported a lot more than we will need for this app but feel free to curate your imports a bit better if you so desire. &lt;/p&gt;&lt;p&gt;Now we will start from the bottom up and declare our &lt;code&gt;main&lt;/code&gt; function. This is the starting point for all Elm apps. It will usually return a &lt;code&gt;Signal&lt;/code&gt; of some type that can be used by the Elm infrastructure to perform the necessary (re)rendering of your application. You can think of a &lt;code&gt;Signal&lt;/code&gt; as a typed stream that emits items as and when they are generated. &lt;/p&gt;&lt;p&gt;As we are using the &lt;code&gt;start-app&lt;/code&gt; package ours will look like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt;
&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="kt"&gt;StartApp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;initialModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I have included the type signature in here for good measure. Our &lt;code&gt;main&lt;/code&gt; simply calls into &lt;code&gt;StartApp.start&lt;/code&gt; passing in our 3 main components. Of course we still need to define these so lets go ahead and do this.&lt;/p&gt;&lt;h2&gt;Model&lt;/h2&gt;&lt;p&gt;Our model will define the entire state of our application. In our case this won't be very big at all, in fact it could literally just be a counter the we increment but for the sake of this experiment we will make it slightly more verbose,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;initialModel&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="nf"&gt;initialModel&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I defined a &lt;code&gt;type alias&lt;/code&gt; because it avoids the mess of having to declare the type as &lt;code&gt;{ events: List String }&lt;/code&gt; in every type signature that includes it. Then I defined our &lt;code&gt;initialModel&lt;/code&gt; using the &lt;code&gt;alias&lt;/code&gt;. The &lt;code&gt;initialModel&lt;/code&gt;, as the name suggests, is our starting state for our application. An empty model.&lt;/p&gt;&lt;h2&gt;View&lt;/h2&gt;&lt;p&gt;So now we have our state we can create a visual representation of that state by creating our &lt;code&gt;view&lt;/code&gt;,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;view&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Address&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt;
&lt;span class="nf"&gt;view&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Mark&lt;/span&gt; &lt;span class="s"&gt;"?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; 
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"Mark"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="kt"&gt;Reset&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; 
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"Reset"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="n"&gt;h2&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; 
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;" Events"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; 
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I'd typically break this into smaller functions to keep it a bit cleaner but for explanation purposes this is good enough. Our view takes an &lt;code&gt;address&lt;/code&gt; and &lt;code&gt;model&lt;/code&gt;. The &lt;code&gt;address&lt;/code&gt; is passed in from the work done by &lt;code&gt;start-app&lt;/code&gt; and provides a mailbox to send messages our actions to in order to update the model. The model in this case is the current state of the entire application.&lt;/p&gt;&lt;p&gt;With these key bits of data we construct a primitive HTML representation. &lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;We define buttons that have &lt;code&gt;onClick&lt;/code&gt; handlers. These handlers simply send actions (we define this type later) to the supplied address.&lt;/li&gt;
  &lt;li&gt;We define an &lt;code&gt;h2&lt;/code&gt; element that counts the number of events in our event list and displays it&lt;/li&gt;
  &lt;li&gt;Finally we define a &lt;code&gt;div&lt;/code&gt; that converts our events list into HTML elements&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Actions&lt;/h2&gt;&lt;p&gt;Lets go back to this &lt;code&gt;address&lt;/code&gt; and &lt;code&gt;actions&lt;/code&gt;. As mentioned the &lt;code&gt;start-app&lt;/code&gt; package creates an internal mailbox and address that you can send things to which will get passed into our &lt;code&gt;view&lt;/code&gt; function. When we send stuff to the address this will ultimately trigger a call to the apps &lt;code&gt;update&lt;/code&gt; function. As you can see from the type signature of &lt;code&gt;view&lt;/code&gt; the &lt;code&gt;Address&lt;/code&gt; is of type &lt;code&gt;Address Action&lt;/code&gt;. &lt;code&gt;Action&lt;/code&gt; is a type that we will define so lets do that and talk about it after.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Mark&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
            &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Reset&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you think back to the UI we had 2 buttons - Mark and Reset. What the &lt;code&gt;Action&lt;/code&gt; type does is create a representation of these UI interactions that can be used within our app to signal intent. Types like the &lt;code&gt;Action&lt;/code&gt; type - the ones that use &lt;code&gt;|&lt;/code&gt; - are called union types. In some ways you can think of them as enums that can accept data as well. By defining this &lt;code&gt;Action&lt;/code&gt; type we explicitly declare all the actions that will change the state within our application. No just &lt;strong&gt;can&lt;/strong&gt; but &lt;strong&gt;must&lt;/strong&gt;. That's predictable, thats wonderful and lovely and makes me feel all warm inside.&lt;/p&gt;&lt;p&gt;So, yes sorry I got all excited, the buttons we defined in our &lt;code&gt;view&lt;/code&gt; method, each, sends one of these types to the supplied mailbox address. So lets define what happens when we do this,&lt;/p&gt;&lt;h2&gt;Update&lt;/h2&gt;&lt;p&gt;Right. We have a model, a view and a way to signal changes in state via actions. Now we need to actually manage those state changes. This is where the final piece of the puzzle falls into place,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="kt"&gt;Mark&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="ow"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;Reset&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="ow"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;update&lt;/code&gt; function is just one big &lt;code&gt;case&lt;/code&gt; statement. It gets passed an action and the current state of our application and depending on the action we return a new state for our application. This generation of new state causes our application to be re-rendered and the glorious cycle begin again.&lt;/p&gt;&lt;p&gt;For completeness sake here is the entire &lt;code&gt;EventLog.elm&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;EventLog&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html&lt;/span&gt;            &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html.Events&lt;/span&gt;     &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html.Attributes&lt;/span&gt; &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Signal&lt;/span&gt;          &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;StartApp&lt;/span&gt;

&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;initialModel&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="nf"&gt;initialModel&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Mark&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
            &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Reset&lt;/span&gt;

&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="nf"&gt;update&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="kr"&gt;case&lt;/span&gt; &lt;span class="n"&gt;action&lt;/span&gt; &lt;span class="kr"&gt;of&lt;/span&gt;
    &lt;span class="kt"&gt;Mark&lt;/span&gt; &lt;span class="n"&gt;date&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="ow"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;Reset&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="ow"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;view&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Address&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt;
&lt;span class="nf"&gt;view&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Mark&lt;/span&gt; &lt;span class="s"&gt;"?"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"Mark"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="kt"&gt;Reset&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"Reset"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="n"&gt;h2&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;" Events"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt;
&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="kt"&gt;StartApp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;initialModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Running the App&lt;/h2&gt;&lt;p&gt;Elm comes with some lovely tools to get your first app up and running without having to worry about the usually front end infrastructure such as grunt, gulp, bower, npm and so on and so forth. One of these is &lt;code&gt;reactor&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sh"&gt;&amp;gt; elm reactor

Elm Reactor 0.3.2 &lt;span class="o"&gt;(&lt;/span&gt;Elm Platform 0.15.1&lt;span class="o"&gt;)&lt;/span&gt;
Listening on http://0.0.0.0:8000/
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running &lt;code&gt;elm reactor&lt;/code&gt; starts up a little file server (typically on port 8000) and if you navigate to &lt;code&gt;http://localhost:8000&lt;/code&gt; you will see the project files listed. By clicking &lt;code&gt;EventLog.elm&lt;/code&gt; reactor will compile your Elm code to JavaScript and inject it into an ephemeral HTML document so you can see and play with our utterly useless creation.&lt;/p&gt;&lt;h2&gt;Wrapping Up start-app&lt;/h2&gt;&lt;p&gt;So in summary&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The &lt;code&gt;main&lt;/code&gt; function starts our app using &lt;code&gt;StartApp.start&lt;/code&gt; which we pass our three main components&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;model&lt;/code&gt; represents the current state of our entire application&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;view&lt;/code&gt; provides a visual representation of our current application state and also generates action signals from user interaction that trigger an update in our application&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;update&lt;/code&gt; function takes these actions and creates a new state model for our application which triggers a re-rendering of our view&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;This is a deceptively simple approach that avoids one of the main pain points in application development - mutable state. Having to manage mutable state introduces a lot of complexity and unpredictability - two things that should never really mix.&lt;/p&gt;&lt;h2&gt;Removing start-app&lt;/h2&gt;&lt;p&gt;So the next question is "what does start-app do?". As I've demonstrated &lt;code&gt;start-app&lt;/code&gt; has provided the glue for realising our application - we just provided the components. Knowing how these things glue together really helps with understanding Elm and its additional features such as signals and ports.&lt;/p&gt;&lt;h2&gt;The Mailbox&lt;/h2&gt;&lt;p&gt;As we've seen in the previous implementation our view function gets passed an &lt;code&gt;address&lt;/code&gt; something that we didn't create ourselves. Internally &lt;code&gt;start-app&lt;/code&gt; creates a mailbox - a concept in Elm that provides a typed address that we can send messages to and a signal that emits a value when the address receives a message. So you can see that when we send an action message to the address via the &lt;code&gt;onClick&lt;/code&gt; handler in our view we can then trigger a state update.&lt;/p&gt;&lt;p&gt;Without the &lt;code&gt;start-app&lt;/code&gt; we need to create the mailbox ourselves,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;actions&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Mailbox&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt;
&lt;span class="nf"&gt;actions&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;mailbox&lt;/span&gt; &lt;span class="kt"&gt;Reset&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Our &lt;code&gt;actions&lt;/code&gt; mailbox has a type of &lt;code&gt;Action&lt;/code&gt; and when we create our mailbox we supply it an example of our &lt;code&gt;Action&lt;/code&gt; type (in this case &lt;code&gt;Reset&lt;/code&gt;). Often you'll see action types defined with a &lt;code&gt;NoOp&lt;/code&gt; and that is often used as an example. Either way he have our mailbox.&lt;/p&gt;&lt;h2&gt;Reactive Model&lt;/h2&gt;&lt;p&gt;Next up we need to take our initial state and turn it into a reactive model. So that when an action message is received it will cause the &lt;code&gt;update&lt;/code&gt; method to alter our state. Now this part is, IMHO, just the cleverest little pattern. The reactive model is just a fold over the signal that is a property of our mailbox starting with our initialState&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="nf"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;foldp&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="n"&gt;initialModel&lt;/span&gt; &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;OK technically it's a &lt;code&gt;foldp&lt;/code&gt; which is a special signal variant of &lt;code&gt;fold&lt;/code&gt; that can yield intermediate results (as an when actions are generated). But thats it. Starting with our initial data when actions occur call the update method - the result of which is yielded by &lt;code&gt;foldp&lt;/code&gt;. We have our reactive model.&lt;/p&gt;&lt;h2&gt;Mapping Main&lt;/h2&gt;&lt;p&gt;Finally we need to replace our &lt;code&gt;main&lt;/code&gt; method and wire up all these parts to create our working application. &lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt;
&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now &lt;code&gt;main&lt;/code&gt; becomes a &lt;code&gt;map&lt;/code&gt; over the view using our &lt;code&gt;model&lt;/code&gt; (the one we made reactive in the last step). As &lt;code&gt;view&lt;/code&gt; takes both an &lt;code&gt;address&lt;/code&gt; and a &lt;code&gt;model&lt;/code&gt; so we create a partially applied or curried function of &lt;code&gt;(view actions.address)&lt;/code&gt; that takes the address from our mailbox. So when the model updates the &lt;code&gt;map&lt;/code&gt; will cause the view function to be called with the new state which will build our HTML representation and yield a signal value with that HTML representation into the core of Elms rendering engine. Once again the &lt;code&gt;map&lt;/code&gt; function (from the &lt;code&gt;Signal&lt;/code&gt; package) yields intermediate responses.&lt;/p&gt;&lt;h2&gt;And rest...&lt;/h2&gt;&lt;p&gt;So that covers the basic architecture of a typical Elm application. You can see how &lt;code&gt;start-app&lt;/code&gt; removes some of the extra steps required to wire up an Elm application and would certainly lower the barrier of entry for people new to Elm. That said there are times you want access to the Mailbox (such as when trying to merge in external signals from &lt;code&gt;ports&lt;/code&gt;) and &lt;code&gt;start-app&lt;/code&gt; hides the mailbox it creates on your behalf.&lt;/p&gt;&lt;p&gt;The underlying principles of Elm are surprisingly simple yet very powerful. They aren't niche concepts either. Fundamentally they are common functional patterns which means, with a modicum of functional thinking, that application design is less made complex as we have to incur less cost in understanding concepts specific to Elm.&lt;/p&gt;</content></entry><entry><title>Zombie Dice Score Card in Elm</title><updated>Wed Jul 29 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/07/29/zombie-dice-score-card-in-elm/"></link><id>urn:yobriefca-se:feed:post:Zombie Dice Score Card in Elm</id><content type="html">&lt;p&gt;&lt;a href="https://yobriefca.se/blog/2015/07/28/window-dot-prompt-in-elm/"&gt;As promised&lt;/a&gt; yesterday evening I spent some time re-writing my &lt;a href="https://yobriefca.se/blog/2015/07/20/zombie-dice-score-card-with-reagent/"&gt;Zombie Dice Score Card&lt;/a&gt;, originally written in ClojureScript and &lt;a href="http://reagent-project.github.io/"&gt;Reagent&lt;/a&gt;, in &lt;a href="http://elm-lang.org"&gt;Elm&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;If you just want to poke around the code then the source for each implementation is available on Github,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href="https://github.com/kouphax/zombie-dice-elm"&gt;The Elm implementation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/kouphax/zombie-dice"&gt;The ClojureScript implementation&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;If you want a bit of an experience report then read on.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;This work was done after spending an evening with some &lt;a href="https://pragmaticstudio.com/elm"&gt;Elm screencasts&lt;/a&gt; and some internet research I cannot promise this is &lt;strong&gt;good&lt;/strong&gt; or &lt;strong&gt;correct&lt;/strong&gt; code and I'll welcome any &lt;a href="https://twitter.com/kouphax"&gt;feedback via Twitter&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Developing the Elm version of the app took the best part of 3 or 4 hours once I discovered how to &lt;a href="https://yobriefca.se/blog/2015/07/28/window-dot-prompt-in-elm/"&gt;integrate with JavaScript via ports&lt;/a&gt;. All in all I'd say there is about 6 or 7 hours work to craft my first Elm app with only a couple of hours introduction to the language. This should be seen as a positive reflection on &lt;strong&gt;Elm&lt;/strong&gt; and not me. &lt;/p&gt;&lt;h2&gt;Types&lt;/h2&gt;&lt;p&gt;My previous experience with type focused languages was a few years of general purpose Scala development and a brief foray into Haskell which ended in mild frustration. I'm certainly more comfortable in dynamic languages and if I was ever bored enough to be drawn into a "dynamic vs static" debate I'd probably side on the dynamic side. With that said picking apart my ClojureScript implementation and recreating it in Elm has really made me appreciate its typed nature. The ability to look at our defined types e.g.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;       &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;     &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;gamesWon&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;score&lt;/span&gt;    &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="kt"&gt;ScoreBoard&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;players&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uid&lt;/span&gt;     &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This explicit declaration really calls out stuff that, in ClojureScript (or &lt;strong&gt;my&lt;/strong&gt; ClojureScript code at least), is implied and typically scattered over the place. For example, in the code below I'm just pulling out keys from a map &lt;code&gt;:id&lt;/code&gt;, &lt;code&gt;:games-won&lt;/code&gt; etc. because &lt;strong&gt;I know&lt;/strong&gt; these things should exist. &lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;finish-game&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;players&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;vals &lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nv"&gt;scoreboard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="nv"&gt;winner&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;reduce &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;&amp;gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:score&lt;/span&gt; &lt;span class="nv"&gt;%1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:score&lt;/span&gt; &lt;span class="nv"&gt;%2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="nv"&gt;%1&lt;/span&gt; &lt;span class="nv"&gt;%2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;players&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;swap!&lt;/span&gt; &lt;span class="nv"&gt;scoreboard&lt;/span&gt; &lt;span class="nv"&gt;update-in&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt; &lt;span class="nv"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ss"&gt;:games-won&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nv"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reset-scores&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Last night when I revisited this code I had forgotten what my model looked like and I had to figure it out by scanning all my code for when things are set and read from my data structure representing my state. I'm sure I could structure my ClojureScript better, perhaps use something like &lt;a href="https://github.com/clojure/core.typed"&gt;core.typed&lt;/a&gt; or &lt;a href="https://github.com/Prismatic/schema"&gt;Prismatic Schema&lt;/a&gt;, but the onus is on the developer. Of course this is not a new and startling revelation this is just one of the many difference in dynamic/static languages. So what does Elm do to make this different? Well, for one, it uses gradual typing. I can declare a function like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;newPlayer&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;       &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;     &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;gamesWon&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;score&lt;/span&gt;    &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This will work fine and the Elm compiler will infer the types and shout at me if I'm passing ambiguous types around. However I can solidify my intentions by adding a type signature for the function,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;newPlayer&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Player&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When I initially started dipping into Elm I thought these signatures would be an after thought for me. I'd only add them in for completeness sake. I was wrong. They are extremely useful and helped massively as I designed my implementation. They also improve the error messages. Scala has this sort of type inference as well but I almost always left it out unless required. Perhaps I've got wiser in my old age or maybe Elm has made the type system more accessible.&lt;/p&gt;&lt;h2&gt;Error Messages&lt;/h2&gt;&lt;p&gt;When something goes wrong in Clojure or ClojureScript the resulting stacktraces can be next to useless for figuring out what is actually wrong. No amount of formatting and colouring improves this, sometimes they are just confusing. Error messages in Elm are the complete opposite.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;-- TYPE MISMATCH  ZombieDice.elm

The type annotation for `playerEntry` does not match its definition.

98| playerEntry: Address Action -&amp;gt; Player -&amp;gt; Html
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Looks like a record is missing the field `uid`

As I infer the type of values flowing through your program, I see a conflict
between these two types:

    Player

    { a | score : Int, uid : Int, gamesWon : Int, name : String } Use --force to continue.
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Just look at that message. It's telling me exactly what is wrong and I knew how to fix it once I'd read this message. Most Elm messages are like this although some of the type related ones did make me scratch my head until I wrapped my head around them a bit more.&lt;/p&gt;&lt;h2&gt;HTML&lt;/h2&gt;&lt;p&gt;Hiccup style syntax in ClojureScript that we use for describing HTML structure is lovely.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:div.row&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:div.col.span_3.name&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:h2&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="nv"&gt;player&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;for &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;win&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:games-won&lt;/span&gt; &lt;span class="nv"&gt;player&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
     &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:src&lt;/span&gt; &lt;span class="s"&gt;"images/gold-brain.png"&lt;/span&gt; &lt;span class="ss"&gt;:height&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;}])&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:div.col.span_3&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:image.brain&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:src&lt;/span&gt; &lt;span class="s"&gt;"images/brain.png"&lt;/span&gt;
                   &lt;span class="ss"&gt;:on-click&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;update-score&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt; &lt;span class="nv"&gt;player&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;}]&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:span.score&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:small&lt;/span&gt; &lt;span class="s"&gt;" x "&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:score&lt;/span&gt; &lt;span class="nv"&gt;player&lt;/span&gt;&lt;span class="p"&gt;)]]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;With paredit enabled creating this sort of structure is an absolute breeze and rather easy to comprehend after it's been written. The same, i'm afraid, can not be said for Elm,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;  &lt;span class="n"&gt;div&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="s"&gt;"row"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;div&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="s"&gt;"col span_3 name"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;h2&lt;/span&gt;
            &lt;span class="kt"&gt;[]&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
          &lt;span class="n"&gt;span&lt;/span&gt;
            &lt;span class="kt"&gt;[]&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;always&lt;/span&gt; &lt;span class="n"&gt;wonGame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gamesWon&lt;/span&gt;&lt;span class="p"&gt;)])],&lt;/span&gt;
      &lt;span class="n"&gt;div&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="s"&gt;"col span_3"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;img&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="s"&gt;"brain"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="n"&gt;src&lt;/span&gt;   &lt;span class="s"&gt;"images/brain.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
              &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Inc&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="kt"&gt;[]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;span&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="kr"&gt;class&lt;/span&gt; &lt;span class="s"&gt;"score"&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;small&lt;/span&gt;
                &lt;span class="kt"&gt;[]&lt;/span&gt;
                &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"x"&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
              &lt;span class="n"&gt;span&lt;/span&gt;
                &lt;span class="kt"&gt;[]&lt;/span&gt;
                &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toString&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;  &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is formatted, I think, using the recommended formatting advice and while it is relatively easy to read and get to the place you want to make changes it caused me a lot pain creating it and refactoring it as I added new features. I almost always misjudged bracket placement and forgot commas resulting in errors. Perhaps it's the fact paredit gives me so much power and ClojureScript doesn't use commas (they're treated as whitespace) but this was painful for me to create.&lt;/p&gt;&lt;h2&gt;It's just one big fold&lt;/h2&gt;&lt;p&gt;The architecture of an Elm app is always the same and it's really rather clever. However there is a bit of initial setup to get everything working. To this end you can use the &lt;a href="https://github.com/evancz/start-app"&gt;start-app package&lt;/a&gt; that removes a lot of the initial boilerplate (there's not really &lt;strong&gt;that much&lt;/strong&gt; boilerplate in reality). I didn't use this package as I wanted to really understand how everything was wired together.&lt;/p&gt;&lt;p&gt;This led to my big revelation. The root of an Elm app is just a map our reactive model that returns the updated view as and when our state changes. The reactive model part is just a fold over the application data which gets updated actions/signals happen that cause the state to transition to another state. &lt;/p&gt;&lt;p&gt;The entire app updates when something changes the state. &lt;/p&gt;&lt;p&gt;No management of discrete little parts and internal state - just rebuild the world. This is the same principle behind React and Reagent (a ClojureScript React wrapper) but in React the mechanisms are somewhat hidden. In Elm it's right there. You can compose different signals, à la Reactive Extensions Observables, to make your app behave in different ways. For example,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;model&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt;
&lt;span class="nf"&gt;model&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="kr"&gt;let&lt;/span&gt;
    &lt;span class="n"&gt;allActions&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mergeMany&lt;/span&gt;
      &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;externalActions&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="kr"&gt;in&lt;/span&gt;
    &lt;span class="n"&gt;foldp&lt;/span&gt; &lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="n"&gt;initialModel&lt;/span&gt; &lt;span class="n"&gt;allActions&lt;/span&gt;
    
&lt;span class="nf"&gt;externalActions&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt;
&lt;span class="nf"&gt;externalActions&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;mergeMany&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;~&lt;/span&gt; &lt;span class="n"&gt;addPlayer&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this example from the app I merge the actions signal that comes from the apps main Mailbox (this is the one that streams all the internal Actions to update the model) and external signals (these are the ones that are generated by JavaScript outside of the Elm app). &lt;code&gt;externalActions&lt;/code&gt; does some manipulation of it's incoming data to create a signal of type &lt;code&gt;Action&lt;/code&gt; that can be merged with our core Action signal.&lt;/p&gt;&lt;p&gt;While I have likely explained this in the worst possible way this approach makes everything wonderfully composable and straightforward. As soon I saw how Elm approached reactive application development I was just blown away by its simplicity.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;I like Elm. Go try it. I can't really say one implementation of my little app is better than the other each has it's good and bad points. Besides I haven't spent enough time with Elm to evaluate it properly yet. All I know is that I've taken to it better than other languages of its ilk.&lt;/p&gt;&lt;p&gt;I'm not sure why it's type system feels more accessible to me compared to Scala or Haskell but I've found that the barrier of entry for Elm is much less than either of those languages. There seems to be, compared to Scala at least, much less concepts to wrap your brain around while still remaining extremely powerful. &lt;/p&gt;&lt;p&gt;Finally I recommend you check out &lt;a href="https://www.youtube.com/watch?v=oYk8CKH7OhE"&gt;this talk&lt;/a&gt; by Evan Czaplicki (creator of Elm) around the design decisions that help make Elm more accessible.&lt;/p&gt;</content></entry><entry><title>window.prompt() in Elm</title><updated>Tue Jul 28 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/07/28/window-dot-prompt-in-elm/"></link><id>urn:yobriefca-se:feed:post:window.prompt() in Elm</id><content type="html">&lt;blockquote&gt;&lt;p&gt;I say "standard stuff" a few times in this article. When I say that I assume, like me, you've dipped your toes into Elm. If that is not the case and you want to change that situation then I recommend go give &lt;a href="https://pragmaticstudio.com/elm"&gt;Pragmatic Studios&lt;/a&gt; your money. I was in no way paid or influenced to say that BTW.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Having spent last night watching the Pragmatic Studios &lt;a href="https://pragmaticstudio.com/elm"&gt;Elm: Building Reactive Web Apps&lt;/a&gt; I am now a fully certified expert in &lt;a href="http://elm-lang.org"&gt;Elm&lt;/a&gt; and so it's time to start writing articles about it. Of course this is nonsense but some of my adventures have inspired me to write an article or two that may be useful to others (and my future self).&lt;/p&gt;&lt;p&gt;I &lt;a href="https://yobriefca.se/blog/2015/07/20/zombie-dice-score-card-with-reagent/"&gt;recently documented&lt;/a&gt; a little hobby project I worked on for keeping score on games of Zombie Dice. The app was written in ClojureScript and after I had wrapped my head around Elm I decided to see what the same app would look like in Elm. This article &lt;strong&gt;isn't&lt;/strong&gt; about that, but rather a small part of it that I found quite challenging.&lt;/p&gt;&lt;p&gt;Unlike ClojureScript (and Clojure) whose philosophy is to embrace the underlying host environment Elm abstracts it away. It's not gone completely it's just not as prevalent or easy to access. In my &lt;a href="https://yobriefca.se/zombie-dice/"&gt;Zombie Dice Score Card&lt;/a&gt; app adding new players uses the standard JavaScript host function &lt;code&gt;window.prompt&lt;/code&gt; to capture the name and I wanted to replicate this functionality in my Elm implementation.&lt;/p&gt;&lt;h2&gt;Basic Implementation&lt;/h2&gt;&lt;p&gt;My first pass at this resulted in a working implementation (well not my &lt;strong&gt;actual&lt;/strong&gt; first pass it was many, &lt;strong&gt;many&lt;/strong&gt; passes before I even figured out what the hell I was doing). While it wasn't going to be practical for my actual needs it did form the basis of something useful that I could build upon.&lt;/p&gt;&lt;p&gt;The key is to using &lt;code&gt;port&lt;/code&gt;s. A &lt;a href="http://elm-lang.org/guide/interop#ports"&gt;port&lt;/a&gt; acts as a bridge between Elm and JavaScript. They either go &lt;code&gt;in&lt;/code&gt; to Elm or &lt;code&gt;out&lt;/code&gt; of Elm. You declare a &lt;code&gt;port&lt;/code&gt; in Elm like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;suppliedNames&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is &lt;code&gt;port&lt;/code&gt; is an innie. You can tell it is an innie because you supply no actual definition. Instead we can &lt;code&gt;send&lt;/code&gt; things to it from the JavaScript side.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;suppliedNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"James"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Assuming, in our HTML file, our Elm app is created and assigned to a variable &lt;code&gt;app&lt;/code&gt; we will have this &lt;code&gt;ports&lt;/code&gt; object that lists all the ports we expose from our app. This will have our &lt;code&gt;suppliedNames&lt;/code&gt; port which will have a method of &lt;code&gt;send&lt;/code&gt; that we can use to signal values through that port.&lt;/p&gt;&lt;p&gt;One other thing is that these innies must be given an initial value when the app is constructed or you'll get an exception. We can do it like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Elm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullscreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Elm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Confirm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;suppliedNames&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So when we boot our app we pass in an initial value.&lt;/p&gt;&lt;p&gt;Lets look at another port,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;totalCapacity&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;totalCapacity&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;toString&lt;/span&gt; &lt;span class="n"&gt;somethingElse&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This one is an outtie. This is used to signal things &lt;strong&gt;out&lt;/strong&gt; of our Elm app into JavaScript. In this case it sends out the sum of some data structure as and when it changes. We can listen to these signals in JavaScript like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;totalCapacity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Outtie ports have a &lt;code&gt;subscribe&lt;/code&gt; method generated for them that takes a function accepting the payload. In our case we simply log it.&lt;/p&gt;&lt;p&gt;Now onto our specific use case. My needs are slightly different to the samples because while they broadcast data out of the Elm side I just want some sort of trigger to say "go ahead and open a prompt". This is 100% impure in that it is &lt;strong&gt;only&lt;/strong&gt; used for side effects and I've found that with Elm being a rather pure language this sort of requirement feels kind of awkward. Thats not a criticism, just an observation. You should expect bad things to feel awkward, it helps you minimise them.&lt;/p&gt;&lt;p&gt;The JavaScript/HTML side of our application is fairly unsurprising (assuming you've not skipped the last few paragraphs).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="html"&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE HTML&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  	  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"confirm.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Elm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fullscreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Elm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Confirm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;suppliedNames&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Players name?"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;suppliedNames&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here we, &lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;boot our app passing in a blank value for &lt;code&gt;suppliedNames&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;subscribe&lt;/code&gt; to a &lt;code&gt;prompt&lt;/code&gt; port which will display the &lt;code&gt;window.prompt&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;send&lt;/code&gt; the captured value from &lt;code&gt;window.prompt&lt;/code&gt; back into our app&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The Elm side of things is where the real stuff happens,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Confirm&lt;/span&gt; &lt;span class="kr"&gt;where&lt;/span&gt;

&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html&lt;/span&gt;        &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Html.Events&lt;/span&gt; &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Signal&lt;/span&gt;      &lt;span class="n"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- SIGNALS &lt;/span&gt;
&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;NoOp&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Prompt&lt;/span&gt;

&lt;span class="nf"&gt;actions&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Mailbox&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt;
&lt;span class="nf"&gt;actions&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;Signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mailbox&lt;/span&gt; &lt;span class="kt"&gt;NoOp&lt;/span&gt;

&lt;span class="c1"&gt;-- PORTS --&lt;/span&gt;
&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;suppliedNames&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;confirm&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="nb"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;confirm&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kt"&gt;Prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;NoOp&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;always&lt;/span&gt; &lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;-- VIEWS --&lt;/span&gt;
&lt;span class="nf"&gt;view&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="kt"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="kt"&gt;Prompt&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="s"&gt;"Set Name"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

&lt;span class="c1"&gt;-- APP -&lt;/span&gt;
&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;Html&lt;/span&gt;
&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="n"&gt;suppliedNames&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Given there is a fair few things happening here lets have a look at the interesting bits.&lt;/p&gt;&lt;p&gt;First of all we set up the various operations our application will perform and create a &lt;code&gt;Mailbox&lt;/code&gt; that can be used to send actions to. If you've ever tinkered with Elm this should be pretty common,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="kr"&gt;type&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;NoOp&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kt"&gt;Prompt&lt;/span&gt;

&lt;span class="nf"&gt;actions&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Mailbox&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt;
&lt;span class="nf"&gt;actions&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
    &lt;span class="kt"&gt;Signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mailbox&lt;/span&gt; &lt;span class="kt"&gt;NoOp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;Prompt&lt;/code&gt; action is the one we want to use to trigger our &lt;code&gt;window.prompt&lt;/code&gt; call via a port. Next we declare our actual ports,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;suppliedNames&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="kt"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt; &lt;span class="nb"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;port&lt;/span&gt; &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signal&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;\&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="ow"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kt"&gt;Prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;NoOp&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;always&lt;/span&gt; &lt;span class="nb"&gt;()&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;suppliedNames&lt;/code&gt; is our innie. This will receive names we've created later on. &lt;code&gt;prompt&lt;/code&gt; is our outtie. What we do is &lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;take the signal from our &lt;code&gt;Mailbox&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;filter&lt;/code&gt; everything except the &lt;code&gt;Prompt&lt;/code&gt; actions&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;map&lt;/code&gt; these values an &lt;code&gt;always&lt;/code&gt; return a &lt;code&gt;unit&lt;/code&gt; value&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Why &lt;code&gt;unit&lt;/code&gt;? Well we don't care about the actual value we just want to reach out to the JavaScript when we get a &lt;code&gt;Prompt&lt;/code&gt; action through our mailbox. Any value is meaningless so lets just go for the &lt;strong&gt;most meaningless&lt;/strong&gt; one we can find.&lt;/p&gt;&lt;p&gt;Then in our &lt;code&gt;view&lt;/code&gt; we have a &lt;code&gt;button&lt;/code&gt; that sends a &lt;code&gt;Prompt&lt;/code&gt; action to our Mailbox,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;button&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="n"&gt;actions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="kt"&gt;Prompt&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Again this should be fairly unsurprising to the Elm-ites (or whatever the collective term is) reading this.&lt;/p&gt;&lt;p&gt;Finally we have our &lt;code&gt;main&lt;/code&gt; that wires it all together,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="haskell"&gt;&lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="ow"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Signal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="n"&gt;suppliedNames&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Again - standard stuff.&lt;/p&gt;&lt;h2&gt;Notes&lt;/h2&gt;&lt;p&gt;As the last title indicated this is just a basic implementation. It requires a bit more work to integrate into an application that requires a more complicated model but its a good starting point. I know this because I've already begun extending it to create the Elm version of my &lt;a href="https://yobriefca.se/zombie-dice/"&gt;Zombie Dice Score Card&lt;/a&gt; which I'll dissect in another article soon.&lt;/p&gt;&lt;p&gt;Now I'm not saying this is idiomatic Elm, I'm not even saying this is a &lt;strong&gt;recommended&lt;/strong&gt; way to do things but it works and "feels" good enough to me. If you want to comment/fix/critique/whatever my work then you can get me on &lt;a href="https://twitter.com/kouphax"&gt;Twitter&lt;/a&gt; and let fly the dogs of conversation.&lt;/p&gt;</content></entry><entry><title>Zombie Dice Score Card</title><updated>Mon Jul 20 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://github.com/kouphax/zombie-dice"></link><id>urn:yobriefca-se:feed:post:Zombie Dice Score Card</id><content type="html"></content></entry><entry><title>Zombie Dice Score Card with Reagent</title><updated>Mon Jul 20 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/07/20/zombie-dice-score-card-with-reagent/"></link><id>urn:yobriefca-se:feed:post:Zombie Dice Score Card with Reagent</id><content type="html">&lt;p&gt;Over the weekend I built a &lt;a href="/zombie-dice"&gt;Zombie Dice Score Card&lt;/a&gt;. I built it because I keep forgetting to get pen and paper for keeping score but mostly because I wanted to build something simple in ClojureScript again.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/zombie-dice.png" alt="Zombie Dice Score Card" _="_" /&gt;&lt;/p&gt;&lt;p&gt;The app is pretty basic, no server side interaction, no local storage interaction and only a few basic actions/features. You simply,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Add players&lt;/li&gt;
  &lt;li&gt;Play zombie dice as usual, rewarding points by clicking the necessary brain on the screen.&lt;/li&gt;
  &lt;li&gt;When someone reaches 13 brains you'll be told to finish the round (all remaining players yet to go on this round have a final chance at stealing the win)&lt;/li&gt;
  &lt;li&gt;Hitting 'Finish Game' will reward the person with the highest number of brains a gold brain and the next game begins.&lt;/li&gt;
  &lt;li&gt;At any time you can reset the current game scores or clear the entire board.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;For the curious the stack used is as follows,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;ClojureScript&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/martinklepsch/tenzing"&gt;Tenzing&lt;/a&gt; - Leiningen template for creating Boot powered client side only ClojureScript applications.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://boot-clj.com/"&gt;Boot&lt;/a&gt; - The new build system on the Clojure(Script) block offering a more programatic approach to build systems (i.e more like Rake than Maven)&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://reagent-project.github.io/"&gt;Reagent&lt;/a&gt; - A simple ClojureScript wrapper around &lt;a href="http://facebook.github.io/react/"&gt;React&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/lbradstreet/cljs-uuid-utils"&gt;cljs-uuid-utils&lt;/a&gt; - ClojureScript library for generating and working with UUID's&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Tenzing&lt;/h2&gt;&lt;p&gt;This is a nice little template for prototyping with ClojureScript or in situations where you can integrate with external services for your server side needs. It's a conditional template that lets you add technologies that you think you'll need. For example I generated the Zombie Dice Score Card project with,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;lein new tenzing zombie-dice +sass +reagent +divshot
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This template gives me an app with &lt;code&gt;SASS&lt;/code&gt;, &lt;code&gt;Reagent&lt;/code&gt; and &lt;a href="https://divshot.com/"&gt;Divshot&lt;/a&gt; configuration and all the necessary build pipeline stuff set up to compile CLJS and SASS when files change. I didn't use &lt;a href="https://divshot.com/"&gt;Divshot&lt;/a&gt; this time around but it's nice to know the option was there.&lt;/p&gt;&lt;p&gt;On another note I did find it a bit weird using Leiningen to generate a Boot application but I guess most Clojure(Script) developers are going to have Leiningen available and Boot doesn't provide templating support.&lt;/p&gt;&lt;h2&gt;Boot&lt;/h2&gt;&lt;p&gt;I didn't need to dive into Boot too much for this tiny project but I do like what I see. The way tasks are defined in Clojure and composed into pipelines using standard Clojure idioms makes creating build pipelines really rather nice.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;deftask&lt;/span&gt; &lt;span class="nv"&gt;build&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;comp &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cljs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sass&lt;/span&gt; &lt;span class="ss"&gt;:output-dir&lt;/span&gt; &lt;span class="s"&gt;"css"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;deftask&lt;/span&gt; &lt;span class="nv"&gt;run&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;comp &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;watch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cljs-repl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You see &lt;code&gt;run&lt;/code&gt; composes a pipeline of other tasks for serving the application, watching for filesystem changes, connecting to a ClojureScript REPL and reloading the application. It then composes another defined task for building the ClojureScript and SASS.&lt;/p&gt;&lt;h2&gt;Reagent&lt;/h2&gt;&lt;p&gt;Aside from a name change Reagent (formerly Cloact) hasn't really change that much since I talked about in &lt;a href="https://yobriefca.se/blog/2014/01/18/adventures-in-clojure-land/"&gt;January 2014&lt;/a&gt;. Sure they've added some new features and refined the API a bit but its approach is still the same. It provides simple approach to using React in a ClojureScript app offering us hiccup style syntax for declaring components along side atoms for state management.&lt;/p&gt;&lt;p&gt;I've never really looked into the other React wrappers so I can't speak to whether Reagent is the best but I can say it's certainly been a pleasure to use on small scale apps.&lt;/p&gt;&lt;h2&gt;Source&lt;/h2&gt;&lt;p&gt;The source for the app is available, as always, on &lt;a href="https://github.com/kouphax/zombie-dice/"&gt;Github&lt;/a&gt; and I welcome any feedback or advice you may have.&lt;/p&gt;&lt;h2&gt;Plans&lt;/h2&gt;&lt;p&gt;I think I'm going to write this in Elm just to see what all the fuss is about. Maybe add some local storage for saving historical games and players. Truth be told I'll probably never touch it again but a man can dream.&lt;/p&gt;&lt;h2&gt;Footnote&lt;/h2&gt;&lt;p&gt;Have you ever played &lt;a href="http://www.sjgames.com/dice/zombiedice/"&gt;Zombie Dice&lt;/a&gt;? If not you should give it a go it's a simple, quick dice based game that you can explain in about 3 minutes and have a full game done in about 15 or 20 minutes. The aim is simple, try and collect/eat 13 brains by rolling dice over a series of rounds. Once someone hits 13 brains everyone finishes that round and the person that has the highest score wins that game. Typically you play best of 3 or 5 games.&lt;/p&gt;</content></entry><entry><title>Meteor: An Introduction by an Expert Beginner</title><updated>Wed Jul 01 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="/presentations/meteor.pdf"></link><id>urn:yobriefca-se:feed:post:Meteor: An Introduction by an Expert Beginner</id><content type="html"></content></entry><entry><title>Isolation</title><updated>Wed Jun 24 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/06/24/isolation/"></link><id>urn:yobriefca-se:feed:post:Isolation</id><content type="html">&lt;p&gt;One morning you woke up and things were different. Not hugely different - just ever so slightly. Almost imperceptibly so. You didn't really notice it at first but eventually you felt that something seemed "off". You had got up, had breakfast, brushed your teeth, cycled to work, showered, made a coffee and sat down at your desk and you just sensed it.&lt;/p&gt;&lt;p&gt;Do you remember when you were a kid and you lost one of your favourite toys but found it again months later after having given up on the search. Did you ever get that weird sensation that it was much smaller, maybe less detailed than you remember? Well you're getting that feeling now, the space around you just "feels" smaller. &lt;/p&gt;&lt;p&gt;After a while you notice it. A wall. That wall wasn't there before - you are sure of it. It doesn't actually look new; in fact it doesn't even look out of place but you're positive it wasn't there before.&lt;/p&gt;&lt;p&gt;The next week, while going to another floor in the office to visit a work colleague, you notice another strange thing. One of the office doors isn't there anymore. As far as you recall there used to be another office between those two doors on the far wall, you'd had a meeting there once and you would swear to it. Other people don't seem bothered - "it looks the same to me" they say - and it's not as if there is evidence of some fresh plastering in the space. It just seems, well, smaller.&lt;/p&gt;&lt;p&gt;Over the next few months to a year little things like this happen. You notice, or at least feel, that things are getting smaller. There is less space to move around and even more widely there are less people occupying the space. Even places you've never been before feel significantly more constrained. There are people you haven't spoken in a while, you don't miss these people immediately because they aren't a really part of your core pack of people you see on a regular basis, but you did sense them missing.&lt;/p&gt;&lt;p&gt;On the moments you take to reflect on your life you notice just how small everything is getting. How compact, how claustrophobic everything is. Basic math tells you that there must be more space between these walls but there are no doors.&lt;/p&gt;&lt;p&gt;One day you wake up and go to your office but looking around you're not even sure how you really got there. There are no other doors just the one door from your house to your office.&lt;/p&gt;&lt;p&gt;You feel alone.&lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;Isolation can happen for many reasons both internal and external. The reasons can be anything from mental state, actions to something as simple as an inflexible and strongly held opinion. &lt;/p&gt;&lt;p&gt;Individuals become isolated a team does not, not as readily at least. Teams, genuine teams of people that care about each other and the work they do, create an internal supportive culture. Individuals problems can be diminished through collective support. Toxic attitudes can be identified and a resolution can be reached.&lt;/p&gt;&lt;p&gt;In the same way a toxic attitude does not thrive in a strong team, a good team cannot thrive in an unsupportive or constrained organisational culture be it in a company or community. We may talk about how culture is important but I don't think we all consider what that means. Being inclusive and supportive requires real effort at all levels of an organisation.&lt;/p&gt;</content></entry><entry><title>Road Trippin'</title><updated>Mon Jun 15 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/06/15/road-trippin/"></link><id>urn:yobriefca-se:feed:post:Road Trippin'</id><content type="html">&lt;blockquote&gt;&lt;p&gt;This is a ramble. There is little originality in this post. It's quite long. There are some pictures. I'm OK with all these things.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Projects are a bit like road trips&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. You know where you want to go and roughly how you think you'll get there, you have your team of people joining you on the journey and no amount of planning is going to ensure you get to where you want to be when you want to be there. That's just happenstance.&lt;/p&gt;&lt;h2&gt;GPS&lt;/h2&gt;&lt;p&gt;If projects are road trips then the GPS is your plan. Before you start off you plonk in your destination and after some internal calculations you get an approximate estimated time of arrival. It knows this because it &lt;strong&gt;assumes&lt;/strong&gt; it knows what route you'll take and it knows a little bit about the roads that make up that route. From this data it is able to extrapolate an &lt;strong&gt;estimated&lt;/strong&gt; time of arrival and total milage. &lt;/p&gt;&lt;p&gt;Most projects still have some element of an upfront plan. It may be very lightweight, just a figure like our GPSs ETA or it may be more detailed like the GPSs routing. Something upfront is useful for budgeting (or the approval of) and determining peoples availability for the project&lt;a name="_2"&gt;&lt;/a&gt;&lt;a href="#2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/ganntchart.png" alt="This is what makes me cry at night" _="_" /&gt;&lt;/p&gt;&lt;p&gt;The GPS and your project plan are both &lt;strong&gt;snapshots&lt;/strong&gt; of the &lt;strong&gt;current&lt;/strong&gt; state of play based on the knowledge they currently have. That massive 2 year Gantt chart that goes right down to the day level that you spent weeks on at the start of the project is immediately useless the day the project starts. The fact it may sometimes appear to be correct is purely by chance. Without taking current and historical data into account and updating the plan your information is lying to you. Having a flexible mechanism for feeding this real time information into your plan quickly is vital - an A1 dead tree Gantt chart mounted on your teams wall should be set on fire and the ashes scattered to the wind.&lt;/p&gt;&lt;p&gt;The plan itself is &lt;strong&gt;NOT&lt;/strong&gt; a goal - it is a guess that gets better the closer to the real result you get. &lt;/p&gt;&lt;p&gt;I repeat the plan itself is &lt;strong&gt;NOT&lt;/strong&gt; a goal.&lt;/p&gt;&lt;h2&gt;Roadworks and accidents&lt;/h2&gt;&lt;p&gt;There are some things a GPS system can't predict namely short term roadworks and accidents. Many systems incorporate some sort of up to date information about these things but not all. The minute you encounter a tailback from an accident or some other unpredictable slowdown in traffic the ETA starts slipping as the GPS uses the incoming data to readjust.&lt;/p&gt;&lt;p&gt;To reiterate - any form of project plan has to be able to use all available data. You can't simply assume the plan you came up with at your most ignorant is still holding true. The quicker you can adjust the plan the quicker you can notice trends and react accordingly. Overly detailed plans are usually both wrong and out of date and in that state the only thing they are good for is finger pointing and blame laying - two things that will ruin a teams morale and further damage the project.&lt;/p&gt;&lt;h2&gt;Don't Miss the Boat&lt;/h2&gt;&lt;p&gt;Of course the end destination (or even various destinations on your trip) may be time dependant. Perhaps if you have to catch a ferry. They may also be cost dependant too of course. Maybe you're a band of cash strapped musicians with £30 left to the next gig to cover food and petrol. Who knows, this is your fantasy - live it.&lt;/p&gt;&lt;p&gt;Anyway the GPS's ETA is still &lt;strong&gt;not&lt;/strong&gt; a goal. Sure it is an indicator wether you'll meet your goal (or your ferry) on time but you can't blame the GPS for having an ETA that means you'll miss that boat.&lt;/p&gt;&lt;p&gt;In fact you can't even go blaming the manufacturers of the GPS - they developed an algorithm that takes all the information it has and, assuming that information is correct at time of calculation, tells you how long it will take. Things change. Understand that. We just have to do our best to ensure we get there when we need to by making sure we think ahead.&lt;/p&gt;&lt;p&gt;The same is true for projects. We'll not dive into why FPFT (Fixed Price &lt;strong&gt;and&lt;/strong&gt; Fixed Time) projects are just horrible incubators for bad software and toxic environments. Instead lets focus on Fixed Price &lt;strong&gt;or&lt;/strong&gt; Fixed Time. Many projects &lt;strong&gt;need&lt;/strong&gt; to go live at a certain date. Incoming legislation may require it. The marketing department may have already made public announcements. Many projects can only get a certain amount of money because red tape demands it or a customers accounting practises are apply practises that don't fit well in the IT sector. The reasons exist for fixing some aspect of the project and they are many. &lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/fixedtimefixedcost.png" alt="Restrictions means sacrifice" _="_" /&gt;&lt;/p&gt;&lt;p&gt;We need to ensure we can manoeuvre within these constraints. When something slips, and it will happen, we need to better understand how we can deal with this instead of trying &lt;strong&gt;manage expectations&lt;/strong&gt; or deferring the truth until its too late. &lt;/p&gt;&lt;p&gt;It still amazes me that projects will spend longer on a piece of relative fiction like a detailed plan upfront yet pretty much skip any honest talk about what happens when the proverbial hits the fan and how to prevent such things. &lt;/p&gt;&lt;p&gt;Remember our GPS and its ETA? Well a plan that can be updated quickly and reflect the current state of play quicker. Make it visible. Like that little ETA on our GPS display that everyone can see. At any point in time, based on what we know, anyone can see if we are slipping and we can work out how to deal with a small slippage rather than waiting until near the end and realising you've missed the boat by a 1/2 a day.&lt;/p&gt;&lt;h2&gt;Come See "The Largest Toenail in the World"&lt;/h2&gt;&lt;p&gt;Road trips can be dull, even with the sweetest tunes and funnest friends, you'll realise that other peoples music sucks and everyone else has fallen asleep. You need to break up the monotony and you can do this by stopping at tourist spots. Get out, stretch the legs, change the scenery, have a nice long wee. Whatever. &lt;/p&gt;&lt;p&gt;Of course the more stops you have the more time it will take to reach the end goal. It may also take you out of your way and eat more fuel. &lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/touristspots.png" alt="Drop them silly features" _="_" /&gt;&lt;/p&gt;&lt;p&gt;But you build this in. You can use the GPS to add via's and give you a more accurate view of how long it will take. You also add contingency. You leave the house a bit earlier to give you some breathing room in case anything or everything goes wrong. You agree that everyone is willing to miss "The Largest Toenail in the World" exhibition if it means shaving off some time to make that ferry or save some petrol. You check that tyre pressure and that oil to make sure the car wont suddenly explode.&lt;/p&gt;&lt;p&gt;Projects are no different except instead of visiting tourist spots we're talking about features. We discover what features people really need. We discover what people &lt;strong&gt;want&lt;/strong&gt; but not &lt;strong&gt;need&lt;/strong&gt;. We accept that should we risk missing our deadline or running out of cash we can drop non-essential features. &lt;/p&gt;&lt;p&gt;We de-risk scary parts of projects by addressing them early or spiking out our understanding of things we don't know yet or are afraid of. We need to question why we have certain features. Who requested that Oculus Rift enabled login screen or gesture based Rich Text Editor? Better yet don't question them. Come to a set of features by asking people who will actually use the system. Them beautiful users. Use them to build what users need. Build prototypes of features &lt;strong&gt;ahead&lt;/strong&gt; of time, through the course of the project, to understand &lt;strong&gt;user needs&lt;/strong&gt; rather than the vision of some middle manager who's never going to actually use the stuff they demand.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/userneeds.png" alt="Talk to your users" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Most importantly &lt;strong&gt;communicate&lt;/strong&gt;. Talk with your team. Talk with your customer. Be honest, be candid. Do it early, do it often. Small change, often, is better than monumental change right before a customer has to walk into a board of Directors and explain why everything is falling apart. People are going to be more open to change if they feel comfortable and trust you.&lt;/p&gt;&lt;p&gt;The more we know the &lt;strong&gt;better&lt;/strong&gt; we deliver (though we may not deliver &lt;strong&gt;more&lt;/strong&gt; or &lt;strong&gt;faster&lt;/strong&gt;). &lt;/p&gt;&lt;h2&gt;Easy Street&lt;/h2&gt;&lt;p&gt;Not all parts of a road trip are fraught with risk like some terrible Mad Max B-Movie clone, some parts you could do blindfolded (not actually blindfolded mind you). Especially the start. &lt;/p&gt;&lt;p&gt;Getting out of the city, the one you where born and raised, is a piece of cake. You know those roads like the back of your hand, you don't even need the GPS at this point. Further on, you may have previously visited a village you're passing through and remember some local tips for avoiding that famous village market rush. You may find that you even shave some time off the journey.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/projectdelivery.png" alt="Real life projects don't behave as you'd expect" _="_" /&gt;&lt;/p&gt;&lt;p&gt;You get this on projects as well. There will be points in the project that you or your team know so well through experience in a given problem or business domain. &lt;/p&gt;&lt;p&gt;It's a double edged sword though. &lt;/p&gt;&lt;p&gt;If you don't recognise this productivity boost is due to previous experience you may feel you're flying through the project smoothly when you may well be in the eye of a storm. If you can identify this as the cause of your new found productivity you can use this opportunity to reduce risk and further understanding. Try spiking out some riskier areas of the project. Imagine this like taking a new route on the way out of your city - it could lead to an even shorter journey. But it may not. At least now you know. You're keeping the risk low though as you are in familiar territory and can get back on track easily. &lt;/p&gt;&lt;p&gt;Take advantage and learn.&lt;/p&gt;&lt;h2&gt;Flat Tyres and Engine Trouble&lt;/h2&gt;&lt;p&gt;Cars - complex metal monsters rolling around on pressurised cushions of air and rubber. No matter how well you look after your car chances are something will go wrong with it. It usually something that can be fixed with a tyre or a fuse or a can of Radweld or something similar. You &lt;strong&gt;could&lt;/strong&gt; call roadside assistance and wait for a few hours but you'd be going nowhere that whole time. But if you or someone your with knows how to change a tyre or replace a fuse you're going to get moving much faster. You may need to come revisit the repair later on but if a quick fix gets you moving (without compromising your safety) then you're going to make much better time overall.&lt;/p&gt;&lt;p&gt;Having a broad level of skill on a project team ensures that downtime for minor problems is minimised. I'm specifically talking about infrastructure and ops but the analogy can span design, testing and project management. The most productive teams I've been involved with have been the ones that can manage their own deployments in a controlled manner. They may have needed help getting the infrastructure up and running with a dedicated Ops team and have occasional reviews with Ops specialists to ensure everything is ship shape but all in all they were self sufficient for the most part.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/notdevops.png" alt="A good team is a cross functional team" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Siloing specialisms may make sense from an organisational perspective but they add additional friction when it comes to the reason that organisation exists - project delivery. Cross functional teams should be nurtured.&lt;/p&gt;&lt;h2&gt;The Town of Lost Souls&lt;/h2&gt;&lt;p&gt;Sometimes your GPS is just not right. Data for out-of-the-way towns can be out of date and it can get you lost. If we're willing to accept stereotypes here, which I will, you're not allowed to get lost and if you do get lost you &lt;strong&gt;must not tell anyone&lt;/strong&gt;. Winding your window down and asking the sweet looking old man how to get to Babbling Brook Lake, even though he so obviously knows exactly how to get there is simply &lt;strong&gt;not allowed&lt;/strong&gt;. Instead, with absolute certainty, you know how to get there you make another lap of the town because you think you saw a sign - burning more daylight and fuel in the process. Eventually you admit you need help, swallow that pride and ask the next person who gives you the best instructions you've ever heard.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/communication.png" alt="A team that talks together stays together" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Communication on projects is essential. I'm not talking about awkward morning stand ups were people pretend everything is OK and waffle about stuff that they may or may have done when in actual fact they haven't even turned their computer on in days because they are scared of the code that refuses to work. I mean open communication in an environment that people feel safe and willing to admit they have problems. Admit you have a problem, allow others to admit the same. Then you can apply &lt;strong&gt;teamwork ointment&lt;/strong&gt; and work through the problem together. &lt;/p&gt;&lt;p&gt;When people are in an environment that makes them cover up their mistakes and issues you end up with a bigger mess. When people are too full of pride or too cocky to admit they are having issues the team is in trouble. &lt;/p&gt;&lt;p&gt;Support and empathy go a long way to making people more productive. &lt;/p&gt;&lt;h2&gt;Sticking to the Plan&lt;/h2&gt;&lt;p&gt;In some parts of Ireland country roads are either completely empty or full of sheep. Get stuck behind a flock, yes that's the collective term apparently, of sheep and you just have to hope they're being driven to a field nearby. A strip of road that would have taken you 5 minutes to travel could now take you 30. However deviating from the GPS and taking an alternate route &lt;strong&gt;could&lt;/strong&gt; save you time. It probably won't take you the 5 minutes the original route was, the GPS generally plots the quickest route, but it may not take you the 30 minutes that sticking to the sheep filled road would. Turning onto the road and allowing the GPS to recalculate may well reveal a better option than the original plan.&lt;/p&gt;&lt;p&gt;Projects are full of potential unknowns (known and unknown) and sometimes you hit roadblocks. Integration pieces may be delayed by a third party not meeting their deadline, hardware requisition may well get bogged down in red tape, people may get &lt;strong&gt;actually&lt;/strong&gt; hit by buses. These things can well cause projects to stall. &lt;/p&gt;&lt;p&gt;Sticking to the plan could result in a significant amount of time where people are twiddling their thumbs waiting for work. Sticking to the plan is not always the best option. In order to keep people moving it may be necessary to find alternative solutions be it in the short or long term. &lt;/p&gt;&lt;p&gt;Changing a plan my seem a bit daunting especially when the impact of sticking to the plan is so vague but a team that is allowed to think and change fast can reduce any associated risk, cost and/or time.&lt;/p&gt;&lt;h2&gt;The Big Finish&lt;/h2&gt;&lt;p&gt;So we've almost made it to the ferry. It's due to leave in 30 minutes and the ETA on the GPS is showing 35 minutes. Lets allows ourselves to get a wee bit fantastical for this last one.&lt;/p&gt;&lt;p&gt;Cue wacky races music.&lt;/p&gt;&lt;p&gt;You've gotta make up time so you push that big red button on the dash marked "In Case of Emergency" and the boosters on the back of your car spring to life. As the G-forces press you into the back of your seat the car careers off the road through barns full of hay, chicken shacks full of eggs, back gardens full of laundry, fences, walls and just about every other trope in 70's cartoons. Just as the ferry is a few meters from the port you and the car make an amazing jump onto the only spare parking space on the ship. &lt;/p&gt;&lt;p&gt;The car covered in sheets, eggs, hay and dirt sags with exhaustion its engine sputters to a halt and a few bits fall of it. You and your travelling companions step out dazed, little birds flutter around your heads. You made it. Now the next leg of your journey can begin.&lt;/p&gt;&lt;p&gt;Most projects have some sort of timed delivery of features. It could be weeks, months or even years (shudder!) but irrespective of the time there will invariably be crunch times. Some worse than others depending on the project, the customer or just your companies approach to project delivery. The one thing about crunch time is that you move into "quick win" territory. Quality and "doing it right" become much less important as time becomes the enemy. "Ship it" and "just get it done" are the teams mantras now. It happens. I'm not saying its should be condoned but even on the best projects sometimes things align in such a way that it happens. &lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/quailtyvsfeatures.png" alt="Crunch time hurts quality" _="_" /&gt;&lt;/p&gt;&lt;p&gt;The problem is as quality goes down and "good enough" becomes significantly "less good" technical debt, bugs and other types of friction leak in. So once you push that deliverable over the finish line you need to ensure you have time after the fact to go back and ensure you clean up any technical debt and edge case bugs you may have introduced. &lt;/p&gt;&lt;p&gt;If you don't allow the team to address these issues you begin layering layers of debt on top of each other and one day someone will have to repay it. It may not be you, it may not even be a member of your team, but leaving a mess for someone else to clean up is just not something a decent human being should do. You want to be a decent human right?&lt;/p&gt;&lt;h2&gt;Finishing up&lt;/h2&gt;&lt;p&gt;I think I've stretched this analogy further than it really should have been stretched but I wanted to be as holistic as possible to the idea of team delivery. &lt;/p&gt;&lt;p&gt;As the old saying goes "Its not the destination, It's the glory of the ride"&lt;a name="_3"&gt;&lt;/a&gt;&lt;a href="#3"&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;. While it may seem, from a single customers point of view, that delivering the software is the be all an end all it's much more nuanced than that. It's about building relationships far more than software because it makes delivery of the end product much easier and increases the chance of delivering exactly what people need. It's about ensuring that your team are supported, allowed to take risks and further themselves both individually and as a team. Happy customers will come back for more, happy teams will stick around. Sure every project may not be all sweetness and light but people know this and it's how we deal with the challenges that make the difference.&lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: At least for the benefit of this post. They are probably entirely different. Go with it.&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="2"&gt;&lt;/a&gt;&lt;a href="#_2"&gt;2&lt;/a&gt;: "Resource Planning" - not even once.&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="3"&gt;&lt;/a&gt;&lt;a href="#_3"&gt;3&lt;/a&gt;: Edward Monkton, Zen Dog&lt;/sup&gt;&lt;/p&gt;</content></entry><entry><title>Provisioning Postgres via Docker</title><updated>Fri May 22 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/05/22/provisioning-postgres-via-docker/"></link><id>urn:yobriefca-se:feed:post:Provisioning Postgres via Docker</id><content type="html">&lt;p&gt;&lt;a href="/blog/2015/05/20/provisioning-postgres-via-vagrant-and-ansible/"&gt;Previously&lt;/a&gt; we looked at &lt;a href="/blog/2015/05/20/provisioning-postgres-via-vagrant-and-ansible/"&gt;provisioning Postgres via Vagrant and Ansible&lt;/a&gt; and in the spirit of trying something a bit different lets do the same thing using &lt;a href="http://docker.io"&gt;Docker&lt;/a&gt;. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;This was new territory for me but this guide will get you far enough for hacking around on a project. If this is an awful way of doing things let me know.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Same rules apply - we want to provision a project level Postgres instance listening on host port &lt;code&gt;5432&lt;/code&gt; with a new database and user. &lt;/p&gt;&lt;h2&gt;boot2docker&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;If you're not using &lt;code&gt;boot2docker&lt;/code&gt; (if you're on a supported Linux platform) you can skip this bit&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Docker is primarily a Linux based tool but there are wrappers for Windows and OSX. Since I'm using a Mac I'll also use &lt;a href="http://boot2docker.io/"&gt;boot2docker&lt;/a&gt; that uses a Linux VM and proxies requests to the VM.&lt;/p&gt;&lt;p&gt;I'm going to assume that at this point we have &lt;code&gt;docker&lt;/code&gt; and, if necessary, &lt;code&gt;boot2docker&lt;/code&gt; installed. If you're using OSX &lt;code&gt;brew&lt;/code&gt; is your friend (&lt;code&gt;brew install boot2docker&lt;/code&gt;).&lt;/p&gt;&lt;p&gt;With &lt;code&gt;boot2docker&lt;/code&gt; installed we need to start it.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;boot2docker init # only need this the first time
boot2docker up
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;After this has started you should see an information message about setting the necessary environment variables to allow docker and boot2docker to talk correctly. There is a quick way to do all this provided by &lt;code&gt;boot2docker&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;$(boot2docker shellinit)
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Dockerfile&lt;/h2&gt;&lt;p&gt;The &lt;code&gt;Dockerfile&lt;/code&gt; specifies the rules for building a docker image that we can use to create container instances from. I'll just dump ours here and break it down after.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;FROM ubuntu

# add postgresql release repository to apt
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" &amp;gt; /etc/apt/sources.list.d/pgdg.list

# install the necesarry packages
RUN apt-get update &amp;amp;&amp;amp; \
  apt-get install -y \
    python-software-properties \
    software-properties-common \
    postgresql-9.4

# run the rest as the postgres user
USER postgres

# create the database and user
RUN /etc/init.d/postgresql start &amp;amp;&amp;amp; \
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &amp;amp;&amp;amp; \
    createdb -O docker docker

# ensure host can connect to postgres correctly
RUN echo "host all  all    0.0.0.0/0  md5" &amp;gt;&amp;gt; /etc/postgresql/9.4/main/pg_hba.conf
RUN echo "listen_addresses='*'" &amp;gt;&amp;gt; /etc/postgresql/9.4/main/postgresql.conf

# expose the 5432 port to outside the container
EXPOSE 5432

# set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.4/bin/postgres", \
      "-D", "/var/lib/postgresql/9.4/main",  \
      "-c", "config_file=/etc/postgresql/9.4/main/postgresql.conf"]
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The flow is somewhat similar to the &lt;a href="/blog/2015/05/20/provisioning-postgres-via-vagrant-and-ansible/"&gt;previous&lt;/a&gt; post,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;We want to use the latest version of Postgres so we add the offical Postgres repo (and pgp-key) to the &lt;code&gt;apt&lt;/code&gt; catalogue.&lt;/li&gt;
  &lt;li&gt;We install all the necessary packages&lt;/li&gt;
  &lt;li&gt;Then we switch to the newly created &lt;code&gt;postgres&lt;/code&gt; user to perform the rest of the operations&lt;/li&gt;
  &lt;li&gt;In &lt;code&gt;RUN&lt;/code&gt; we start the database, create the user &lt;code&gt;docker&lt;/code&gt; with password &lt;code&gt;docker&lt;/code&gt; and finally create the &lt;code&gt;docker&lt;/code&gt; database.&lt;/li&gt;
  &lt;li&gt;We then update the necessary &lt;code&gt;*.conf&lt;/code&gt; files to allow us to connect to the Postgres instance outside of the container.&lt;/li&gt;
  &lt;li&gt;Next we &lt;code&gt;EXPOSE&lt;/code&gt; the 5432 port to the outside world (in a &lt;code&gt;boot2docker&lt;/code&gt; world this exposes it from the container to the VM, we still have a bit of work to do to get it exposed to the actual host.&lt;/li&gt;
  &lt;li&gt;Finally we stipulate how to start the Postgres instance when the container starts.&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Running&lt;/h2&gt;&lt;p&gt;So now we have specified the rules in the &lt;code&gt;Dockerfile&lt;/code&gt; (this is somewhat similar to the Ansible playbook concept, sort of, but not quite) we want to build a versioned image from this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;docker build --tag postgres:9.4 --rm  .
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running this from the same directory as the &lt;code&gt;Dockerfile&lt;/code&gt; will build an image called &lt;code&gt;postgres&lt;/code&gt; tagged with &lt;code&gt;9.4&lt;/code&gt;. The &lt;code&gt;--rm&lt;/code&gt; command tells docker to remove the intermediary images created.&lt;/p&gt;&lt;p&gt;We can see that our image has been created using the &lt;code&gt;docker images&lt;/code&gt; command&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;&amp;gt;  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
postgres            9.4                 f25b0a52a81b        26 minutes ago      358.8 MB
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next we need to take this image and run it as a container,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;docker run -p 5432:5432 --name postgres-instance -d postgres:9.4
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Lets have a look at the option here,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;-p 5432:5432&lt;/code&gt; publishes the &lt;code&gt;EXPOSE&lt;/code&gt;d &lt;code&gt;5432&lt;/code&gt; port effectively making it accessible outside the container.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;--name postgres-instance&lt;/code&gt; is the friendly name we want to give the container&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;-d postgres:9.4&lt;/code&gt; is the image name and tag to create the container from&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;We can verify the container instance is running using the &lt;code&gt;docker ps&lt;/code&gt; command&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;&amp;gt; docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
f54de34e1308        postgres:9.4        "/usr/lib/postgresql   22 minutes ago      Up 22 minutes       0.0.0.0:5432-&amp;gt;5432/tcp   postgres-instance
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At this point we should be able to access the Postgres instance via the IP of the VM. To ge the IP we can use the &lt;code&gt;ip&lt;/code&gt; command of &lt;code&gt;boot2docker&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;&amp;gt; boot2docker ip
192.168.59.103
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;boot2docker Port Forwarding&lt;/h2&gt;&lt;p&gt;If we are using &lt;code&gt;boot2docker&lt;/code&gt; we still wont be able to access the instance via localhost. As already mentioned &lt;code&gt;boot2docker&lt;/code&gt; transparently uses a Linux VM (via VirtualBox) to run docker within. By default this VM doesn't forward any ports to the host so we need to tell &lt;code&gt;boot2docker&lt;/code&gt; (via &lt;code&gt;VBoxManage&lt;/code&gt;) to open that port.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;VBoxManage modifyvm "boot2docker-vm" --natpf1 "postgres-port,tcp,127.0.0.1,5432,,5432"
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This command tells VirtualBox to forward the 5432 TCP port of the &lt;code&gt;boot2docker&lt;/code&gt; VM to the host.&lt;/p&gt;&lt;p&gt;At this point we should be able to access the Postgres instance via &lt;code&gt;localhost&lt;/code&gt; on the host as well as the VMs IP address directly.&lt;/p&gt;</content></entry><entry><title>Provisioning Postgres via Vagrant and Ansible</title><updated>Wed May 20 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/05/20/provisioning-postgres-via-vagrant-and-ansible/"></link><id>urn:yobriefca-se:feed:post:Provisioning Postgres via Vagrant and Ansible</id><content type="html">&lt;p&gt;This article covers a simple workflow for setting up a &lt;a href="http://vagrantup.com"&gt;Vagrant&lt;/a&gt; instance running &lt;a href="http://postgresql.org"&gt;Postgres&lt;/a&gt; provisioned by &lt;a href="http://ansible.com"&gt;Ansible&lt;/a&gt;. The first time I did this I found that most articles didn't cover everything and I had to look around to solve a few minor issues.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;It's worth noting that I can avoid all this and use Nic Ferriers very useful &lt;a href="http://www.pgdevbox.com/"&gt;Postgres Dev Box&lt;/a&gt;. This work may be unnecessary but it's still fun - &lt;strong&gt;Funecessary&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I'm going to make an assumption that you know what both Vagrant and Ansible are and you understand the basic premise of how they work. I'll also assume that you have ansible installed on the host machine (Ansible is purely SSH and doesn't need agents installed on target machines).&lt;/p&gt;&lt;h2&gt;Vagrantfile&lt;/h2&gt;&lt;p&gt;We start with the &lt;code&gt;Vagrantfile&lt;/code&gt;. This is the file that tells Vagrant what VM base box to use (Ubuntu Precise 32), what ports should be forwarded to the host (5432 - the default Postgres port) and what provider should be used to provision the VM once it has been brought up (Ansible).&lt;/p&gt;&lt;p&gt;If you already have a local instance of Postgres running you'll want to shut it down or you'll get port collisions and Vagrant will complain.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Vagrant&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;configure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"precise32"&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;box_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"http://files.vagrantup.com/precise32.box"&lt;/span&gt;

    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;network&lt;/span&gt; &lt;span class="s2"&gt;"forwarded_port"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;guest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;

    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;provision&lt;/span&gt; &lt;span class="s2"&gt;"ansible"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;ansible&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="n"&gt;ansible&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;playbook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"playbook.yml"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;playbook.yml&lt;/h2&gt;&lt;p&gt;Ansible provisioning in Vagrant accepts, at the minimum, a playbook to run and the provider takes care of ensuring that ansible is called correctly. Our Vagrantfile above is configured to point to &lt;code&gt;playbook.yml&lt;/code&gt; that sits alongside the &lt;code&gt;Vagrantfile&lt;/code&gt; so we need to create that.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;- hosts: all
  sudo: yes
  gather_facts: no
  tasks:
    - name: add keyserver to apt
      command: apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
    - name: add custom postgres repo to apt
      lineinfile: dest=/etc/apt/sources.list.d/pgdg.list
                  line="deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" state=present
                  create=yes

- hosts: all
  sudo: yes
  gather_facts: no
  tasks:
    - name: ensure apt cache is up to date
      apt: update_cache=yes
    - name: ensure packages are installed
      apt: name={{item}}
      with_items:
          - python-software-properties
          - software-properties-common
          - libpq-dev
          - python-psycopg2
          - postgresql-9.4

- hosts: all
  sudo: yes
  sudo_user: postgres
  gather_facts: no

  handlers:
    - name: restart postgresql
      service: name=postgresql state=restarted

  tasks:
    - name: postgresql should listen on all ports
      lineinfile: dest=/etc/postgresql/9.4/main/postgresql.conf
                  regexp="^listen_addresses"
                  line="listen_addresses = '*'" state=present

    - name: postgresql should allow access to host
      copy:
        dest: /etc/postgresql/9.4/main/pg_hba.conf
        content: |
          local   all   postgres   peer
          local   all   all        peer
          host    all   all        0.0.0.0/0   md5
      notify: restart postgresql

- hosts: all
  sudo: yes
  sudo_user: postgres
  gather_facts: no

  vars:
    dbname: vagrant
    dbuser: vagrant
    dbpassword: vagrant

  tasks:
    - name: ensure database is created
      postgresql_db: name={{dbname}}

    - name: ensure user has access to database
      postgresql_user: db={{dbname}} name={{dbuser}} password={{dbpassword}} priv=ALL
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It probably best to tease apart what each part of this playbook does. I've broken the playbook out into four distinct sections that perform specific related tasks to make this easier to explain,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Add the Postgres apt repository to allow us to get the latest release of Postgres (&lt;code&gt;apt&lt;/code&gt; by default will install &lt;code&gt;9.1&lt;/code&gt; but I want &lt;code&gt;9.4&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Update the &lt;code&gt;apt&lt;/code&gt; cache and install the necessary packages (ultimately &lt;code&gt;postgresql-9.4&lt;/code&gt; but we need a few base packages as well.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;By this point Postgres will be installed and a &lt;code&gt;postgres&lt;/code&gt; user created but if you try and connect to the instance from outside the VM it still wont work. We have some additional work to do. This is exactly what the next block does.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Update the &lt;code&gt;postgresql.conf&lt;/code&gt; file to allow Postgres to listen on all ports and update &lt;code&gt;pg_hba.conf&lt;/code&gt; to allow the host machine to connect to Postgres. &lt;em&gt;NB&lt;/em&gt; these rules are very lax, for a bit of local development work this is fine but it would be best to tighten these up for more serious work. The service should be restarted after both of these tasks.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So now we have the ability to connect to the database from the host machine we also want to set up a dedicated database and user.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Create a &lt;code&gt;vagrant&lt;/code&gt; database and create and associate a &lt;code&gt;vagrant&lt;/code&gt; user to this database.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;If we save both files and run &lt;code&gt;vagrant up&lt;/code&gt; we will end up with a provisioned Vagrant box running Postgres that can be access from the host machine on port 5432.&lt;/p&gt;</content></entry><entry><title>The Builder Pattern</title><updated>Fri May 15 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="/presentations/thebuilderpattern.pdf"></link><id>urn:yobriefca-se:feed:post:The Builder Pattern</id><content type="html"></content></entry><entry><title>The Builder Pattern</title><updated>Wed May 13 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/05/13/the-builder-pattern/"></link><id>urn:yobriefca-se:feed:post:The Builder Pattern</id><content type="html">&lt;style&gt;
  body {
    background-color: #253239;
    color: #cfd8dc;
  }

  .row h1 {
    display:none;
  }

  .homer a {
    color: #cfd8dc;
  }

  blockquote, p code, li code {
    background-color: #314249;
    color: #cfd8dc;
    border: none;
  }
&lt;/style&gt;
&lt;blockquote&gt;&lt;p&gt;This article is adapted from a presentation I gave on the Builder Pattern for Deloitte Digital on Friday the 15th May 2015.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.001.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Lets talk a bit about the Builder Pattern. But why the builder pattern in particular? Well being a bit of a technical butterfly jumping around from language to language, technology to technology, there are a surprisingly small amount of patterns that port well. So I tend to fall &lt;strong&gt;into&lt;/strong&gt; patterns when writing code more than actually thinking "oh this could be solved using the XYZ pattern to decouple the doo-hickey from the weehoo". The builder pattern, however, I've always found to be very portable across technology and language domains. It may not exist in most domains to solve the original problem it was created for but it is really rather versatile and can be used to create cleaner code. It is also a very applicable when constructing representations of your data such as HTML, XML, JSON etc. and this is an area you're unlikely to avoid in any project regardless of technology or language.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.002.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Originally the builder pattern was created to find a solution to the problem of the telescoping constructor anti-pattern but it has grown beyond this use over time. That said defining the original scope of the problem will help us understand the builder pattern better.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.003.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Here we have the definition of &lt;code&gt;Reading&lt;/code&gt; class. It represents some sort of discrete reading from a sensor or something similar. It has a number of immutable properties and a single constructor for creating an instance of a &lt;code&gt;Reading&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Now lets imagine all of these &lt;code&gt;Reading&lt;/code&gt; properties are optional and can be defined with default values. So we can begin by filling in constructors to support easy construction of this class with defaults.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.004.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;With this constructor we call the previous "full" constructor passing a default value in. This is the first link in a chain of constructor calls.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.005.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Our next constructor calls the previous one passing a default date which results in that constructor calling its previous one passing in it's default value as well. We now have our second link.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.006.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;And so this continues on for a while as we gradually create a longer chain of constructor calls&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.007.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Successively adding another constructor to the chain of constructors.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.008.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;You'll notice that only a single default value is passed to the next constructor so we aren't duplicating construction logic and default values.&lt;/p&gt;&lt;p&gt;If we zoom out and look at an outline of our class constructors we can get a better picture of what has happened.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.009.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;If you've ever owned or just seen a collapsible telescope you can see where the telescoping constructor anti-pattern gets its name.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.010.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;So at this point we have a few minor problems&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;We have taken a simple class and filled it full of boilerplate.&lt;/li&gt;
  &lt;li&gt;We have coupled all the constructors into a chain which makes the class rather brittle which can make refactoring slow and prone to error.&lt;/li&gt;
  &lt;li&gt;We've scattered our default values across the class. While each value only has a single point of definition the declarations are decentralised.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;If you think about it for a while you'll likely see a few simple ways to remove these issues without having to resort to nothing more than a bit of tweaking and refactoring of the current solution.&lt;/p&gt;&lt;p&gt;But lets consider some other scenarios.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.011.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;So what happens when we want to support constructing a &lt;code&gt;Reading&lt;/code&gt; and we only want to set the &lt;code&gt;time&lt;/code&gt; to a non-default value?&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.012.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Well that was fairly easy - but we have created &lt;strong&gt;yet another constructor&lt;/strong&gt;. Not the end of the world but we have diverted slightly from our original constructor chain creating another branch of behaviours to test. We've also duplicated default value logic across more than one constructor. Again not the end of the world and we could pull the values out into constants but we've certainly made everything less clean.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.013.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;What happens then if we want to introduce a constructor that only sets the &lt;code&gt;sourceId&lt;/code&gt; property? Now we have a problem,&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.015.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;We already have a constructor that takes a single &lt;code&gt;int&lt;/code&gt; for &lt;code&gt;id&lt;/code&gt;. Working around this may require exposing the knowledge of default values outside the class and have the constructing object know to pass in &lt;code&gt;-1&lt;/code&gt; to the 2 arg constructor but then we have externalised coupling and what's the point of having a nice encapsulated OO design if you end up doing something like this?&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Those among you who use &lt;strong&gt;sensible&lt;/strong&gt; languages that have features such as default parameter values and copy constructors will find this isn't as much of a problem but even in those languages its possible to tie yourself up in knots.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.016.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;
&lt;div class="vimeo"&gt;&lt;iframe src="https://player.vimeo.com/video/127740033" width="700" height="439" frameborder="0" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.017.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;As I originally mentioned the builder pattern is remarkably versatile and isn't just used to solve this one anti-pattern. There are a number of benefits to be gained by applying the builder pattern to code.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;In many situations you achieve more concise, expressive code which can aid readability and maintenance of the code&lt;/li&gt;
  &lt;li&gt;You can use it to compose complex objects without having to fully understand how the object is constructed internally&lt;/li&gt;
  &lt;li&gt;You can optimise object creation. The builder pattern can be both immediate and lazy (performing the building steps only when needed) which allows you to defer potentially costly steps to the last minute, or pipeline and batch certain steps where it gives a benefit.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;To demonstrate this lets take a look at a method for building a customer order.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.018.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Here we have a fairly procedural view of creating an order, adding some line items and marking it for priority shipping.&lt;/p&gt;&lt;p&gt;There is a lot going on and it's up to this method to know exactly how all these bits and pieces fit together. Some things could go wrong,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;We may forget to call &lt;code&gt;.addLine()&lt;/code&gt; for an item creating a bug&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;Product.find&lt;/code&gt; could be an expensive operation and we are doing it a few times.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;An alternative, employing the builder pattern, could look similar to this,&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.019.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;There is much less code here, its subjectively easier to read and understand and there are a few more benefits.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;You don't need to understand exactly how line items are added and you can't accidentally miss out an &lt;code&gt;addLine&lt;/code&gt; call.&lt;/li&gt;
  &lt;li&gt;We don't call &lt;code&gt;Product.find&lt;/code&gt; ourselves, instead the builder knows to call it and it may be possible for the builder to selectively batch these calls to reduce the overhead.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;One more very common use of the builder pattern is through the construction of representations of our data and communication across domains.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.020.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;To demonstrate this overly wordy phrase lets take a look at a few examples.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.021.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Here is one example of a Java DSL that is used to construct a SQL query. The fluent syntax gives you a consistent language to express your intent and, once complete, the builder can take all the knowledge and convert it into a SQL dialect. A huge benefit to this approach is that it removes the need to manually guard against SQL injection attacks.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.022.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Another example, this time form Scala, uses the builder pattern, albeit rather minimally to allow us to build a JSON representation of a piece of data.&lt;/p&gt;&lt;p&gt;Although it's not wise to end on a low note it is worth mentioning that as with all things in this world the builder pattern has its own share of issues.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.023.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;The 2 biggest causes for concern are,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Using the builder pattern to create a DSL such as the SQL example above can result in rather complex builder code. Typically builders have their fair share of boilerplate to support their API, however, this is an acceptable trade off because we've can reduce boilerplate and complexity in our &lt;strong&gt;business logic&lt;/strong&gt; but edge cases can a desire to make the perfect DSL can cause complexity creeep. Ensuring you're not generating unnecessary complexity is important when creating a builder.&lt;/li&gt;
  &lt;li&gt;As we've witnessed in our very first example (Lomboks builder didn’t set the default values for our optional fields) it is sometimes possible to use a builder and yet create "incomplete" objects missing values that need to be set. If your builder is part of a library that is used across projects then this could cause frustration. Documentation and correct handling of invalid states is essential.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;&lt;img src="/images/thebuilderpattern/thebuilderpattern.001.png" alt="The Builder Pattern" _="_" /&gt;&lt;/p&gt;&lt;p&gt;And that is the builder pattern in a nutshell. It is very likely you'll have already come across uses of the builder pattern in the wild. It is a very simple and flexible pattern but should, as with all things, be treated with respect.&lt;/p&gt;</content></entry><entry><title>Less Java with Lombok</title><updated>Fri May 08 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/05/08/less-java-with-lombok/"></link><id>urn:yobriefca-se:feed:post:Less Java with Lombok</id><content type="html">&lt;p&gt;So Java, you've heard of it right? It's the language thats used to &lt;a href="https://twitter.com/avalanche123/status/7062890318143488"&gt;convert XML into stack traces&lt;/a&gt;. You know the one... it's &lt;a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html"&gt;really popular&lt;/a&gt; and has a massive ecosystem of powerful open source tools and techs but everyone says it's the worst. Sadly making fun of Java lacks any real challenge because, if I can be blunt, Java makes it easy to do so (see I just did it without thinking).&lt;/p&gt;&lt;p&gt;There are a lot of things that make Java an easy target but in my frequent exposure to Java there are 3 things that really wind me up.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Type erasure &amp;amp; Non-reified generics lead to a whole host of frustrating problems when you're trying to build nice clean composable solutions,&lt;/li&gt;
  &lt;li&gt;Following on from #1 the type system in general is a real pain. It's neither powerful enough to use it in any real meaningful way nor is it loose enough to allow dynamic typing. It's basically an annoying landlord who happens to live with you, in your bed, and likes farting and being the little spoon.&lt;/li&gt;
  &lt;li&gt;Finally the sheer verbosity of the whole thing. When 90% of your code is boilerplate classes and methods required to work around limitations in the language you really need to work to make the simplest of code expressive and maintainable.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;But I've already said it is popular and interestingly its popularity has been growing. Probably in part due to the power of the JVM and the huge powerful sea of tooling and tech that exist for it. That said I have a theory that it also acts as a sort of natural constraint that in some ways help prevent language astronauts code golf functionality into an unreadable one line functional expression that looks like a computer novice tried to quit Vim. This is all a personal theory formed after reviewing a fairly substantial amount of Scala code, YMMV.&lt;/p&gt;&lt;p&gt;Any way... Without switching to another language there is very little we can do about #1 &amp;amp; #2 so at the very least addressing #3 could make your time in Java more tolerable. Java 8 added a few features that can improve the expressiveness of your code including &lt;a href="https://leanpub.com/whatsnewinjava8/read#leanpub-auto-functional-interfaces"&gt;Functional Interfaces&lt;/a&gt; and &lt;a href="https://leanpub.com/whatsnewinjava8/read#leanpub-auto-default-methods"&gt;Default Methods&lt;/a&gt; but by and large we are still producing a large volume of boilerplate. These are the challenges with having a great backward compatibility story.&lt;/p&gt;&lt;h2&gt;I put on my robe and wizard hat&lt;/h2&gt;&lt;p&gt;Fear ye not you beautifully desperate people there are things you can do. A word of caution first - there is a certain amount of "magic" coming up. Yes magic is a bit dodgy because it requires wizards to control but there are levels of magic and a lot of it is just quick slight of hand. Once you're shown the reveal you can't unsee it. Admittedly I was a skeptic of what I'm about to write about but after spending a bit of time with it I approve of considered and careful wielding of this magic.&lt;/p&gt;&lt;h2&gt;Lombok&lt;/h2&gt;&lt;p&gt;So lets remove the boilerplate from Java without having to rewrite our solutions in a new language. To do this we are going to use &lt;a href="https://projectlombok.org/"&gt;Lombok&lt;/a&gt;. Now the website is very light on explanation but &lt;a href="https://projectlombok.org/"&gt;Lombok&lt;/a&gt; is an annotation based code generation library that focuses on removing the noise and boilerplate from your code without the need for actual authentigenuine magic. So while it may feel like magic being released from the shackles of Javas boilerplate oppression it is just a bunch of common patterns rolled into annotations. Oh and yes, if you've read any of my older posts, annotations are a pet hate of mine so you'll understand I'm not saying any of this lightly.&lt;/p&gt;&lt;p&gt;So whats the elevator pitch for Lombok (after I've spent over 500 words meandering over nonsense)? OK lets go.&lt;/p&gt;&lt;p&gt;Assume we have a &lt;code&gt;Character&lt;/code&gt; (in the RPG sense not the textual sense) class that has just 2 properties - a &lt;code&gt;name&lt;/code&gt; and a &lt;code&gt;level&lt;/code&gt;. Now the &lt;code&gt;name&lt;/code&gt; is set once and never changes and the &lt;code&gt;level&lt;/code&gt; fluctuates as this character kills dragons, marries trolls and harvests magic mushrooms (usually it goes up but you can never really tell with magic mushrooms).&lt;/p&gt;&lt;p&gt;Got it? Good.&lt;/p&gt;&lt;p&gt;This &lt;code&gt;Character&lt;/code&gt; class is, and you'll forgive me for using such a stupid term - a DTO. It's a bag of state that gets passed around your application. This happens A LOT in Java applications because of a crappy type system. If you know Java well enough you'll understand that a big-bag-o-state/DTO needs decent &lt;code&gt;toString&lt;/code&gt;, &lt;code&gt;equals&lt;/code&gt; and &lt;code&gt;hashCode&lt;/code&gt; implementations because that thing is going to get logged and compared like a demon (surprisingly demons get compared and logged more than you'd think - I guess hell really is full of Java developers). The implementation of such a class would look like this.... brace yourself.... POJO is coming...&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Character&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getLevel&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setLevel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="n"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Character&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;canEqual&lt;/span&gt;&lt;span class="o"&gt;((&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="n"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;other$name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="n"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;other$name&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="n"&gt;$name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other$name&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;PRIME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;PRIME&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;$name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;PRIME&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;canEqual&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="n"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"se.yobriefca.tinkering.Character(name="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", level="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;")"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now as a developer I care about maybe 2 or 3 of those &lt;strong&gt;50&lt;/strong&gt; lines of code. With Lombok you can do this...&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;lombok.Data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Character&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Oh look, squint a bit and we have a Scala &lt;code&gt;case class&lt;/code&gt; or Kotlin &lt;code&gt;data class&lt;/code&gt; but we are still writing the Javas. Neat right&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;? One annotation, completely constrained to that class and we've eliminated a lot of boilerplate and sprinkled in some "best practise" (the &lt;code&gt;hashCode&lt;/code&gt; and &lt;code&gt;toString&lt;/code&gt; stuff is often sadly neglected by many developers).&lt;/p&gt;&lt;p&gt;But wait! Maybe we want to support creating a &lt;code&gt;Character&lt;/code&gt; with some sort of initial level as well as a name. OK, here goes.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="nd"&gt;@AllArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@RequiredArgsConstructor&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Character&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What I've done is, via annotations, add both an &lt;code&gt;AllArgsConstructor&lt;/code&gt; and a &lt;code&gt;RequiredArgsConstructor&lt;/code&gt;. The names are fairly self explanatory but we can now instantiate our instances like so,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;Character&lt;/span&gt; &lt;span class="n"&gt;c1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"James"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;Character&lt;/span&gt; &lt;span class="n"&gt;c2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"James"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A HA! But what if we have a more complicated object with a plethora of required and optional fields? Well Lombok also gives you an annotation that generates a fluent builder interface for your object.&lt;/p&gt;&lt;p&gt;So this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@Builder&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Character&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Gives you this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;Character&lt;/span&gt; &lt;span class="n"&gt;c3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Character&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;builder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;level&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"James"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Of course you can mix and match these annotations to a certain extent to achieve exactly what you need to build your lovely little property bag. As an unsurprising bonus this is all done in a consistent and predictable way that works very well with most 3rd party libraries such as serialisation libraries or data access libraries.&lt;/p&gt;&lt;p&gt;But wait - there's more - it's not all about them POJOs/DTOs/State Bags lets look at some of the interesting things you can also do with Lombok.&lt;/p&gt;&lt;h2&gt;Type Inference&lt;/h2&gt;&lt;p&gt;Declaring variables in Java can be an exercise in repeating yourself multiple times. For example,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ValueOutputterProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;valueOutputterProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ValueOutputterProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's kind of obvious what this is because it says so three times which is not only wasteful and slow to type but its yet more unnecessary noise that reduces readability. Lombok allows us to shorten this declaration making it look more like a Scala declaration&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;valueOutputterProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ValueOutputterProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Not exactly earth shattering but it certainly reduces the noise we have to tune out to see the important code.&lt;/p&gt;&lt;h2&gt;@Log (inc. @Slf4j @Log4j etc.)&lt;/h2&gt;&lt;p&gt;You know that line you write every time you create a new class? You know the one that allows you to log things?&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WorkerThing&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt; &lt;span class="n"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;LoggerFactory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getLogger&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WorkerThing&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doWork&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Doing work"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That one on the second line. We can make it go away with Lombok,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@Slf4j&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WorkerThing&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doWork&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Doing work"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We've traded a single long line of declaration for a single annotation. Clean. There is an annotation for each of the common logging libraries.&lt;/p&gt;&lt;h2&gt;Cleanup&lt;/h2&gt;&lt;p&gt;You know those pesky &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;finally&lt;/code&gt; blocks that we wrap around resources to ensure we don't leak connections or keep streams open unnecessarily?&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;DBConnection&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;DBConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DROP TABLE users"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;close&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If we are using Java 1.7 onwards we could use the &lt;a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html"&gt;&lt;code&gt;try-with-resources&lt;/code&gt;&lt;/a&gt; statement to make it cleaner,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DBConnection&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;DBConnection&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DROP TABLE users"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But Lombok gives us another option - the &lt;code&gt;@Cleanup&lt;/code&gt; annotation,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@Cleanup&lt;/span&gt; &lt;span class="n"&gt;DBConnection&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;DBConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DROP TABLE users"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;@Cleanup&lt;/code&gt; annotation generates the necessary boilerplate to ensure we are dutifully closing any resource we create even when something goes wrong.&lt;/p&gt;&lt;h2&gt;Experimental&lt;/h2&gt;&lt;p&gt;Lombok has a bunch of features that are still tagged as &lt;strong&gt;experimental&lt;/strong&gt;. These features either have known issues or have yet to prove themselves thoroughly in the field of Java-ing. I'll demonstrate one of them now - &lt;code&gt;@ExtensionMethod&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;I really like C# extension methods, Kotlins extension methods, Scalas extension methods (&lt;code&gt;implicit def&lt;/code&gt;'s) and so on. &lt;code&gt;@ExtensionMethod&lt;/code&gt; gives you this feature for Java.&lt;/p&gt;&lt;p&gt;First we create a static suite of helper methods,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringExtensions&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;makeMusical&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"¸¸?·¯·?¸¸?·¯·?¸¸"&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"¸¸?·¯·?¸¸?·¯·?¸¸"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then we annotate that class we want to utilise the new extension methods in,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@ExtensionMethod&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;&lt;span class="n"&gt;StringExtensions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;})&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally we call the static methods within this class as if they were instance methods of the type that the method accepts.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"EXTEEEEEENSION METHODSSSSSS"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeMusical&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Under the hood the call simply gets translated to&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;StringExtensions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeMusical&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"EXTEEEEEENSION METHODSSSSSS"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Which gives us the rather pretty output of,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;¸¸?·¯·?¸¸?·¯·?¸¸EXTEEEEEENSION METHODSSSSSS¸¸?·¯·?¸¸?·¯·?¸¸
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In fact &lt;code&gt;@ExtensionMethod&lt;/code&gt; can take any class with &lt;code&gt;static&lt;/code&gt; methods and make them callable as instance methods such as &lt;code&gt;java.util.Arrays&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;OK extension methods are just a nice bit of syntactic sugar but they can be really nice when creating a consistent approach to code style.&lt;/p&gt;&lt;h2&gt;Tooling Support&lt;/h2&gt;&lt;p&gt;In short "yes there is". Eclipse and IntelliJ both have Lombok plugins with powerful refactoring support (turn Lombok'd classes into their equivalent pure Java class and vice versa). Some of the features, especially the experimental ones, have some little issues that can make editors complain, even though the project will compile and run fine, but it's no worse than some of the bizarre syntax issues you can get with Scala for example.&lt;/p&gt;&lt;h2&gt;Summing up&lt;/h2&gt;&lt;p&gt;I've thrown a lot of code around and didn't cover all Lombok has to offer. I focused on the features that I think are handy to use and I would certainly recommend you apply you're use of Lombok carefully. As with all powerful things don't over do it just because you can. A little goes a long way.&lt;/p&gt;&lt;p&gt;Finally I recommend you check out the &lt;a href="https://projectlombok.org/"&gt;official Project Lombok site&lt;/a&gt;, read the caveats on the various features that catch your eye and just give it a go yourself.&lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: There are some other things generated here such as the bizarre &lt;code&gt;java.beans.ConstructorProperties&lt;/code&gt; annotation. I pray I never need to know what that does.&lt;/sup&gt;&lt;/p&gt;</content></entry><entry><title>Not All Frameworks</title><updated>Wed May 06 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/05/06/not-all-frameworks/"></link><id>urn:yobriefca-se:feed:post:Not All Frameworks</id><content type="html">&lt;p&gt;It's interesting, to me at least, that many people in the software industry have very black and white opinions. Maybe its the fact we wrangle binary (albeit at a very very high level these days) on a daily basis that gives rise to such binary views, or perhaps its reflective of the relative young age of the industry (and the mentality this fosters). Who knows. &lt;/p&gt;&lt;p&gt;Sadly it does reflect, often negatively, in the solutions we produce. I've worked in legacy apps that are littered with the snake oil covered spent cases of silver bullets and read my fair share of "&lt;code&gt;Why we are using &amp;lt;%=arbitrary_new_shiny_thing%&amp;gt;&lt;/code&gt;" posts followed, in time, by "&lt;code&gt;Actually were going back to an older reliable tech because &amp;lt;%=reasons%&amp;gt;&lt;/code&gt;". Then of course there is the wasteland of abandoned open source projects written in yesterdays techs while the sole creator is belligerently re-writing everything in todays tech because it's going to change the shape of IT.&lt;/p&gt;&lt;p&gt;I know I've been guilty of many of these things (and probably will be again) but in my older age I've started to care less about arguments that are, more or less, irrelevant. For example any argument that ignores the most important aspect of technology application, that of context, immediately has me thinking &lt;code&gt;[CITATION NEEDED]&lt;/code&gt;. Without context its very easy to validate your own argument like so...&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Find the &lt;strong&gt;worst&lt;/strong&gt; examples of the things you disagree with. Hearsay and rumour is also acceptable.&lt;/li&gt;
  &lt;li&gt;Counter this information with the &lt;strong&gt;best&lt;/strong&gt; examples of the things you agree with. Drop in words like &lt;strong&gt;performant&lt;/strong&gt;, &lt;strong&gt;webscale&lt;/strong&gt; and &lt;strong&gt;intuitive&lt;/strong&gt; and you're bound to walk away feeling self-satisfied.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Your "opponent" can do exactly the same thing and achieve the same results. It's like magic.&lt;/p&gt;&lt;p&gt;This seems to be the current state of affairs when it comes to talking about &lt;strong&gt;frameworks vs libraries&lt;/strong&gt;. In fact I think it always has been. The &lt;strong&gt;frameworks vs libraries&lt;/strong&gt; debate is one of those topics that is always very black and white, usually focused on specific technologies. The general theme is that frameworks are bad and libraries are good and therefore anything that could be categorised as a framework is quickly blacklisted in favour of a few libraries no matter how mature the framework is or how broken the library is.&lt;/p&gt;&lt;p&gt;"But frameworks don't compose" - You know what bucko, neither does most of your crappy code. "Libraries give me flexibility" - yes they do but can you say with confidence that you know what you are doing? Libraries aren't as ready to deal with cross cutting concerns in your solution and lets be honest do we really want to be sitting around designing login/registration/forgot password workflow every time we create an app? The yaks have been built and the bike sheds shaved.&lt;/p&gt;&lt;p&gt;But the biggest problem I see with peoples "frameworks are evil" mindset is that the definition is far from perfect. The definition I like the most is very coarse grained and open to interpretation which isn't ideal but it feels acceptable,&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;With a library your code is responsible for calling the library whereas with a framework your code is executed by a framework &lt;strong&gt;within&lt;/strong&gt; the context of the framework.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Good enough.&lt;/p&gt;&lt;p&gt;But as I alluded to - there are libraries that are &lt;strong&gt;clearly&lt;/strong&gt; libraries and there are frameworks that are &lt;strong&gt;clearly&lt;/strong&gt; frameworks - but the other 90% live in this amazing fuzzy area I typically call practicality. With a dash of context many of these technologies can be viewed in a different light. In fact we've even invented this term, one that over the course of my time in IT I've started to really dislike, &lt;strong&gt;micro-frameworks&lt;/strong&gt;. We've been told that frameworks are pure evil and will ruin your software project (when in fact its usually people) but, you know, as a starting point for a project or service libraries don't quite have enough opinion so instead we'll create a term that makes us feel better about the whole thing - a &lt;strong&gt;micro-framework&lt;/strong&gt;. It's like a library but with opinion. One of the greatest examples of this abuse of verbage is &lt;a href="http://projects.spring.io/spring-boot/"&gt;Spring Boot&lt;/a&gt; - it's a fine framework that helps hide a lot of friction and complexity that comes with the Spring web stack. It has been tagged with the &lt;strong&gt;micro-framework&lt;/strong&gt; label more times than I cared to count. But thats the thing, almost nothing has been taken away from it's core functionality as a Spring MVC framework - it has simply chosen to hide it behind carefully constructed opinion and default behaviour but it is still hiding the behemoth that is Spring. &lt;/p&gt;&lt;p&gt;So &lt;a href="http://projects.spring.io/spring-boot/"&gt;Spring Boot&lt;/a&gt; takes a framework and makes it a &lt;strong&gt;micro-framework&lt;/strong&gt; (it doesn't really of course but there you go). What about the other end of the spectrum? Lets say you just started a nice shiny greenfield project and people are all up in your face about microservices so you start out building some of these fancy microservices. In time you'll start to notice common usage patterns, common behaviour or technical requirements. Being the clever spod that you are you'll start extracting these patterns and behaviours out so they can be reused across your services. Eventually you'll refine a lot of it so that parts of the lifecycle of your services are completely managed by these patterns. Now you've only gone and built a framework haven't you? Clearly that was bad design, back to the drawing board old bean eh? No. Look at &lt;a href="http://www.dropwizard.io/"&gt;Dropwizard&lt;/a&gt; as another example. A nicely curated set of well documented and tested libraries thrown together with a bit of glue code (again opinion) - all in all you end up at the same place you did with Spring Boot (again, not quite but superficially speaking you have). One is a set of curated libraries and the other is a single mass of curated classes - it's all in the packaging.&lt;/p&gt;&lt;p&gt;A framework gives you consistency and predictably - these are often good things. Decisions you shouldn't waste time making are made for you, stuff you aren't aware of yet are being dealt with while you remain blissfully unaware, security is a big one, frameworks often deal with matters of security quite well. Libraries, not so much. Its all in the wrist, it's how you use it that makes the difference. Depending on any 3rd party code too much creates nasty coupling and you should avoid this. &lt;/p&gt;&lt;p&gt;A wonderful example of avoiding 3rd party coupling is shown in &lt;a href="https://www.destroyallsoftware.com/"&gt;The Destroy All Software&lt;/a&gt; episode &lt;a href="https://www.destroyallsoftware.com/screencasts/catalog/fast-tests-with-and-without-rails"&gt;#10: Fast Tests with and Without Rails&lt;/a&gt; by Gary Bernhardt. In the episode he shows how you can get faster test cycles by isolating model code within Rails. This isolation means the model itself has no reliance on Rails (making the startup times for tests much faster) and yet can still utilise the powerful features of Rails when necessary.&lt;/p&gt;&lt;p&gt;In any project of sufficient scale you'll likely end up creating something akin to a framework anyway. It could be loving handcrafted from libraries over time or you could get immediate productivity by adopting a suitable ready made framework. The choice is yours because you have the context available to make an informed decision. So look - saying frameworks are bad and libraries good is missing the point. It's missing context. It's also missing the fact that many &lt;strong&gt;good&lt;/strong&gt; frameworks are &lt;strong&gt;good&lt;/strong&gt; because they give flexibility similar to libraries but without having to make common decisions over and over. That sort of quality isn't easy to develop so don't go judging all frameworks as if they were evil. Attempting to pigeon hole any particular technology is also a wasted effort because its so easy to subvert the standard definitions.&lt;/p&gt;&lt;p&gt;In short - use what works, don't tightly couple yourself with &lt;strong&gt;any&lt;/strong&gt; third party code (library or framework), don't be quick to judge, listen to the biased nonsensewaffle that most people speak with skepticism and think. Please, please, above all else, &lt;strong&gt;think&lt;/strong&gt;.&lt;/p&gt;</content></entry><entry><title>Quick Markdown Preview in Vim</title><updated>Thu Apr 30 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/04/30/quick-markdown-preview-in-vim/"></link><id>urn:yobriefca-se:feed:post:Quick Markdown Preview in Vim</id><content type="html">&lt;p&gt;A while back a rather excellent person (&lt;a href="https://twitter.com/tosbourn"&gt;@tosborn&lt;/a&gt;) posted an article on &lt;a href="http://tosbourn.com/view-markdown-files-terminal/"&gt;previewing files in your terminal&lt;/a&gt;. The concept is simple. Using &lt;a href="http://pandoc.org/"&gt;Pandoc&lt;/a&gt; we can convert the markdown file to HTML and pipe the result into &lt;a href="http://lynx.browser.org/"&gt;Lynx&lt;/a&gt; a terminal based browser. While this doesn't exactly give you an accurate representation of what the output will look like in a graphical browser it gives you an overall quick view of the overall layout and structure. Handy when you are building up a README for a library or tool.&lt;/p&gt;&lt;p&gt;What I'm going to show is using the same technique to render markdown files but do it without leaving the editor (assuming your editor is Vim). I'm going to assume you have both Pandoc and Lynx installed (the article linked above will explain how to do this for Mac).&lt;/p&gt;&lt;p&gt;I'm a &lt;a href="http://www.vim.org/"&gt;Vim&lt;/a&gt; user (when I can) and when I'm working with markdown files it's usually in the context of making quick edits and verifying them. To achieve this I've lifted the original process outlined in the link above and mapped it to a key mapping in Vim,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;:map &amp;lt;Leader&amp;gt;x :w&amp;lt;cr&amp;gt;:!pandoc % \| lynx -stdin&amp;lt;cr&amp;gt;:redraw!&amp;lt;cr&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Lets break this down a tiny bit,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;:map &amp;lt;Leader&amp;gt;x&lt;/code&gt; - map the following to keystroke&lt;code&gt;,x&lt;/code&gt; (&lt;code&gt;,&lt;/code&gt; is my &lt;a href="http://learnvimscriptthehardway.stevelosh.com/chapters/06.html"&gt;leader&lt;/a&gt; key in vim)&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;:w&amp;lt;cr&amp;gt;&lt;/code&gt; - save the current file&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;:!&lt;/code&gt; - start executing a shell command&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;pandoc %&lt;/code&gt; - put the current file &lt;code&gt;%&lt;/code&gt; through pandoc&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;\| lynx -stdin&amp;lt;cr&amp;gt;&lt;/code&gt; - pipe the pandoc output into lynx and issue command&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;:redraw!&amp;lt;cr&amp;gt;&lt;/code&gt; - redraw the vim window (this prevents the "Press Enter to continue prompt")&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So hitting &lt;code&gt;,x&lt;/code&gt; gives me a quick look at how the markdown file would look without leaving the terminal.&lt;/p&gt;</content></entry><entry><title>Don't Call It A Comeback</title><updated>Wed Apr 29 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/04/29/dont-call-it-a-comeback/"></link><id>urn:yobriefca-se:feed:post:Don't Call It A Comeback</id><content type="html">&lt;blockquote&gt;&lt;p&gt;Taking a break is only a break if you get back into it. Otherwise thats just retiring.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Activity on this site has declined recently. Initially it was intentional (in 2014 I decided to reduce my speaking engagements and focus on contracting), take a break, clear the head etc. but I got sidetracked and kept meaning to get back to a bit of writing and speaking. This proved harder than usual. Travelling every week kind of knocks the wind out of you, or me at least.&lt;/p&gt;&lt;p&gt;In an effort to shine a light on the lack of activity I created a &lt;a href="http://yobriefca.se/metrics"&gt;contribution chart&lt;/a&gt; to show the drop off and, interestingly enough, I've been in a bit of a dip since before I actively decided to take a break. Or perhaps I'm just finding my rhythm. Or perhaps consistency isn't my strong point, you know like MongoDB.&lt;/p&gt;&lt;p&gt;Anyway the plan is to get back at it, talk more, write more and rant more. I've missed it.&lt;/p&gt;&lt;p&gt;For the interested the &lt;a href="http://yobriefca.se/metrics"&gt;contribution chart&lt;/a&gt; was thrown together using &lt;a href="http://d3js.org/"&gt;D3&lt;/a&gt; a hacked version of the &lt;a href="http://bl.ocks.org/mbostock/4063318"&gt;calendar view example&lt;/a&gt; and CSV data generated every time this site generates.&lt;/p&gt;</content></entry><entry><title>Notes on Wake</title><updated>Mon Feb 16 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2015/02/16/notes-on-wake/"></link><id>urn:yobriefca-se:feed:post:Notes on Wake</id><content type="html">&lt;p&gt;I've spent the morning tinkering with &lt;a href="http://www.wakelang.com/"&gt;Wake&lt;/a&gt; because I had time and it popped up on Twitter at the right time.&lt;/p&gt;&lt;p&gt;It's an interesting language that aims to be&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;a fast, expressive, type safe language that gives you testability from the ground up.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;What follows is a write up of my thoughts from the short time I've spent with &lt;a href="http://www.wakelang.com/"&gt;Wake&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Baked in Dependency Injection&lt;/h2&gt;&lt;p&gt;I have plenty of experience in Java and have seen my fair share of codebases that make extensive use of dependency injection. The verbosity of Java has created many a monster. &lt;code&gt;Providers&lt;/code&gt;, &lt;code&gt;Factories&lt;/code&gt;, &lt;code&gt;FactoryProviders&lt;/code&gt; and &lt;code&gt;ProviderFactoryProviders&lt;/code&gt; fill project hierarchies all in an attempt to make the language bend to our will.&lt;/p&gt;&lt;p&gt;The main problem, aside from the obvious verbosity of Java, is its lack of syntax to support dependency injection. Vast technologies have been built around this simple concept alone. However without the language level support you are forced to go for either wizardry (see &lt;code&gt;@Inject&lt;/code&gt;) or verbosity (through additional annotations) - neither of which are &lt;a href="http://yobriefca.se/blog/2014/08/25/invasion-of-privacy/"&gt;desirable&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;One of &lt;a href="http://www.wakelang.com/"&gt;Wake&lt;/a&gt;s fundamental features is the use of &lt;a href="http://wakelang.com/#provisions"&gt;provisions&lt;/a&gt; which is essentially a means to bind dependencies to particular behaviour. Classes define exactly what they &lt;code&gt;need&lt;/code&gt; and &lt;code&gt;provide&lt;/code&gt; in order to get the job done.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;every&lt;/span&gt; &lt;span class="n"&gt;Main&lt;/span&gt; &lt;span class="nl"&gt;is:&lt;/span&gt;

  &lt;span class="n"&gt;needs&lt;/span&gt; &lt;span class="n"&gt;Printer&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printLine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the example above we can see that Main explicitly &lt;code&gt;needs&lt;/code&gt; &lt;code&gt;Printer&lt;/code&gt;. When the application is executed an instance of &lt;code&gt;Printer&lt;/code&gt; will be injected into our &lt;code&gt;Main&lt;/code&gt;. In this case Wake knows how to inject &lt;code&gt;Printer&lt;/code&gt; as it is from the standard library. We can also &lt;code&gt;provide&lt;/code&gt; our own dependencies including any rules required to construct them.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;every&lt;/span&gt; &lt;span class="n"&gt;Main&lt;/span&gt; &lt;span class="nl"&gt;is:&lt;/span&gt;

  &lt;span class="n"&gt;needs&lt;/span&gt;
    &lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;provides&lt;/span&gt;
    &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"James"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Internet"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;every&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="nl"&gt;is:&lt;/span&gt;

  &lt;span class="n"&gt;needs&lt;/span&gt;
    &lt;span class="n"&gt;Printer&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Text&lt;/span&gt;    &lt;span class="n"&gt;me&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="n"&gt;otherPerson&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printLine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;otherPerson&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" I'm "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;me&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There is a bit more code here so let me break down some of the important lines.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;We have a &lt;code&gt;Person&lt;/code&gt; class. This class &lt;code&gt;needs&lt;/code&gt; both a &lt;code&gt;Printer&lt;/code&gt; and some &lt;code&gt;Text&lt;/code&gt; that represents the persons name.&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;Person&lt;/code&gt; class has a &lt;code&gt;void&lt;/code&gt; method that takes a single &lt;code&gt;Text&lt;/code&gt; argument and uses the injected &lt;code&gt;Printer&lt;/code&gt; to print a greeting.&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;Main&lt;/code&gt; class, as before, &lt;code&gt;needs&lt;/code&gt; a &lt;code&gt;Printer&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;This time the &lt;code&gt;Main&lt;/code&gt; class also &lt;code&gt;provides&lt;/code&gt; a few things. It &lt;code&gt;provides&lt;/code&gt; a &lt;code&gt;Printer&lt;/code&gt; which I will expand on shortly and it also &lt;code&gt;provides&lt;/code&gt; a &lt;code&gt;Person&lt;/code&gt; type. More so it specifies a way to construct a &lt;code&gt;Person&lt;/code&gt; instance without having to specify a &lt;code&gt;Printer&lt;/code&gt; each time - &lt;code&gt;Person(Printer, ?Text)&lt;/code&gt;. In essence this is saying &lt;em&gt;when Main is asked to provide and instance of Person always use this particular &lt;code&gt;Printer&lt;/code&gt; instance and take a &lt;code&gt;Text&lt;/code&gt; argument&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;Our &lt;code&gt;Main&lt;/code&gt; class is called via the &lt;code&gt;main()&lt;/code&gt; entry point and at this point creates a new &lt;code&gt;Person&lt;/code&gt; instance using the &lt;code&gt;Person&lt;/code&gt; provider defined in &lt;code&gt;Main&lt;/code&gt; (hence the use of &lt;code&gt;this&lt;/code&gt; - &lt;code&gt;(Person("James") from this)&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;We call &lt;code&gt;sayHi&lt;/code&gt; on this provided instance.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It may seem initially weird for an object to define its own &lt;code&gt;needs&lt;/code&gt; and &lt;code&gt;provides&lt;/code&gt; but this need not be the case. It is possible to create discrete provider classes that encapsulate construction behaviour which can be injected into our target class. So instead of &lt;code&gt;from this&lt;/code&gt; we could say, for instance, &lt;code&gt;from PersonProvider&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;This syntax allows us to create defined contracts for our classes without a lot of extra verbosity that could mask intent. It also gives us certain guarantees around type safety and compile time checking the our object graphs are correctly defined. Finally, as an added bonus, this syntax gives us a rather clean way of wiring up stubs and mocks during testing by simply swapping out our providers with stubbed providers.&lt;/p&gt;&lt;h2&gt;Testing&lt;/h2&gt;&lt;p&gt;On the subject of testing I was a little surprised to read &lt;em&gt;"gives you testability from the ground up"&lt;/em&gt; and to not see any examples of how to write tests. Of course the language is still quite young and things are changing. After a bit of poking around I discovered tests are annotation based,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Asserts&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@TestClass&lt;/span&gt;
&lt;span class="n"&gt;every&lt;/span&gt; &lt;span class="n"&gt;MyFirstTest&lt;/span&gt; &lt;span class="nl"&gt;is:&lt;/span&gt;

  &lt;span class="nd"&gt;@Test&lt;/span&gt;
  &lt;span class="n"&gt;itIsWhatItIs&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Asserts&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;Asserts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;that&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;Equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running &lt;code&gt;make&lt;/code&gt; will run all the discovered tests in the &lt;code&gt;tests/&lt;/code&gt; folder. Fairly straightforward.&lt;/p&gt;&lt;h2&gt;Other Nuances&lt;/h2&gt;&lt;p&gt;There are few things in Wake that exist in an attempt to make code more concise, more readable and potentially drive design down a particular route.&lt;/p&gt;&lt;h3&gt;Naming Variables&lt;/h3&gt;&lt;p&gt;Most variables do not need to be given a name. Our &lt;code&gt;Person&lt;/code&gt; class above could be written in a more concise manner,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;every&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="nl"&gt;is:&lt;/span&gt;

  &lt;span class="n"&gt;needs&lt;/span&gt; &lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;sayHi&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;$Text&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printLine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;$Text&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" I'm "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
  &lt;li&gt;We have one instance of &lt;code&gt;Printer&lt;/code&gt; so we don't need to give it a variable name and can refer to it directly by its type.&lt;/li&gt;
  &lt;li&gt;The same applies for &lt;code&gt;Text&lt;/code&gt; however as we have 2 &lt;code&gt;Text&lt;/code&gt; instances we can make use of the &lt;code&gt;$&lt;/code&gt; symbol to avoid &lt;a href="http://www.wakelang.com/#shadowing"&gt;conflicts&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The argument here is that the type of a variable is often the most meaningful name. This is easily a very contentious point which makes sense in some situations but not necessarily in others. Thankfully its possible to give variables names which I did in the first instance in order to avoid introducing too many new concepts in one go. This approach can take you down the "thinking in types" path where you make the types more rich rather than just place holders for rich data.&lt;/p&gt;&lt;h3&gt;Method Names&lt;/h3&gt;&lt;p&gt;Like in Swift and Objective-C, Wake methods names don't need to follow the &lt;name&gt;(&lt;args&gt;) structure. Arguments can in fact be spread throughout the name. While it may seem odd at first it can make complex methods a bit clearer.&lt;/args&gt;&lt;/name&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;sayHelloTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;As&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;$Text&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Printer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;printLine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" I'm "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;$Text&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;sayHelloTo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Emma"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="n"&gt;As&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"James"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Wrapping Up&lt;/h2&gt;&lt;p&gt;I've spent more time putting this article together than I have spent with the language but all in all there are some compelling features in &lt;a href="http://www.wakelang.com/"&gt;Wake&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The contract based approach of &lt;code&gt;needs&lt;/code&gt; and &lt;code&gt;provides&lt;/code&gt; makes the structure of your application very clear and aids testing&lt;/p&gt;&lt;p&gt;The ability to reduce the volume of code by eliminating unnecessary cruft (variable names for example), &lt;strong&gt;where it fits&lt;/strong&gt;, goes a step further to allowing us to create more maintainable code.&lt;/p&gt;&lt;p&gt;As of right now the documentation has some holes in it (for example around testing) and the language is still very young but I think its a language worth keeping an eye on.&lt;/p&gt;&lt;p&gt;I've only scratched the surface of the language and its future plans so I recommend, if this is the first time you're reading about Wake, you check out the site for more information - &lt;a href="http://www.wakelang.com/"&gt;http://www.wakelang.com/&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>The Twelve Factor App</title><updated>Tue Jan 20 00:00:00 UTC 2015</updated><author><name>James Hughes</name></author><link href="/presentations/twelve-factor-app.pdf"></link><id>urn:yobriefca-se:feed:post:The Twelve Factor App</id><content type="html"></content></entry><entry><title>Yesql - SQL in SQL in Clojure</title><updated>Tue Nov 25 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/11/25/yesql-sql-in-sql-in-clojure/"></link><id>urn:yobriefca-se:feed:post:Yesql - SQL in SQL in Clojure</id><content type="html">&lt;blockquote&gt;&lt;p&gt;Fair warning. This was initially a transcript for a yesql screencast I intended to do but time is dragging on and I really can't see myself getting to that any time soon.&lt;/p&gt;&lt;p&gt;Why does this matter?&lt;/p&gt;&lt;p&gt;Well I wouldn't be surprised if this reads pretty much like the yesql README.md with some minor embellishments here and there (and perhaps some opinion). But I'm putting it out there just, well, just because. Yesql is lovely and consider this a tribute of sorts.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;&lt;a href="https://github.com/krisajenkins/yesql"&gt;Yesql&lt;/a&gt; is a Clojure library for accessing SQL capable databases using SQL. Unlike its contemporaries yesql, as the name alludes to, embraces SQL. The argument here is why create a DSL, no matter how beautifully crafted, when we already have a perfect DSL already in the form of SQL.&lt;/p&gt;&lt;p&gt;To go a bit further lets look at the rationale in yesql's own README.&lt;/p&gt;&lt;p&gt;You could write your data access like this, as embedded SQL strings,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt; &lt;span class="s"&gt;"SELECT * FROM users WHERE country_code = ?"&lt;/span&gt; &lt;span class="s"&gt;"GB"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But this doesn't scale very well,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;commands can get scattered about the code base,&lt;/li&gt;
  &lt;li&gt;its hard to see syntax errors, and,&lt;/li&gt;
  &lt;li&gt;longer statements can be difficult to read without any sort of syntax highlighting.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The next logical step taken to solve this problem is by adding an additional layer of abstraction. Language specific DSLs are created to dynamically build SQL.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;select &lt;/span&gt;&lt;span class="ss"&gt;:*&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;= &lt;/span&gt;&lt;span class="ss"&gt;:country_code&lt;/span&gt; &lt;span class="s"&gt;"GB"&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The benefit these DSLs have is that they remove the need to know the specific dialect of SQL you're using. As each database has a slightly different SQL dialect you simply specify an adapter, such as PostgresAdapater) and the translated SQL will magically be Postgres compatible. Moving to a different database engine is a matter of specifying a new adapter with no code changes.&lt;/p&gt;&lt;p&gt;Sounds good right? Well not quite - as with almost all abstractions they start to break down when you tread off the happy path.&lt;/p&gt;&lt;p&gt;Every database system will likely have additional, non-core features, that you'll probably want to exploit, in fact these features are usually factored into selection criteria when you were deciding what to use. Postgres array and JSON data types are a good example of this. These are features that your clever DSL abstraction can't support without extra wrangling because they address the generality of problems. Suddenly you're back to writing raw sql for some of the queries and we are back where we started.&lt;/p&gt;&lt;p&gt;Yesql tackles these problems by putting SQL in SQL files and allows you to reference these blobs of SQL as clojure functions. So you'll lose the ability to execute the same code across different databases, which if my experience is anything to go by isn't the worst thing that can happen, but you'll gain better editor support, correct syntax highlighting and one less abstraction to ruin your mood.&lt;/p&gt;&lt;p&gt;Seeing it in action is the best way to demonstrate its simple but powerful api. We'll start by cloning a ready rolled project template&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;git clone -b 00-starting-point git@github.com:yobriefcasts/007-yesql.git
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is nothing more than a project generated by &lt;code&gt;lein new&lt;/code&gt; with 2 dependencies added,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;One is the Yesql library dependency itself, currently at version 0.4.0, and&lt;/li&gt;
  &lt;li&gt;The other is the h2 database driver which we will use as our database in these examples&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Finally we have a file based h2 database instance which is pre-populated with a people table containing a few rows of dummy data.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;1	James	Hughes	james@yobriefca.se
2	Emma	Hughes	emma@yobriefca.se
3	Ollie	Hughes	ollie@yobriefca.se
4	Nate	Hughes	nate@yobriefca.se
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;The First Query&lt;/h2&gt;&lt;p&gt;So lets get this data out of the database.&lt;/p&gt;&lt;p&gt;As is common across most SQL based libraries in Clojure Yesql derives its connections from a &lt;code&gt;db-spec&lt;/code&gt;. This is the same &lt;code&gt;db-spec&lt;/code&gt; map you'd pass Clojure's JDBC library or even korma. I'll provide &lt;a href="http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html"&gt;a link&lt;/a&gt; to the relevant documentation if you want more information.&lt;/p&gt;&lt;p&gt;The &lt;code&gt;db-spec&lt;/code&gt; required to connect to the current h2 database is already defined in the &lt;code&gt;core.clj&lt;/code&gt; file.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;db-spec&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:classname&lt;/span&gt;    &lt;span class="s"&gt;"org.h2.Driver"&lt;/span&gt;
              &lt;span class="ss"&gt;:subprotocol&lt;/span&gt;  &lt;span class="s"&gt;"h2"&lt;/span&gt;
              &lt;span class="ss"&gt;:subname&lt;/span&gt;      &lt;span class="s"&gt;"./resources/demo.db"&lt;/span&gt;
              &lt;span class="ss"&gt;:user&lt;/span&gt;         &lt;span class="s"&gt;"sa"&lt;/span&gt;
              &lt;span class="ss"&gt;:password&lt;/span&gt;     &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So now if we want to create a SQL file &lt;code&gt;get-all-people.sql&lt;/code&gt; under the &lt;code&gt;src/sql&lt;/code&gt; directory we can write a simple &lt;code&gt;select *&lt;/code&gt; statement from the &lt;code&gt;people&lt;/code&gt; table.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sql"&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Its worth noting that the &lt;code&gt;sql&lt;/code&gt; folder name isn't necessary I simply used it for neatness. By default when calling &lt;a href="https://github.com/krisajenkins/yesql"&gt;Yesql&lt;/a&gt; - paths will be relative to the &lt;code&gt;src&lt;/code&gt; directory so you can organise it however you want.&lt;/p&gt;&lt;p&gt;Now we need to call this from our code. You'll notice in &lt;code&gt;core.clj&lt;/code&gt; that I have already included a single require for &lt;code&gt;defquery&lt;/code&gt; from the &lt;code&gt;yesql.core&lt;/code&gt; namespace.&lt;/p&gt;&lt;p&gt;&lt;code&gt;defquery&lt;/code&gt; is one of the 2 macros that gives you an entry point into &lt;a href="https://github.com/krisajenkins/yesql"&gt;Yesql&lt;/a&gt;, we will get to the other one shortly. &lt;code&gt;defquery&lt;/code&gt; takes a name and a path to the query file. This allows &lt;code&gt;defquery&lt;/code&gt; to parse the SQL file, convert it into a callable function and define a &lt;code&gt;var&lt;/code&gt; based on the name you've given it.&lt;/p&gt;&lt;p&gt;We can define our &lt;code&gt;get-all-people&lt;/code&gt; query like so,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defquery&lt;/span&gt; &lt;span class="nv"&gt;get-all-people&lt;/span&gt; &lt;span class="s"&gt;"sql/get-all-people.sql"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Although I've kept them the same the name of the file and the query name don't need to match. If we evaluate this line now we get back our &lt;code&gt;def&lt;/code&gt;'d function. Subsequent evaluations, for example if you've changed the contents of the SQL, will result in the &lt;code&gt;def&lt;/code&gt; being redefined.&lt;/p&gt;&lt;p&gt;If we call our new function as-is we'll get an error.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get-all-people&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is because we also need to supply the &lt;code&gt;db-spec&lt;/code&gt; that was already defined for us. If we call the function correctly we should see our results.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get-all-people&lt;/span&gt; &lt;span class="nv"&gt;db-spec&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Lets give our output a bit more structure. We can import &lt;code&gt;clojure.pprint&lt;/code&gt;s &lt;code&gt;pprint&lt;/code&gt; function&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;yesql-tinkering.core&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;yesql.core&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;defquery&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;clojure.pprint&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;pprint&lt;/span&gt;&lt;span class="p"&gt;]]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And run our query again.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pprint&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get-all-people&lt;/span&gt; &lt;span class="nv"&gt;db-spec&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Passing data to queries&lt;/h2&gt;&lt;p&gt;Executing static SQL isn't going to get you very far, if it does and you are only writing read-only systems with ready rolled queries - can I please come work with you? At the very least your queries often require &lt;code&gt;WHERE&lt;/code&gt; clauses that need to take values based on some user input from outside the database.&lt;/p&gt;&lt;p&gt;To demonstrate how we do this in &lt;a href="https://github.com/krisajenkins/yesql"&gt;Yesql&lt;/a&gt; lets add a new query that filters by surname. We can start by creating a new SQL file &lt;code&gt;src/sql/get-people-by-surname.sql&lt;/code&gt; and write our query. As with most frameworks that make use of parameterised queries we put token in place of the value we want to pass in, in the case of Yesql this can be a keyword that will associate a descriptive name against the argument..&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sql"&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next we can use the defquery macro to "realise" this query.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defquery&lt;/span&gt; &lt;span class="nv"&gt;get-people-by-surname&lt;/span&gt; &lt;span class="s"&gt;"sql/get-people-by-surname.sql"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can call this as we previous did except we need to pass it an argument for last_name.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pprint&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get-people-by-surname&lt;/span&gt; &lt;span class="nv"&gt;db-spec&lt;/span&gt; &lt;span class="s"&gt;"Hughes"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Documentation&lt;/h2&gt;&lt;p&gt;Lets diverge a little from our path. Imagine a more complex SQL statement that accepts more than one argument, lets say it has a very domain specific name like &lt;code&gt;isolate_aggregated_upstream_resources&lt;/code&gt; - how would you 1. find out what this statement does and 2. how you would call it correctly? Well most obviously you could simply look at the SQL and work it out but that's not really an effective way to go about things.&lt;/p&gt;&lt;p&gt;In out last example SQL statement we used a parameter name of last_name, well this wasn't just for fun, if well look at the documentation for our query we can see that the parameter name is used as an argument name to our function&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.repl/doc&lt;/span&gt; &lt;span class="nv"&gt;get-people-by-surname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is handy but we can go one step further and supply &lt;code&gt;doc&lt;/code&gt; compatible documentation in our SQL in the form of comments. By prepending a comment on top of our SQL we can dive into supplying additional commentary around what this SQL is actually doing.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sql"&gt;&lt;span class="c1"&gt;-- Find all people for a given surname&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If we evaluate our &lt;code&gt;defquery&lt;/code&gt; again and look at the function signature we should see our comment available as a docstring.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.repl/doc&lt;/span&gt; &lt;span class="nv"&gt;get-people-by-surname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nv"&gt;yesql-tinkering.core/get-people-by-surname&lt;/span&gt;
&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nv"&gt;db&lt;/span&gt; &lt;span class="nv"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
  &lt;span class="nv"&gt;Find&lt;/span&gt; &lt;span class="nv"&gt;all&lt;/span&gt; &lt;span class="nv"&gt;people&lt;/span&gt; &lt;span class="nb"&gt;for &lt;/span&gt;&lt;span class="nv"&gt;a&lt;/span&gt; &lt;span class="nv"&gt;given&lt;/span&gt; &lt;span class="nv"&gt;surname&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Grouping queries&lt;/h2&gt;&lt;p&gt;The one file per query thing is all well and good until it isn't. You could rather quickly end up with a raft of folders with 3 or 4 queries each which is a bit of a pants way of arranging things.&lt;/p&gt;&lt;p&gt;Yesql provides a way to group similar queries in a single file, well they don't &lt;strong&gt;have&lt;/strong&gt; to be similar but lets assume we have some logical grouping here.&lt;/p&gt;&lt;p&gt;So lets add a few accessor methods for getting people out of our database. If we create a SQL file &lt;code&gt;src/sql/get-persons.sql&lt;/code&gt; and add 2 queries, one for getting a user by ID and another for getting a user by email address. When you use &lt;code&gt;defquery&lt;/code&gt; you specify the name of the function in the declaration and the entire file is used as a basis for the query however when we are defining multiple queries at once we need both logically separate the queries in the file and associate a name with each one. We do this by adding a structured comment above each query starting with &lt;code&gt;name:&lt;/code&gt; and followed by the name you want to refer to the query as in your code.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sql"&gt;&lt;span class="c1"&gt;-- name: get-person-by-id&lt;/span&gt;
&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;

&lt;span class="c1"&gt;-- name: get-person-by-email&lt;/span&gt;
&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;email_address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;email_address&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To define these queries in our code we need to import the other macro in &lt;code&gt;yesql&lt;/code&gt;s arsenal &lt;code&gt;defqueries&lt;/code&gt;. We can then use this to import our queries,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defqueries&lt;/span&gt; &lt;span class="s"&gt;"sql/get-persons.sql"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You'll notice that &lt;code&gt;defqueries&lt;/code&gt; returns a vector of the defined functions and we can see our two function handles. We can call each of these in the usual way.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get-person-by-id&lt;/span&gt; &lt;span class="nv"&gt;db-spec&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get-person-by-email&lt;/span&gt; &lt;span class="nv"&gt;db-spec&lt;/span&gt; &lt;span class="s"&gt;"james@yobriefca.se"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Non-Queries&lt;/h2&gt;&lt;p&gt;We've still only dealt with querying our database but most systems usually need to manipulate the contained data by either writing new rows or updating and deleting existing rows.&lt;/p&gt;&lt;p&gt;Rather than add a new set of macros - &lt;a href="https://github.com/krisajenkins/yesql"&gt;yesql&lt;/a&gt; uses naming conventions to signal to the macros that the operation is a non-query operation.&lt;/p&gt;&lt;p&gt;For example if we create a SQL file containing a statement for inserting a new user that expects &lt;code&gt;first_name&lt;/code&gt;, &lt;code&gt;last_name&lt;/code&gt; and &lt;code&gt;email_address&lt;/code&gt;, the database will auto generate our ID value.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sql"&gt;&lt;span class="k"&gt;insert&lt;/span&gt; &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="n"&gt;people&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email_address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;email_address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can use &lt;code&gt;defquery&lt;/code&gt; again to create our function except this time we prepend the name with an exclamation mark. This is the marker to tell yesql that this is a statement rather than a query.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defquery&lt;/span&gt; &lt;span class="nv"&gt;insert-person!&lt;/span&gt; &lt;span class="s"&gt;"sql/insert-person.sql"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The function expects the usual &lt;code&gt;db-spec&lt;/code&gt; object as well as our arguments&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.repl/doc&lt;/span&gt; &lt;span class="nv"&gt;insert-person!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And we can call it in the normal way.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;insert-person!&lt;/span&gt; &lt;span class="nv"&gt;db-spec&lt;/span&gt; &lt;span class="s"&gt;"Bill"&lt;/span&gt; &lt;span class="s"&gt;"Thompson"&lt;/span&gt; &lt;span class="s"&gt;"bill@thompson.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This style can be used with any non-query statement such as &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt;, &lt;code&gt;delete&lt;/code&gt;, &lt;code&gt;alter&lt;/code&gt; and all those other statements your database engine supports.&lt;/p&gt;&lt;p&gt;Statements will return the number of rows affected by default however in the case of insert statements against tables that have auto-generated ids you may want the id of the newly inserted row returned instead. You can achieve this in &lt;a href="https://github.com/krisajenkins/yesql"&gt;yesql&lt;/a&gt; with the less than exclamation (&lt;code&gt;&amp;lt;!&lt;/code&gt;) instead. If we we redefine our &lt;code&gt;insert-person!&lt;/code&gt; function to use this we can see that it will return a different set of information&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defquery&lt;/span&gt; &lt;span class="nv"&gt;insert-person&amp;lt;!&lt;/span&gt; &lt;span class="s"&gt;"sql/insert-person.sql"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;insert-person&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;db-spec&lt;/span&gt; &lt;span class="s"&gt;"Emma"&lt;/span&gt; &lt;span class="s"&gt;"Thompson"&lt;/span&gt; &lt;span class="s"&gt;"emma@thompson.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What is actually returned depends on the database engine, h2 returns a map containing the id but some engines return the full row.&lt;/p&gt;&lt;h2&gt;Wrap Up&lt;/h2&gt;&lt;p&gt;Thats just about covers the main features of &lt;a href="https://github.com/krisajenkins/yesql"&gt;yesql&lt;/a&gt; which as you can see is a neat little solution to bridging the gap between the database and your code. It avoids all the normal pain of wrangling with other peoples conventions and leaky abstractions and allows you to use SQL directly which is a much better medium to express queries against relational data stores.&lt;/p&gt;</content></entry><entry><title>Preflight Checks</title><updated>Thu Oct 09 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/10/09/preflight-checks/"></link><id>urn:yobriefca-se:feed:post:Preflight Checks</id><content type="html">&lt;p&gt;What I'm about to describe is in no way a new thing, you'll find evidence of this sort of pattern everywhere. I've noticed some people do it instinctively and others simply fall into the trap that this sort of technique/pattern tries to avoid. Therefore I think its worth a bit of a mention.&lt;/p&gt;&lt;h2&gt;An Example in a Different Context&lt;/h2&gt;&lt;p&gt;Lets explore the problem in a slightly different context - dynamic vs static languages. Imagine the given scenario in JavaScript&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// "aa"&lt;/span&gt;
&lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bc"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// "bc"&lt;/span&gt;
&lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bc"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;    &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We've defined a function called &lt;code&gt;longest&lt;/code&gt; that returns the longest string argument between the two passed to it. We also defined a couple of sample uses of the function,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code&gt;"aa"&lt;/code&gt; is longer than &lt;code&gt;"b"&lt;/code&gt; so &lt;code&gt;"aa"&lt;/code&gt; gets returned&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;"bc"&lt;/code&gt; is longer than &lt;code&gt;"a"&lt;/code&gt; so &lt;code&gt;"bc"&lt;/code&gt; gets returned&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Here be dragons!&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;In the last example our return value isn't what we expected. At best we'd expect &lt;code&gt;"bc"&lt;/code&gt;, with a length of 2, to be longer than &lt;code&gt;1&lt;/code&gt;, whose string value would be &lt;code&gt;"1"&lt;/code&gt; and therefore have a length of 1. But that isn't the case. Numbers don't have a &lt;code&gt;length&lt;/code&gt; property and therefore the result of &lt;code&gt;1.length&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt;. Comparing &lt;code&gt;undefined&lt;/code&gt; to 2 is going to yield false, hence our unexpected return value.&lt;/p&gt;&lt;p&gt;Lets look at this problem in another language, one that is statically typed - TypeScript,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aa"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bb"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;longest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bb"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Trying to compile this sample will fail on the last line because a number isn't a string. This happens long before you get to run your code.&lt;/p&gt;&lt;h2&gt;The Problem, Solution and Result&lt;/h2&gt;&lt;p&gt;So what's the problem? Well in the dynamic example we won't know about the issue until such times as the application gets into a certain state and the manifest problem may happen elsewhere far far down the call chain causing great confusion and frustration.&lt;/p&gt;&lt;p&gt;Solutions to this problem are available.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;In static languages we actually have to bend the type system to encounter such problems because we have type checking and compilation up front providing a certain level of application correctness.&lt;/li&gt;
  &lt;li&gt;In dynamic languages we avoid these issues by writing tests that (should) ensure correctness of our application before we run them.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Ultimately being able to know something is wrong &lt;strong&gt;as early as possible&lt;/strong&gt; is essential to making releases pain free and predictable. In turn, being able to release with confidence allows you to release more often and tighten those vital product feedback loops.&lt;/p&gt;&lt;h2&gt;The Problem at a Higher Level&lt;/h2&gt;&lt;p&gt;Now lets move away from code and compiler level concerns and think about your application as a deployable thing. Your app will no doubt have dependencies on external services, datastores, configuration or other applications within the same host. All of these things will cause your app to go into some form of failure state should they be unavailable (lets leave aside the fact that services can go down during the course of a running application, thats a different matter entirely). The issue may not be immediately obvious either - a misconfigured application that you call may only present errors for a given set of requirements and finding out whats wrong in a production or other similarly closed off environment can be a a test of any ones patience.&lt;/p&gt;&lt;p&gt;Being able to detect, as early as possible; like we do with a compiler or test suite, if our applications environment is in a valid state can save a lot of pain further down the line.&lt;/p&gt;&lt;p&gt;Some of you may be ready to tell me that your favourite provisioning tool (Puppet, Chef, Ansible, Salt etc.) gives you certain guarantees already and that is true to a certain extent but they are often not a sure fire guarantee.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Nothing is perfect and sometimes even the most tested tools behave differently&lt;/li&gt;
  &lt;li&gt;You may not have it set up correctly to deal with certain unique states of deployment boxes&lt;/li&gt;
  &lt;li&gt;You may have ultra-secure environments that you can't even run these tools on without a lot of pain and so manual provisioning is necessary.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Additionally if you are following any of the points in the &lt;a href="http://12factor.net/"&gt;Twelve Factor App Methodology&lt;/a&gt; you'll notice you are relying more on the hosting environment and this can amplify the problem.&lt;/p&gt;&lt;h2&gt;The Preflight Check&lt;/h2&gt;&lt;p&gt;The pre-flight check pattern aims to address these problems by ensuring invalid states in application dependencies fail deployment in a way that lights a flare above the problem so it can be fixed.&lt;/p&gt;&lt;p&gt;A few things led me to develop this habit/pattern&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;I was deploying smaller services to Heroku that promotes &lt;a href="http://12factor.net/config"&gt;pushing config down to the OS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;I was a standard Play! stack which often has less-than-helpful error messages when things go wrong during startup&lt;/li&gt;
  &lt;li&gt;I was provisioning things using vagrant and puppet/chef/ansible which, during the early stages, I often forgot to update as the applications needs evolved.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Ultimately I got bored of opaque stacktraces and bizarre behaviour in odd edge cases and so attempted to ensure my hosting environment was in good shape before we "took off" so to speak.&lt;/p&gt;&lt;p&gt;In short a pre-flight check is code that runs when your application starts, asserts the state of the environment or external services/applications. If something fails the code will halt startup with a meaningful message that can be used to quickly diagnose the problem.&lt;/p&gt;&lt;p&gt;In Play! this took the form of a hook in &lt;code&gt;Global.scala&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Global&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;GlobalSettings&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;onStart&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;performPreflightChecks&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;performPreflightChecks&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="n"&gt;getOrElse&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[PREFLIGHT_CHECK] DATABASE_URL is not set"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[PREFLIGHT_CHECK] DATABASE_URL is not set"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is an exceptionally primitive example (that I've written without the aid of a compiler so it may be incorrect) - in fact as the pattern developed I eventually extracted the preflight check pattern into it's own plugin that performed a number of things,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Ran a number of checks (classes that extended &lt;code&gt;Runnable&lt;/code&gt;) within a configurable namespace.&lt;/li&gt;
  &lt;li&gt;Logged a report of checks that passed and failed (regardless of if everything passed or not)&lt;/li&gt;
  &lt;li&gt;Exposed a method that can be called in tests - where deployment environments also supported running test directly.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Some of the checks I've used have been,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Ensuring ImageMagick is correctly installed with the correct codecs (difficult to get right even if you use a provisioning tool I've found)&lt;/li&gt;
  &lt;li&gt;Ensuring System environment variables are set correctly&lt;/li&gt;
  &lt;li&gt;Database connectivity&lt;/li&gt;
  &lt;li&gt;OpenCV JNI bindings available&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Of course, this is in no way limited to Play! or Heroku, I've used this across a number of technologies (Clojure, Ruby, Dropwizard etc.) and environments (locally, vagrant, AWS, Rackspace, Heroku etc.) and the time tackling silly oversights has been greatly reduced.&lt;/p&gt;&lt;p&gt;Many technologies already have the capacity to perform preflight checks in a way that integrates with your ops story (Dropwizards healthchecks are a perfect example) but preflight checks would mandate running these checks as the application starts as well.&lt;/p&gt;&lt;p&gt;In short, regardless of technology or platform, it is essential that deployments validate application and hosting environment state before going live. The preflight check pattern aims to address this issue by ensuring you map out all you dependencies of your intended host irrespective of where that may be.&lt;/p&gt;</content></entry><entry><title>Operation Sprint Finish</title><updated>Sun Oct 05 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/10/05/operation-sprint-finish/"></link><id>urn:yobriefca-se:feed:post:Operation Sprint Finish</id><content type="html">&lt;p&gt;A few months back I closed off a busy period. Between normal 9-5 working and running some &lt;a href="http://yobriefca.se/talks/"&gt;workshops and talks&lt;/a&gt;, I didn't have much free to devote to side projects and my writing. As soon as things died down I was ready to dive into all those ideas that I had logged and pushed to the back of my mind. Thats about as far as I got. Thinking. Little to no action, and even when I did muster the motivation to actually start something I either got distracted with a different; fresher idea or over thought the problem so hard that I either reduced it to something that already exists or over complicated it that it lost the appeal. A lot of these are signs of burnout so I decided to take some time to clear my head. I've done little to no blogging, accidentally ran a &lt;a href="http://yobriefca.se/presentations/functional-programming-with-clojure.pdf"&gt;Clojure workshop&lt;/a&gt; and generally thought about how to approach the problem of tackling my ideas rather than the ideas themselves.&lt;/p&gt;&lt;p&gt;I am now into my third week of what I've playfully dubbed &lt;strong&gt;Operation Sprint Finish&lt;/strong&gt; and the reason why I'm talking about this now is that this week is my writing week - a week purely devoted to writing about all those topics I have listed in Wunderlist (though not necessarily publishing everything this week). Based on the last 2 weeks I've genuinely made decent progress on two of my personally important ideas than I did prior to having structure.&lt;/p&gt;&lt;p&gt;So what is &lt;strong&gt;Operation Sprint Finish&lt;/strong&gt;? Well I first started by listing as many of the ideas I've had as possible (I ended up with about 20 or so that I really wanted to tackle). Next I selected 5 of these and designated them as &lt;strong&gt;immediate&lt;/strong&gt; and the rest as &lt;strong&gt;sometime in the future&lt;/strong&gt;. Well technically I selected 6 but that also includes the ever present &lt;strong&gt;writing, screencasting and presenting&lt;/strong&gt; work (which all fall under the same high level category - or if you're into agile buzzwords - &lt;strong&gt;epic&lt;/strong&gt;). These main ideas are the first batch of ideas I want to take as far as I logically can before I determine them either redundant or release them in some form. From these six epics I generated a list of things that need done and could be done reasonably within a one week period. Most of these were marked as &lt;strong&gt;Idea XYZ Sprint #1&lt;/strong&gt;, &lt;strong&gt;Idea ABC Sprint #1&lt;/strong&gt; etc. with some overarching objectives and goals. Then using &lt;strong&gt;&lt;a href="http://www.random.org/"&gt;REALSCIENCE&lt;/a&gt;&lt;/strong&gt; I picked one of the items and moved that into play for the next week/sprint.&lt;/p&gt;&lt;p&gt;For one week I focus squarely on that task. If new things come up they go on the backlog, new ideas go on the &lt;strong&gt;sometime in the future&lt;/strong&gt; list (or swapped with one of the &lt;strong&gt;immediate&lt;/strong&gt; epics as there can be only 6). At the end of the week I leave a comment (I'm using &lt;a href="http://trello.com"&gt;Trello&lt;/a&gt; to orchestrate all this) on the epic so that I can pick up any future tasks quickly and pick my next item. In fact I seem to find picking the tasks for the next 2 weeks at once a rather nice incentive as I know what to look forward to.&lt;/p&gt;&lt;p&gt;From the first two weeks I've managed to build&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;A slightly more opinionated &lt;a href="http://www.luminusweb.net"&gt;Luminus&lt;/a&gt; project structure that gives me a great starting point for the app I was working on as well as future web apps using a Clojure stack.&lt;/li&gt;
  &lt;li&gt;A semicomplete prototype for an Android app (using Kotlin which was a pleasure) and a lot of design ideas for the app which was very frustrating but also rather fulfiling.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Both of these weeks have resulted in results that are far more complete than I've managed in recent times.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/operation-sprint-finish.png" alt="Operation Sprint Finish" _="_" /&gt;&lt;/p&gt;&lt;p&gt;And now this week I can start putting some articles on my site that I've been itching to talk about for a while. Perhaps even record this screencast I've been threating to do.&lt;/p&gt;&lt;p&gt;What I've done is allow myself to not be distracted, not talk myself out of an idea because the idea on the the other side is greener and worst case, should I hit a hard part (like how to approach the user experience of the mobile app I'm working on) I don't just give up and instead think it through and potentially crack it (which I think I did).&lt;/p&gt;&lt;p&gt;Indeed I expect many people to be saying "So what? You're just applying the agile-y stuff to getting side projects done" and thats true but I still think there is value in writing about it. Besides, at the very least, this article still counts towards my current sprint deliverable which I guess is something, right?&lt;/p&gt;</content></entry><entry><title>Functional Programming with Clojure</title><updated>Tue Sep 23 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="/presentations/functional-programming-with-clojure.pdf"></link><id>urn:yobriefca-se:feed:post:Functional Programming with Clojure</id><content type="html"></content></entry><entry><title>Tyrannosaurus Rx</title><updated>Wed Sep 03 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="/presentations/tyrannosaurus-rx.pdf"></link><id>urn:yobriefca-se:feed:post:Tyrannosaurus Rx</id><content type="html"></content></entry><entry><title>Simple stubbing with lein-cooper and lein-oneoff</title><updated>Tue Aug 26 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/08/26/simple-stubbing-with-lein-cooper-and-lein-oneoff/"></link><id>urn:yobriefca-se:feed:post:Simple stubbing with lein-cooper and lein-oneoff</id><content type="html">&lt;p&gt;Work on &lt;a href="https://github.com/kouphax/lein-cooper"&gt;lein-cooper&lt;/a&gt; has been pretty quiet. It's not because it has been abandoned but because it does enough for my needs. As and when it starts getting issues or pull requests coming in I can start adapting it to fit the wider needs of others.&lt;/p&gt;&lt;p&gt;Recently I came across a lovely leiningen plugin called &lt;a href="https://github.com/mtyaka/lein-oneoff"&gt;&lt;code&gt;lein-oneoff&lt;/code&gt;&lt;/a&gt;. &lt;code&gt;oneoff&lt;/code&gt; allows you to specify dependencies for a clojure script at the top of a clojure script (a bit like what &lt;a href="http://groovy.codehaus.org/Grape"&gt;Grape&lt;/a&gt; does in the Groovy world I believe). This means you don't have to spin up all the project infrastructure to run a simple script. So instead of &lt;code&gt;lein new server&lt;/code&gt; and editing the &lt;code&gt;core.clj&lt;/code&gt; file to mock up a simple "Hello World" sample you could do something like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defdeps&lt;/span&gt;
  &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;org.clojure/clojure&lt;/span&gt; &lt;span class="s"&gt;"1.6.0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;ring/ring-jetty-adapter&lt;/span&gt; &lt;span class="s"&gt;"1.3.0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;ring&lt;/span&gt; &lt;span class="s"&gt;"1.3.1"&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;server&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;ring.adapter.jetty&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;run-jetty&lt;/span&gt;&lt;span class="p"&gt;]]))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;request&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:status&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
   &lt;span class="ss"&gt;:headers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt; &lt;span class="s"&gt;"text/html"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="ss"&gt;:body&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run-jetty&lt;/span&gt; &lt;span class="nv"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:port&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If this were saved as &lt;code&gt;server.clj&lt;/code&gt; we could use &lt;code&gt;lein-oneoff&lt;/code&gt; (declared in the global &lt;code&gt;~/.lein/profiles.clj&lt;/code&gt;) to run it like so,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;&amp;gt; lein oneoff server.clj
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice the &lt;code&gt;defdeps&lt;/code&gt; form at the top of the file, this will be used to grab dependencies and set up the appropriate classpath for running the script.&lt;/p&gt;&lt;p&gt;Pretty neat.&lt;/p&gt;&lt;p&gt;I also noticed that this would be a perfect accompaniment for a typical usage of &lt;code&gt;lein-cooper&lt;/code&gt; - &lt;strong&gt;stubbing out external services&lt;/strong&gt;. If you're building a system that integrates with other services (either internal or external) you introduce a bit of friction when it comes to developing and testing (at the integration level at least) your system. Either you have to build and run dependent services or rely on external services that may or may not be available. A simple solution is to develop against simple stubs for these services. This is what &lt;code&gt;coop-off&lt;/code&gt; (the new power couple portmanteau I've given to the &lt;code&gt;lein-cooper&lt;/code&gt;/&lt;code&gt;lein-oneoff&lt;/code&gt; combination) can make easier.&lt;/p&gt;&lt;p&gt;Just create a &lt;code&gt;stubs&lt;/code&gt; folder in your project, create standalone service stubs (like the example below that also demonstrates the use of &lt;code&gt;oneoff&lt;/code&gt;s &lt;code&gt;*command-line-args*&lt;/code&gt; for accessing arguments passed with the command),&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defdeps&lt;/span&gt;
  &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;org.clojure/clojure&lt;/span&gt; &lt;span class="s"&gt;"1.6.0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;ring/ring-jetty-adapter&lt;/span&gt; &lt;span class="s"&gt;"1.3.0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;org.clojure/data.json&lt;/span&gt; &lt;span class="s"&gt;"0.2.5"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;ring&lt;/span&gt; &lt;span class="s"&gt;"1.3.1"&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;server&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;ring.adapter.jetty&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;run-jetty&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
            &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;clojure.data.json&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="nv"&gt;json&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;request&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:status&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;
    &lt;span class="ss"&gt;:headers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="ss"&gt;:body&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;json/write-str&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:success&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt; &lt;span class="ss"&gt;:data&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;port&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;or &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Integer.&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;first &lt;/span&gt;&lt;span class="nv"&gt;*command-line-args*&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;start-server&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;port&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;run-jetty&lt;/span&gt; &lt;span class="nv"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:port&lt;/span&gt; &lt;span class="nv"&gt;port&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;start-server&lt;/span&gt; &lt;span class="nv"&gt;port&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And you can integrate the running of this and other stubs alongside using &lt;code&gt;lein-cooper&lt;/code&gt;. Just create a &lt;code&gt;Procfile&lt;/code&gt; for &lt;code&gt;lein-cooper&lt;/code&gt; with commands to run everything,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;web: lein ring server
data: lein oneoff stubs/data.clj 3000
auth: lein oneoff stubs/auth.clj 3002
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally run the whole thing via &lt;code&gt;lein cooper&lt;/code&gt;. &lt;/p&gt;&lt;p&gt;The standalone stubs will be executed alongside your own service without the overhead of having to manage separate projects or integrate test code into your own project.&lt;/p&gt;</content></entry><entry><title>Invasion of Privacy</title><updated>Mon Aug 25 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/08/25/invasion-of-privacy/"></link><id>urn:yobriefca-se:feed:post:Invasion of Privacy</id><content type="html">&lt;p&gt;There is a pattern I see more and more often in Java projects that use Spring as an IoC container thingymajig. I doubt it's limited to Spring and probably common among other IoC containers but I don't really get a lot of exposure to others because,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;When it's my own project I generally don't need one, and,&lt;/li&gt;
  &lt;li&gt;When it's a project I'm involved in most people tend toward Spring (bar that one time Guice was preferred).&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;So don't consider this a rant against Spring and more around a common pattern that seems to be creeping in to many codebases.&lt;/p&gt;&lt;p&gt;But yes, the problem, check this out.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyBestService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

	&lt;span class="nd"&gt;@Inject&lt;/span&gt;
	&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ADependency&lt;/span&gt; &lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

	&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doAThing&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
		&lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doStuff&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
		&lt;span class="c1"&gt;// more things&lt;/span&gt;
	&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I see this in high ranking Google search results and documentation as an accepted standard approach. Problem is this introduces two very terrible problems.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;It hides dependency knowledge&lt;/li&gt;
  &lt;li&gt;It &lt;strong&gt;poisons&lt;/strong&gt; your domain with technical design considerations&lt;/li&gt;
  &lt;li&gt;It bleeds across the codebase.&lt;/li&gt;
&lt;/ol&gt;&lt;h2&gt;Hiding Knowledge&lt;/h2&gt;&lt;p&gt;This is easily the worst problem IMHO. Without peering into this the code for this class there is simply no way to know what this class depends on. Even then there is little way to know if the dependency is &lt;strong&gt;required&lt;/strong&gt; or &lt;strong&gt;optional&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;The minute you remove explicitness from code you've started down a dangerous path. It becomes impossible to reason about a system without knowing the entire system. The friction this causes grows with time.&lt;/p&gt;&lt;h2&gt;Poisoning your Domain&lt;/h2&gt;&lt;p&gt;If you are injecting private fields into your classes you'll probably need some sort of mechanism to do this. Enter annotations. Commonly you'll see &lt;code&gt;@Inject&lt;/code&gt; from the &lt;code&gt;javax.inject&lt;/code&gt; namespace being used. While &lt;code&gt;@Inject&lt;/code&gt; is more generic than framework specific annotations it is still a &lt;strong&gt;technical consideration&lt;/strong&gt; it isn't something related to your &lt;strong&gt;domain&lt;/strong&gt;. Your classes should be &lt;strong&gt;pure&lt;/strong&gt; in the sense they should be simple and reflect the domain rather than how you built it. The mechanism for IoC is completely irrelevant to your solution.&lt;/p&gt;&lt;p&gt;Worse, now you've made injection a special case, your tests need built in a special way - &lt;code&gt;@InjectMocks&lt;/code&gt; springs to mind (no pun intended).&lt;/p&gt;&lt;h2&gt;Bleeding Across the Codebase&lt;/h2&gt;&lt;p&gt;When you use private field injection you see &lt;code&gt;@Inject&lt;/code&gt; everywhere. Jumping to what is injected is a matter of understanding the dependency chain which is spread across multiple files. There is no centralised pool of knowledge about how your system is wired together and this makes it harder to reason about.&lt;/p&gt;&lt;h2&gt;Remediation&lt;/h2&gt;&lt;p&gt;First and foremost &lt;strong&gt;build your system without a DI framework as a first class citizen&lt;/strong&gt;. Of course use IoC to prevent heavy coupling and potential God classes but do it au naturale. &lt;/p&gt;&lt;p&gt;Secondly use &lt;strong&gt;constructor injection&lt;/strong&gt;. Somehow the people leaning on frameworks forgot about constructor injection. They also forgot about setter injection but I've often found &lt;strong&gt;optional&lt;/strong&gt; dependencies are also a bit of a design smell so I'm not really recommending them. Of course the frameworks themselves already support constructor injection so it won't be a big leap to change your evil ways. &lt;/p&gt;&lt;p&gt;So this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyBestService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

	&lt;span class="nd"&gt;@Inject&lt;/span&gt;
	&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ADependency&lt;/span&gt; &lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

	&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doAThing&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
		&lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doStuff&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
	&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Becomes,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyBestService&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

	&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;ADependency&lt;/span&gt; &lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

	&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MyBestService&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ADependency&lt;/span&gt; &lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;dependency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
	&lt;span class="o"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doAThing&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
		&lt;span class="n"&gt;dependency&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doStuff&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
	&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Constructor injection is beautifully explicit. You can't construct a new instance of a class without explicitly declaring its dependencies - its right there for all the world to see. You could argue that this makes passing many dependencies very noisy. You'd be right. &lt;strong&gt;Bad design should be noisy&lt;/strong&gt; and passing 30 dependencies into a constructor is &lt;strong&gt;bad design&lt;/strong&gt;. This is something &lt;code&gt;@Inject&lt;/code&gt; can easily hide as you add more and more dependencies to a class as there is no requirement for it to be centralised. &lt;/p&gt;&lt;p&gt;This has a knock on effect on your tests too - it makes them &lt;strong&gt;normal&lt;/strong&gt;. You are no longer bound by whatever framework you use to inject mocks and you can explicitly pass stubs without having to understand any particular magic. Or - if you're still so inclined you can still use your framework of choice.&lt;/p&gt;&lt;p&gt;Finally &lt;strong&gt;use a centralised way to bootstrap your application&lt;/strong&gt;. Spring has &lt;code&gt;AppConfig&lt;/code&gt;, Guice has &lt;code&gt;Module&lt;/code&gt; and framework-less stacks use &lt;code&gt;new&lt;/code&gt; in the right place, you do remember &lt;code&gt;new&lt;/code&gt; right? This allows you to push the logic for constructing your dependency graph into a central place that can and should be readable making it much easier to understand the design of your system and refactor easier.&lt;/p&gt;&lt;p&gt;So there you go - use containers if it makes your life easier but try and avoid making your code either magic or noisy.&lt;/p&gt;</content></entry><entry><title>React</title><updated>Wed Jul 30 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="/presentations/react.pdf"></link><id>urn:yobriefca-se:feed:post:React</id><content type="html"></content></entry><entry><title>Nomadic Tendencies</title><updated>Thu Jul 24 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/07/24/nomadic-tendencies/"></link><id>urn:yobriefca-se:feed:post:Nomadic Tendencies</id><content type="html">&lt;blockquote&gt;&lt;p&gt;"The future came and went in the mildly discouraging way that futures do."&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;Neil Gaiman, Good Omens: The Nice and Accurate Prophecies of Agnes Nutter, Witch&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;&lt;p&gt;If there is one thing that shows the immaturity of the software industry (relative to other industries) it's the ever present need to jump to the next big technology. Tales of "leaving X for Y" dot the path from one trend to the next. "I'm so much more productive in Y over X" is the battle cry of the newly indoctrinated acolytes of Y. Vast swathes of half finished, abandoned open source projects written in X collect dust in their respective repositories sitting alongside new shiny half-finished, soon to be abandoned open source projects written in Y. And the industry marches on.&lt;/p&gt;&lt;p&gt;And yet most software that we use is still awful. It still exhibits the same problems, it still breaks in the most unpredictable ways and it leaves people as angry and frustrated as ever.&lt;/p&gt;&lt;p&gt;But I guess that's to be expected. If we regularly reinvent everything in "the next big thing" when do we ever find time to refine anything? The industry marches on, but the industry never moves. Like a perpetual early beta phase our systems are built up just enough before we tear them down and rewrite them in something new. Even enterprises, who tend to move much slower and are therefore buffered from smaller fluctuations in trend, suffer from the same problems at a different scale.&lt;/p&gt;&lt;p&gt;Ultimately this behavior puts the focus on the technology and how it can make our systems better. Our users are once more second class citizens because what &lt;strong&gt;we&lt;/strong&gt; want is somehow more important (and who needs users anyway when people are throwing millions at pointless smoke and mirror apps).&lt;/p&gt;&lt;p&gt;But the problem is not the technology.&lt;/p&gt;&lt;p&gt;The problem is that we are in love with our tools when we should be in love with our users. We need to talk to our users more and less to our peers. Spend less time on Hacker News debating stuff you only partially understand and try and get a better insight into how you can use your current, very pedestrian, technology stack to genuinely improve the lives of your users.&lt;/p&gt;</content></entry><entry><title>A Case for Luminus</title><updated>Mon Jul 21 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/07/21/a-case-for-luminus/"></link><id>urn:yobriefca-se:feed:post:A Case for Luminus</id><content type="html">&lt;p&gt;First and foremost - frameworks. I view frameworks suspiciously and prefer to &lt;a href="http://yobriefca.se/blog/2014/01/04/building-systems-libraries-and-frameworks/"&gt;build my systems&lt;/a&gt; out of a core set of libraries and allow it to evolve and breathe a little without the artificial constraints of a framework. There is a time and place for frameworks, but that time and place never seems to be here and now were my work is concerned. This may be one of the many reasons I find Clojure so enjoyable.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.luminusweb.net/"&gt;Luminus&lt;/a&gt; is a "Clojure web framework". It says so right on the the tagline of the &lt;a href="http://www.luminusweb.net/"&gt;Luminus homepage&lt;/a&gt;. This single line has meant I have hesitated diving into Luminus since I starting breaking the back of my Clojure dabbling. I wanted to avoid it until I felt it useful or necessary to invest my time in it, a framework, after-all, needs to be understood as a whole unlike its modular library counterparts.&lt;/p&gt;&lt;p&gt;So my first foray into building web apps in Clojure meant building them out from scratch using a foundation of Ring, Compojure, Hiccup - the usual suspects. Data access meant looking at a few possibilities , assessing each one and making a decision. Various authentication libraries needed thorough inspections. This, as you can expect, leads to a tremendous amount of yak-shaving and time spent satisfying curiosity.&lt;/p&gt;&lt;p&gt;So I had a look at &lt;a href="http://www.luminusweb.net/"&gt;Luminus&lt;/a&gt;. I wanted to see what decisions a framework had made and possibly steal some of the better ideas. I used &lt;a href="http://www.luminusweb.net/"&gt;Luminus&lt;/a&gt; on my next project. I didn't steal the good ideas for my own work, I didn't need to - the image I have in my head when someone mentions framework is simply not what &lt;a href="http://www.luminusweb.net/"&gt;Luminus&lt;/a&gt; is. &lt;/p&gt;&lt;p&gt;In its most basic form a framework is a collection of 3rd party and bespoke libraries tied together with some code that abstracts the library specifics into a more cohesive whole. &lt;a href="http://www.luminusweb.net/"&gt;Luminus&lt;/a&gt; doesn't really have this. There is no bespoke modules or libraries, there is no abstraction. &lt;a href="https://clojars.org/search?q=luminus"&gt;Clojars&lt;/a&gt; has no &lt;code&gt;luminus&lt;/code&gt; library. It does have a Leiningen &lt;a href="https://clojars.org/luminus/lein-template"&gt;template&lt;/a&gt; that gives you a decent starting point but even that isn't even a heavily opinionated starting point and can be &lt;a href="http://www.luminusweb.net/docs/profiles.md"&gt;customised&lt;/a&gt; to suit your own needs.&lt;/p&gt;&lt;p&gt;So what is Luminus? IMHO Luminus is what you'd end up with if you wanted to personally build a curated list of recommended libraries to build web applications in Clojure. It's what would happen if you bothered to document that process and offer alternatives. In fact while the curated set of libraries makes for a productive development process with reduced decision anxiety it's the documentation that launches it out of the park. Rather than "Do it like this", the documentation offers recommendations and guides for using alternative libraries such as &lt;a href="http://www.luminusweb.net/docs/migrations.md"&gt;migrations&lt;/a&gt;, data access, &lt;a href="http://www.luminusweb.net/docs/html_templating.md"&gt;HTML templating&lt;/a&gt; and &lt;a href="http://www.luminusweb.net/docs/security.md"&gt;authentication&lt;/a&gt;. &lt;a href="http://www.luminusweb.net/"&gt;Luminus&lt;/a&gt; gives you enough to get started and generously gives you a foot up to the next level. &lt;/p&gt;&lt;p&gt;At no point in time does &lt;a href="http://www.luminusweb.net/"&gt;Luminus&lt;/a&gt; hide anything from you, it wears its lineage as a badge of honour and won't get offended if you decide it's wrong in places. &lt;/p&gt;&lt;p&gt;I can respect that.&lt;/p&gt;</content></entry><entry><title>Working with Play apps in the Console</title><updated>Fri Jul 11 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/07/11/working-with-play-apps-in-the-console/"></link><id>urn:yobriefca-se:feed:post:Working with Play apps in the Console</id><content type="html">&lt;p&gt;Having spent a fair amount of time working with Clojure in the last 6 months I've become rather accustomed to the REPL driven development approach. If you're unfamiliar with this approach I recommend reading/viewing Jay Fields &lt;a href="http://blog.jayfields.com/2014/01/repl-driven-development.html"&gt;post on the topic&lt;/a&gt;. Long story short RDD involves writing a small bits of code and executing them immediately. This gives you immediate feedback without having to write tests or start an entire system each time. You can of course make your REPL based experimentation more concrete by saving your work off as a set of tests once your code has settled down a bit.&lt;/p&gt;&lt;p&gt;Like Clojure, and unlike Java, Scala supports the concept of REPL driven development and I've been using it more often since my exposure to its charms in Clojure. Of course Scala syntax is a lot meatier (and I do mean a lot) than Clojure syntax so there is a bit more friction but every now and again its faster when you just want to poke around a concept or idea. &lt;/p&gt;&lt;p&gt;A common example when working with &lt;a href="http://playframework.com"&gt;Play&lt;/a&gt; is querying the application database with Anorm. The one problem here is that to use Anorm and the Play provided extensions you need to have a running Play instance that it can extract configuration values from. &lt;/p&gt;&lt;p&gt;For example if we tried to get all the users from a database we could write something like this via the console (&lt;code&gt;sbt console&lt;/code&gt; or &lt;code&gt;activator console&lt;/code&gt; in the root of our project)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;play.api.db._&lt;/span&gt;
&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;anorm._&lt;/span&gt;
&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withConnection&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SQL&lt;/span&gt;&lt;span class="s"&gt;"SELECT * FROM users"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mapUser&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But this would result in an error.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RuntimeException&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;There&lt;/span&gt; &lt;span class="kt"&gt;is&lt;/span&gt; &lt;span class="kt"&gt;no&lt;/span&gt; &lt;span class="kt"&gt;started&lt;/span&gt; &lt;span class="kt"&gt;application&lt;/span&gt;
  &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;package$&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;package&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="err"&gt;27&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Play$$anonfun$current$1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="err"&gt;71&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Play$$anonfun$current$1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="err"&gt;71&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getOrElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="err"&gt;120&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Play&lt;/span&gt;&lt;span class="n"&gt;$&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="err"&gt;71&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="mi"&gt;43&lt;/span&gt; &lt;span class="n"&gt;elided&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Bummer. Thankfully Play provides a quick way to stand up an running instance which can be used in the console&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StaticApplication&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"."&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;default&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="n"&gt;connected&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;h2:file:./data/db&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Prod&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;res18&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;play.core.StaticApplication&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StaticApplication&lt;/span&gt;&lt;span class="k"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="n"&gt;a730f2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now if we try our query again&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withConnection&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SQL&lt;/span&gt;&lt;span class="s"&gt;"SELECT * FROM users"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mapUser&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;res19&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;scala.collection.immutable.Map&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;,&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;kouphax&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;JHGW$FSWF$KJJK$3231&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To be super clean we can even stop the running instance&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;scala&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;StaticApplication&lt;/code&gt; isn't perfect. For instance it will start your application in production mode but for my needs this has been acceptable. If you do want to provide greater control over how your application is started then look no further than the &lt;code&gt;StaticApplication&lt;/code&gt; source.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StaticApplication&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;applicationPath&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;File&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ApplicationProvider&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DefaultApplication&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;applicationPath&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getClassLoader&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Mode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Prod&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="nc"&gt;Play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;applicationPath&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Using this knowledge you could create your own &lt;code&gt;application&lt;/code&gt; instance using &lt;code&gt;DefaultApplication&lt;/code&gt; and ask Play to start it using &lt;code&gt;Play.start(application)&lt;/code&gt;. &lt;/p&gt;</content></entry><entry><title>Play for (Java|Scala)</title><updated>Wed Jul 09 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="/presentations/play-for-java-scala.pdf"></link><id>urn:yobriefca-se:feed:post:Play for (Java|Scala)</id><content type="html"></content></entry><entry><title>Estimates as Deadlines and the End of Everything</title><updated>Thu Jun 19 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/06/19/estimates-as-deadlines-and-the-end-of-everything/"></link><id>urn:yobriefca-se:feed:post:Estimates as Deadlines and the End of Everything</id><content type="html">&lt;p&gt;"If you guess how many USB sticks are in the jar you win an iPad" they said with a smile that had been seared into their face from years of standing at conference popup stands selling the same message.&lt;/p&gt;&lt;p&gt;I glance over at the sacred prize. For randomly picking a number out of thin air I could easily walk away with a brand spanking new...&lt;/p&gt;&lt;p&gt;"Wait", I say noticing something is odd about the prize. "Is that, an original iPad?".&lt;/p&gt;&lt;p&gt;"Well its not a knock off it that's what you mean"&lt;/p&gt;&lt;p&gt;"No no I mean is it an original version, an iPad one?"&lt;/p&gt;&lt;p&gt;"Oh, yep it is"&lt;/p&gt;&lt;p&gt;"And that's what you win?"&lt;/p&gt;&lt;p&gt;"Of course"&lt;/p&gt;&lt;p&gt;"Why are you giving away an ancient iPad?"&lt;/p&gt;&lt;p&gt;"Well no one has ever actually won so we haven't felt the need to replace it"&lt;/p&gt;&lt;p&gt;"Why has no one ever won?"&lt;/p&gt;&lt;p&gt;"Well no one has ever guessed the exact amount of 1GB USB sticks in this jar"&lt;/p&gt;&lt;p&gt;"So you need to be exact, not just be 'the closest'?"&lt;/p&gt;&lt;p&gt;"Of course, now listen buddy, are you just going to question me about the prize or are you going to listen while I talk to you about our latest release of Super Pro Manager 4.0 - Agile Enterprise Edition."&lt;/p&gt;&lt;p&gt;I walk off, its not even worth the effort. I'd either need a the historical data of guesses or a chance to take each stick out and count them individually. Even then the prize at the end of it is completely out of line with what I'd expected in the first place - a &lt;strong&gt;new&lt;/strong&gt; iPad.&lt;/p&gt;&lt;h2&gt;The Cold Hard Truth&lt;/h2&gt;&lt;p&gt;The story above sounds incredibly silly yet we as an industry still do this on a daily basis.&lt;/p&gt;&lt;p&gt;I'm still hearing, to this day, stories of people treating estimates as targets. I'm hearing them from the enterprise companies that will never change and I'm hearing them from the companies that grace our stages at conferences and talk about how they are "enabling agile teams to deliver value by running lean with a shared responsibility".&lt;/p&gt;&lt;p&gt;The reality is many teams are forced into this fixed template of Scrum with its overhead of extra ceremony and expected to deliver more without giving up the practices of old. It's crunch time every sprint for the team. Its unsustainable. We see the senior members "rise up" and become "cross team" conduits for best practice and status updates. They have successfully extracted themselves from the daily grind and frustration of the sprint.&lt;/p&gt;&lt;p&gt;It may sound like just another one of those developer frustrations that managers tell us "same as it ever was, back in my day..." ad nauseam - but it's not. When we combine it with the faux-agile, process over people, approach most organisations actually have then we have a serious problem. Seriouser, in fact. The teams that were already shipping software in a healthy manner continue to do that, the ones who changed over the last few years in response to careful agile marketering and brand engagement (through certification and top dollar coaching) have just heaped more pain onto their process.&lt;/p&gt;&lt;p&gt;The iterative and incremental application of big design upfront methodology practices will completely and utterly dehumanise a team. People begin to tell themselves that the flat team, the shared vision, one goal stuff was nonsense anyway, it pays the bills for the houses and flats they never really get to see because of the "Unending Project Death March" being exacted one overladen, overprocess-ised, overtime driven sprint at a time. The team don't get their 3 months head space of building solutions against slipping timelines followed by a few weeks of intense crunching, no, they get a constant slightly less intense crunching &lt;strong&gt;all the time&lt;/strong&gt;, non-stop, forever and ever and ever and ever and ever.&lt;/p&gt;&lt;p&gt;Don't believe me? Trust me this is so widespread, so pandemic, that one musical collective even wrote a song about it - &lt;a href="https://www.youtube.com/watch?v=-aLjup934Rk"&gt;Godspeed You! Black Emperor - The Dead Flag Blues&lt;/a&gt;. Of course they had to give it a fictional political lean to avoid getting into trouble at work but at the risk of getting them found out allow me to break their brutally honest piece down a bit.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The car is on fire, and there's no driver at the wheel&lt;/p&gt;&lt;p&gt;And the sewers are all muddied with a thousand lonely suicides&lt;/p&gt;&lt;p&gt;And a dark wind blows&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;This is every IT project. Ever.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The government is corrupt&lt;/p&gt;&lt;p&gt;And we're on so many drugs&lt;/p&gt;&lt;p&gt;With the radio on and the curtains drawn&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;The "government", yeah right, more like a cheeky nod to managment and sales. The drugs are Scrum and Agile and we are listening to the things these people say without looking outside our walls following our estimates and hoping it will all be different this time.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;We're trapped in the belly of this horrible machine&lt;/p&gt;&lt;p&gt;And the machine is bleeding to death&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Public and private money spent on failing systems across every industry and we are all just cogs.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The sun has fallen down&lt;/p&gt;&lt;p&gt;And the billboards are all leering&lt;/p&gt;&lt;p&gt;And the flags are all dead at the top of their poles&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;The flags are the principles we once felt we stood for - making a difference, changing the world, all dead or dying.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;It went like this:&lt;/p&gt;&lt;p&gt;The buildings tumbled in on themselves&lt;/p&gt;&lt;p&gt;Mothers clutching babies&lt;/p&gt;&lt;p&gt;Picked through the rubble&lt;/p&gt;&lt;p&gt;And pulled out their hair&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Buildings clearly represents the promises and contracts based on estimates. The fallout of these being wrong are clear.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The skyline was beautiful on fire&lt;/p&gt;&lt;p&gt;All twisted metal stretching upwards&lt;/p&gt;&lt;p&gt;Everything washed in a thin orange haze&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;At some point you start to accept that this is as good as it gets. You're immune to the mess, in love with the decimation you and your contemporaries have wrought. But its all still broken.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I said, "Kiss me, you're beautiful -&lt;/p&gt;&lt;p&gt;These are truly the last days"&lt;/p&gt;&lt;p&gt;You grabbed my hand&lt;/p&gt;&lt;p&gt;And we fell into it&lt;/p&gt;&lt;p&gt;Like a daydream&lt;/p&gt;&lt;p&gt;Or a fever&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;You give up, you go with it, you accept that broken is the only thing this can ever be. You allow yourself to believe this, you forget everything you ever cared about.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;We woke up one morning and fell a little further down&lt;/p&gt;&lt;p&gt;For sure it's the valley of death&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;You adopt a new technology, a new platform, you let it consume you, you raise your rates.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I open up my wallet&lt;/p&gt;&lt;p&gt;And it's full of blood&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Consultancy.&lt;/p&gt;&lt;h3&gt;The End of Everything&lt;/h3&gt;&lt;p&gt;It may have seemed like a small thing to enforce estimates as targets but you've just ruined the world a bit more. Your teams write crappy broken software because they hate what they do. They hate what we do because they are forced to work against arbitrary timescales like battery chickens - no light, hemmed in pushing out sub-standard cheap bits of code as often as possible. Quantity over quality, no time for quality, no way no how.&lt;/p&gt;&lt;p&gt;This is the end of everything.&lt;/p&gt;</content></entry><entry><title>Publish and Subscribe with core.async's pub and sub</title><updated>Wed Jun 04 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/06/04/publish-and-subscribe-with-core-dot-asyncs-pub-and-sub/"></link><id>urn:yobriefca-se:feed:post:Publish and Subscribe with core.async's pub and sub</id><content type="html">&lt;p&gt;Following on from my &lt;a href="/blog/2014/06/01/combining-and-controlling-channels-with-core-dot-asyncs-merge-and-mix/"&gt;previous article&lt;/a&gt; that covered &lt;code&gt;core.async&lt;/code&gt;'s &lt;code&gt;merge&lt;/code&gt; and &lt;code&gt;mix&lt;/code&gt; this article will delve into another suite of complementary &lt;strong&gt;high level&lt;/strong&gt; methods - namely the channel driven publish/subscribe model that &lt;code&gt;core.async&lt;/code&gt; can support.&lt;/p&gt;&lt;p&gt;While the pub/sub mechanism provided by core.async is higher level than working directly with &lt;code&gt;chan&lt;/code&gt;s and &lt;code&gt;mult&lt;/code&gt;s to achieve the same thing (in fact that is how the pub/sub operations are implemented internally) it should still be considered a set of lower level primitives compared to say a specific event bus notification with topic namespacing, wildcard broadcasting and other such specialised features.&lt;/p&gt;&lt;p&gt;I've attempted to visualise how the various parts of a pub/sub setup would interact. We'll use this as a starting point and expand on how this all falls together through the course of the article.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/pubsub.png" alt="Pub Sub" _="_" /&gt;&lt;/p&gt;&lt;h2&gt;Setup&lt;/h2&gt;&lt;p&gt;Once again we'll use ClojureScript to demonstrate the use of pub/sub. I'll refer you the &lt;strong&gt;Setting Up&lt;/strong&gt; section of my &lt;a href="/blog/2014/06/01/combining-and-controlling-channels-with-core-dot-asyncs-merge-and-mix/"&gt;previous article&lt;/a&gt; if you want to get a basic ClojureScript setup running with &lt;code&gt;core.async&lt;/code&gt;&lt;/p&gt;&lt;h2&gt;Publishing&lt;/h2&gt;&lt;p&gt;Channels are &lt;code&gt;core.async&lt;/code&gt;s foundation and this is no different when you move up a level of abstraction to a pub/sub model. Looking at the publishing side first there are 2 main types of actors or components involved,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The publisher channel - Putting stuff on this channel will cause the publishing mechanism to kick in&lt;/li&gt;
  &lt;li&gt;The publication - calling &lt;code&gt;pub&lt;/code&gt; on the publisher (we'll see this in a minute) produces a &lt;code&gt;publication&lt;/code&gt; that subscribers can &lt;code&gt;sub&lt;/code&gt;/subscribe to.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;This may appear a bit more jarring than a typical approach where you just have a monolithic pub/sub object (e.g. a bus) that you publish and subscribe to directly. However I found the idea a bit easier to understand when I thought of the publication as a realised &lt;strong&gt;topic&lt;/strong&gt; or set of &lt;strong&gt;topics&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;If you think of a simple message bus implementation (in whatever language floats your boat - I've went with Swift, nah just kidding its JavaScript) you would traditionally do something like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="c1"&gt;// grab/create a bus - typically global.&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;bus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Bus&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;bus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"account:created"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="c1"&gt;// do something with the knowledge that&lt;/span&gt;
  &lt;span class="c1"&gt;// an account has been created&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// at some point we publish to the same topic&lt;/span&gt;
&lt;span class="nx"&gt;bus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"acoount:created"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"petedaleet17"&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The topic in this example is &lt;code&gt;account:created&lt;/code&gt; however in reality its just a string, there is no real confidence that you are achieving what you expect. A spelling error will result in rather unpredictable results and as we have introduced a high degree of decoupling it may well be difficult to track down. The observant will notice there is in fact a spelling error in the code.&lt;/p&gt;&lt;p&gt;In &lt;code&gt;core.async&lt;/code&gt; the notion of a &lt;code&gt;publication&lt;/code&gt; is a realised entity you perform actions on and pass around. It's the transport mechanism, that is the "concept". This inversion is interesting. 99.9% of the time I'm ultimately interested in publishing or subscribing to a topic, I really don't care about the mechanics of that operation and this &lt;strong&gt;topic-first&lt;/strong&gt; view better supports that thought model.&lt;/p&gt;&lt;p&gt;Now just to confuse things a bit more, a publication also defines a way to derive the topic of a published value so its entirely possible for a publication to handle subscriptions for any number of topics. Another analogy that may help, or just make things worse, is that a &lt;code&gt;publication&lt;/code&gt; could be considered a &lt;code&gt;router&lt;/code&gt; that defines a set of rules for where to publish data.&lt;/p&gt;&lt;p&gt;To expand on this lets look at some code.&lt;/p&gt;&lt;p&gt;First we need to import a few things into out ClojureScript file (probably &lt;code&gt;core.cljs&lt;/code&gt;)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;chat.core&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;cljs.core.async&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;chan&lt;/span&gt; &lt;span class="nv"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;timeout&lt;/span&gt; &lt;span class="nv"&gt;pub&lt;/span&gt; &lt;span class="nv"&gt;sub&lt;/span&gt; &lt;span class="nv"&gt;unsub&lt;/span&gt; &lt;span class="nv"&gt;unsub-all&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require-macros&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;cljs.core.async.macros&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;go&lt;/span&gt;&lt;span class="p"&gt;]]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now lets introduce our publisher and our publication,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="c1"&gt;; publisher is just a normal channel&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;; publication is a thing we subscribe to&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;publication&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pub&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can see the &lt;code&gt;publisher&lt;/code&gt; is simply a channel, no fancy annotations or extra functions being applied. It's just a vanilla channel that you can put stuff and take stuff off if you so desired.&lt;/p&gt;&lt;p&gt;The &lt;code&gt;publication&lt;/code&gt; ont he other hand is composed by calling &lt;code&gt;pub&lt;/code&gt; on &lt;code&gt;publisher&lt;/code&gt; passing a function. When you put stuff onto &lt;code&gt;publisher&lt;/code&gt; this function is used to derive the topic of the data put onto the channel. It will get passed the put data and is expected to return &lt;strong&gt;something&lt;/strong&gt;, &lt;strong&gt;anything&lt;/strong&gt; that represents the topic.&lt;/p&gt;&lt;p&gt;In this case I am making an assumption that our data will be a map with a &lt;code&gt;:topic&lt;/code&gt; entry. Of course if &lt;code&gt;(:topic data)&lt;/code&gt; returns &lt;code&gt;nil&lt;/code&gt; that will be our topic. Any topics that have no subscribers will be disregarded so &lt;code&gt;nil&lt;/code&gt; will typically be an acceptable &lt;strong&gt;dead letter office&lt;/strong&gt; so to speak.&lt;/p&gt;&lt;h2&gt;Subscribing&lt;/h2&gt;&lt;p&gt;So now we have something we can push data onto - the &lt;code&gt;publisher&lt;/code&gt; channel, and something that will accept subscriptions - the &lt;code&gt;publication&lt;/code&gt;. Next we need to subscribe and start putting stuff on that our subscribers can consume. We could start putting stuff on the channel now but no one will be around to receive it just yet.&lt;/p&gt;&lt;p&gt;The yin to &lt;code&gt;pub&lt;/code&gt;s yang is &lt;code&gt;sub&lt;/code&gt;. &lt;code&gt;sub&lt;/code&gt; always takes a &lt;strong&gt;publication&lt;/strong&gt;, a &lt;strong&gt;topic&lt;/strong&gt; and a &lt;strong&gt;channel&lt;/strong&gt; (you can also specify how and when the subscribing channel will be closed). As I've already mentioned you'll notice that the subscriber channels don't subscribe directly to the publisher channel. This would allow you to create many publications from a single source channel.&lt;/p&gt;&lt;p&gt;Taking our previous example we could subscribe to a number of topics like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="c1"&gt;; define a bunch of subscribers&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;subscriber-one&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;subscriber-two&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;subscriber-three&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;; subscribe&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt; &lt;span class="nv"&gt;publication&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt; &lt;span class="nv"&gt;subscriber-one&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt; &lt;span class="nv"&gt;publication&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt; &lt;span class="nv"&gt;subscriber-two&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt; &lt;span class="nv"&gt;publication&lt;/span&gt; &lt;span class="ss"&gt;:user-logged-in&lt;/span&gt;  &lt;span class="nv"&gt;subscriber-two&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt; &lt;span class="nv"&gt;publication&lt;/span&gt; &lt;span class="ss"&gt;:change-page&lt;/span&gt;     &lt;span class="nv"&gt;subscriber-three&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We now have 3 channels&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;subscriber-one&lt;/code&gt; is subscribed to the &lt;code&gt;:account-created&lt;/code&gt; topic&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;subscriber-two&lt;/code&gt; is subscribed to both the &lt;code&gt;:account-created&lt;/code&gt; topic and the &lt;code&gt;:user-logged-in&lt;/code&gt; topic&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;subscriber-three&lt;/code&gt; is subscribed to the &lt;code&gt;:change-page&lt;/code&gt; topic&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Now when messages are put on to the &lt;code&gt;publisher&lt;/code&gt; the &lt;code&gt;publication&lt;/code&gt; will inspect the data and determine where to route the message. To make this more visual we can listen to and print the values from these channels by trying to take from them in a &lt;code&gt;go-loop&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;take-and-print&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;channel&lt;/span&gt; &lt;span class="nv"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go-loop&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;prefix&lt;/span&gt; &lt;span class="s"&gt;": "&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;take-and-print&lt;/span&gt; &lt;span class="nv"&gt;subscriber-one&lt;/span&gt; &lt;span class="s"&gt;"subscriber-one"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;take-and-print&lt;/span&gt; &lt;span class="nv"&gt;subscriber-two&lt;/span&gt; &lt;span class="s"&gt;"subscriber-two"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;take-and-print&lt;/span&gt; &lt;span class="nv"&gt;subscriber-three&lt;/span&gt; &lt;span class="s"&gt;"subscriber-three"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If we start putting messages on the publisher we should see some console output.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:change-page&lt;/span&gt; &lt;span class="ss"&gt;:dest&lt;/span&gt; &lt;span class="s"&gt;"/#home"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;; subscriber-three: { :topic :change-page :dest "/#home" }&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;; subscriber-one: { :topic :account-created :username "billy" }&lt;/span&gt;
&lt;span class="c1"&gt;; subscriber-two: { :topic :account-created :username "billy" }&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:user-logged-in&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;; subscriber-two: { :topic :user-logged-in :username "billy" }&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:user-logged-out&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;; No subscribers so nothing to see&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Of course subscribing to something is entirely additive. You need some way to take subscriptions away. At this point &lt;code&gt;unsub&lt;/code&gt; and &lt;code&gt;unsub-all&lt;/code&gt; are what you need.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="c1"&gt;; unsubscribe subscriber-two from account-created&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;unsub&lt;/span&gt; &lt;span class="nv"&gt;publication&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt; &lt;span class="nv"&gt;subscriber-two&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;;test&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;; subscriber-one: { :topic :account-created :username "billy" }&lt;/span&gt;

&lt;span class="c1"&gt;; unsubscribe every subscriber from the :account-created topic&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;unsub-all&lt;/span&gt; &lt;span class="nv"&gt;publication&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;;test&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;; nada&lt;/span&gt;

&lt;span class="c1"&gt;; finally unsubscribe every channel from every topic&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;unsub-all&lt;/span&gt; &lt;span class="nv"&gt;publication&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;;test&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:change-page&lt;/span&gt; &lt;span class="ss"&gt;:dest&lt;/span&gt; &lt;span class="s"&gt;"/#home"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:account-created&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:user-logged-in&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;publisher&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:topic&lt;/span&gt; &lt;span class="ss"&gt;:user-logged-out&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="s"&gt;"billy"&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="c1"&gt;; wonderful silence&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Summing Up&lt;/h2&gt;&lt;p&gt;So that covers the &lt;code&gt;pub&lt;/code&gt;/&lt;code&gt;sub&lt;/code&gt; model you get with &lt;code&gt;core.async&lt;/code&gt; which gives a slightly higher abstraction from having to deal with the internal logic of managing a &lt;code&gt;mult&lt;/code&gt; to perform this operation while still remaining suitably low level to allow you to build upon it for your own needs.&lt;/p&gt;&lt;p&gt;Remember that because everything still boils down to channels, which you have access to, there is huge potential to combine various abstractions to construct simple pipelines for all your needs.&lt;/p&gt;&lt;p&gt;One day I'll finish my channel based &lt;a href="https://en.wikipedia.org/wiki/Rube_Goldberg_machine"&gt;Rube Goldberg machine&lt;/a&gt; put a message at one end and watch as it gets piped through mults, publications, merged channels, alts and anything else I can think of finally ending up in a &lt;code&gt;console.log&lt;/code&gt;...&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;"Hi James".&lt;/p&gt;
&lt;/blockquote&gt;</content></entry><entry><title>Combining &amp; Controlling Channels with core.async's merge and mix</title><updated>Sun Jun 01 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/06/01/combining-and-controlling-channels-with-core-dot-asyncs-merge-and-mix/"></link><id>urn:yobriefca-se:feed:post:Combining &amp; Controlling Channels with core.async's merge and mix</id><content type="html">&lt;p&gt;Most discussions around &lt;a href="https://github.com/clojure/core.async"&gt;&lt;code&gt;core.async&lt;/code&gt;&lt;/a&gt;, be it in Clojure or ClojureScript, tend to focus around the key concepts of the library - specifically &lt;code&gt;chan&lt;/code&gt;s and the &lt;code&gt;go&lt;/code&gt;/&lt;code&gt;go-loop&lt;/code&gt; macros. This isn't a bad thing as that is were the power of the library comes from, on the other hand &lt;a href="https://github.com/clojure/core.async"&gt;&lt;code&gt;core.async&lt;/code&gt;&lt;/a&gt; also has a few powerful higher-level features that let you do some very interesting things and they deserve a bit of love as well.&lt;/p&gt;&lt;p&gt;Two such features are &lt;a href="http://clojure.github.io/core.async/#clojure.core.async/merge"&gt;&lt;code&gt;merge&lt;/code&gt;&lt;/a&gt; and &lt;a href="http://clojure.github.io/core.async/#clojure.core.async/mix"&gt;&lt;code&gt;mix&lt;/code&gt;&lt;/a&gt;. Both methods have a similar goal - combining multiple input channels into a single output channel however in practise they are rather different.&lt;/p&gt;&lt;p&gt;At a high level you could draw the 2 operations like so,&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/mix-merge.png" alt="High level view of merge and mix" _="_" /&gt;&lt;/p&gt;&lt;p&gt;In fact the &lt;code&gt;mix&lt;/code&gt; diagram is slightly more complicated in reality but we can expand on that as we go.&lt;/p&gt;&lt;h2&gt;Setting up&lt;/h2&gt;&lt;p&gt;So lets look at some examples of these operations. I'm talking in the context of ClojureScript in this case but same reules and principles apply in plain Clojure.&lt;/p&gt;&lt;p&gt;If you want to try these examples and are rather new to ClojureScript I recently outlined a &lt;a href="http://yobriefca.se/blog/2014/05/30/basic-clojurescript-setup/"&gt;basic ClojureScript setup&lt;/a&gt; which will get you up and running.&lt;/p&gt;&lt;p&gt;Assuming you have an empty ClojureScript project one way or another you need to add a reference to &lt;code&gt;core.async&lt;/code&gt; in the &lt;code&gt;:dependencies&lt;/code&gt; section of the projects &lt;code&gt;project.clj&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;  &lt;span class="ss"&gt;:dependencies&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;org.clojure/clojurescript&lt;/span&gt; &lt;span class="s"&gt;"0.0-2173"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;org.clojure/core.async&lt;/span&gt; &lt;span class="s"&gt;"0.1.303.0-886421-alpha"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then in your ClojureScript source (probably &lt;code&gt;src/&amp;lt;project_name&amp;gt;/core.cljs&lt;/code&gt;) you need to import a few things. I'll assume you have at least a basic understanding of channels and &lt;code&gt;core.async&lt;/code&gt; already so we can just import everything we need for the examples.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;chat.core&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;cljs.core.async&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;mix&lt;/span&gt; &lt;span class="nv"&gt;admix&lt;/span&gt; &lt;span class="nv"&gt;toggle&lt;/span&gt; &lt;span class="nb"&gt;merge &lt;/span&gt;&lt;span class="nv"&gt;chan&lt;/span&gt; &lt;span class="nv"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require-macros&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;cljs.core.async.macros&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;go&lt;/span&gt;&lt;span class="p"&gt;]]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we are about ready for the examples.&lt;/p&gt;&lt;h2&gt;&lt;a href="http://clojure.github.io/core.async/#clojure.core.async/merge"&gt;&lt;code&gt;merge&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;&lt;code&gt;merge&lt;/code&gt; is the simpler of the two features and as the API documentation says combines 1..N source channels and returns a channel which contains all values taken from them. The operation is entirely immutable. That is, once you use &lt;code&gt;merge&lt;/code&gt; to create a channel you can't add or remove channels later. When all input channels have closed the merged channel will also close.&lt;/p&gt;&lt;p&gt;This is useful when you have multiple event streams, represented as channels, and you want to process them in the same way and in a centralised manner e.g. when you are wanting to parse multiple simultaneous server requests, web socket events or user interactions from various parts of the user interface.&lt;/p&gt;&lt;p&gt;To keep the example simple we will just create 3 channels that randomly publish their names every now and then,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="c1"&gt;; declare the channels&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;in-channel-one&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;in-channel-two&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;in-channel-three&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;; define the function for publishing&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;randomly-constantly&lt;/span&gt;
  &lt;span class="s"&gt;"Constantly publishes the given value to the given channel in random&lt;/span&gt;
&lt;span class="s"&gt;   intervals every 0-5 seconds."&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;channel&lt;/span&gt; &lt;span class="nv"&gt;publish-value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;loop &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;timeout&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;* &lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;rand-int &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;channel&lt;/span&gt; &lt;span class="nv"&gt;publish-value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;

&lt;span class="c1"&gt;; start putting stuff on the channels&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;randomly-constantly&lt;/span&gt; &lt;span class="nv"&gt;in-channel-one&lt;/span&gt;   &lt;span class="s"&gt;"channel-one"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;randomly-constantly&lt;/span&gt; &lt;span class="nv"&gt;in-channel-two&lt;/span&gt;   &lt;span class="s"&gt;"channel-two"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;randomly-constantly&lt;/span&gt; &lt;span class="nv"&gt;in-channel-three&lt;/span&gt; &lt;span class="s"&gt;"channel-three"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So now we have 3 channels that will randomly have their name pushed onto them we now need to do something with them. For the sake of simplicity lets assume all we need to do is log the result. We could write 3 distinct &lt;code&gt;go&lt;/code&gt; loops (or suitably abstract it into a reusable function),&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;loop &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;in-channel-one&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;loop &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;in-channel-two&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;loop &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;in-channel-three&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But regardles of how much you abstract away the mechanics you are still dealing with the 3 channels as 3 distinct entities when in many cases you should be dealing with a single channel derived from multiple sources. We achieve this with &lt;code&gt;merge&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;merged&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;merge &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;in-channel-one&lt;/span&gt;
                    &lt;span class="nv"&gt;in-channel-two&lt;/span&gt;
                    &lt;span class="nv"&gt;in-channel-three&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;merged&lt;/code&gt; is now a channel that we can &lt;code&gt;take&lt;/code&gt; from and recieve values from all 3 channels. Now we can perform our &lt;code&gt;go&lt;/code&gt; loop over the single channel instead,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;loop &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;merged&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;&lt;a href="http://clojure.github.io/core.async/#clojure.core.async/mix"&gt;&lt;code&gt;mix&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;&lt;code&gt;merge&lt;/code&gt; is fine when you want to just grab a bunch of channels and treat them as one but sometimes this is not enough. When it comes to channels that produce effects visible to the user there is often a need to better control these messages. Imagine a chat application where each person is represented as a channel, or perhaps a log dashboard where each channel is a service in your system streaming log data.&lt;/p&gt;&lt;p&gt;In such situations, where the volume is high, there may be times you want to focus on a particular set of logs or chat messages, or surpress someone or something that is being particularly chatty. Maybe these messages can be discarded, maybe they are important and need to looked at later. These are the things that &lt;code&gt;merge&lt;/code&gt; fails to address. These are the things that &lt;code&gt;mix&lt;/code&gt; &lt;strong&gt;does&lt;/strong&gt; address.&lt;/p&gt;&lt;p&gt;The key differences that set &lt;code&gt;mix&lt;/code&gt; apart from &lt;code&gt;merge&lt;/code&gt; are that,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;It introduces an intermediary component - the mixer&lt;/li&gt;
  &lt;li&gt;It is configurable, you can add and remove input channels&lt;/li&gt;
  &lt;li&gt;Channels can be muted, paused and solo'ed on demand&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So lets take our 3 channels above and apply the abilities of &lt;code&gt;mix&lt;/code&gt; to the situation.&lt;/p&gt;&lt;p&gt;First of all we need to create 2 things.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The output channel - unlike &lt;code&gt;merge&lt;/code&gt; this isn't created for us&lt;/li&gt;
  &lt;li&gt;The mixer - we create this via the &lt;code&gt;mix&lt;/code&gt; method&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="c1"&gt;; manually declare our output channel&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;output-channel&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="c1"&gt;; create a mixer linked to the output channel&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;mixer&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;mix&lt;/span&gt; &lt;span class="nv"&gt;output-channel&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can also, at this point, set up our &lt;code&gt;go&lt;/code&gt; loop for printing the data put onto the output channel&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;loop &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!&lt;/span&gt; &lt;span class="nv"&gt;output-channel&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;recur&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Unlike &lt;code&gt;merge&lt;/code&gt; we still haven't declared what input channels should be associated with the mixer and ultimately output channel. We can do this using &lt;a href="http://clojure.github.io/core.async/#clojure.core.async/admix"&gt;&lt;code&gt;admix&lt;/code&gt;&lt;/a&gt;,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;admix&lt;/span&gt; &lt;span class="nv"&gt;mixer&lt;/span&gt; &lt;span class="nv"&gt;in-channel-one&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;admix&lt;/span&gt; &lt;span class="nv"&gt;mixer&lt;/span&gt; &lt;span class="nv"&gt;in-channel-two&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;admix&lt;/span&gt; &lt;span class="nv"&gt;mixer&lt;/span&gt; &lt;span class="nv"&gt;in-channel-three&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At this point we should start seeing stuff being logged to the console exactly like we did with &lt;code&gt;merge&lt;/code&gt;. This is where &lt;code&gt;mix&lt;/code&gt; starts to get interesting.&lt;/p&gt;&lt;h3&gt;&lt;a href="http://clojure.github.io/core.async/#clojure.core.async/toggle"&gt;&lt;code&gt;toggle&lt;/code&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;toggle&lt;/code&gt; allows you to control how the mixer responds to each input channel. You pass it a state map of channels and associated mixer properties. With &lt;code&gt;toggle&lt;/code&gt; you can do any combination (though many would not make sense) of,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;:mute&lt;/code&gt; - keep taking from the input channel but discard any taken values&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;:pause&lt;/code&gt; - stop taking from the input channel&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;:solo&lt;/code&gt; - listen &lt;strong&gt;only&lt;/strong&gt; to this (and other &lt;code&gt;:solo&lt;/code&gt;ed channels). Whether or not the non-soloed channels are muted or paused can be controlled via the &lt;a href="http://clojure.github.io/core.async/#clojure.core.async/solo-mode"&gt;&lt;code&gt;solo-mode&lt;/code&gt;&lt;/a&gt; method.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So lets imagine one of our mixed channels (&lt;code&gt;in-channel-one&lt;/code&gt;) it getting a bit chatty. It could swamp our logging output and we might miss something important in another channel. We can use toggle to temporarily mute it,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toggle&lt;/span&gt; &lt;span class="nv"&gt;mixer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="nv"&gt;in-channel-one&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:mute&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now our output will only be displaying the other 2 channels. But suppose the data coming in from channel one was actually important, as it stands muting a channel simply discards any takes that happen. If we want to stop taking anything from the channel (and therefore allow it to buffer on the channel) we can pause the channel instead.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toggle&lt;/span&gt; &lt;span class="nv"&gt;mixer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;in-channel-one&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:mute&lt;/span&gt;  &lt;span class="nv"&gt;false&lt;/span&gt;
                                 &lt;span class="ss"&gt;:pause&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally if we want to &lt;strong&gt;only&lt;/strong&gt; concern ourselves with channel one we can &lt;code&gt;solo&lt;/code&gt; it&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toggle&lt;/span&gt; &lt;span class="nv"&gt;mixer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;in-channel-one&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:solo&lt;/span&gt;  &lt;span class="nv"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You'll notice I didn't set &lt;code&gt;:pause&lt;/code&gt; back to &lt;code&gt;false&lt;/code&gt; because soloed channels ignore their other properties..&lt;/p&gt;&lt;h2&gt;Summing Up&lt;/h2&gt;&lt;p&gt;We covered both the &lt;code&gt;merge&lt;/code&gt; and &lt;code&gt;mix&lt;/code&gt; methods of &lt;code&gt;core.async&lt;/code&gt;. Both methods are higher level ways to combine and control multiple input channels into a single unified output channel.&lt;/p&gt;&lt;p&gt;&lt;code&gt;merge&lt;/code&gt; offers a simple straigthforward way to combine channels but offers you little control after the fact. &lt;code&gt;mix&lt;/code&gt; gives you greater control over the input channels and is exceptionally useful when you need to manage input streams.&lt;/p&gt;</content></entry><entry><title>Basic ClojureScript Setup</title><updated>Fri May 30 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/05/30/basic-clojurescript-setup/"></link><id>urn:yobriefca-se:feed:post:Basic ClojureScript Setup</id><content type="html">&lt;p&gt;ClojureScript is a bit of an awkward one to get into. If Clojure isn't your natural language then you may well find the clojarian setup and the long compilation times a bit of a frustration. Even if you're accustomed to the REPL based development approach that fits so well with Clojure there is still a certain amount of friction to be had with ClojureScript.&lt;/p&gt;&lt;p&gt;Recently a strong desire to play with and better understand &lt;a href="https://github.com/clojure/core.async"&gt;&lt;code&gt;core.async&lt;/code&gt;&lt;/a&gt; in ClojureScript I decided to try and put together a lightweight workflow. In doing so I ended up producing a Clojure library (&lt;a href="http://yobriefca.se/primrose/"&gt;primrose&lt;/a&gt;) and a Leiningen plugin (&lt;a href="http://yobriefca.se/lein-cooper/"&gt;lein-cooper&lt;/a&gt;) to better support this workflow.&lt;/p&gt;&lt;p&gt;It's probably a sure sign that you are doing something wrong when you feel you need to create a bunch of developer focused tools to support a workflow for an already established ecosystem but you gotta do, what you gotta do.&lt;/p&gt;&lt;p&gt;Here goes.&lt;/p&gt;&lt;h2&gt;Starting point&lt;/h2&gt;&lt;p&gt;ClojureScript projects are still Clojure projects and so there are few bits and pieces that you need to wire up a basic empty project. You've got your &lt;code&gt;project.clj&lt;/code&gt;, the &lt;a href="https://github.com/emezeske/lein-cljsbuild"&gt;&lt;code&gt;lein-cljsbuild&lt;/code&gt;&lt;/a&gt; plugin for compiling ClojureScript to JavaScript and (at the very least) an &lt;code&gt;index.html&lt;/code&gt; for bringing everything together.&lt;/p&gt;&lt;p&gt;Thankfully David Nolen has you covered. &lt;a href="https://github.com/swannodette/mies"&gt;Mies&lt;/a&gt; is a very basic ClojureScript project template so getting up and running is a matter of,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;lein new mies &amp;lt;project name&amp;gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; &amp;lt;project name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At this point you are good to go. To compile the ClojureScript you can run,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;lein cljsbuild once
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you open &lt;code&gt;index.html&lt;/code&gt; and view the developer console you should see some predictable output. Alternatively if you run,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;lein cljsbuild auto
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The plugin will watch your project sources and recompile everything when changes are made. This is significantly faster that manually compiling every time as compilation is incremental. So instead of 10s of seconds for each compile you'll be getting sub-second compilations.&lt;/p&gt;&lt;h2&gt;Ajax&lt;/h2&gt;&lt;p&gt;When you view HTML files via &lt;code&gt;file://&lt;/code&gt; you won't be able to, at least by default, make ajax calls (due to security concerns such as &lt;a href="https://en.wikipedia.org/wiki/Same-origin_policy"&gt;Same-origin policy&lt;/a&gt;).&lt;/p&gt;&lt;p&gt;To fix this you'll want to serve your &lt;code&gt;index.html&lt;/code&gt; from a web server. You could go with &lt;code&gt;python -m simplehttpserver&lt;/code&gt; as many often do but if you want to keep everything under the same roof (Clojure/Leiningen) then I'd recommend &lt;a href="https://github.com/tailrecursion/lein-simpleton"&gt;&lt;code&gt;lein-simpleton&lt;/code&gt;&lt;/a&gt; - a Leiningen plugin for serving static files from the current directory. If you add &lt;code&gt;lein-simpleton&lt;/code&gt; to the projects &lt;code&gt;project.clj&lt;/code&gt; file,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="ss"&gt;:plugins&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;lein-cljsbuild&lt;/span&gt; &lt;span class="s"&gt;"1.0.2"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
          &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;lein-simpleton&lt;/span&gt; &lt;span class="s"&gt;"1.3.0"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can now run &lt;code&gt;lein simpleton &amp;lt;port&amp;gt;&lt;/code&gt; to start serving static files (your &lt;code&gt;index.html&lt;/code&gt;). Alternatively if you have actual server side code in the same project you can replace &lt;code&gt;lein-simpleton&lt;/code&gt; with &lt;a href="https://github.com/weavejester/lein-ring"&gt;&lt;code&gt;lein-ring&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Two for One&lt;/h2&gt;&lt;p&gt;So now if you run both&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code&gt;lein cljsbuild auto&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;lein simpleton &amp;lt;port&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;You have a dynamic(ish) environment for working and testing your ClojureScript work.&lt;/p&gt;&lt;p&gt;Problem is I don't like having multiple windows open for long running processes and we now have 2. Ruby has a nice answer for this which plays very well with &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; - &lt;a href="https://github.com/ddollar/foreman"&gt;Foreman&lt;/a&gt;. Foreman takes a file (&lt;code&gt;Procfile&lt;/code&gt;) that contains a list of named command line processes and runs them. These processes are supposed to be long running (like &lt;code&gt;simpleton&lt;/code&gt; and &lt;code&gt;cljsbuild auto&lt;/code&gt;) and Foreman merges the output of these files into a single stream that makes development and debugging a bit easier.&lt;/p&gt;&lt;p&gt;Again if you want to keep this sort of thing all under one roof I've thrown together a simple plugin for ingesting a &lt;code&gt;Procfile&lt;/code&gt; and doing what Foreman does - in Leiningen.&lt;/p&gt;&lt;h2&gt;lein-cooper&lt;/h2&gt;&lt;p&gt;&lt;a href="https://github.com/kouphax/lein-cooper"&gt;&lt;code&gt;lein-cooper&lt;/code&gt;&lt;/a&gt; gives you that Foreman feeling. So lets add this as a plugin.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="ss"&gt;:plugins&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;lein-cljsbuild&lt;/span&gt; &lt;span class="s"&gt;"1.0.2"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
          &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;lein-simpleton&lt;/span&gt; &lt;span class="s"&gt;"1.3.0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
          &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;lein-cooper&lt;/span&gt; &lt;span class="s"&gt;"0.0.1"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we can create a &lt;code&gt;Procfile&lt;/code&gt; at the root of our project to run our two commands.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;web: lein simpleton
cljs: lein cljsbuild auto
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now if we run &lt;code&gt;lein cooper&lt;/code&gt; we will have single call to start up our dynamic(ish) environment for working with ClojureScript. Once you are done just &lt;code&gt;CTRL-C&lt;/code&gt; the terminal and everything will shutdown cleanly.&lt;/p&gt;&lt;h2&gt;Summing Up&lt;/h2&gt;&lt;p&gt;So we now have a very basic project setup that will allow us to tinker with ClojureScript, make Ajax calls, edit code and cleanly automate away all the stopping and restarting and problematic debugging. It's not quite the REPL driven style of a typical Clojure project but it's minimal enough to try a few smaller scale things.&lt;/p&gt;</content></entry><entry><title>lein-cooper - Foreman style process runner for Leiningen</title><updated>Fri May 30 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/lein-cooper/"></link><id>urn:yobriefca-se:feed:post:lein-cooper - Foreman style process runner for Leiningen</id><content type="html"></content></entry><entry><title>primrose - Experimental promise/future utility functions</title><updated>Wed May 28 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/primrose/"></link><id>urn:yobriefca-se:feed:post:primrose - Experimental promise/future utility functions</id><content type="html"></content></entry><entry><title>Devops: A Case Study</title><updated>Wed May 28 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="/presentations/devops-a-case-study.pdf"></link><id>urn:yobriefca-se:feed:post:Devops: A Case Study</id><content type="html"></content></entry><entry><title>The Weird and Wonderful Characters of Clojure</title><updated>Mon May 19 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/05/19/the-weird-and-wonderful-characters-of-clojure/"></link><id>urn:yobriefca-se:feed:post:The Weird and Wonderful Characters of Clojure</id><content type="html">&lt;style&gt;
  h2 code {background-color: transparent;}
&lt;/style&gt;
&lt;blockquote class="warning" style="text-align:left"&gt;This ancient article has been superseded by a &lt;a href="https://clojure.org/guides/weird_characters"&gt;much better and up to date article on the Clojure website&lt;/a&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;p&gt;A reference collection of characters used in Clojure that are difficult to "google". Descriptions sourced from various blogs, &lt;a href="http://stackoverflow.com/questions/tagged/clojure"&gt;StackOverflow&lt;/a&gt;, &lt;a href="http://en.wikibooks.org/wiki/Learning_Clojure"&gt;Learning Clojure&lt;/a&gt; and the &lt;a href="http://clojure.org/documentation"&gt;official Clojure docs&lt;/a&gt; - sources attributed where necessary. Type the symbols into the box below to search (or use &lt;code&gt;CTRL-F&lt;/code&gt;). Sections not in any particular order but related items are grouped for ease. If I'm wrong or missing anything worthy of inclusion tweet me &lt;a href="http://twitter.com/kouphax"&gt;@kouphax&lt;/a&gt; or mail me at &lt;a href="mailto:james@yobriefca.se"&gt;james@yobriefca.se&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;&lt;h2&gt;&lt;code&gt;#&lt;/code&gt; - Dispatch macro&lt;/h2&gt;&lt;p&gt;You'll see this macro character beside another e.g. &lt;code&gt;#(&lt;/code&gt; or &lt;code&gt;#"&lt;/code&gt;. This topic will act as a bit preamble before looking at your specific case.&lt;/p&gt;&lt;p&gt;&lt;code&gt;#&lt;/code&gt; is the dispatch macro, a reader macro that tells the Clojure reader (the thing that takes a file of Clojure text and parses it for consumption in the compiler) to go and look at another &lt;strong&gt;read table&lt;/strong&gt; for the definition of the next character - in essence this allows extending default reader behaviour.&lt;/p&gt;&lt;p&gt;Clojure doesn't provide support for creating reader macros but it is possible through &lt;a href="http://briancarper.net/blog/449/"&gt;a bit of hackery&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;If you see &lt;code&gt;#&lt;/code&gt; &lt;strong&gt;at the end&lt;/strong&gt; of a symbol then this is used to automatically generate a new symbol. This is useful inside macros to keep macro specifics from leaking into the userspace. A regular &lt;code&gt;let&lt;/code&gt; will fail in a macro definition&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defmacro &lt;/span&gt;&lt;span class="nv"&gt;m&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/m&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;CompilerException&lt;/span&gt; &lt;span class="nv"&gt;java.lang.RuntimeException&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;Can&lt;/span&gt;&lt;span class="ss"&gt;'t&lt;/span&gt; &lt;span class="k"&gt;let &lt;/span&gt;&lt;span class="nv"&gt;qualified&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;user/x&lt;/span&gt;, &lt;span class="nv"&gt;compiling&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;NO_SOURCE_PATH&lt;/span&gt;&lt;span class="ss"&gt;:1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Instead you need to append &lt;code&gt;#&lt;/code&gt; to the end of the variable name and let Clojure generate a unique symbol for it.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defmacro &lt;/span&gt;&lt;span class="nv"&gt;m&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;x#&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nv"&gt;x#&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/m&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If we expand this macro we can see the &lt;code&gt;gensym&lt;/code&gt;'d name&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;macroexpand &lt;/span&gt;&lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;m&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;let*&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;x__681__auto__&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nv"&gt;x__681__auto__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Another place you'll see the &lt;code&gt;#&lt;/code&gt; is in &lt;a href="http://clojure.org/reader#The%20Reader--Tagged%20Literals"&gt;tagged literals&lt;/a&gt;. Most commonly you'll see this use in &lt;a href="https://github.com/edn-format/edn"&gt;EDN&lt;/a&gt; (extensible data notation - a rich data format that can be used in Clojure) and in ClojureScript (&lt;code&gt;#js&lt;/code&gt;). Search for &lt;code&gt;#inst&lt;/code&gt;, &lt;code&gt;#uuid&lt;/code&gt; or &lt;code&gt;#js&lt;/code&gt; for some more info.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/reader"&gt;Clojure Documentation - Reader&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://briancarper.net/blog/449/"&gt;Clojure Reader Macros&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://clojuredocs.org/clojure_core/clojure.core/gensym"&gt;ClojureDocs - gensym&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;#{&lt;/code&gt; - Set macro&lt;/h2&gt;&lt;p&gt;See the dispatch (&lt;code&gt;#&lt;/code&gt;) macro for additional details.&lt;/p&gt;&lt;p&gt;&lt;code&gt;#{&lt;/code&gt; defines a set (a collection of unique values) specifically a &lt;code&gt;hash-set&lt;/code&gt;. The following are equivalent,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;hash-set &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Attempting to create a &lt;code&gt;set&lt;/code&gt; using this literal form will throw if there are duplicates. Instead the &lt;code&gt;hash-set&lt;/code&gt; function should be used on a vector&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;IllegalArgumentException&lt;/span&gt; &lt;span class="nv"&gt;Duplicate&lt;/span&gt; &lt;span class="nv"&gt;key&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="nv"&gt;clojure.lang.PersistentHashSet.createWithCheck&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;PersistentHashSet.java&lt;/span&gt;&lt;span class="ss"&gt;:68&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;set &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;; convert vector to set, removing duplicates&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/data_structures#Data%20Structures-Sets"&gt;Clojure Documentation: Sets&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;#_&lt;/code&gt; - Discard macro&lt;/h2&gt;&lt;p&gt;See the dispatch (&lt;code&gt;#&lt;/code&gt;) macro for additional details.&lt;/p&gt;&lt;p&gt;&lt;code&gt;#_&lt;/code&gt; tells the reader to ignore the next form completely.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;_&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The docs suggest that "The form following &lt;code&gt;#_&lt;/code&gt; is completely skipped by the reader. (This is a more complete removal than the comment macro which yields nil).". This can prove useful for debugging situations or for multline comments. I've never used it.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/reader"&gt;Clojure Documentation - Reader&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;#"&lt;/code&gt; - Regular Expression macro&lt;/h2&gt;&lt;p&gt;See the dispatch (&lt;code&gt;#&lt;/code&gt;) macro for additional details.&lt;/p&gt;&lt;p&gt;&lt;code&gt;#"&lt;/code&gt; indicates the start of a regular expression pattern.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;re-matches &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="s"&gt;"^test$"&lt;/span&gt; &lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="s"&gt;"test"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This form is compiled at &lt;em&gt;read time&lt;/em&gt; into a &lt;code&gt;java.util.regex.Pattern&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/other_functions#Other%20Useful%20Functions%20and%20Macros-Regex%20Support"&gt;Clojure Documentation: Regex Support&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;#(&lt;/code&gt; - Function macro&lt;/h2&gt;&lt;p&gt;See the dispatch (&lt;code&gt;#&lt;/code&gt;) macro for additional details.&lt;/p&gt;&lt;p&gt;&lt;code&gt;#(&lt;/code&gt; begins the short hand syntax for an inline function definition. The following 2 bits of code are similar,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="c1"&gt;; anonymous function takin a single argument and printing it&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;line&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;line&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;; anonymous function takin a single argument and printing it - shorthand&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The macro expands the shorthand syntax into a function definition whose arity (the number of arguments it takes) is defined by how the &lt;code&gt;%&lt;/code&gt; placeholders are declared. See the &lt;code&gt;%&lt;/code&gt; character for discussion around arity.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;macroexpand &lt;/span&gt;&lt;span class="o"&gt;`#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fn*&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.core/println&lt;/span&gt; &lt;span class="nv"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; argument names shortened for clarity&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;#'&lt;/code&gt; - Var macro&lt;/h2&gt;&lt;p&gt;&lt;code&gt;#'&lt;/code&gt; is the var quote. It is the same a the &lt;code&gt;var&lt;/code&gt; method,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;nine&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/nine&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;nine&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;var &lt;/span&gt;&lt;span class="nv"&gt;nine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/nine&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'nine&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/nine&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When used it will attempt to return the referenced var. This is useful when you want to talk about the reference/declaration instead of the value it represents. See the use of &lt;code&gt;meta&lt;/code&gt; in the metadata (&lt;code&gt;^&lt;/code&gt;) discussion.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/special_forms#var"&gt;Clojure Official Documentation: Special Forms&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;#inst&lt;/code&gt;, &lt;code&gt;#uuid&lt;/code&gt; &amp;amp; &lt;code&gt;#js&lt;/code&gt; etc. - tagged literals&lt;/h2&gt;&lt;p&gt;Commonly found in EDN and ClojureScript this use of &lt;code&gt;#&lt;/code&gt; is called the tagged literal. Look at this example,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;java.util.Date.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;inst&lt;/span&gt; &lt;span class="s"&gt;"2014-05-19T19:12:37.925-00:00"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When we create a new date it is represented as a tagged literal, or in this case a tagged string. We can use Clojures &lt;code&gt;read-string&lt;/code&gt; to read this back (or use it directly)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;inst&lt;/span&gt; &lt;span class="s"&gt;"2014-05-19T19:12:37.925-00:00"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;java.util.Date&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;read-string&lt;/span&gt; &lt;span class="s"&gt;"#inst \"2014-05-19T19:12:37.925-00:00\""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;inst&lt;/span&gt; &lt;span class="s"&gt;"2014-05-19T19:12:37.925-00:00"&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;read-string&lt;/span&gt; &lt;span class="s"&gt;"#inst \"2014-05-19T19:12:37.925-00:00\""&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nv"&gt;java.util.Date&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A tagged literal tells the reader how to parse the literal value. Other common uses include &lt;code&gt;#uuid&lt;/code&gt; for generating UUIDs and in the ClojureScript world an extremely common use of tagged literals is &lt;code&gt;#js&lt;/code&gt; which can be used to convert ClojureScript data structures into JavaScript structures directly.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://github.com/edn-format/edn#tagged-elements"&gt;EDN Tagged Elements&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;%&lt;/code&gt; - Argument placeholder&lt;/h2&gt;&lt;p&gt;&lt;code&gt;%&lt;/code&gt; is not a macro but a placeholder for use in the &lt;code&gt;#(&lt;/code&gt; macro. It represents an argument that will be passed into the function when it is expanded.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;macroexpand &lt;/span&gt;&lt;span class="o"&gt;`#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fn*&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.core/println&lt;/span&gt; &lt;span class="nv"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; takes a single arg, uses it once&lt;/span&gt;

&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;macroexpand &lt;/span&gt;&lt;span class="o"&gt;`#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fn*&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.core/println&lt;/span&gt; &lt;span class="nv"&gt;arg&lt;/span&gt; &lt;span class="nv"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; takes a single arg, uses it twice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Numbers can be placed directly after the &lt;code&gt;%&lt;/code&gt; to indicate the arguments position. Numbers are also used by the &lt;code&gt;#(&lt;/code&gt; macro to determine the number of arguments to pass in.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;macroexpand &lt;/span&gt;&lt;span class="o"&gt;`#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;%1&lt;/span&gt; &lt;span class="nv"&gt;%2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fn*&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;arg1&lt;/span&gt; &lt;span class="nv"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.core/println&lt;/span&gt; &lt;span class="nv"&gt;arg1&lt;/span&gt; &lt;span class="nv"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; takes 2 args&lt;/span&gt;

&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;macroexpand &lt;/span&gt;&lt;span class="o"&gt;`#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;%4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fn*&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;arg1&lt;/span&gt; &lt;span class="nv"&gt;arg2&lt;/span&gt; &lt;span class="nv"&gt;arg3&lt;/span&gt; &lt;span class="nv"&gt;arg4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.core/println&lt;/span&gt; &lt;span class="nv"&gt;arg4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; takes 4 args doesn't use 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So you don't have to use the arguments but you do need to declare them in the order you'd expect an external caller to pass them in.&lt;/p&gt;&lt;p&gt;&lt;code&gt;%&lt;/code&gt; and &lt;code&gt;%1&lt;/code&gt; can be used interchangably,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;macroexpand &lt;/span&gt;&lt;span class="o"&gt;`#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt; &lt;span class="nv"&gt;%1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; use both % and %1&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;fn*&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;clojure.core/println&lt;/span&gt; &lt;span class="nv"&gt;arg1&lt;/span&gt; &lt;span class="nv"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; still only takes 1 argument&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;@&lt;/code&gt; - Deref macro&lt;/h2&gt;&lt;p&gt;&lt;code&gt;@&lt;/code&gt; is the deref macro, it is the shorthand equivalent of the &lt;code&gt;deref&lt;/code&gt; function so these 2 forms are the same,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atom&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/x&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;deref &lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;@&lt;/code&gt; is used to get the current value of a reference. The above example uses &lt;code&gt;@&lt;/code&gt; to get the current value of an &lt;a href="http://clojure.org/atoms"&gt;atom&lt;/a&gt; but &lt;code&gt;@&lt;/code&gt; can be applied to other things such as &lt;code&gt;future&lt;/code&gt;s, &lt;code&gt;delay&lt;/code&gt;s, &lt;code&gt;promise&lt;/code&gt;s etc. to force computation and potentially block.&lt;/p&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;^&lt;/code&gt; - Metadata&lt;/h2&gt;&lt;p&gt;&lt;code&gt;^&lt;/code&gt; is the metadata marker. Metadata is a map of values (with shorthand option) that can be attached to various forms in Clojure. This provides extra information for these forms and can be used for documentation, compilation warnings, typehints and other features.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:debug&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; meta map with single boolean value&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/five&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can access the metadata by the &lt;code&gt;meta&lt;/code&gt; method which should be executed against the declaration itself (rather than the returned value).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:debug&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/five&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;meta &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'five&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ns&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;&amp;lt;Namespace&lt;/span&gt; &lt;span class="nv"&gt;user&amp;gt;&lt;/span&gt;, &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt;, &lt;span class="ss"&gt;:column&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:debug&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt;, &lt;span class="ss"&gt;:line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:file&lt;/span&gt; &lt;span class="s"&gt;"NO_SOURCE_PATH"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As we have a single value here we can use a shorthand notation for declaring the metadata &lt;code&gt;^:name&lt;/code&gt; which is useful for flags as the value will be set to true.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="ss"&gt;:debug&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/five&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;meta &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'five&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ns&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;&amp;lt;Namespace&lt;/span&gt; &lt;span class="nv"&gt;user&amp;gt;&lt;/span&gt;, &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt;, &lt;span class="ss"&gt;:column&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:debug&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt;, &lt;span class="ss"&gt;:line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:file&lt;/span&gt; &lt;span class="s"&gt;"NO_SOURCE_PATH"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Another use of &lt;code&gt;^&lt;/code&gt; is for type hints. These are used to tell the compiler what type the value will be and allow it to perform type specific optimisations thus potentially making resultant code a bit faster.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="nv"&gt;Integer&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/five&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;meta &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'five&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ns&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;&amp;lt;Namespace&lt;/span&gt; &lt;span class="nv"&gt;user&amp;gt;&lt;/span&gt;, &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt;, &lt;span class="ss"&gt;:column&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:file&lt;/span&gt; &lt;span class="s"&gt;"NO_SOURCE_PATH"&lt;/span&gt;, &lt;span class="ss"&gt;:tag&lt;/span&gt; &lt;span class="nv"&gt;java.lang.Integer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can see in that example the &lt;code&gt;:tag&lt;/code&gt; property is set.&lt;/p&gt;&lt;p&gt;You can also stack the shorthand notations,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="nv"&gt;Integer&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="ss"&gt;:debug&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="ss"&gt;:private&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/five&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;meta &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'five&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:ns&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;&amp;lt;Namespace&lt;/span&gt; &lt;span class="nv"&gt;user&amp;gt;&lt;/span&gt;, &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt;, &lt;span class="ss"&gt;:column&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:private&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt;, &lt;span class="ss"&gt;:debug&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt;, &lt;span class="ss"&gt;:line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;, &lt;span class="ss"&gt;:file&lt;/span&gt; &lt;span class="s"&gt;"NO_SOURCE_PATH"&lt;/span&gt;, &lt;span class="ss"&gt;:tag&lt;/span&gt; &lt;span class="nv"&gt;java.lang.Integer&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/metadata"&gt;Clojure Official Documentation: Metadata&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://en.wikibooks.org/wiki/Learning_Clojure/Meta_Data"&gt;Learning Clojure: Meta Data&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;'&lt;/code&gt; - Quote macro&lt;/h2&gt;&lt;p&gt;Can be used against symbols as part of a dispatch macro (see &lt;code&gt;#'&lt;/code&gt;). Also used to quote forms and prevent their evaluation as with the &lt;code&gt;quote&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; fails as it tries to evaluate 1 as a function&lt;/span&gt;

&lt;span class="nv"&gt;ClassCastException&lt;/span&gt; &lt;span class="nv"&gt;java.lang.Long&lt;/span&gt; &lt;span class="nv"&gt;cannot&lt;/span&gt; &lt;span class="nv"&gt;be&lt;/span&gt; &lt;span class="nb"&gt;cast &lt;/span&gt;&lt;span class="nv"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;clojure.lang.IFn&lt;/span&gt;  &lt;span class="nv"&gt;user/eval925&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;NO_SOURCE_FILE&lt;/span&gt;&lt;span class="ss"&gt;:1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; quote&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;quote &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; using the longer quote method&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/special_forms#quote"&gt;Clojure Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;;&lt;/code&gt; - Comment&lt;/h2&gt;&lt;p&gt;&lt;code&gt;;&lt;/code&gt; is a comment. In fact its a comment &lt;strong&gt;macro&lt;/strong&gt; that takes all input from its starting point to the end of the line and ensures the reader ignore it.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt; &lt;span class="s"&gt;"x"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; this is a comment&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/x&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;; this is a comment too&lt;/span&gt;
&lt;span class="nv"&gt;&amp;lt;returns&lt;/span&gt; &lt;span class="nv"&gt;nothing&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;:&lt;/code&gt; - Keyword&lt;/h2&gt;&lt;p&gt;&lt;code&gt;:&lt;/code&gt; is the indicator for a Keyword which is an interned string that provides fast comparison and lower memory overhead.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt; &lt;span class="ss"&gt;:test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;clojure.lang.Keyword&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Alternativley you can use &lt;code&gt;keyword&lt;/code&gt; to create a keyword from a string&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;keyword &lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="ss"&gt;:test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A neat thing about keywords is they also implement &lt;code&gt;IFn&lt;/code&gt; and can act as functions for extracting values from maps which is very nice.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;my-map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:one&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="ss"&gt;:two&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/my-map&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:one&lt;/span&gt; &lt;span class="nv"&gt;my-map&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; get the value for :one by invoking it as function&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:three&lt;/span&gt; &lt;span class="nv"&gt;my-map&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; it can safely access non-keys&lt;/span&gt;
&lt;span class="nv"&gt;nil&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:three&lt;/span&gt; &lt;span class="nv"&gt;my-map&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; it can return a default if specified&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/data_structures#Data%20Structures-Keywords"&gt;Clojure Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;::&lt;/code&gt; - Qualified keyword&lt;/h2&gt;&lt;p&gt;&lt;code&gt;::&lt;/code&gt; is used to fully qualify a keyword with the current namespace.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:my-keyword&lt;/span&gt;
&lt;span class="ss"&gt;:my-keyword&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;::my-keyword&lt;/span&gt;
&lt;span class="ss"&gt;:user/my-keyword&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;= &lt;/span&gt;&lt;span class="ss"&gt;::my-keyword&lt;/span&gt; &lt;span class="ss"&gt;:my-keyword&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I have found this useful when creating macros. If I want to ensure a macro, that calls another method in the macro namespace, correctly expands to call the method I have used ::my-method to refer to the fully qualified name.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://stackoverflow.com/questions/5771168/what-is-the-used-for-in-clojure"&gt;What is the :: used for in clojure?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;/&lt;/code&gt; - Namespace separator&lt;/h2&gt;&lt;p&gt;Can be the division function &lt;code&gt;/&lt;/code&gt; but can also act as a separator in a symbol name to break apart the symbol name and the namespace it resides in &lt;code&gt;my-namepace/utils&lt;/code&gt;. This allows symbols to be fully qualified to prevent collisions or spread.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/reader"&gt;Cloure Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;$&lt;/code&gt; - Inner class reference&lt;/h2&gt;&lt;p&gt;Used to reference inner classes and interfaces in Java. Seperates the container class name and the inner class name,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;basex.core&lt;/span&gt; &lt;span class="nv"&gt;BaseXClient$EventNotifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn- &lt;/span&gt;&lt;span class="nv"&gt;build-notifier&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;notifier-action&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reify&lt;/span&gt; &lt;span class="nv"&gt;BaseXClient$EventNotifier&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;this&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;notifier-action&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;EventNotifier&lt;/code&gt; is an inner interface of the &lt;code&gt;BaseXClient&lt;/code&gt; class which is an imported Java class.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blog.jayfields.com/2011/01/clojure-using-java-inner-classes.html"&gt;Clojure: Using Java Inner Classes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;-&amp;gt; -&amp;gt;&amp;gt; some-&amp;gt; cond-&amp;gt; as-&amp;gt;&lt;/code&gt; etc. - Threading macros&lt;/h2&gt;&lt;p&gt;These are threading macros. Almost all of them take an initial value and &lt;strong&gt;thread&lt;/strong&gt; this value through a number of forms. Lets imagine (for reasons unknown) we wanted to take a number, find the square root, cast it to an int, then a string then back to an integer again. We could write it like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Integer.&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Math/sqrt&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The threading macro allows us to unravel this deep nesting,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&amp;gt; &lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Math/sqrt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;int str &lt;/span&gt;&lt;span class="nv"&gt;Integer.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or if you prefer multiline and consistent brackettering&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&amp;gt; &lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Math/sqrt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;Integer.&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What the macro does is take the value returned from each expression and push it in as the first argument to the next one.&lt;/p&gt;&lt;p&gt;&lt;code&gt;-&amp;gt;&amp;gt;&lt;/code&gt; is the same but different. Rather than push the last value in as the &lt;strong&gt;first&lt;/strong&gt; argument it passes it in as the &lt;strong&gt;last&lt;/strong&gt; argument.&lt;/p&gt;&lt;p&gt;The "etc." in the title refers to the fact there are a whole host of threading macros that perform variations on the same theme (&lt;code&gt;cond-&amp;gt;&lt;/code&gt;, &lt;code&gt;some-&amp;gt;&lt;/code&gt;, &lt;code&gt;as-&amp;gt;&lt;/code&gt; and their &lt;code&gt;-&amp;gt;&amp;gt;&lt;/code&gt; equivalents). There is also an entire library, &lt;a href="https://github.com/rplevy/swiss-arrows"&gt;swiss-arrows&lt;/a&gt;, dedicated to the threading macros.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://blog.fogus.me/2009/09/04/understanding-the-clojure-macro/"&gt;Understanding the Clojure -&amp;gt; macro&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/clojure/clojure/blob/67571d1844e7b9a0cab6089245d7e5cde208c67e/changes.md"&gt;Clojure Changelog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;~&lt;/code&gt; - Unquote macro&lt;/h2&gt;&lt;p&gt;See &lt;code&gt;`&lt;/code&gt; (syntax quote) for additional information.&lt;/p&gt;&lt;p&gt;&lt;code&gt;~&lt;/code&gt; is unquote. That is within as syntax quoted (&lt;code&gt;`&lt;/code&gt;) block &lt;code&gt;~&lt;/code&gt; will &lt;strong&gt;unquote&lt;/strong&gt; the associated symbol i.e. resolve it in the current context.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; create a named ref representing the number 5&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/five&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="c1"&gt;; five will yeild its internal value&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="c1"&gt;; syntax quoting five will fully resolve the SYMBOL&lt;/span&gt;
&lt;span class="nv"&gt;user/five&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;`~&lt;/span&gt;&lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="c1"&gt;; within a syntax quoted block ~ wil resolve the value in the current context&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This forms the meat and potatoes of creating macros which are, to be highly reductionist, functions that return blocks of syntax with parts evaluated in varying contexts.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.braveclojure.com/writing-macros/"&gt;Clojure for the Brave and True - Writing Macros&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://aphyr.com/posts/305-clojure-from-the-ground-up-macros"&gt;Clojure from the ground up: macros&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/macros"&gt;Clojure Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;~@&lt;/code&gt; - Unquote splicing macro&lt;/h2&gt;&lt;p&gt;See &lt;code&gt;`&lt;/code&gt; (syntax quote) and &lt;code&gt;~&lt;/code&gt; (unquote) for additional information.&lt;/p&gt;&lt;p&gt;&lt;code&gt;~@&lt;/code&gt; is unquote-splicing. Where unquote (&lt;code&gt;~&lt;/code&gt;) deals with single values (or treats its attached item as a single item) &lt;code&gt;~@&lt;/code&gt; works on lists and expands them out into multiple statements. Think JavaScripts &lt;code&gt;.apply&lt;/code&gt; method that takes an array and expands it out as arguments to the applied function.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;three-and-four&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/three-and-four&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="nv"&gt;three-and-four&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; treates as a single statement produces a nested list&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;~@&lt;/span&gt;&lt;span class="nv"&gt;three-and-four&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;; expand out as seperate statements&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Again this gives us a lot of power in macros.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.braveclojure.com/writing-macros/"&gt;Clojure for the Brave and True - Writing Macros&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://aphyr.com/posts/305-clojure-from-the-ground-up-macros"&gt;Clojure from the ground up: macros&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/macros"&gt;Clojure Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;`&lt;/code&gt; - Syntax quote&lt;/h2&gt;&lt;p&gt;See &lt;code&gt;~@&lt;/code&gt; (unquote splicing) and &lt;code&gt;~&lt;/code&gt; (unquote) for additional information.&lt;/p&gt;&lt;p&gt;&lt;code&gt;`&lt;/code&gt; is the syntax quote. When used on a symbol it resolves the symbol in the current context,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;five&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/five&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="nv"&gt;five&lt;/span&gt;
&lt;span class="nv"&gt;user/five&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When used with lists (remember every thing in Clojure is data) it forms a &lt;strong&gt;template&lt;/strong&gt; for the data structure and won't immediately resolve it.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;ClassCastException&lt;/span&gt; &lt;span class="nv"&gt;java.lang.Long&lt;/span&gt; &lt;span class="nv"&gt;cannot&lt;/span&gt; &lt;span class="nv"&gt;be&lt;/span&gt; &lt;span class="nb"&gt;cast &lt;/span&gt;&lt;span class="nv"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;clojure.lang.IFn&lt;/span&gt;  &lt;span class="nv"&gt;user/eval832&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;NO_SOURCE_FILE&lt;/span&gt;&lt;span class="ss"&gt;:1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You'll see this most often in the context of macros. We can write one now,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defmacro &lt;/span&gt;&lt;span class="nv"&gt;debug&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;_=&amp;gt;&lt;/span&gt;   &lt;span class="o"&gt;`&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;val#&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;_=&amp;gt;&lt;/span&gt;      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="s"&gt;"DEBUG: "&lt;/span&gt; &lt;span class="nv"&gt;val#&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="nv"&gt;_=&amp;gt;&lt;/span&gt;      &lt;span class="nv"&gt;val#&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/debug&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;+ &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nv"&gt;DEBUG&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;  &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;Code updated based on recommendations from Leif Foged&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;The macro takes a single statement wraps it in a &lt;strong&gt;quoted&lt;/strong&gt; &lt;code&gt;let&lt;/code&gt; block, evaluates and prints the result and then evaluates the body. In effect this &lt;code&gt;defmacro&lt;/code&gt; call returns a quoted data structure representing the program we are writing with it. The &lt;code&gt;`&lt;/code&gt; allows this to happen.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.braveclojure.com/writing-macros/"&gt;Clojure for the Brave and True - Writing Macros&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://aphyr.com/posts/305-clojure-from-the-ground-up-macros"&gt;Clojure from the ground up: macros&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://clojure.org/macros"&gt;Clojure Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;*var-name*&lt;/code&gt; - Earmuffs&lt;/h2&gt;&lt;p&gt;Earmuffs (a pair of asterisk bookending var names) is a &lt;strong&gt;naming convention&lt;/strong&gt; in many LISPs used to denote &lt;strong&gt;special vars&lt;/strong&gt;. Most commonly in Clojure this seems to be used to denote &lt;strong&gt;dynamic&lt;/strong&gt; vars i.e. ones that can change depending on where you are in the program. The earmuffs act as a warning that "here be dragons" and to never assume the state of the var. Remember this is a &lt;strong&gt;convention&lt;/strong&gt; not a &lt;strong&gt;rule&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Core Clojure examples are &lt;code&gt;*out*&lt;/code&gt; and &lt;code&gt;*in*&lt;/code&gt; which represent the standard in and out Writers for Clojure.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://stackoverflow.com/questions/1986961/how-is-the-var-name-naming-convention-used-in-clojure"&gt;How is the &lt;em&gt;var-name&lt;/em&gt; naming-convention used in clojure?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/*out*"&gt;Clojure API Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;&amp;gt;!!&lt;/code&gt;, &lt;code&gt;&amp;lt;!!&lt;/code&gt;, &lt;code&gt;&amp;gt;!&lt;/code&gt; &amp;amp; &lt;code&gt;&amp;lt;!&lt;/code&gt; - core.async channel macros&lt;/h2&gt;&lt;p&gt;These symbols are channel operations in &lt;code&gt;core.async&lt;/code&gt; - a Clojure/ClojureScript library for channel based asynchronous programming (specifically &lt;a href="http://en.wikipedia.org/wiki/Communicating_sequential_processes"&gt;CSP - Communicating Sequential Processes&lt;/a&gt;).&lt;/p&gt;&lt;p&gt;If you imagine, for the sake of argument, a channel is a bit like a queue that things can put stuff on and take stuff off then these symbols support that simple API.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;&amp;gt;!!&lt;/code&gt; &amp;amp; &lt;code&gt;&amp;lt;!!&lt;/code&gt; are &lt;strong&gt;blocking&lt;/strong&gt; &lt;strong&gt;put&lt;/strong&gt; and &lt;strong&gt;take&lt;/strong&gt; respectively&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;&amp;gt;!&lt;/code&gt; &amp;amp; &lt;code&gt;&amp;lt;!&lt;/code&gt; are, simply, &lt;strong&gt;put&lt;/strong&gt; and &lt;strong&gt;take&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The difference being the blocking versions operate outside &lt;code&gt;go&lt;/code&gt; blocks and block the thread they operate on.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;my-channel&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; create a channel&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!!&lt;/span&gt; &lt;span class="nv"&gt;my-channel&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;; put stuff on the channel&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;lt;!!&lt;/span&gt; &lt;span class="nv"&gt;my-channel&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;; take stuff off the channel&lt;/span&gt;
&lt;span class="nv"&gt;hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The non-blocking versions need to be executed within a &lt;code&gt;go&lt;/code&gt; block otherwise they'll throw an exception&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;c&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chan&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/c&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;c&lt;/span&gt; &lt;span class="s"&gt;"nope"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;AssertionError&lt;/span&gt; &lt;span class="nv"&gt;Assert&lt;/span&gt; &lt;span class="nv"&gt;failed&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;&amp;gt;!&lt;/span&gt; &lt;span class="nv"&gt;used&lt;/span&gt; &lt;span class="nb"&gt;not &lt;/span&gt;&lt;span class="nv"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;go&lt;/span&gt; &lt;span class="nv"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;block&lt;/span&gt;
&lt;span class="nv"&gt;nil&lt;/span&gt;  &lt;span class="nv"&gt;clojure.core.async/&amp;gt;!&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;async.clj&lt;/span&gt;&lt;span class="ss"&gt;:123&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;While the difference between these is well outside the scope of this article fundamentally the &lt;code&gt;go&lt;/code&gt; blocks operate and manage their own resources pausing &lt;strong&gt;execution&lt;/strong&gt; of code without blocking threads. This makes asynchronously executed code appear to be synchronous and removing the pain of managing asynchronous code from the code base.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://github.com/clojure/core.async/blob/master/examples/walkthrough.clj"&gt;core.async Code Walkthrough&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/clojure/core.async/wiki"&gt;core.async Wiki&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;&amp;lt;symbol&amp;gt;?&lt;/code&gt; - Predicate Marker&lt;/h2&gt;&lt;p&gt;Putting &lt;code&gt;?&lt;/code&gt; at the end of a symbol is a &lt;strong&gt;naming convention&lt;/strong&gt; common across many languages that support special characters in their symbol names It is used to indicate the thing is a predicate i.e. that it &lt;strong&gt;poses a question&lt;/strong&gt;. For example imagine using an API that dealt with buffer manipulation&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;my-buffer&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;buffers/create-buffer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;buffers/empty&lt;/span&gt; &lt;span class="nv"&gt;my-buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;At a glance how would you know if the method &lt;code&gt;empty&lt;/code&gt; in this case,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Returned true if the passed in buffer was empty, or,&lt;/li&gt;
  &lt;li&gt;Cleared the buffer&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;While the author could have renamed &lt;code&gt;empty&lt;/code&gt; to &lt;code&gt;is-empty&lt;/code&gt; the richness of symbol naming in Clojure allows us to express intent more symbolically.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;my-buffer&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;buffers/create-buffer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;buffers/empty?&lt;/span&gt; &lt;span class="nv"&gt;my-buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is simply a recommended &lt;strong&gt;convention&lt;/strong&gt; not a &lt;strong&gt;requirement&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://github.com/bbatsov/clojure-style-guide#naming"&gt;Clojure Style Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;&amp;lt;symbol&amp;gt;!&lt;/code&gt; - Unsafe Operations&lt;/h2&gt;&lt;p&gt;The Clojure style guide has this to say&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The names of functions/macros that are not safe in STM transactions should end with an exclamation mark (e.g. &lt;code&gt;reset!&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;You'll most commonly see this appended to function names whose purpose is to mutate state e.g connecting to a data store, updating an atom or closing a file stream.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;my-stateful-thing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atom&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="ss"&gt;'user/my-stateful-thing&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;swap!&lt;/span&gt; &lt;span class="nv"&gt;my-stateful-thing&lt;/span&gt; &lt;span class="nv"&gt;inc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="nv"&gt;user=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nv"&gt;my-stateful-thing&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is simply a recommended &lt;strong&gt;convention&lt;/strong&gt; not a &lt;strong&gt;requirement&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://github.com/bbatsov/clojure-style-guide#naming"&gt;Clojure Style Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;&lt;h2&gt;&lt;code&gt;_&lt;/code&gt; - Irrelevant var&lt;/h2&gt;&lt;p&gt;When you see this used as function arguments or similar it is a common naming convention for vars or arguments you are not interested in using. That is you don't intend to use them so you aren't really interested in thinking of a useful name for them.&lt;/p&gt;&lt;p&gt;This is an example using the &lt;code&gt;add-watch&lt;/code&gt; function that can be used to add callback style behavior when atoms change value. Imagine, given an atom, we want to print the new value everytime it changes,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atom&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;add-watch&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt; &lt;span class="nv"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;new-value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;new-value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reset!&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;; prints 6&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reset!&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;; prints 9&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;add-watch&lt;/code&gt; takes 4 arguments but in our case we only really care about the last argument - the new value of the atom. I don't really want to spend time thinking of names for these arguments I'll never use, nor do I want to generate a long line of text unnecessarily (of course I could have used the shorthand &lt;code&gt;#(println %4)&lt;/code&gt; but that defeats the purpose of this example).&lt;/p&gt;
&lt;hr /&gt;
&lt;script&gt;
(function(){
  var script = document.createElement('script');
  script.src = "https://code.jquery.com/jquery-1.11.3.min.js"
  script.onload = function(){
    // wraps h3 in block
    jQuery("h2").each(function(){
      jQuery(this).nextUntil("h2")
           .andSelf()
           .wrapAll("&lt;div class='block'/&gt;")
    })

    jQuery('blockquote:first').after(jQuery("&lt;input type='search' id='filter' placeholder='Search symbols..'/&gt;").css({
      "font-size"      : "1.5em",
      "width"          : "100%",
      "border"         : "1px solid #e0e0e0",
      "text-indent"    : "0.5em",
      "color"          : "#999",
      "font-family"    : "Open Sans",
      "padding-top"    : "0.2em",
      "padding-bottom" : "0.2em"
    }))

    var all = jQuery('h2').parents(".block")

    var filter = function(e){
      if(this.value === "") {
        all.show();
      } else {
        all.hide()
        jQuery('h2 code:contains(' + this.value + ')').parents(".block").show();
      }
    };

    jQuery('#filter').on("keyup", filter).on("click", filter);
  }
  document.getElementsByTagName('head')[0].appendChild(script);
})();
&lt;/script&gt;
&lt;blockquote&gt;&lt;p&gt;Many thanks to everyone who has contributed ideas and [the copious amounts of] spelling corrections (crikey I'm bad at speelingz - so thanks Michael R. Mayne, lobsang_ludd). I've tried to call out people who have specifically asked for things. Sorry if I've missed you.&lt;/p&gt;
&lt;/blockquote&gt;</content></entry><entry><title>A Brief Overview of BaseX</title><updated>Sat May 17 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/05/17/a-brief-overview-of-basex/"></link><id>urn:yobriefca-se:feed:post:A Brief Overview of BaseX</id><content type="html">&lt;p&gt;There are technologies that I have used to solve problems that just make me happy while using them. They make solving the problem so easy that I don't have to burn time getting them to work or try bending them to my needs.&lt;/p&gt;&lt;p&gt;&lt;a href="http://basex.org"&gt;BaseX&lt;/a&gt;, an &lt;a href="http://en.wikipedia.org/w/index.php?title=XML_database"&gt;XML database&lt;/a&gt;, is one of those. Yes it's an XML database, yes XML is, like, totally lame and all but trust me, it exists and there are contexts it works.&lt;/p&gt;&lt;p&gt;So I wanted to write about BaseX because while it may not be shiny it makes up for it in pure practicality. One day you may need to interrogate XML and maybe BaseX will save your sanity as it did mine. Also XQuery is rather wonderful.&lt;/p&gt;&lt;h2&gt;BaseX&lt;/h2&gt;&lt;p&gt;&lt;a href="http://basex.org"&gt;BaseX&lt;/a&gt; is an XML database written in Java. That may sound like "the worst" but let me tell you why it is rather the opposite.&lt;/p&gt;&lt;p&gt;So yeah it's cool because it's &lt;strong&gt;NoSQL&lt;/strong&gt;. It stores XML documents so it's doubly cool because it's a &lt;strong&gt;document store&lt;/strong&gt;. Not only that but it doesn't care about DTDs and XML schemas so, as long as they are well formed, it can store any XML document. That makes it triply cool because it's &lt;strong&gt;schemaless&lt;/strong&gt;. Heavens, it's basically MongoDB for XML. Well not quite&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; - the design goals are not the same.&lt;/p&gt;&lt;p&gt;So what is BaseX useful for? Well one of the areas that it shines is Open Data. There is a lot of publicly available data locked up in static XML documents.&lt;/p&gt;&lt;p&gt;One example is &lt;a href="http://iatiregistry.org/"&gt;IATI&lt;/a&gt; - the International Aid Transparency Initiative. IATI is many things,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;an organisation that promotes transparency of foreign aid spend,&lt;/li&gt;
  &lt;li&gt;an XML standard for publishing data consistently, and,&lt;/li&gt;
  &lt;li&gt;a registry to access this open data&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Many organisations and governments that spend money on foreign aid publish data via IATI. Data is usually published as many XML files and according to the registry there are 241 organisations publishing 3,343 datasets (XML files). Thats gigabytes of data. Not exactly &lt;strong&gt;big data&lt;/strong&gt; but still a respectable volume of XML.&lt;/p&gt;&lt;p&gt;To top it all off the registry only provides a centralised lookup for datasources, pointers to XML files, via metadata. It &lt;strong&gt;does not&lt;/strong&gt; extract the data from within the XML files. So the data itself is still locked up in static XML files spread across the globe.&lt;/p&gt;&lt;p&gt;None of this is specific to IATI. Like it or not there is a lot of data exactly like this.&lt;/p&gt;&lt;p&gt;Now imagine you want to ask a question of this type of distributed XML data and you've discovered the sweet spot for BaseX. You feed a BaseX database a batch of XML files, it indexes the contents and allows you to query over the top of it as if it was a single root document.&lt;/p&gt;&lt;h2&gt;Querying&lt;/h2&gt;&lt;p&gt;If there is one thing that can be said about XML - it's really well thought out. Yeah yeah there are aspects of the ecosystem that could be described as &lt;strong&gt;over thought&lt;/strong&gt; but as a data format its not half bad. Thanks to this "thoughtout-ed-ness" BaseX doesn't need to invent any new concepts to support querying. XML has already got its own query language, well actually two, XPath and XQuery. Both are mature, well documented, well understood query languages.&lt;/p&gt;&lt;h3&gt;XPath&lt;/h3&gt;&lt;p&gt;XPath is like the CSS selectors of the XML world. A bit more verbose than CSS selector syntax but essentially the same idea. In fact there are some features, such as selecting nodes based on conditions of their children, that CSS selectors could do with. Take this rather contrived example,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="xml"&gt;/repository/projects/project[@title="My Blog"]/releases/release[@type="beta"]/release-date/text()
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This would return the &lt;code&gt;release-date&lt;/code&gt; of all the &lt;code&gt;beta&lt;/code&gt; releases of the project titled &lt;code&gt;My Blog&lt;/code&gt;. I did say contrived didn't I? So XPath is useful for &lt;strong&gt;selecting&lt;/strong&gt; things but not manipulating or transforming anything.&lt;/p&gt;&lt;h3&gt;XQuery&lt;/h3&gt;&lt;p&gt;XQuery is useful when you want to do more than just select nodes in a document. In fact XQuery is kind of an amazing language. Seriously. Its based on a SQL-like expression set called &lt;a href="http://en.wikipedia.org/wiki/FLWOR"&gt;FLWOR&lt;/a&gt; (FOR, LET, WHERE, ORDER BY and RETURN) that when mixed with XPath you have a nice functional language. Everything is a sequence making it conceptually rather LISPy yet the syntax is much like ML.&lt;/p&gt;&lt;p&gt;Here is a slightly more complex, real life authenti-genuine, example featuring list manipulation, dynamic XPath execution, external parameter passing and other typical XQuery stuff,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="xquery"&gt;&lt;span class="k"&gt;declare&lt;/span&gt; &lt;span class="k"&gt;variable&lt;/span&gt; &lt;span class="nv"&gt;$start&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;declare&lt;/span&gt; &lt;span class="k"&gt;variable&lt;/span&gt; &lt;span class="nv"&gt;$limit&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;declare&lt;/span&gt; &lt;span class="k"&gt;variable&lt;/span&gt; &lt;span class="nv"&gt;$reporting-org&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;$filter&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s2"&gt;"$db//iati-activity"&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;$filter&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$reporting-org&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="err"&gt;""&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"[reporting-org/@ref=$reporting-org]"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;$db&lt;/span&gt;         &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nf"&gt;db:open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'iati'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;$bindings&lt;/span&gt;   &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nt"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'$db'&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;$db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="s1"&gt;'$reporting-org'&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nv"&gt;$reporting-org&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;$activities&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nv"&gt;$activity&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;xquery:eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$bindings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="k"&gt;order by&lt;/span&gt; &lt;span class="nv"&gt;$activity&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;iati-identifier&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$activity&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;result&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;  &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;ok&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;true&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/ok&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;  &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;iati-activities&lt;/span&gt; &lt;span class="nt"&gt;generated-datetime&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;"{&lt;/span&gt; &lt;span class="nf"&gt;current-dateTime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;query&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;      &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;total-count&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$activities&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/total-count&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;      &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;start&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$start&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/start&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;      &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;limit&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;$limit&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/limit&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/query&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;subsequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$activities&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$limit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="l"&gt;  &lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/iati-activities&amp;gt;&lt;/span&gt;&lt;span class="l"&gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/result&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Clients&lt;/h3&gt;&lt;p&gt;I've already said BaseX is written, unsuprisingly, in Java but as with most data stores use is not limited to the JVM. BaseX runs as a server that you send commands to over TCP and, as you'd expect, there are many &lt;a href="http://docs.basex.org/wiki/Clients"&gt;clients available&lt;/a&gt; to abstract this lower level communication.&lt;/p&gt;&lt;p&gt;I've used the Java, Scala and Ruby client and they are all pretty much the same. I'm also currently in the process of building a Clojure based one that wraps the standard Java client. Again the API is very similar to the other clients.&lt;/p&gt;&lt;p&gt;BaseX doesn't have an extensive feature set and so the clients aren't exactly complicated.&lt;/p&gt;&lt;p&gt;The clients follow a similar pattern of having two &lt;strong&gt;modes&lt;/strong&gt;. A &lt;strong&gt;standard mode&lt;/strong&gt; that provides functionality for connecting to a server and sending arbitrary commands and a &lt;strong&gt;query mode&lt;/strong&gt; that allows you to create a query on the server, bind external values to the query and stream the results to the client. You can think of queries in &lt;strong&gt;query mode&lt;/strong&gt; as similar to SQLs PreparedStatements.&lt;/p&gt;&lt;p&gt;In reality &lt;strong&gt;query mode&lt;/strong&gt; is just a bit of rich sugar to make the common task of querying a bit less bloaty and there isn't anything in it you can't achieve in &lt;strong&gt;standard mode&lt;/strong&gt; - its all just sending a basic set of commands over a socket. The power lives within the server.&lt;/p&gt;&lt;p&gt;Some clients (Java, Scala, C# and Clojure for example) also support the notion of events. With these clients you can ask sessions to &lt;code&gt;watch&lt;/code&gt; and &lt;code&gt;unwatch&lt;/code&gt; a special database type of &lt;code&gt;event&lt;/code&gt;. The typical signature for &lt;code&gt;watch&lt;/code&gt; looks like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;watch&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"my-event"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;EventNotifier&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nd"&gt;@Override&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;On the database side you need to have created an event called &lt;code&gt;my-event&lt;/code&gt; for this to succeed&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sql"&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="n"&gt;EVENT&lt;/span&gt; &lt;span class="n"&gt;my&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then at some point you can fire an event and pass values to it.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sql"&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;"my-event"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;"1 to 4"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Each event can be passed an XQuery expression that will be evaluated and passed to any registered handlers on the client side. This can be one or more opened session as long as they have &lt;code&gt;watch&lt;/code&gt;ed the named event.&lt;/p&gt;&lt;h3&gt;Modules&lt;/h3&gt;&lt;p&gt;BaseX can be extended via modules too. These can add extra features to the database itself as well as enhance XQuery itself. There are a &lt;a href="http://docs.basex.org/wiki/Module_Library"&gt;heap of ready-rolled&lt;/a&gt; modules for all your needs. Just to call a few out,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://docs.basex.org/wiki/JSON_Module"&gt;JSON Module&lt;/a&gt; - extend XQuery to support reading and writing JSON. Yep JSON in and out of an XML database. Bananas.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://docs.basex.org/wiki/Higher-Order_Functions_Module"&gt;Higher Order Functions Module&lt;/a&gt; - get some monads in your XQuery&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://docs.basex.org/wiki/Full-Text_Module"&gt;Full Text Module&lt;/a&gt; - Every database needs full text search capabilities these days it seems and BaseX need not be an exception.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://docs.basex.org/wiki/Unit_Module"&gt;Unit Testing Module&lt;/a&gt; - Testing is dead but don't let that hold you back.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://docs.basex.org/wiki/Geo_Module"&gt;Geospatial Module&lt;/a&gt; - another must have trend in databases these days.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;There are a heap of them allowing you to customise BaseX to your needs.&lt;/p&gt;&lt;h3&gt;Other Notable Things&lt;/h3&gt;&lt;p&gt;There are a few other things worth mentioning that don't fit into their own category.&lt;/p&gt;&lt;h4&gt;Admin GUI&lt;/h4&gt;&lt;p&gt;One of the things I miss with a lot of the NoSQL databases is a good Admin app. These apps let me poke around the data in my database, model solution and generally visualise all those bits and bytes.&lt;/p&gt;&lt;p&gt;SQL Server got this bit right. SQL Server Management Studio is a really handy tool for understanding your data and its container a bit better.&lt;/p&gt;&lt;p&gt;BaseX has you covered. It has a rather nice client that lets you write XPath/XQuery, manage documents and visualise not only the data but also profiles queries and shows how they can be optimised.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/basex.png" alt="BaseX Admin GUI" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Its written in some Java based desktop technology so the UI itself is a bit basic but given what you can do with it, thats more than forgiven.&lt;/p&gt;&lt;h4&gt;Binary Data&lt;/h4&gt;&lt;p&gt;BaseX lets to read and write binary data as well. Being binary you can't do much more than read and write it but it means that you can, if needs be, store binary blobs alongside your XML.&lt;/p&gt;&lt;h4&gt;Scaling&lt;/h4&gt;&lt;p&gt;At one point I was using BaseX to query across a large amount of XML data. Not a &lt;strong&gt;huge&lt;/strong&gt; amount but large, about 13GB worth. The query I was running, while it wasn't basic wasn't exactly complicated either but it did result in a large result set and the query times where not suitable for real time applications (single queries running 3-6 seconds). In my case that was fine. I suspect there are very few cases where you'd genuinely need real time responses from a large blob of XML but still.&lt;/p&gt;&lt;p&gt;BaseX doesn't provide any sort of answer for distributed environments, you'd need to provide some sort of basic sharding strategy yourself if that is what you are after. Perhaps if someone gets drunk enough they'll write some sort of Hadoop integration for offline querying of massive XML datasets. Perhaps.&lt;/p&gt;&lt;h3&gt;In a Nutshell&lt;/h3&gt;&lt;p&gt;BaseX fulfils a need. Not a need many people will have but a need nonetheless. It does it well and it does it without inventing a new query language or offering 1000 shiny features. It uses XQuery which is, quite honestly, a wonderful language to work with. It's functional, minimal, expressive and powerful. The Admin GUI offers some nice insight into your databases and the clients offered across most common and current languages make working with BaseX straightforward.&lt;/p&gt;&lt;p&gt;While I am on this subject BaseX is of course not the only XML database. You've got others like eXist, MarkLogic and Sedna. I've never ventured into other systems (save for a brief look at eXist) so can't comment on them but in terms of feature set and licensing BaseX &lt;a href="http://en.wikipedia.org/w/index.php?title=XML_database&amp;amp;section=5#Language_features"&gt;looks&lt;/a&gt; to be one of the better implementations.&lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: I'd imagine, though, if a Java Certified Enterprise Architect decided to write a document store from scratch they'd probably end up with an XML database har! har!.&lt;/sup&gt;&lt;/p&gt;&lt;sup&gt;&lt;/sup&gt;</content></entry><entry><title>Clojure Cookbook</title><updated>Thu May 15 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/05/15/clojure-cookbook/"></link><id>urn:yobriefca-se:feed:post:Clojure Cookbook</id><content type="html">&lt;p&gt;&lt;img src="/images/blog/clojure_cookbook.png" alt="Clojure Cookbook" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Broad. If I had to describe the &lt;a href="http://clojure-cookbook.com/"&gt;Clojure Cookbook&lt;/a&gt; in a single word &lt;strong&gt;broad&lt;/strong&gt; would be it. If you're looking for in-depth explanations of all the minutiae and edge cases of a particular topic in Clojure this book is probably not going to fit the bill. If, on the other hand, you're spelunking through the Clojure landscape building apps and generally tinkering with everything you get your hands on then this book is the perfect companion.&lt;/p&gt;&lt;h2&gt;What is it?&lt;/h2&gt;&lt;p&gt;The &lt;a href="http://clojure-cookbook.com/"&gt;Clojure Cookbook&lt;/a&gt;, as the name suggests, is a collection of recipes (short-ish articles) for achieving various things with Clojure. Recipes cover a broad range of topics. You'll encounter string manipulation, writing web apps, Datomic, testing with Midje, Cascalog and a bunch of other things. On quick calculation there are about 169 recipes in the book from about 60 contributors, all reviewed and curated by the books authors.&lt;/p&gt;&lt;h2&gt;Who is it for?&lt;/h2&gt;&lt;p&gt;Good question. A broad book is going to have broad appeal. If you're starting on your Clojure journey you will initially find usefulness in the more low level recipes in the book and eventually graduate to the more general higher level recipes such as writing web apps and accessing databases. But if you've been working with Clojure for some time there is always going to be some aspect or task you've not had to deal with yet and again many recipes in this book could prove useful. At the very least they could act as refreshers for the tasks you only need to approach once in a while.&lt;/p&gt;&lt;p&gt;Another way to consider using this book is as inspiration. If you're wanting to take your Clojure experience to the next level and venture into new areas you could use this book as a sort of guide. This is where I see the charm in this type of book rather than the function. In my downtime I like flicking open the contents and picking a recipe at random and having a play with whatever technique or technology it's focused on.&lt;/p&gt;&lt;p&gt;That said if you like to get down to the nuts and bolts of a topic and really dig into it you're going to need another way to do that. The recipes in this book usually offer some insight into the rationale behind them (they "why?" if you will) but given their terse nature you could be left with outstanding questions. Also, the level of detail in each recipe varies e.g. you can expect more discussion around an article covering, say, string manipulation than you will about broader topics like distributed computing. That said you are never left wondering why you'd actually do the thing you are reading about - it's either obvious or discussed.&lt;/p&gt;&lt;h2&gt;Worth it?&lt;/h2&gt;&lt;p&gt;Yeah. Listen - you don't need to buy this book. Its available for free &lt;a href="https://github.com/clojure-cookbook/clojure-cookbook"&gt;online&lt;/a&gt; and a quick Googling could often lead you to articles on the same topics but lets think about this a little differently. Books like this are the result of community collaboration, of people coming together and producing a body of work that deserves a place on your bookshelf. So, without getting to idealistic, not only are you enriching yourself but you are celebrating community and I value that.&lt;/p&gt;&lt;p&gt;As a body of work - online or off - whether you are diving into Clojure for the first time or a seasoned veteran about to venture into a new area of the ecosystem there is something in this book you can get value from.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;I was fortunate enough to receive a complimentary copy of the Clojure Cookbook but I was in no way contracted to write a review. I do so simply because it's the least I could do to promote a book I find useful.&lt;/p&gt;
&lt;/blockquote&gt;</content></entry><entry><title>On onpaste</title><updated>Thu May 08 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/05/08/on-onpaste/"></link><id>urn:yobriefca-se:feed:post:On onpaste</id><content type="html">&lt;p&gt;When they say,&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;We'd lose our security certificate if we allowed pasting [into &lt;code&gt;input&lt;/code&gt; fields]. It could leave us open to a "brute force" attack.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;They forget that their webpage is simply a client of their backend service. There are many other &lt;a href="http://www.getpostman.com/"&gt;existing ones&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;They forget that stopping "brute force" attacks is a matter for their back end.&lt;/p&gt;&lt;p&gt;They forget that JavaScript can be disabled, fail to load or used to turn off 'onpaste'.&lt;/p&gt;&lt;p&gt;They forget that these practices force their customers to use simpler passwords&lt;/p&gt;&lt;p&gt;They forget that their actions make their customers more vulnerable.&lt;/p&gt;&lt;p&gt;They forget that when a user, who was for all intents and purposes forced by them to use a simple guessable password, has their account compromised they use words that put the blame on that user and make them feel bad. &lt;/p&gt;&lt;p&gt;I say &lt;em&gt;forget&lt;/em&gt; but I really mean &lt;em&gt;ignore the fact&lt;/em&gt;. I guess it's easier to hide behind outdated words and policy than understand the &lt;strong&gt;needs of the user&lt;/strong&gt;, your customers.&lt;/p&gt;</content></entry><entry><title>Managing Environment Variables in Clojure</title><updated>Tue Apr 29 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/04/29/managing-environment-variables-in-clojure/"></link><id>urn:yobriefca-se:feed:post:Managing Environment Variables in Clojure</id><content type="html">&lt;blockquote&gt;&lt;p&gt;If you've never read the &lt;a href="http://12factor.net/"&gt;Twelve Factor App&lt;/a&gt; methodology I strongly recommend you do, it's a great guide born from real life experience of building many, many web applications.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;One of the rules in the &lt;a href="http://12factor.net/"&gt;Twelve Factor App&lt;/a&gt; methodology is that you &lt;a href="http://12factor.net/config"&gt;"Store config in the environment"&lt;/a&gt; or, more specifically, store config that is likely to change &lt;strong&gt;across environments&lt;/strong&gt; in the environment. It makes sense as it ties the target environment and its necessary configuration together instead of having to juggle &lt;code&gt;if ENV=test|dev&lt;/code&gt; style flags around your code base or swap out different config files per build.&lt;/p&gt;&lt;p&gt;One other positive side effect of this approach is that secure configuration doesn't accidentally get checked into source. Every one needs database credentials or client secrets but no-one want to share them with the world.&lt;/p&gt;&lt;p&gt;Supporting this approach in Clojure is fairly simple and there are a few ways to do it.&lt;/p&gt;&lt;h2&gt;System/getenv&lt;/h2&gt;&lt;p&gt;Clojure sits on the JVM and the JVM has the &lt;code&gt;System&lt;/code&gt; namespace which provides utilities and classes for accessing the running systems features. &lt;code&gt;System.getenv&lt;/code&gt; and &lt;code&gt;System.getenv(String)&lt;/code&gt; give us access to environment variables from Clojure.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;System/getenv&lt;/span&gt; &lt;span class="s"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;;=&amp;gt; http://user:pass@dburl/mydb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can also provide a fallback for environment variables that don't exist&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;or &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;System/getenv&lt;/span&gt; &lt;span class="s"&gt;"NOEXIST_DATABASE_URL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="s"&gt;"http://localhost/mydb"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;;=&amp;gt; http://localhost/mydb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this case &lt;code&gt;NOEXIST_DATABASE_URL&lt;/code&gt; isn't set as an environment variable so we fallback to our hardcoded version. &lt;/p&gt;&lt;p&gt;This isn't uncommon and often its done to support development environments. For example &lt;a href="http://heroku.com"&gt;Heroku&lt;/a&gt; (where the original 12 factor app methodology arose) add environment variables for any addons you may activate such as &lt;code&gt;DATABASE_URL&lt;/code&gt; for the Postgres DB addon. So rather than write different handlers per environment many people would either.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Use the fallback URL to represent their development environment&lt;/li&gt;
  &lt;li&gt;Add the environment variable to their shell&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Both of the approaches have problems. Using a fallback URL can lead to problems where an environment isn't correctly configured and starts using the wrong database. If it can write to it there will be trouble, if it can't you'll be scratching your head wondering whats going wrong for longer then necessary. Setting a project specific environment variable in your shell can lead to pain when things get overwritten across projects and pollution if you're setting it in an automated fashion (&lt;code&gt;.bashrc&lt;/code&gt;) or you'll simply forget to set it per use when doing it manually.&lt;/p&gt;&lt;h2&gt;weavejester/environ&lt;/h2&gt;&lt;p&gt;A neater solution is available in the form of &lt;a href="https://github.com/weavejester/environ"&gt;&lt;code&gt;environ&lt;/code&gt;&lt;/a&gt; - a simple library for managing environment variables. The clever thing &lt;a href="https://github.com/weavejester/environ"&gt;&lt;code&gt;environ&lt;/code&gt;&lt;/a&gt; does is merge environment variables from multiple sources into a single map.&lt;/p&gt;&lt;p&gt;So by adding it to our &lt;code&gt;project.clj&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;environ&lt;/span&gt; &lt;span class="s"&gt;"0.5.0"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can import and use it where we need,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;environ.core&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;env&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt; &lt;span class="ss"&gt;:database-url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;;=&amp;gt; http://localhost/mydb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So how are the environment variables sourced? Well environ looks in a number of areas in order,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;A &lt;code&gt;.lein-env&lt;/code&gt; file in the project directory&lt;/li&gt;
  &lt;li&gt;Environment variables&lt;/li&gt;
  &lt;li&gt;Java system properties&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;It also keywordises the variable names as you can see from the example above.&lt;/p&gt;&lt;p&gt;Great, so why is this more useful? The big benefit I've discovered is the &lt;code&gt;.lein-env&lt;/code&gt; file in the root of my project. This file holds a map of environment values that can be useful during development. There are 2 ways to create this file.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The RIGHT way as set out by the creator of the project, and,&lt;/li&gt;
  &lt;li&gt;The WRONG way as in how I use it&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;&lt;code&gt;environ&lt;/code&gt; also has a plugin available &lt;code&gt;lein-environ&lt;/code&gt; which sucks profile specific settings from &lt;code&gt;~/.lein/profiles.clj&lt;/code&gt; and/or a project specific &lt;code&gt;profiles.clj&lt;/code&gt; (which should not be checked into source) and creates the &lt;code&gt;.lein-env&lt;/code&gt; file when Leiningen does its thing (effectively making &lt;code&gt;.lein-env&lt;/code&gt; a transient file). A &lt;code&gt;.lein-env&lt;/code&gt; file looks a little something like this.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:env&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:database-url&lt;/span&gt; &lt;span class="s"&gt;"http://localhost/mydb"&lt;/span&gt; 
         &lt;span class="ss"&gt;:client-token&lt;/span&gt; &lt;span class="s"&gt;"QWERTY12345"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;strong&gt;Caveat:&lt;/strong&gt; Now you &lt;strong&gt;can&lt;/strong&gt; create this manually (this is what I have done for a small project I'm working on) but it's not officially recommended for a few reasons. Firstly the minute you add the &lt;code&gt;lein-environ&lt;/code&gt; plugin to the project your important setting will get wiped out and secondly you lose out on the ability to vary setting across profiles as well that you'd get from &lt;code&gt;profiles.clj&lt;/code&gt;. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Big thanks to &lt;a href="https://twitter.com/weavejester"&gt;@weavejester&lt;/a&gt; the creator of &lt;code&gt;environ&lt;/code&gt; for the tip off about my dubious use of &lt;code&gt;.lein-env&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;So back to &lt;code&gt;.lein-env&lt;/code&gt; - You end up with the &lt;code&gt;.lein-env&lt;/code&gt; file on your machine with development (or profile) specific values and you forget about fallbacks and setting environment variables and the potential conflicts with other projects. Then on your production and test systems you can source values from actual environment variables without having to change your strategy or use &lt;code&gt;(if (= "test" (:env config)))&lt;/code&gt; style checks.&lt;/p&gt;&lt;p&gt;Its worth noting that &lt;code&gt;.lein-env&lt;/code&gt; is already added as a match in the default &lt;code&gt;.gitignore&lt;/code&gt; from &lt;code&gt;lein new ...&lt;/code&gt; so wont be checked into &lt;code&gt;git&lt;/code&gt; - &lt;strong&gt;BUUUUUUT...&lt;/strong&gt; if you do manage to check in and push your &lt;code&gt;.lein-env&lt;/code&gt; file (or any project specific &lt;code&gt;profiles.clj&lt;/code&gt; file) and it does happen to contain secure tokens and passwords (hashed or otherwise) you must assume those values are already compromised even if you manage to purge them from history.&lt;/p&gt;</content></entry><entry><title>Play! 2 for Scala (Pluralsight)</title><updated>Tue Apr 15 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="http://pluralsight.com/training/Courses/Description/play-2-scala"></link><id>urn:yobriefca-se:feed:post:Play! 2 for Scala (Pluralsight)</id><content type="html"></content></entry><entry><title>Why Scala? Because C#</title><updated>Thu Apr 03 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/04/03/why-scala-because-c-number/"></link><id>urn:yobriefca-se:feed:post:Why Scala? Because C#</id><content type="html">&lt;blockquote&gt;&lt;p&gt;Strange as it may sound this isn't a post about promoting the adoption of Scala but if its a conversation you're already having there may be some points here that might help. I often hear rationale for Scala over Java based around the functional style, immutability and slightly better concurrency model baked in but I've always found there is a simpler point to be made. Time will tell wether the recent release of Java 8 will add enough to old faithful to slow the adoption of Scala.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I often get asked by people I'm introducing to Scala, "Why Scala and why not Java" and more often that not the topic turns to comparisons between Scala and C#. Its not just me either. Last year I was involved in a few rounds of procurement, interviewing prospective IT companies and team and a common pattern that came out of those interviews was people would be offering us teams composed of their C# developers over their Java developers. Scala experience was, at that time, still a bit light on the ground and yes you could argue that the general trend toward fully open source stacks in many markets was squeezing the amount of available work for MS folks but the message most people delivered was their C# developers were generally more capable of transitioning to Scala than their Java people. I can't necessarily dispute this either because I certainly feel my background in C# helped me ramp up on my first Scala project much faster than my Java experience had.&lt;/p&gt;&lt;p&gt;Allow me a little example (ripped right out of my &lt;a href="https://speakerdeck.com/kouphax/scala-for-c-number-developers"&gt;Scala for C# Developers&lt;/a&gt; talk)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="s"&gt;"CHRISTMAS"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;makeChristmasy&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; ***CHRISTMAS***&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There are three important things to know here&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The &lt;code&gt;String&lt;/code&gt; class in Java is &lt;code&gt;final&lt;/code&gt; and can't be extended.&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;String&lt;/code&gt; class in Scala is &lt;code&gt;final&lt;/code&gt; and can't be extended (it is the Java &lt;code&gt;String&lt;/code&gt; class)&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;String&lt;/code&gt; class in C# is &lt;code&gt;sealed&lt;/code&gt; and can't be extended&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Yet we just called a function on it that isn't part of Strings signature (it's common knowledge Scala has a lot of bells and whistles but trust me the String class doesn't normally have a &lt;code&gt;makeChristmasy&lt;/code&gt; method)&lt;/p&gt;&lt;p&gt;To implement this we can make use of Scalas &lt;a href="http://docs.scala-lang.org/overviews/core/implicit-classes.html"&gt;Implicit Classes&lt;/a&gt;. We can write a class that wraps a class, &lt;code&gt;String&lt;/code&gt; in our case, &lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FancyString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
 &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;makeChristmasy&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"***"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"***"&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Import this into our namespace and voila our &lt;code&gt;String&lt;/code&gt; variables and values have the &lt;code&gt;makeChristmasy&lt;/code&gt; method available to them. Under the hood the Scala compiler is auto wrapping the String instances with FancyString where necessary. Kinda like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FancyString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"CHRISTMAS"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;makeChristmasy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Sure its essentially a bit of syntactic sugar but it can, when used wisely, help make code clearer.&lt;/p&gt;&lt;p&gt;C# can do this too. C# has extension methods,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp"&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FancyString&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;MakeChristmasy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"***"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"***"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So what? Scala has a feature that C# has and Java doesn't. Hell lets do the same in Ruby,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;  
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;make_christmasy&lt;/span&gt;
    &lt;span class="s2"&gt;"***&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;***""&lt;/span&gt;
&lt;span class="s2"&gt;  end  &lt;/span&gt;
&lt;span class="s2"&gt;end &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So now can Ruby developers transition to Scala faster than Java devs? No, well maybe I'm not sure, stop confusing me, besides open classes in Ruby are a vastly different and scarier thing than extension methods and implicit classes.&lt;/p&gt;&lt;h2&gt;Your point being...?&lt;/h2&gt;&lt;p&gt;There is a certain power and flexibiltiy that a feature like implicits offer. An ability to coerce the underlying language into something that makes more sense in the given context. Yeah its open for abuse but we're supposed to be grown ups here people. Lets not force people to use plastic knives to cut steak just in case they stab themselves. Of course its not just implicits - the language is simply a richer language.&lt;/p&gt;&lt;p&gt;What Scala offers is somewhat like C# on the JVM. Java fell behind C# a long time ago and even with the release of Java 8 it is still not quite there. There is a degree of expressiveness in C# that enabled me in my .NET days to produce better, more grokable systems in C# than anything I produced in Java. I found the same with Scala. Sure there are many, many dark corners in Scala but they can be avoided be that through a common understanding, peer review, automated checks like Sonar or even through the use of Scalas in-progress ability to add/remove language features through modular design. &lt;a href="http://www.thoughtworks.com/radar/#/languages-and-frameworks/257"&gt;"Scala: The Good Parts"&lt;/a&gt; as Thoughtworks termed it in their tech radar is a theoretical subset of the language that offers better collection support, terser syntax and a reduction in the need for pattern boilerplate without having to upgrade your JVM.&lt;/p&gt;&lt;p&gt;So while the gap between Java and Scala has lessened it's still there and given the cycle time for new Java releases it's not going to shorten any time soon. Perhaps you think there are times when you want your language to be a constraint on your team. I'd be suprised if you've never encountered a room full of mad language scientists trying to golf some operation down to 3 convoluted lines because "FUNCTIONAL PROGRAMMING YAY". But imposing artificial constraints because of a team problem is just going to frustrate the "good" people and frankly instead of an unmaintainable mess due to bonkers code golf you'll probably get an unmaintainable mess due to over application of EVERY PATTERN BECAUSE OBJECTS!&lt;/p&gt;&lt;p&gt;So why Scala? Because there is a core feature set in the language that enables people to produce nice clean straightforward code with less boilerplate and enough power and flexibility to make the intent of the resulting code clearer. Like C#.&lt;/p&gt;</content></entry><entry><title>Experience Report: Migrating from Middleman to Stasis</title><updated>Tue Apr 01 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/04/01/experience-report-migrating-from-middleman-to-stasis/"></link><id>urn:yobriefca-se:feed:post:Experience Report: Migrating from Middleman to Stasis</id><content type="html">&lt;p&gt;I recently migrated my statically generated site (yep this site) from a Ruby/&lt;a href="http://middlemanapp.com"&gt;Middleman&lt;/a&gt; solution to a Clojure/&lt;a href="https://github.com/magnars/stasis"&gt;Stasis&lt;/a&gt; based solution. This post is a kind of experience report on the migration process. It isn't a guide for using &lt;a href="https://github.com/magnars/stasis"&gt;Stasis&lt;/a&gt; and if that is something you're looking for I can't recommend Christian Johansens "&lt;a href="http://cjohansen.no/building-static-sites-in-clojure-with-stasis"&gt;Building static sites with Clojure&lt;/a&gt;" post enough.&lt;/p&gt;&lt;h2&gt;Background&lt;/h2&gt;&lt;p&gt;My site has transitioned through various platforms - from WordPress to Posterous, Jekyll and Middleman. Its also went through a few types - Internal corporate blog, public blog and finally a site that represents my role as a contractor.&lt;/p&gt;&lt;p&gt;I never really enjoyed WordPress or Posterous because there was a distinct lack of control and way too many features and points of failure and while Jekyll gave me flexibility its pattern for extension was rather undocumented and ambiguious. Middleman offered me the flexibility with a very simple programming model.&lt;/p&gt;&lt;p&gt;The next migration, the one I'm covering now, came about because the site had accrued a lot of content debt over the years and I needed to tidy it up. Problems such as&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;duplicated and redundant YAML frontmatter settings and logic,&lt;/li&gt;
  &lt;li&gt;a lot of different file types&lt;/li&gt;
  &lt;li&gt;the migrated Posterous stuff was locked up in prerendered HTML&lt;/li&gt;
  &lt;li&gt;many of the older posts use embedded gists and I wasn't happy with that&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Decision to Migrate&lt;/h2&gt;&lt;p&gt;So why move from Ruby to Clojure, Middleman to Stasis? Truth be told it was initially a learning experience. I was knee deep in using Clojure and decided the project was substantial enough to help further my education. I had started with refactoring the existing Ruby codebase but the content tidy up effort was proving awkward in Ruby. Once I started the work in Clojure it was clear I was able to better express my intent.&lt;/p&gt;&lt;h2&gt;Observations&lt;/h2&gt;&lt;p&gt;The next few sections are observations I've made during the migration.&lt;/p&gt;&lt;h3&gt;Stasis gives you less (and that's OK)&lt;/h3&gt;
&lt;blockquote&gt;&lt;p&gt;Statis just offers a few functions that are useful when creating static web sites.&lt;/p&gt;&lt;p&gt;No more. There are no batteries included.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Thats directly from the Stasis &lt;a href="https://github.com/magnars/stasis/blob/master/README.md"&gt;README&lt;/a&gt; and as you can guess it's spot on. Fundamentally speaking Stasis gives you two entry points&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;A ring handler for serving content&lt;/li&gt;
  &lt;li&gt;An &lt;code&gt;export-pages&lt;/code&gt; function for saving content to disk&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Both of these expect a map of &lt;code&gt;path&lt;/code&gt; and &lt;code&gt;content&lt;/code&gt; and thats it. You could serve an simple hello world style site like this.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;stasis.core/export-pages&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"/index.html"&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;Hello World&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                          &lt;span class="s"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running that would result in and &lt;code&gt;index.html&lt;/code&gt; file in the &lt;code&gt;/build&lt;/code&gt; folder of your project with the contents above.&lt;/p&gt;&lt;p&gt;To complement this stasis also gives you a method to &lt;code&gt;slurp&lt;/code&gt; a directory, read its contents and generate one of these content maps. This is an excellent starting point for transforming file based content.&lt;/p&gt;&lt;p&gt;In my site I slurp a directory of metadata enriched markdown files (YAML frontmatter) and thread them through a bunch of transformation functions. The heart of my content processing is a simple little function&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;entries&lt;/span&gt;
  &lt;span class="p"&gt;[]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;entries&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;slurp-content&lt;/span&gt; &lt;span class="s"&gt;"resources/content/entries"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;-&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;entries&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;filter &lt;/span&gt;&lt;span class="ss"&gt;:published&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;map &lt;/span&gt;&lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;assoc &lt;/span&gt;&lt;span class="nv"&gt;%&lt;/span&gt; &lt;span class="ss"&gt;:uri&lt;/span&gt;
                      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;case&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;keyword &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:type&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                        &lt;span class="ss"&gt;:article&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;article-uri&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="ss"&gt;:screencast&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;screencast-uri&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="ss"&gt;:talk&lt;/span&gt;       &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:url&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="ss"&gt;:project&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:url&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;sort-by &lt;/span&gt;&lt;span class="ss"&gt;:date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;slurp-content&lt;/code&gt; in this function extracts the file contents and converts it to a map based on the YAML frontmatter and the content body&lt;/p&gt;&lt;p&gt;At the end each bit of content (article, screencast, talk entry etc.) is a map that can be passed around to generate category pages, RSS feeds, HTML pages. Content as data so to speak. So in future if I want to provide a JSONified version of all talk metadata I can just add another function that merges my new paths into the site map that Stasis uses.&lt;/p&gt;&lt;p&gt;This sort of work was mostly handled by Middleman internally and transformations configured by providing different file extensions to the content (which could be stacked like &lt;code&gt;.html.erb.md&lt;/code&gt;). This meant that behaviour was controlled by manipulating content rather than it being treated as data. In Stasis this means there is a bit more code involved but the end result is perfectly acceptable.&lt;/p&gt;&lt;p&gt;There is a simplicity in Stasis I could never achieve with Middleman.&lt;/p&gt;&lt;h3&gt;Expressiveness of Clojure&lt;/h3&gt;&lt;p&gt;When it came to tidying up the content I found the use of an interactive REPL and a few simple libraries meant I was able to achieve things quickly and easily. When working with files combining the REPL with Git gave me a transactional approach to messing with files without fear. I've saved most of my efforts in the fom of a &lt;a href="https://gist.github.com/kouphax/9854290"&gt;gist&lt;/a&gt; for this work.&lt;/p&gt;&lt;p&gt;One of the biggest challenges I came up against was taking the old articles that contained embedded gists and replacing these with syntax highlighted code blocks. This involved&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Scraping the &lt;code&gt;script&lt;/code&gt; tags that linked to embedded gists from the articles&lt;/li&gt;
  &lt;li&gt;Using the Github API to load the content and file type of each Gist (some had multiple files)&lt;/li&gt;
  &lt;li&gt;Run the content through the pygments based syntax highlighter to produce a code block&lt;/li&gt;
  &lt;li&gt;Swap the &lt;code&gt;script&lt;/code&gt; and potential &lt;code&gt;noscript&lt;/code&gt; block from the offending files&lt;/li&gt;
  &lt;li&gt;Write the new content out.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It sounds easy on paper (and maybe for you it is) but previous attempts in Ruby had driven me to give up. With Clojure and &lt;a href="https://github.com/cgrand/enlive"&gt;Enlive&lt;/a&gt; this work was easier to express and achieve. Of course it wasn't without problems. I found &lt;a href="https://github.com/cgrand/enlive"&gt;Enlive&lt;/a&gt; confusing at times but the interactive nature of the REPL meant I could break the problem down and isolate pain points.&lt;/p&gt;&lt;p&gt;Clojures REPL is more powerful than &lt;code&gt;irb&lt;/code&gt; and this allowed me to babystep my problems. Clojures syntax, simplicity and powerful standard lib allowed me to express my intent more clearly, with less code and fewer problems. The two together gave me an environment that allowed me to get to where I wanted with fewer frustrations.&lt;/p&gt;&lt;h3&gt;Generation Time&lt;/h3&gt;&lt;p&gt;I build my site with an alias I set up &lt;code&gt;lein build-site&lt;/code&gt;. Previously I used &lt;code&gt;bundle exec middleman build&lt;/code&gt;. At first I noticed that the time to generate the static site in Clojure felt a lot longer. In fact the generation process itself is probably a bit slower. But, there is always a &lt;em&gt;but&lt;/em&gt;, overall a fresh build and deploy (thanks to &lt;a href="https://travis-ci.org/kouphax/yobriefca.se"&gt;Travis&lt;/a&gt;) actually takes a tiny bit less (a few seconds difference, nothing to write home about).&lt;/p&gt;&lt;p&gt;It seems a lot of the build time in Ruby comes from the resolution of dependencies of which Middleman has a lot more than Stasis. Note: This analysis is by no means in-depth of course and ultimatley it still only takes about 3 minutes from commiting a chance to seeing it live (depending on the speed of Travis picking it up).&lt;/p&gt;&lt;h2&gt;Credits&lt;/h2&gt;&lt;p&gt;I've previously mentioned Christian Johansens "&lt;a href="http://cjohansen.no/building-static-sites-in-clojure-with-stasis"&gt;Building static sites with Clojure&lt;/a&gt;" post and I'd like to acknowledge that it was the post that finally made me decide to migrate. His post was invaluable during the inital migration (especially around the syntax highlighting).&lt;/p&gt;&lt;p&gt;While the post kicked of the decision to migrate the following list of technolgies made that transition possible.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://github.com/magnars/stasis"&gt;Stasis&lt;/a&gt; - provides the core of the site generation&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/magnars/optimus"&gt;Optimus&lt;/a&gt; - Static asset optimisation (bundling, minification etc.)&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/cgrand/enlive"&gt;Enlive&lt;/a&gt; - DOM manipulation library that was an essential part of unembedding the gists and replaceing code blocks with syntax highlighted blocks.&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/Raynes/cegdown"&gt;cegdown&lt;/a&gt; - Markdown processing library. Essentially a Clojure wrapper for &lt;a href="https://github.com/sirthias/pegdown"&gt;Pegdown&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/lancepantz/clj-yaml"&gt;clj-yaml&lt;/a&gt; - Encoding and decoding of YAML frontmatter&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/Raynes/fs"&gt;fs&lt;/a&gt; - A bunch of file system utilities. Used a lot when renaming and replating content files.&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/pelle/slugger"&gt;slugger&lt;/a&gt; - Genreates HTML friendly slugs for articles. This is a port of the stringex Ruby library used in Middleman which was essential to avoid breaking original urls&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/bfontaine/clygments"&gt;clygments&lt;/a&gt; - A Clojure wrapper around then Pygments syntax highlighting library.&lt;/li&gt;
&lt;/ul&gt;</content></entry><entry><title>flic - Simple feature toggles</title><updated>Thu Mar 13 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/flic/"></link><id>urn:yobriefca-se:feed:post:flic - Simple feature toggles</id><content type="html"></content></entry><entry><title>Defocus</title><updated>Tue Mar 04 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/03/04/defocus/"></link><id>urn:yobriefca-se:feed:post:Defocus</id><content type="html">&lt;p&gt;Let me tell you a story. It's one of those stories you look back on and laugh at how ridiculously clueless you had been. It's not a great story, it's a bit silly in fact.&lt;/p&gt;&lt;p&gt;In my first year of my Software Engineering course at university we were introduced to a number of languages including but not entirely limited to Java, Ada95, Pascal, Prolog, Standard ML and JavaScript. The intent behind this exposure to so many languages in one go was to get us to understand the various language styles such as object orineted, logic and functional. My previous experience of languages was limted to breaking everything in JavaScript, Pascal, Visual Basic and its subset VBA. It wasn't easy, jumping between styles and syntax and concepts. But you keep going and eventually you get it. So eventually I got it. Just in time to move to the next language.&lt;/p&gt;&lt;p&gt;In particular I really enjoyed working with ML, I still own &lt;a href="http://www.amazon.com/Elements-Programming-ML97-Edition-2nd/dp/0137903871"&gt;Elements of ML Programming&lt;/a&gt; with it's bizarre front cover resembling a treasure map. The functional approach it used, and the power of this appraoch for building terse solutions was a breath of fresh air compared to the Java 1.2 stuff we were using on another course. I enjoyed it so much I wanted to do well in it and so would work through the course material, the optional material and then some other stuff during the practical lectures. Nothing big, just little functions that did factorials and the usual stuff.&lt;/p&gt;&lt;p&gt;One day I was going through the usual exercises determined to get the mandatory exercises done and crack on with the optional ones (usually more interesting). About an hour and a half in I was trying to write a function that given a number would return the number if it was less than 10 otherwise it would return 1. Simple, right? I got about this far.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sml"&gt;&lt;span class="cm"&gt;(* do some number adjustment magic *)&lt;/span&gt;
&lt;span class="kr"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;adjust&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
                        &lt;span class="kr"&gt;then&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
                        &lt;span class="kr"&gt;else&lt;/span&gt; &lt;span class="n"&gt;&amp;lt;???????&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;adjust&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="cm"&gt;(* expects 3 *)&lt;/span&gt;
&lt;span class="n"&gt;adjust&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="cm"&gt;(* expects 1 *)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I'm head down learning new things, trying to understand the syntax of ML and I'm stumped because I don't know how to return 1 from a function. I thought and thought and thought about it. Nothing. Eventually I remembered dividing a number by itself yield 1. Pure. Unadulterated. Genius.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="sml"&gt;&lt;span class="kr"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;adjust&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="n"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
                        &lt;span class="kr"&gt;then&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
                        &lt;span class="kr"&gt;else&lt;/span&gt; &lt;span class="n"&gt;number/number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But nooooooooo. Running this gives us the following error,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;Standard ML of New Jersey v110.74 &lt;span class="o"&gt;[&lt;/span&gt;built: Fri Jan 24 17:53:58 2000&lt;span class="o"&gt;]&lt;/span&gt;
- stdIn:4.30-4.43 Error: operator and operand don&lt;span class="err"&gt;'&lt;/span&gt;t agree &lt;span class="o"&gt;[&lt;/span&gt;literal&lt;span class="o"&gt;]&lt;/span&gt;
  operator domain: real * real
  operand:         int * int
  in expression:
    number / number
-
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;My ML-fu was still weak and this had me stumped. I tried various tweaks of the same thing but I could not get this thing to return a 1. Defeated by a dumb number I left the class early and went for a walk.&lt;/p&gt;&lt;p&gt;About 10 minutes into the walk I'd forgotten about the class and the absolutely mind bending puzzle of returning 1 from a function. Hmmmmm Returning 1. Just return 1. &lt;code&gt;else 1&lt;/code&gt;. Wait, wait you can just return things. 1 is a thing. Return the thing that is 1. The thing that is 1 is 1. Return 1.&lt;/p&gt;&lt;p&gt;Ffffffffffffffffffffffffffffffffffffffacepalm&lt;/p&gt;&lt;p&gt;It only took about 11 or 12 minutes of extracting myself out of that narrow focus I was in. A focus so narrow that fundamental programming concepts such as literals weren't even in my periphery. I simply stopped thinking correctly. I could have sat there until the class finished and burnt more time.&lt;/p&gt;&lt;p&gt;Of course I'm not saying anything new here. I'm recounting a 14 year old story that still happens to me today. But I think now I've matured enough to know that my continued effort is becoming net negative and to walk away. It feels like common sense now and you may think the same thing. But its a point worth reminding yourself of sometimes.&lt;/p&gt;&lt;p&gt;Focusing on a single point for too long will blind you to alternatives. Take time to reflect, take time to think AWAY from the problem. Use a shower, hammock or long walk. Do some exercise. Talk to a third party such as a rubber duck or a real person. Ask yourself "how would I do this in something I'm familiar with?".&lt;/p&gt;&lt;p&gt;Defocus.&lt;/p&gt;</content></entry><entry><title>Building Command Line Apps with Clojure</title><updated>Sun Mar 02 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/03/02/building-command-line-apps-with-clojure/"></link><id>urn:yobriefca-se:feed:post:Building Command Line Apps with Clojure</id><content type="html">&lt;p&gt;I recently read an article by &lt;a href="http://rkn.io"&gt;Ryan Neufeld&lt;/a&gt; around &lt;a href="http://www.rkn.io/2014/02/27/clojure-cookbook-command-line-args/"&gt;parsing command line arguments with Clojure&lt;/a&gt;. It's a good article and you should read it if the idea of building command line utilities in Clojure floats your boat.&lt;/p&gt;&lt;p&gt;To complement that article I've decided to take the building of the command line app one step further and support running your creation as a standalone command. Of course its possible, as demonstrated in the article, to run the app using Leiningen directly but you typically want to be bundling your app into a self conatined package (minus any necessary runtimes - in our case the JVM) for deployment to other environments.&lt;/p&gt;&lt;p&gt;So lets start were Ryans article left off, a simple command line app that can be run via Leiningen. The &lt;code&gt;core.clj&lt;/code&gt; looks like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;runs.core&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;clojure.tools.cli&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;cli&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:gen-class&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;


&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;-main&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;args&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let &lt;/span&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;opts&lt;/span&gt; &lt;span class="nv"&gt;args&lt;/span&gt; &lt;span class="nv"&gt;banner&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cli&lt;/span&gt; &lt;span class="nv"&gt;args&lt;/span&gt;
                                &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"-h"&lt;/span&gt; &lt;span class="s"&gt;"--help"&lt;/span&gt; &lt;span class="s"&gt;"Print this help"&lt;/span&gt;
                                 &lt;span class="ss"&gt;:default&lt;/span&gt; &lt;span class="nv"&gt;false&lt;/span&gt; &lt;span class="ss"&gt;:flag&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt;&lt;span class="p"&gt;])]&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;when &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:help&lt;/span&gt; &lt;span class="nv"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;println &lt;/span&gt;&lt;span class="nv"&gt;banner&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And the &lt;code&gt;project.clj&lt;/code&gt; looks like this&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defproject &lt;/span&gt;&lt;span class="nv"&gt;runs&lt;/span&gt; &lt;span class="s"&gt;"0.1.0-SNAPSHOT"&lt;/span&gt;
  &lt;span class="ss"&gt;:description&lt;/span&gt; &lt;span class="s"&gt;"FIXME: write description"&lt;/span&gt;
  &lt;span class="ss"&gt;:url&lt;/span&gt; &lt;span class="s"&gt;"http://example.com/FIXME"&lt;/span&gt;
  &lt;span class="ss"&gt;:license&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="s"&gt;"Eclipse Public License"&lt;/span&gt;
            &lt;span class="ss"&gt;:url&lt;/span&gt; &lt;span class="s"&gt;"http://www.eclipse.org/legal/epl-v10.html"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="ss"&gt;:dependencies&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;org.clojure/clojure&lt;/span&gt; &lt;span class="s"&gt;"1.5.1"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                 &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;org.clojure/tools.cli&lt;/span&gt; &lt;span class="s"&gt;"0.2.4"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
  &lt;span class="ss"&gt;:main&lt;/span&gt; &lt;span class="nv"&gt;runs.core&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;As Ryans article mentions we can run this via &lt;code&gt;lein run&lt;/code&gt; and pass our arguments in.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;lein run -- -h
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Uberjar&lt;/h2&gt;&lt;p&gt;First thing we want to do is take Leiningen out of the equation and bundle the app as a standalone JAR. Unlike other JVM build tools that need to be extended with plugins such as &lt;a href="http://maven.apache.org/plugins/maven-shade-plugin/"&gt;shade&lt;/a&gt;, &lt;a href="http://one-jar.sourceforge.net/"&gt;onejar&lt;/a&gt; and &lt;a href="https://github.com/sbt/sbt-assembly"&gt;assembly&lt;/a&gt; Leingingen comes with &lt;code&gt;uberjar&lt;/code&gt; prebundled. To create a basic uberjar, a JAR containting your application code and all the necessary dependencies, we can call &lt;code&gt;lein uberjar&lt;/code&gt;. There are also a bunch of configuration options that go along with uberjar but what we have specified is sufficient to build our jar. The JAR will be output to &lt;code&gt;target&lt;/code&gt; dir appended with &lt;code&gt;-standalone&lt;/code&gt;. We can run this JAR using via the usual Java way&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;java -jar target/app-0.1.0-SNAPSHOT-standalone.jar -h
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;lein-bin&lt;/h2&gt;&lt;p&gt;I don't know about you but I'm lazy and I like my commands names to represent what they do (roughly) so the thought of running &lt;code&gt;java -jar blahblah.jar -h&lt;/code&gt; every time I want to run the command is just "meh". We could write a shell script to run the more verbose command but thats less than ideal (it's messy and on top of my laziness I'm a tad OCD about carting around wrapper scripts).&lt;/p&gt;&lt;p&gt;Enter &lt;a href="https://github.com/Raynes/lein-bin"&gt;lein-bin&lt;/a&gt; a Leiningen plugin that utilises the fact ZIP files and therefore JARs can have stuff prepended to the front of it. What it does is prepend shell commands to your JAR making it executable in and of itself.&lt;/p&gt;&lt;p&gt;So to use &lt;code&gt;lein-bin&lt;/code&gt; we need to add the plugin to our projects &lt;code&gt;project.clj&lt;/code&gt; or the global Leiningen profile (&lt;code&gt;~/.lein/profiles.clj&lt;/code&gt;)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:plugins&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="nv"&gt;lein-bin&lt;/span&gt; &lt;span class="s"&gt;"0.3.4"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can also add some &lt;code&gt;lein-bin&lt;/code&gt; related configuration like changing the output name (there are some other options as well, all noted in the &lt;code&gt;lein-bin&lt;/code&gt; &lt;a href="https://github.com/Raynes/lein-bin/blob/master/README.markdown"&gt;README&lt;/a&gt;)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="ss"&gt;:bin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt; &lt;span class="s"&gt;"runs"&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So running &lt;code&gt;lein bin&lt;/code&gt; now leaves us with another file in &lt;code&gt;target&lt;/code&gt; called &lt;code&gt;runs&lt;/code&gt; that is executable.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="nv"&gt;target/runs&lt;/span&gt; &lt;span class="nv"&gt;-h&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we've got a command line util called &lt;code&gt;runs&lt;/code&gt; (yeah a terrible name) rather than some verbose Java command or being force to use a build tool on all environments. It's the little things.&lt;/p&gt;</content></entry><entry><title>SPAs and Enabling CORS in Spark</title><updated>Thu Feb 20 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/02/20/spas-and-enabling-cors-in-spark/"></link><id>urn:yobriefca-se:feed:post:SPAs and Enabling CORS in Spark</id><content type="html">&lt;p&gt;Having been tinkering with &lt;a href="http://sparkjava.com"&gt;Spark&lt;/a&gt; again I needed to build a simple bit of infrastructure for building single page applications that could be deployed anywhere. I also needed to potentially support CORS (Cross Origin Resource Sharing) so that remote client-side JavaScript applications could actually access the JSON API aspect of the solution. Doing this with Spark is pretty simple but I thought I'd document the process to avoid some people (or future me) having to answer the same questions again. I'm going to assume you know a bit about Spark, if not have a skim of the &lt;a href="http://sparkjava.com/readme.html"&gt;README&lt;/a&gt; on the site.&lt;/p&gt;&lt;h2&gt;Building a SPA&lt;/h2&gt;&lt;p&gt;An SPA or Single Page Application is an application that serves a single HTML page and included JavaScript on that page is responsible for dealing with talking to the server, getting data, rendering different views and handling view navigation. This is different from the traditional approach of rendering views on the server through the typical request/response lifecycle. I'm not going to debate the merits and drawbacks of either strategy in this post let's just assume that you've decided to go with an SPA in a Java context and you want something simple. I'm also not going to suggest any sort of front-end framework here - that'll depend on what you need, right? So a sample anatomy of an SPA could look like this,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;The root path at &lt;code&gt;http://website/&lt;/code&gt; returns a static &lt;code&gt;index.html&lt;/code&gt; file&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;index.html&lt;/code&gt; file loads the necessary static assets such as JavaScript and CSS&lt;/li&gt;
  &lt;li&gt;The JavaScript calls local API endpoints to load data&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; and render content through client side templates or straightforward DOM manipulation&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;That's it. So in Spark to support these needs you need to do a few things,&lt;/p&gt;&lt;h2&gt;The API endpoints&lt;/h2&gt;&lt;p&gt;JavaScript loves JSON, it makes sense. So we need to create an API endpoint that returns JSON responses.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JsonRoute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/api"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="o"&gt;{{&lt;/span&gt;
            &lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}};&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This stubbed route simply returns a map of key value pairs. If you look at the route type (&lt;code&gt;JsonRoute&lt;/code&gt;) you'll notice it's not a typical Spark route type. In fact it's a class implemented by me. &lt;code&gt;JsonRoute&lt;/code&gt;, we'll look at the code shortly, makes use of a Spark class - &lt;code&gt;ResponseTransformerRoute&lt;/code&gt; which is a specialized route type that supports the ability to "transform" the returned response model (the object that get returned by the routes handle method). In this implementation, below, I'm simply passing the model into a Jackson &lt;code&gt;ObjectMapper&lt;/code&gt;,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;JsonRoute&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;ResponseTransformerRoute&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ObjectMapper&lt;/span&gt; &lt;span class="n"&gt;mapper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ObjectMapper&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;JsonRoute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mapper&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;writeValueAsString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Throwable&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;RuntimeException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This allows us to abstract out the process of transforming API responses into JSON and serving the correct response type as well. Yep I could have done this directly in my route declaration but I like to keep the main entry point for my application clean and readable as that's where most people will start to reason about how the application behaves. So that gives us a nice API endpoint. We can access this by running the application and hitting &lt;code&gt;http://locahost:4567/api&lt;/code&gt;.&lt;/p&gt;&lt;h2&gt;Serving Static Assets&lt;/h2&gt;&lt;p&gt;Next up we want to be able to support serving the JavaScript, CSS and HTML that makes up our non-api aspects of our solution. Spark makes this easy&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;staticFileLocation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/public"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This command (declared in the main method) will wire up a handler to serve resources that are stored in &lt;code&gt;src/main/resources/public&lt;/code&gt; as static files. You can also specify external files (outside the built jar for example) but I've went with resources in this example. Then we create an index.html page in this folder along with some JavaScript and CSS,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="n"&gt;doctype&lt;/span&gt; &lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="n"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="n"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/css/styles.css"&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"css-broken-warning"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;The&lt;/span&gt; &lt;span class="n"&gt;stylesheet&lt;/span&gt; &lt;span class="n"&gt;didn&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="n"&gt;load&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maybe&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt; &lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"/js/app.js"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The CSS file in this case hides the CSS Broken Banner to prove this works&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="css"&gt;&lt;span class="nf"&gt;#css-broken-warning&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;display&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And the JavaScript simply appends a new H2 node to the document, again just to prove the whole thing works.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"h2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript Works"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You'll rip these dummy files out when you actually being to write your app and it's better to write test for this sort of thing generally.&lt;/p&gt;&lt;p&gt;We can run the app again and browse to &lt;code&gt;http://localhost:4567/index.html&lt;/code&gt; and make sure everything works.&lt;/p&gt;&lt;h2&gt;Serving the Root&lt;/h2&gt;&lt;p&gt;If you go to the root URL right now (&lt;code&gt;http://localhost:4567/&lt;/code&gt;) you see that we still haven't satisfied the first condition of our SPA structure, we need to navigate to the index.html page specifically to see the app in action. To support the route URL serving the &lt;code&gt;index.html&lt;/code&gt; file we can use a Spark before filter.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getResourceAsStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/public/index.html"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;halt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;IOUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// if the resource doesn't exist we just carry on.&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The filter, locked down to the &lt;code&gt;/&lt;/code&gt; route checks for the index.html resource and spits its contents out via a halt call (stop processing filters and handlers for this route). If it doesn't find it it just passed responsibility to the remaining filters and handlers. But why a filter? Well you could use a route and do something similar,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getResourceAsStream&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/public/index.html"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;IOUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IOException&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;RuntimeException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;But I think the filter is a bit neater. I the route case we have to return something. We can't just pass on to the next handler so instead we throw which may not make sense in all cases. Again it comes down to need here and YMMV so you now have 2 approaches to do the same thing. So running the app and hitting &lt;code&gt;http://localhost:4567/&lt;/code&gt; gives use our SPA infrastructure in place.&lt;/p&gt;&lt;h2&gt;CORS&lt;/h2&gt;&lt;p&gt;Supporting CORS in any web framework is a matter of Header manipulation - if you can do that you're sorted. Spark is no exception. CORS can be configured in a few different ways, you can set a few different types of headers, specify different header values etc. The example I provide here aims to give you a bit of configurability but uses it in the most permissive way possible (allowing any origins, header values and HTTP methods).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;enableCORS&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Filter&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;@Override&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;header&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Access-Control-Allow-Origin"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;header&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Access-Control-Request-Method"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;header&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Access-Control-Allow-Headers"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;});&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This method, which should be called early and called once in the main method, attaches a Spark filter to every route (you should alter that to your needs) that sets the necessary CORS headers to the passed in values. We can call this from the main method like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;enableCORS&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"*"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"*"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"*"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Which is as open as possible (more than it needs to be in most cases) for remote clients. You can test this using &lt;a href="http://client.cors-api.appspot.com/client"&gt;test-cors.org&lt;/a&gt; on your local instance.&lt;/p&gt;&lt;p&gt;Now we have a simple CORS enabled setup to build you SPAs using the wealth of technologies that Java offers.&lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: You could, if you desired, return HTML snippets, or JS commands rather than data but for the purposes of this post lets assume an API as this a lot nicer when supporting 3rd party apps that want to integrate - DATA IS KING and all that.&lt;/sup&gt;&lt;/p&gt;&lt;sup&gt;&lt;/sup&gt;</content></entry><entry><title>Adventures in Clojure Land</title><updated>Sat Jan 18 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/01/18/adventures-in-clojure-land/"></link><id>urn:yobriefca-se:feed:post:Adventures in Clojure Land</id><content type="html">&lt;p&gt;Over the last few evenings I've built a very simple application using an exclusivley Clojure stack. This is my attempt to reason about that experience. It's not supposed to be a tutorial/how-to and should be taken as "here a some things I looked at and found interesting/useful, look at them too". If you want to see the resultant code you can get it via the &lt;a href="github.com/kouphax/depression-test"&gt;depression-test repo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;I've dabbled in &lt;a href="http://clojure.org/"&gt;Clojure&lt;/a&gt; a few times in the past but nothing even slightly serious and usually going no further than some REPL tinkering so most of this was still very new to me. In fact my previous SSD died a horrible death so I even started on a fresh machine. I decided to approach the experience by intentionally over-engineering the solution, I included libraries that I really didn't need for such a small scale solution. I did this because there is an associated cost of entry for each new library you use and the increased surface area I was exposing myself to meant I'd feel pain that basic "Hello World" examples don't really give you.&lt;/p&gt;&lt;h2&gt;Tech Stack&lt;/h2&gt;&lt;p&gt;In terms of the tech stack I ended up using I had,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://leiningen.org/"&gt;Leiningen&lt;/a&gt; - the build to of the Clojure world. Also included a few plugins,&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/weavejester/lein-ring"&gt;lein-ring&lt;/a&gt; - start/stop your ring based apps&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/emezeske/lein-cljsbuild"&gt;lein-cljsbuild&lt;/a&gt; - compile your ClojureScript into JavaScript&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/ring-clojure/ring"&gt;Ring&lt;/a&gt; - Web server/middelware library (like &lt;code&gt;connect&lt;/code&gt; in the node.js world)&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/weavejester/compojure"&gt;Compojure&lt;/a&gt; - Routing library that works well with Ring&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/weavejester/hiccup"&gt;Hiccup&lt;/a&gt; - DSL for building HTML pages/snippets in Clojure&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://clojure-liberator.github.io/liberator/"&gt;Liberator&lt;/a&gt; - library for building RESTful web apps.&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/clojure/clojurescript"&gt;ClojureScript&lt;/a&gt; - I decided to build the front end interactions with ClojureScript and a few libraries,&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://facebook.github.io/react/"&gt;react.js&lt;/a&gt; - shiny new JS framework for building UIs&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://holmsand.github.io/cloact/"&gt;cloact&lt;/a&gt; - a clojurescript-ified library on top of react.js&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/yogthos/cljs-ajax"&gt;cljs-ajax&lt;/a&gt; - a neater ajax library&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Leiningen &amp;amp; Plugins&lt;/h3&gt;&lt;p&gt;Lein appears to be the defacto build tool for Clojure projects. Yes, you can use Maven or Gradle or whatever but there are certain benefits to using Lein.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Its written with Clojure projects in mind so fits better&lt;/li&gt;
  &lt;li&gt;The build file &lt;code&gt;project.clj&lt;/code&gt; is just a Clojure data structure and incredibly easy to grok when in the Clojure mindset&lt;/li&gt;
  &lt;li&gt;It feels, in my limited exposure, really simple. Dependency resolution, adding new plugins, using custom templates or generators to structure new apps - all of it "just works". Admitedly I didn't do an awful lot with it but having done plenty of work with &lt;code&gt;SBT&lt;/code&gt; and Maven it was incredibly simple but no less powerful to use.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;&lt;a href="https://github.com/emezeske/lein-cljsbuild"&gt;lein-cljsbuild&lt;/a&gt; feel essential when working with ClojureScript. It seems to be the only tools that transpiles ClojureScript in JavaScript.&lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/weavejester/lein-ring"&gt;lein-ring&lt;/a&gt; is nice for getting up and running quickly but having just took a stab at deploying my app to &lt;a href="http://www.heroku.com"&gt;Heroku&lt;/a&gt; it appears to be a bit troublesome down the line (though most likely down to my inexperience).&lt;/p&gt;&lt;h3&gt;Ring, Compojure &amp;amp; Hiccup&lt;/h3&gt;&lt;p&gt;I'd not be suprised to find that this trio underpinned most Clojure based web apps. Seems like a brilliant combination that works really well together. While my app doesn't use ring directly it's no less essential and as your app grows you'll invariably find yourself adding ring middleware directly to customise your app. Compojure is excellent for succintly expressing the routes in your app.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defroutes&lt;/span&gt; &lt;span class="nv"&gt;app-routes&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;GET&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nv"&gt;views/index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ANY&lt;/span&gt; &lt;span class="s"&gt;"/questions"&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="nv"&gt;resources/questions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;route/resources&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;route/not-found&lt;/span&gt; &lt;span class="s"&gt;"Not Found"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In this small sample alone I'm using the &lt;code&gt;defroutes&lt;/code&gt; macro, &lt;code&gt;GET&lt;/code&gt; &amp;amp; &lt;code&gt;ANY&lt;/code&gt; verbs, the &lt;code&gt;route/resources&lt;/code&gt; handler for serving static assets and the &lt;code&gt;route/not-found&lt;/code&gt; handler for dealing with 404's in a custom way. Even with my novice mind this was incredibly easy to construct and easy to read. These routes are then turned into just another bit of ring middleware and added to the stack,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;app&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;handler/site&lt;/span&gt; &lt;span class="nv"&gt;app-routes&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;app&lt;/code&gt; here referes back to the configuration in my &lt;code&gt;project.clj&lt;/code&gt; that the &lt;code&gt;lein-ring&lt;/code&gt; plugin uses to mount our application in the web server&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="ss"&gt;:ring&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:handler&lt;/span&gt; &lt;span class="nv"&gt;depression-test.core/app&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;handler/site&lt;/code&gt; part simply wraps our routes in more ring middleware that are common to web sites (e.g. middleware for extracting HTTP post form parameters etc.)&lt;/p&gt;&lt;p&gt;Finally hiccup gives use a Cloure DSL for building HTML blocks and pages. The &lt;code&gt;index&lt;/code&gt; view is built with it,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nb"&gt;index &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;html5&lt;/span&gt;
             &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:head&lt;/span&gt;
              &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:title&lt;/span&gt; &lt;span class="s"&gt;"depression-test"&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
             &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:body&lt;/span&gt;
              &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:div#stage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
              &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;include-js&lt;/span&gt; &lt;span class="s"&gt;"/app/app.js"&lt;/span&gt;&lt;span class="p"&gt;)]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;While this page is essentially static an culd easily be represented as an embedded resource you can see that this sort of expressive DSL allows us to dynamically construct views because, as is incredibly common in Clojure, it's just data.&lt;/p&gt;&lt;h3&gt;Liberator&lt;/h3&gt;&lt;p&gt;The inclusion of Liberator here was overkill but it was extracted from a slightly larger project I started at the same time as this. Liberator sits on to top of ring and focuses on allowing you to build RESTful APIs using a ridiculously easy to reason about structure. Usually when I work with frameworks or libraries that attempt to model RESTful APIs in a generic way theyend up being incredibly complex. Liberator is quite the opposite,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;defresource&lt;/span&gt; &lt;span class="nv"&gt;questions&lt;/span&gt;
  &lt;span class="c1"&gt;;; return all the people in the application in json format&lt;/span&gt;
  &lt;span class="ss"&gt;:available-media-types&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="ss"&gt;:handle-ok&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;_&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="nv"&gt;data/questions&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;defresource&lt;/code&gt; is a macro used to generate a resource that can be used in a route. In fact my &lt;code&gt;(ANY "/questions" [] resources/questions)&lt;/code&gt; route is basically taking any request to the &lt;code&gt;/questions&lt;/code&gt; URI and letting liberator deal with it. Our resource definition simply specifies that this endpoint can deal with JSON media types and will coerce our &lt;code&gt;questions&lt;/code&gt; list into a JSON response. It does this thanks to a bunch of defaults and an incredibly predicatable decision tree. If you want to accept different verbs, handle authentication etc. the approach to doing it is straight forward. Liberator have went to the effort of mapping the decision required to the turn our above resource into a JSON reponse via the &lt;a href="http://clojure-liberator.github.io/liberator/assets/img/decision-graph.svg"&gt;mind boggilingly awesome decision graph&lt;/a&gt; which will show you what hooks and behaviours you can work with to make the magic happen in your RESTful API. I feel Liberator is one of those important technologies. I've built a lot of APIs in recent times and the sheer "obviousness" of Liberator is the polar opposite of the unpredictable magic in many other frameworks.&lt;/p&gt;&lt;h3&gt;React.js&lt;/h3&gt;&lt;p&gt;Everyone is talking about react.js - the new hotness from Facebook. I'll not dwell to much on it. I recommend to you check out &lt;a href="http://www.youtube.com/watch?v=x7cQ3mrcKaY"&gt;this video&lt;/a&gt; around the design decisions of react and how its avoiding the usual terribleness of two-way data binding and state management. While my resultant app doesn't use react directly I did originally write the UI in Javascript/React before re-writing it in ClojureScript/cloact.&lt;/p&gt;&lt;h3&gt;Cloact&lt;/h3&gt;&lt;p&gt;One of many ClojureScript wrappers around React. This one appeared the most straight forward. It uses it's own &lt;code&gt;atom&lt;/code&gt; implementation to manage state and change propogation&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;ns &lt;/span&gt;&lt;span class="nv"&gt;depression-test.core&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:require&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;cloact.core&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="nv"&gt;cloact&lt;/span&gt; &lt;span class="ss"&gt;:refer&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;atom&lt;/span&gt;&lt;span class="p"&gt;]]))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;def &lt;/span&gt;&lt;span class="nv"&gt;selections&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atom&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;span class="nv"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;swap!&lt;/span&gt; &lt;span class="nv"&gt;selections&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;_&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;By declaring &lt;code&gt;selections&lt;/code&gt; as a cloact &lt;code&gt;atom&lt;/code&gt; then later using the atom api (&lt;code&gt;swap!&lt;/code&gt;) to change its value any cloact component that uses the selections atom will get re-rendered throught the usual react method. The &lt;code&gt;render-component&lt;/code&gt; method of cloact kicks everything into action,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cloact/render-component&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:div.questions&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:h1&lt;/span&gt; &lt;span class="s"&gt;"Could you be depressed?"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:p&lt;/span&gt; &lt;span class="s"&gt;"This test will help you to assess whether you could be suffering from depression."&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:p&lt;/span&gt; &lt;span class="s"&gt;"Answer the questions based on how you've been feeling during the last two weeks."&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;score&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;.-body&lt;/span&gt; &lt;span class="nv"&gt;js/document&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;cljs-ajax&lt;/h3&gt;&lt;p&gt;Nice little shim over the normal ClojureScript ajax stuff. Not much else to say,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="clojure"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;defn &lt;/span&gt;&lt;span class="nv"&gt;load-questions&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;GET&lt;/span&gt; &lt;span class="s"&gt;"/questions"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:response-format&lt;/span&gt; &lt;span class="ss"&gt;:json&lt;/span&gt;
                     &lt;span class="ss"&gt;:keywords?&lt;/span&gt; &lt;span class="nv"&gt;true&lt;/span&gt;
                     &lt;span class="ss"&gt;:handler&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;callback&lt;/span&gt; &lt;span class="nv"&gt;%&lt;/span&gt;&lt;span class="p"&gt;)}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;IDE/Editor&lt;/h2&gt;&lt;p&gt;I ended up writing most of the code in Sublime Text 2. I've never quite got my Vim setup right on this new machine and I've no clue about Emacs and ST2 did the job. I installed a few plugins to make working with a LISP a bit nicer.&lt;/p&gt;&lt;h3&gt;&lt;a href="https://github.com/wuub/SublimeREPL"&gt;SublimeREPL&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Interactive REPL for Sublime that supports, among many other things, Clojure and ClojureScript. The REPL is where you end up doing most of the work in Clojure eventually pasting the same code into a file when you are done. SublimeREPL supports leiningen so you have access to your dependencies and app code when working in the REPL.&lt;/p&gt;&lt;h3&gt;&lt;a href="https://github.com/odyssomay/paredit"&gt;Paredit&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Paredit mode comes from Emacs and makes working with LISPs parenthesis a bit easier by making sure you can unbalance your braces.&lt;/p&gt;&lt;h3&gt;&lt;a href="https://github.com/odyssomay/sublime-lispindent"&gt;lispindent&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Provides more lispified logic for dealing with indenting.&lt;/p&gt;&lt;p&gt;All of these are installed via the Sublime Text package manager.&lt;/p&gt;&lt;h2&gt;Thoughts &amp;amp; Comments&lt;/h2&gt;&lt;h3&gt;Clojure&lt;/h3&gt;&lt;p&gt;Clojure is a LISP and immediatley feels different to every other language I commonly use, but regardless, when you actually work with it there is a certain feeling of "naturalness" about the whole thing. The homoiconic nature, where "everything is just data" means that solving problems is suprisingly intuative and this applies to using libraries and frameworks. Most things are just lists and maps and you can inspect these things easily in the REPL. The amount of experimental spikes that work more or less first time is incredibly high and the final code is usually very terse. I'm by no means an expert, not even "average" at Clojure but it's a joy to use thanks to its well thought out foundation.&lt;/p&gt;&lt;p&gt;Another thing I've noticed is that Clojure libs dont suffer from the same API volatility that you see in likes of Node and Ruby. For example some popular libraries haven't had commits in 7 months - and it's not because they have stagnated or been neglected - it's because they're done. IMHO thats the sign of an incredibly mature, design concious community.&lt;/p&gt;&lt;p&gt;That said - some documentation leaves a lot to be desired if it even exists. I did often find myself searching to try and find what I wanted to know and usually resulted in just reading the code.&lt;/p&gt;&lt;h3&gt;ClojureScript&lt;/h3&gt;&lt;p&gt;I enjoyed working with ClojureScript but I didn't quite get my workflow right. ClojureScript takes a while to compile but this can be automated. Problem was I couldn't get this automation to work with &lt;code&gt;lein-ring&lt;/code&gt; and so I found my self waiting ~20 seconds to try each change - it's cool though, I write Scala regularly. That said ClojureScript intrigues me more than any of the other JS transpilers around - I very quickly stopped seeing the point of CoffeeScript for example. The fact it works well with node.js (ClojureScript all the way down) is equally interesting.&lt;/p&gt;&lt;h2&gt;Onward&lt;/h2&gt;&lt;p&gt;Clojure, which was/is, very new to me helped me put together a simple application rather quickly as to how it stands up against larger projects remains to be seen. I'm going to try and see if I can improve my ClojureScript workflow as its a rather compelling technology but right now its a bit slow to use (e.g. I got better results using &lt;a href="https://github.com/swannodette/mies"&gt;meis&lt;/a&gt; and &lt;code&gt;lein cljsbuild auto&lt;/code&gt; but that skipped the server stuff). I also need to re-work this app to work on heroku without sacrificing the ease of &lt;code&gt;lein ring server&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Its worth caveating that the &lt;a href="github.com/kouphax/depression-test"&gt;code&lt;/a&gt; should in no way be assumed to be best practise, in fact its probably awful.&lt;/p&gt;</content></entry><entry><title>Convenient Colouring of Console Output in Scala</title><updated>Mon Jan 06 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/01/06/convenient-colouring-of-console-output-in-scala/"></link><id>urn:yobriefca-se:feed:post:Convenient Colouring of Console Output in Scala</id><content type="html">&lt;p&gt;If you're writing console based applications or anything that may write meaningful information out to a console/terminal then you should probably consider making the output less bland. It's not just because people using your software like shiny things but it helps identify the severity or context of certain output. Look at tools like &lt;a href="http://gruntjs.com/"&gt;grunt&lt;/a&gt; or any of the modern test frameworks compared to &lt;a href="http://maven.apache.org/"&gt;maven&lt;/a&gt;. For a start they are much less noisy, maven is a bit of a chatty cathy, but they also make clever use of colours - red for bad things, green for good etc. This is something that maven could do with, discerning useful information out of the monochrome wall of text that maven spews out is a slow task.&lt;/p&gt;&lt;p&gt;In a recent Scala project this was something that I needed to do and found the default way of doing it rather noisy. For example writing out a red line and resetting the output requires this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${Console.RED}[ERROR] "&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${Console.WHITE}$errorMsg"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Thats just to spit out a tiny bit of red, imagine what this looks like for more complex examples. So to tidy this up a bit I created an implicit class to add some colour features to strings (I come from a C# background and always liked &lt;strong&gt;sensible&lt;/strong&gt; use of &lt;a href="http://msdn.microsoft.com/en-us//library/bb383977.aspx"&gt;extension methods&lt;/a&gt;, and therefore implicit classes).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConsoleColorise&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AnyVal&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;Console._&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;black&lt;/span&gt;     &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$BLACK$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;red&lt;/span&gt;       &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$RED$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;green&lt;/span&gt;     &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$GREEN$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;yellow&lt;/span&gt;    &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$YELLOW$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;blue&lt;/span&gt;      &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$BLUE$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;magenta&lt;/span&gt;   &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$MAGENTA$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;cyan&lt;/span&gt;      &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$CYAN$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;white&lt;/span&gt;     &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$WHITE$str"&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;blackBg&lt;/span&gt;   &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$BLACK_B$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;redBg&lt;/span&gt;     &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$RED_B$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;greenBg&lt;/span&gt;   &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$GREEN_B$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;yellowBg&lt;/span&gt;  &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$YELLOW_B$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;blueBg&lt;/span&gt;    &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$BLUE_B$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;magentaBg&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$MAGENTA_B$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;cyanBg&lt;/span&gt;    &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$CYAN_B$str"&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;whiteBg&lt;/span&gt;   &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"$WHITE_B$str"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can use this class in your code by importing it and then,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[ERROR] "&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;red&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;errorMsg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;white&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can see it in action below,&lt;/p&gt;
&lt;script type="text/javascript" src="http://asciinema.org/a/7084.js" id="asciicast-7084" async="async"&gt;&lt;/script&gt;</content></entry><entry><title>Continuous Delivery &amp; Boy Scouting Your Deployment Pipeline</title><updated>Sun Jan 05 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/01/05/continuous-delivery-and-boy-scouting-your-deployment-pipeline/"></link><id>urn:yobriefca-se:feed:post:Continuous Delivery &amp; Boy Scouting Your Deployment Pipeline</id><content type="html">&lt;p&gt;Talking to teams and stakeholders within organisations over the last few years I've seen very strong concerns that &lt;a href="http://en.wikipedia.org/wiki/Continuous_delivery"&gt;continuous delivery&lt;/a&gt; was not for them. Usually it was because there was already a process in place - a process that involved lead times in the weeks or months, a lot of repeated manual work and many phases of QA prior to any release being deployed by some third party vendor. They worry, especially, that they would have very little chance of automating everything in the process.&lt;/p&gt;&lt;h2&gt;Throwing the baby out with the bath water&lt;/h2&gt;&lt;p&gt;The goal of continuous delivery isn't actually about automating everything (it just so happens that in many cases automation has significant benefits). Continuous delivery is about reducing the time to deliver a single change to production so to establish a tighter feedback loop between your company or product and your users. &lt;/p&gt;&lt;p&gt;However a lot of the time taken to deploy a release may well be out of your control, usually for the reasons quoted above. The thing is though, things change. It takes time but things always change. To quote Dr. Ian Malcolm - "&lt;a href="http://www.youtube.com/watch?v=SkWeMvrNiOM"&gt;Life finds a way&lt;/a&gt;". For example - your QA department might well suddenly discover the joys of automating a lot of their manual regression tests, your service management contract may expire and some new executive might suggest you try this new fangled "agile cloud as a service stuff" on the back of promises like 10x productivity&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. There are lots of reasons, and most of them a blessing in disguise even if they are a bit ill-informed. &lt;strong&gt;Being ready allows you to capitalise on this organistational change&lt;/strong&gt; and make it last.&lt;/p&gt;&lt;h2&gt;Visualise your Pipeline&lt;/h2&gt;&lt;p&gt;Draw a diagram or outline the steps that a change or single commit goes through to get onto a production box. This isn't done enough in projects, there are fragments of it in peoples heads and there are foggy areas of uncertainty. Drawing a pipeline, one that covers multiple deparments is actually quite an eye opener. &lt;/p&gt;&lt;p&gt;Another thing that this task does is help you realise that deployment isn't a single big black box that is always as slow as the slowest step. Its a series of steps and by visualising it you can simulate changes to it. That 1/2 day it takes to deploy to a preview environment for developer sanity checking may pale in comparison to the 4 week period that you wait to see if the production deployment was successful but bringing that 1/2 day down to 10 minutes for a team of, say, 6 developers has a more long term benefit. Even more, if you get to extend the process for deploying to preview into the QA environments at a later date - the work is mostly already done&lt;a name="_2"&gt;&lt;/a&gt;&lt;a href="#2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;So you may not have hugely reduced the time to a &lt;strong&gt;production&lt;/strong&gt; release but you've helped tighten up the development feedback and prepared yourself for a future where these changes can be reused further down the pipeline.&lt;/p&gt;&lt;h2&gt;Boy Scouting&lt;/h2&gt;&lt;p&gt;But a lot of this work is far from trivial and right now it might be hard to sell it to the people holding the purse strings and Gantt charts so what can you do? Well, a few years ago Robert C. Martin (Uncle Bob) suggested that you can &lt;a href="http://programmer.97things.oreilly.com/wiki/index.php/The_Boy_Scout_Rule"&gt;apply the Boy Scout rule to code bases&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The Boy Scouts have a rule: "Always leave the campground cleaner than you found it." If you find a mess on the ground, you clean it up regardless of who might have made the mess. You intentionally improve the environment for the next group of campers.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Applying this rule to a code base means that every check in you should, &lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;tidy up a little bit of extra code,&lt;/li&gt;
  &lt;li&gt;add another useful unit test,&lt;/li&gt;
  &lt;li&gt;refactor a tiny bit of code, or,&lt;/li&gt;
  &lt;li&gt;tidy up a bit of formatting&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Anything to have a positive impact on the quality of code in the project. &lt;/p&gt;&lt;p&gt;I proffer that &lt;strong&gt;the same rule can be applied to your deployment pipeline&lt;/strong&gt;. Things like - introduce a script that starts up a simple vagrant environment, add a little bit of Puppet/Chef/Salt to create a production-like environment on developers machines, automate that file copy/paste process that Steve in Team B does during every release with a script, document the deployment process, publish the deployment pipeline, add a bit more validation to your CI server. Anything that makes the pipeline that tiny bit less painful to flow through.&lt;/p&gt;&lt;p&gt;In time these small incremental changes have a compound positive effect that results in fewer issues with deployment (and on-boarding) and acts as an example to people involved downstream in the deployment pipeline. &lt;/p&gt;&lt;p&gt;Ultimately the technical change required to move towards continuous delivery takes time and effort but it is nothing compared to the cultural and organisational changes. Proactively addressing the technical changes that can be made now can benefit your current process and also prepare you for a more effective delivery model in the future.&lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: Both of these things have happened in recent years to projects I've been involved in.&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="2"&gt;&lt;/a&gt;&lt;a href="#_2"&gt;2&lt;/a&gt;: We haven't even pointed out that this developer deployment is going to be of immediate benefit for on-boarding new team members as well.&lt;/sup&gt;&lt;/p&gt;</content></entry><entry><title>Building Systems: Libraries and Frameworks</title><updated>Sat Jan 04 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/01/04/building-systems-libraries-and-frameworks/"></link><id>urn:yobriefca-se:feed:post:Building Systems: Libraries and Frameworks</id><content type="html">&lt;p&gt;My &lt;a href="http://yobriefca.se/blog/2013/04/29/micro-service-architecture/"&gt;Micro Service Architecture post&lt;/a&gt; has been by far my most popular post and has generated a lot of discussion. A recurring theme across these discussions is that of using small "fit for purpose" libraries or micro frameworks over larger frameworks. So I wanted to capture some of my thoughts on this.&lt;/p&gt;&lt;h2&gt;What is Small/Large?&lt;/h2&gt;&lt;p&gt;My view, as already expressed in the original post, is that smaller frameworks and libraries should be favoured over larger frameworks when building micro services. In fact thats my general rule of thumb when building systems of any scale. But of course this is entirely subjective - what I define as small is probably considerably large in some other peoples views (especially as we are being completely language and platform agnostic here). But there are a few things I consider when deciding if a library/framework is small or large,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;The goal of the library/framework&lt;/strong&gt;. Small things set out to fix a single problem, big things aim to be a panacea. &lt;a href="http://www.mindrot.org/projects/jBCrypt/"&gt;jBcrypt&lt;/a&gt; does strong password hashing, &lt;a href="http://commons.apache.org/"&gt;Apache Commons&lt;/a&gt; does everything. &lt;a href="http://www.playframework.com/documentation/2.0/ScalaAnorm"&gt;ANORM&lt;/a&gt; gives you some simple sugar over JDBC, &lt;a href="http://hibernate.org/"&gt;Hibernate&lt;/a&gt; has its own flipping query language for crying out loud.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Prefer libraries over frameworks&lt;/strong&gt;. The difference being you call libraries whereas frameworks call you - frameworks are Soviet Russia. This definition makes libraries inherently more composable than frameworks. Libraries are often less prone to abstraction distraction (or at the very least their abstractions are rather specific to their goal). &lt;strong&gt;Proper&lt;/strong&gt; usage of libraries allows you to roll your own framework and conventions rather than workaround generic conventions baked into frameworks.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Feature count&lt;/strong&gt;. Kitchen-sink libraries or frameworks are often just full of features you don't need. "Well don't use them then" - ah if it were that simple. The unix philosophy should apply to services and libraries (do one thing and do it well). I mean, Apache Commons? C'mon Really? Why not just use a language with a decent core library instead?&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Useful/Redundant Feature Ratio&lt;/strong&gt;. If your framework/library has more features I don't need than ones I do, or if I need to use a feature I don't need or want just to use the other features - thats big. This is one reason why I consider Spring to be "big". It's entire premise is built around a big IoC container. The way I build systems I simply don't need an IoC container&lt;a name="_1"&gt;&lt;/a&gt;&lt;a href="#1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;. So being forced to use something I don't need to gain other features I can get elsewhere without the overhead - well thats big&lt;a name="_2"&gt;&lt;/a&gt;&lt;a href="#2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Tooling support&lt;/strong&gt;. If your library or framework needs a special IDE or admin application or other 3rd party installs - its big. Not necessarily "bad" but "big".&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Why Small?&lt;/h2&gt;&lt;p&gt;There are a million analogies you can use here, like "I don't buy a taco kit just to get taco shells" but lets try and stick to the reality here. I'm also going to avoid the obvious stuff like "frameworks lead to leaky abstractions", "frameworks lead to compromise or extra work" - there are plenty of articles about that stuff and YMMV anyway.&lt;/p&gt;&lt;p&gt;Firstly there is the obvious point that if you running a large number of micro services on a small number of machines then footprint matters, extra stuff loaded into a services memory allocation that aren't used creates a lot of waste but this doesn't really apply to most small/medium sized systems in reality.&lt;/p&gt;&lt;p&gt;Secondly design. Large frameworks are designed with rather monolithic architectures in mind. Modular service based systems have significantly different design needs that are orthogonal to the feature sets of large frameworks. This leads to questions like "Why not just use Spring for this instead of Dropwizard, I know Spring and you can do the same thing with them?". Ultimately it leads to design decisions that are more suited to monolithic architectures resulting a sort of schizophrenic architecture that becomes difficult to reason about. You'll have to actively work against other peoples design decisions instead of establishing your own.&lt;/p&gt;&lt;p&gt;Finally there is an argument that frameworks reduce the LOC metric which I think is &lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;a terrible metric in the first place, and,&lt;/li&gt;
  &lt;li&gt;just plain wrong.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Just because you're taking other peoples work, neatly tucked away in a jar or package, and writing some of your own code around it doesn't mean you've reduced the LOC metric in any meaningful way. Yeah you've written less but if thats all you care about... mon dieu! &lt;/p&gt;&lt;p&gt;In fact the minute you take on other peoples code you've become responsible for it, you need to have some understanding about it, you need to maintain it (security holes etc.) and one day you'll stepping through that code to understand why things behave the way they do. Working with libraries in this context is actually a lot easier than frameworks, they typically have a much smaller surface area. When it comes to upgrades as well libraries can be significantly easier to manage without affecting other aspects of the system. The quick win you get with frameworks when you start a project will need to be repaid and you better be capable to explaining how your system works - the customers don't care that its a known bug in Rails - it's a bug in the system you delivered to them, you need to fix it. Starting with nothing and knowingly justifying every library inclusion you make will make the process of delivering and maintaining systems much more fluid instead of the promise a huge productivity spike at the start&lt;a name="_3"&gt;&lt;/a&gt;&lt;a href="#3"&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Oh, and don't get me started on the fact that many developers see unused features as a challenge.&lt;/p&gt;&lt;p&gt;I've had much better &lt;strong&gt;long term&lt;/strong&gt; success and predictability by avoiding large frameworks and instead composing solutions using only what we needed even if it requires a bit of extra work up front or convincing. Admittedly a frameworks promise of productivity is a difficult thing to move people away from, especially if they married to the framework or don't really think beyond their own role. &lt;/p&gt;
&lt;hr /&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="1"&gt;&lt;/a&gt;&lt;a href="#_1"&gt;1&lt;/a&gt;: IoC containers are suited to special types of systems - ones that have very large, deep or complex dependency graphs. In other words poorly designed systems.&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="2"&gt;&lt;/a&gt;&lt;a href="#_2"&gt;2&lt;/a&gt;: I know of a project that uses Spring inside their Dropwizard services. It's like people don't realise that you can actually call constructors using &lt;code&gt;new&lt;/code&gt;. Not to mention the fact the &lt;code&gt;initialize()&lt;/code&gt; method in a Dropwizard service is essentially an IoC container. If you're putting a container inside a container you probably need help.&lt;/sup&gt;&lt;/p&gt;&lt;p&gt;&lt;sup&gt;&lt;a name="3"&gt;&lt;/a&gt;&lt;a href="#_3"&gt;3&lt;/a&gt;: This sort of thinking is articulated really well by &lt;a href="http://dan.bodar.com/2012/02/28/crazy-fast-build-times-or-when-10-seconds-starts-to-make-you-nervous/"&gt;Dan Bodart&lt;/a&gt; in his &lt;a href="http://www.infoq.com/presentations/Crazy-Fast-Build-Times-or-When-10-Seconds-Starts-to-Make-You-Nervous"&gt;Crazy Fast Build Times talk&lt;/a&gt;. His main argument is the tightening up of feedback loops but there are plenty of points that apply here too.&lt;/sup&gt;&lt;/p&gt;</content></entry><entry><title>So Long, and Thanks for All the Code</title><updated>Thu Jan 02 00:00:00 UTC 2014</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2014/01/02/so-long-and-thanks-for-all-the-code/"></link><id>urn:yobriefca-se:feed:post:So Long, and Thanks for All the Code</id><content type="html">&lt;p&gt;2014 is going to be a bit different. At the tail end of 2013 I decided to hand in and work out my notice with &lt;a href="http://kainos.com"&gt;Kainos&lt;/a&gt;. So from February onwards I'll be doing a spot of contracting, validating a few business ideas and generally seeing how I can benefit the tech community in and around Belfast as a free agent.&lt;/p&gt;&lt;p&gt;Recently the cost of being a full time employee in a company that is growing rapidly has outweighed the value I've drawn from it and committing to another year wouldn't have been the right decision for me. So, instead, I'll be giving up the warm comforting embrace of full time employment with a company that has given me plenty of support through the years to rough it in the wilds of the tech industry. Hopefully this move will offer me greater flexibility and control in what I do.&lt;/p&gt;&lt;p&gt;As of now it seems I'm going to be pretty busy for the first part of the year but if there is anything commercial or even community related you feel I could help with (this site should tell you &lt;a href="/background"&gt;everything you need to know&lt;/a&gt;) don't hesitate to &lt;a href="mailto://james@yobriefca.se"&gt;contact me&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>LocalOnly Actions in Play!</title><updated>Tue Dec 17 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/12/17/localonly-actions-in-play/"></link><id>urn:yobriefca-se:feed:post:LocalOnly Actions in Play!</id><content type="html">&lt;p&gt;A &lt;code&gt;LocalOnly&lt;/code&gt; action in Play! is essentially an action on a controller that can only be called from the machine this it resides on. This is useful for restricting some endpoints (perhaps ops focused or database seeding &lt;em&gt;tasks&lt;/em&gt;) from being called by all and sundry.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;LocalOnlyAction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;AnyContent&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Action&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;AnyContent&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;Action&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remoteAddress&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="s"&gt;"127.0.0.1"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="s"&gt;"::0"&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;NotFound&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can use this Action wrapper like so,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LocalOnlyAction&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Local Only Action Executed"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This can be useful in the early stages of a project when your design is constantly influx and you need to perform actions on running systems. As your system grows this is perhaps a less favourable appraoch and you should consider moving these sort of tasks out of the application entriely.&lt;/p&gt;</content></entry><entry><title>Activate on Heroku</title><updated>Wed Dec 11 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/12/11/activate-on-heroku/"></link><id>urn:yobriefca-se:feed:post:Activate on Heroku</id><content type="html">&lt;p&gt;&lt;a href="http://activate-framework.org/"&gt;Activate&lt;/a&gt; is an object persistence framework for Scala. Unlike an ORM that openly tries to map DB concepts to objects &lt;a href="http://activate-framework.org/"&gt;Activate&lt;/a&gt; goes up a further level making the persistence aspect almost completely transparent. You work with objects and in doing so persistence happens as a side effect. For example given a class that extends the &lt;code&gt;Entity&lt;/code&gt; trait,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We can create &lt;strong&gt;and persist&lt;/strong&gt; this instance like so&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;transactional&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"James"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Updating it will result in the persisted state of the instance being mutated as well. &lt;a href="http://activate-framework.org/"&gt;Activate&lt;/a&gt; uses the concurrency model of &lt;a href="http://en.wikipedia.org/wiki/Software_transactional_memory"&gt;Software Transactional Memory&lt;/a&gt; at its core. As there is often some discrepencies between the conceptual model the database domain and objects &lt;a href="http://activate-framework.org/"&gt;Activate&lt;/a&gt; uses &lt;code&gt;Dialects&lt;/code&gt; that handle the translation between the two (for example persisting an object with a &lt;code&gt;List&lt;/code&gt; of &lt;code&gt;Int&lt;/code&gt;s in a relational database that doesn't support array columns). In fact persistence is abstracted suitably that it becomes completely pluggable - use a document store like Mongo or a relational store like Postgres. &lt;a href="http://activate-framework.org/"&gt;Activate&lt;/a&gt; attempts to make this irrelevant.&lt;/p&gt;&lt;p&gt;I want to caveat all of this with the fact I'm not an &lt;a href="http://activate-framework.org/"&gt;Activate&lt;/a&gt; expert. I've succesfully used it in small volume projects and it's allowed me to be pretty productive. Which leads me to onto using it with &lt;a href="http://www.heroku.com/"&gt;Heroku&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Adapting Activate for Heroku&lt;/h2&gt;&lt;p&gt;I have used Activate on 2 projects hosted on Heroku recently and I wanted to share a snippet that I wrote for the first one that I've found useful in the second. When you use Activate you need to create a &lt;code&gt;Context&lt;/code&gt; that configures Activate for your application domain. One thing you need to set is the &lt;code&gt;storage&lt;/code&gt; mechanism. On Heroku I used the provided Postgres instance which is made available to your application via the &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable. So in JDBC based applications you need to coerce this into a JDBC connection string. To do this in Activate I used the code below which may come in useful for people wanting to do the same thing.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;PersistenceContext&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ActivateContext&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PooledJdbcRelationalStorage&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;        &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URI&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getenv&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DATABASE_URL"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;jdbcDriver&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"org.postgresql.Driver"&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;       &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getUserInfo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;   &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getUserInfo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;        &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="s"&gt;"jdbc:postgresql://${uri.getHost()}:${uri.getPort()}${uri.getPath()}?ssl=true&amp;amp;sslfactory=org.postgresql.ssl.NonValidatingFactory"&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;dialect&lt;/span&gt;    &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;postgresqlDialect&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;</content></entry><entry><title>Scala for C# Developers</title><updated>Thu Dec 05 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="/presentations/scala-for-c_-developers.pdf"></link><id>urn:yobriefca-se:feed:post:Scala for C# Developers</id><content type="html"></content></entry><entry><title>sbt-tasks - Rails/Rake custom tasks for SBT</title><updated>Wed Nov 27 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/sbt-tasks/"></link><id>urn:yobriefca-se:feed:post:sbt-tasks - Rails/Rake custom tasks for SBT</id><content type="html"></content></entry><entry><title>Tinsmith: Online Scala REPL/Worksheet</title><updated>Sun Nov 17 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/11/17/tinsmith-online-scala-repl-slash-worksheet/"></link><id>urn:yobriefca-se:feed:post:Tinsmith: Online Scala REPL/Worksheet</id><content type="html">&lt;p&gt;I recently released a basic self-hosted, we based Scala REPL called &lt;a href="https://github.com/kouphax/tinsmith"&gt;Tinsmith&lt;/a&gt; (alternatively play with the &lt;a href="http://tinsmith.herokuapp.com"&gt;online demo&lt;/a&gt;). If you've been following Scala news recently you may have noticed a similar project &lt;a href="http://codebrew.io"&gt;codebrew.io&lt;/a&gt; being talked about and in fact &lt;a href="https://github.com/kouphax/tinsmith"&gt;Tinsmith&lt;/a&gt; is heavily built off the same stack.&lt;/p&gt;&lt;p&gt;So why bother doing yet another REPL/Worksheet when one already exists? Well the first spike came into existence because &lt;a href="http://codebrew.io"&gt;codebrew.io&lt;/a&gt; was rather sluggish one day I was using it and I wanted something I could run locally or deploy to a dedicated remote instance when I needed. Then as I had my own instance running there were things I realised I wanted to do differently or didn't need at all and so rather than cloning codebrew I decided to use some common techs to produce a slightly different experience.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;doesn't&lt;/strong&gt; execute code automatically instead you need to explicitly execute code by &lt;code&gt;Alt-Enter&lt;/code&gt; or &lt;code&gt;⌘-Enter&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;doesn't&lt;/strong&gt; have the bells and whistles such as Doc lookup and autocomplete&lt;/li&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;doesn't&lt;/strong&gt; have Github integration (though it did have gist integration at one point but I decided against it)&lt;/li&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;does&lt;/strong&gt; save the code to the querystring to make pages bookmarkable (like &lt;a href="http://jsconsole.com"&gt;jsconsole&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;does&lt;/strong&gt; return the console output as well as the REPL output&lt;/li&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;does&lt;/strong&gt; explicitly include some useful libs (Scala Utils, Scalaz, Guava etc.)&lt;/li&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;does&lt;/strong&gt; run as a single application and can be quickly deployed to Heroku&lt;/li&gt;
  &lt;li&gt;Tinsmith &lt;strong&gt;does&lt;/strong&gt; use WebSockets instead of Ajax&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;This is in no way meant to act as competition to the clearly better &lt;a href="http://codebrew.io"&gt;codebrew.io&lt;/a&gt; - it's simply something I needed quickly and decided other people might benefit from it as well. Afterall it's just the &lt;a href="https://github.com/jedesah/scala-codesheet-api"&gt;scala-codesheet&lt;/a&gt; and &lt;a href="http://codemirror.net/index.html"&gt;codemirror&lt;/a&gt; thrown together with a bit of webby glue.&lt;/p&gt;</content></entry><entry><title>tinsmith - Self Hosted Scala REPL</title><updated>Wed Nov 13 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/tinsmith/"></link><id>urn:yobriefca-se:feed:post:tinsmith - Self Hosted Scala REPL</id><content type="html"></content></entry><entry><title>Play! 2 for Java Pluralsight Course</title><updated>Fri Nov 08 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/11/08/play-2-for-java-pluralsight-course/"></link><id>urn:yobriefca-se:feed:post:Play! 2 for Java Pluralsight Course</id><content type="html">&lt;p&gt;&lt;img src="/images/blog/pluralsight.png" alt="Pluralsight" _="_" /&gt;&lt;/p&gt;&lt;p&gt;A few days ago my &lt;a href="http://pluralsight.com"&gt;Pluralsight&lt;/a&gt; course - &lt;a href="http://pluralsight.com/training/Courses/TableOfContents/play-2-java"&gt;Play! 2 for Java&lt;/a&gt; was published through - you guessed it - &lt;a href="http://pluralsight.com"&gt;Pluralsight&lt;/a&gt;. Its aimed at people new to the framework and is intended to act as a primer for building a simple app.&lt;/p&gt;&lt;p&gt;So if you're a &lt;a href="http://pluralsight.com"&gt;Pluralsight&lt;/a&gt; subscriber or you've snagged one of the free passes that are readily available at conferences and meetups you should certainly checkout my course - a good response will invariably lead to me producing more videos so it's win win, right? But does this mean my old video series is done and dusted? Well no, the approach and delivery between my short information blasts vs Pluralsights more detailed approach are miles apart and honestly doing the course has actually piqued my interest in doing more screencasts soon.&lt;/p&gt;&lt;p&gt;This also explains my blog silence over the last while lets just say weekly travel with 4AM starts and late finishes, 2 kids and a video production does not leave a person much free time. Combine all this with the fact that video editing and mixing is a skill I've yet to develop it all adds up to a rather stressful time. That said &lt;a href="http://pluralsight.com"&gt;Pluralsight&lt;/a&gt; are actually incredibly understanding and patient and if you've got something to share I recommend to contact them.&lt;/p&gt;&lt;p&gt;Hope you enjoy it.&lt;/p&gt;</content></entry><entry><title>Web Summit</title><updated>Fri Nov 08 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/11/08/web-summit/"></link><id>urn:yobriefca-se:feed:post:Web Summit</id><content type="html">&lt;p&gt;Last week, along with 9999 other people, I attended the &lt;a href="http://www.websummit.net/"&gt;Web Summit&lt;/a&gt; in Dublin. The Summit is touted as being Europes answer to &lt;a href="http://sxsw.com/"&gt;SXSW&lt;/a&gt; and honestly it's certainly not my usual type of conference but, hey, when someone offers you a free media pass you don't turn it down. And so this post exists because it's the least I could do.&lt;/p&gt;&lt;p&gt;So was it the European answer to SXSW? Pretty damn close in both spirit and execution. It was only 2 days which is much shorter than the SXSW marathon but still by the end of the second day I was nearly burnt out.&lt;/p&gt;&lt;p&gt;I spent most of my time in the Developer Stage/Tent - a stage curated by Eamon Leonard of &lt;a href="https://www.engineyard.com/"&gt;Engine Yard&lt;/a&gt; and a few other Engine Yarders. Key takeaways for me included -&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;(Micro) Service Architecture&lt;/strong&gt; is here in force. Essentially taking the Unix command philosophy of "Do one thing and do it well" and apply it to service oriented architectures. This leads to resilient self healing systems. In fact &lt;a href="https://twitter.com/chadfowler"&gt;Chad Fowler&lt;/a&gt; made the excellent analogy of systems being like the human body where cells (services) are constantly dying and being replaced yet the whole system continues to run.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Immutability doesn't just apply to code&lt;/strong&gt;. At &lt;a href="http://www.6wunderkinder.com"&gt;Wunderkit/6wunderkinder&lt;/a&gt; they have an excellent story around immutable infrastructure. They practise it so much that their approach to spinning up new machines is to "throw away the SSH keys" so that the only thing you can do is create or destroy machines.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Testing is overrated&lt;/strong&gt; - super contentious but the general arguments seem to be around &lt;em&gt;typical&lt;/em&gt; testing techniques such as unit and integration testing. This idea fits well with MSA because if it is extremely quick to spin up new services or multiple version of the same service then business and operational metrics with automatic rollback to a "last known good version" trumps any form of extensive testing. I'm not saying DON'T TEST but you need to remember unit/integration tests for a volatile service are just extra work. When the service becomes more stable, maybe then think about how you wrap it in a safety net of extra unit tests? I dunno... we could write books on that debate but they've echoed many of the questions I've had myself.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The rest of the conference I floated around, seen many startups, shook MANY MANY hands. The evening events where nothing short of epic. Dame Lane was closed off and Summiters could wander between bars getting bathed in food, drink and music.&lt;/p&gt;&lt;p&gt;As I said at the start this wasn't my typical event as evidenced by the fact I spent most of it geeking out over technical things rather than taking in the Digital Marketing &amp;amp; Cloud stages. It's cool they had something for everyone in a city that I missed (which made it a welcome visit).&lt;/p&gt;&lt;p&gt;So great thanks and applaud go out &lt;a href="https://twitter.com/paddycosgrave"&gt;Paddy Cosgrave&lt;/a&gt; and co. for a well put together event.&lt;/p&gt;</content></entry><entry><title>biomatic - Twitter Bio Generator for Realz!</title><updated>Thu Nov 07 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="http://biomatic.herokuapp.com/"></link><id>urn:yobriefca-se:feed:post:biomatic - Twitter Bio Generator for Realz!</id><content type="html"></content></entry><entry><title>Dropwizard - Production Ready Web Services</title><updated>Wed Nov 06 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="/presentations/dropwizard.pdf"></link><id>urn:yobriefca-se:feed:post:Dropwizard - Production Ready Web Services</id><content type="html"></content></entry><entry><title>Play! 2 for Java (Pluralsight)</title><updated>Fri Nov 01 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="http://pluralsight.com/training/Courses/Description/play-2-java"></link><id>urn:yobriefca-se:feed:post:Play! 2 for Java (Pluralsight)</id><content type="html"></content></entry><entry><title>Better Management Through Agile Teams</title><updated>Thu Oct 17 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/10/17/better-management-through-agile-teams/"></link><id>urn:yobriefca-se:feed:post:Better Management Through Agile Teams</id><content type="html">&lt;p&gt;Wait is waste! Value stream mapping, as &lt;a href="http://en.wikipedia.org/wiki/Value_stream_mapping"&gt;Wikipedia reads&lt;/a&gt;, is a lean manufacturing technique used to analyse and design the flow of materials and information required to bring a product or service to a consumer.&lt;/p&gt;&lt;p&gt;The process of visualising a value stream is valuable for many reasons, one of which is that it can highlight bottlenecks or wait states. A wait state is a period of time in which actors in the value stream are waiting for something to happen, e.g. waiting for another dependent actor to finish their task. Actors in a wait state are typically not being utilised and therefore wasting time and money. Visualising the stream and quantifying the cost of these wait periods can often lead to fundamental organisational change.&lt;/p&gt;&lt;p&gt;In plan based or waterfall delivery wait states are ever present, at least from a single project perspective. All phases have a certain lead time while actors (analysts, developers, ops, testers etc.) start work and feed back more work to the other actors. The larger the phases, the longer that lead time is (within limits of course). Developers develop while testers wait, Testers test while developers wait. So how do you deal with these wait states? Typically speaking a customer isn't going to be to fond of paying for people to twiddle their thumbs. Depending on the period of time the wait state is expected to last you're either going to have to take the hit or repurpose the actors elsewhere. Repurposing is a very common practise - you'll see analysts swoop in at the start of a project write up a big document and slingshot onto the next project, resourcing has be carefully orchestrated so that a developer can roll off one project onto another that is about to start development and of course this all requires that the project goes according to plan - its like a giant game of Jenga. In the middle of a riot.&lt;/p&gt;&lt;p&gt;These practises may help reduce &lt;strong&gt;visible&lt;/strong&gt; wait states but fail totally at solving the root problem - you've simply diverted peoples attention. You've traded off the &lt;strong&gt;measurable&lt;/strong&gt; for the &lt;strong&gt;unmeasuarable&lt;/strong&gt;. You've lost valuable insight by losing your analysts, increased the time taken to deliver features to due ramp up time for new developers and reduced effectiveness of testing because all the testers usually have to go on are out of date documents. Its hard to make a change when you don't realise doing it badly. The other glaring issue here is that the practitioners aren't the ones covering the cost of these problems - its the customer.&lt;/p&gt;&lt;p&gt;So what of agile &amp;amp; lean? The agile/lean model actually tends toward reducing wait states naturally. The model promotes 2 things&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;an "everybody, all at once, from early on" (&lt;a href="http://www.amazon.com/Lean-Architecture-Agile-Software-Development/dp/0470684208"&gt;Lean Architecture&lt;/a&gt;) mentality. This is realised in the form of a team being the primary unit of delivery as opposed to a specialist with the support of a team.&lt;/li&gt;
  &lt;li&gt;Delivery in small batch sizes. Deliver in smaller increments reduces lead time between deliverables. In fact any large constraints that can potentially create lag become immediately apparent and &lt;strong&gt;need&lt;/strong&gt; to be dealt with to facilitate quick return.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The &lt;strong&gt;team&lt;/strong&gt; analyses, the &lt;strong&gt;team&lt;/strong&gt; develops, the &lt;strong&gt;team&lt;/strong&gt; tests, the &lt;strong&gt;team&lt;/strong&gt; deploys, the &lt;strong&gt;team&lt;/strong&gt; doesn't need to wait. Start to finish the people that know the most deliver the solution and they do so by delivering small chunks of features to the customer to support continuous and evolving understanding of the solution. This reduces any potential bottlenecks from knowledge loss or misinterpretation of what the end user wants.&lt;/p&gt;&lt;p&gt;If you're coming from an organisation that is used to cycling siloed roles and specialists (analysts, developers, testers) the transition to a more team focused cross functional model isn't an easy one but the result is a more effective, predictable approach to delivery which benefits both yourself and the customer.&lt;/p&gt;</content></entry><entry><title>On Evangelism...</title><updated>Sun Jul 28 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/07/28/on-evangelism-dot-dot-dot/"></link><id>urn:yobriefca-se:feed:post:On Evangelism...</id><content type="html">&lt;p&gt;So i'm a Developer Evangelist now.&lt;/p&gt;
&lt;p&gt;Officially.&lt;/p&gt;
&lt;p&gt;For &lt;a href="http://kainos.com"&gt;Kainos&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now it might seem a bit odd being a developer evangelist for a company that doesn't have product or service but I strongly believe it's a vital role regardless of company type.  The fact that Kainos now has a Developer Evangelist is a public declaration that we have a duty of care to help grow the technical communities we affect.  It's not to say we didn't do this before but now it's out in the open - we can start setting goals and measuring our successes and failures.  &lt;/p&gt;
&lt;p&gt;I want Kainos to be recognised as a great place to work for people wanting to grow their technical career - not through random word of mouth or shiny recruitment campaigns but by real honest community engagement and commitment to contributing to Open Source.  Initiatives that were once internal by default will be external unless there is good reason (customer confidentiality being a key one).  Doing things in the open not only helps other people but provides us with a fresh perspective on things we've probably taken for granted.  We have so much technical expertise across a huge range of technologies and we want to discover ways of sharing the things we do in a more public forum. &lt;/p&gt;
&lt;p&gt;This is not about sales, marketing or recruitment - all these things are happy side effects if I've done my job correctly.  No this is about showing we genuinely give a damn.  This is about building trust in the developer communities around us.  This is about finding the people in our company that want to speak at conferences or work on open source projects and giving them that support.  This is about people and community.  This is about craft.  I know it's cheesy but I truly believe in the concepts of Software Craftsmanship - it's about genuinely caring about the work you do.&lt;/p&gt;
&lt;p&gt;Of course those that know me will know none of this new to me, hence why I'm the first of many developer evangelists that Kainos will likely empower but the fact I have time and budget to do this stuff means I can ramp up my activities and experiment.  &lt;a href="http://yobriefca.se/blog/2013/07/24/try-things/"&gt;Try Things&lt;/a&gt; is one such experiment and I hope by the year is out to have started something interesting and engaging with it in both Belfast and London.  &lt;/p&gt;
&lt;p&gt;This is only the beginning, here goes nothing...&lt;/p&gt;
&lt;p&gt;&lt;sup&gt;&lt;strong&gt;*&lt;/strong&gt; It's probably worth nothing that while the role is &lt;em&gt;full time&lt;/em&gt; this doesn't mean I immediately distance myself from day to day project work - that would be pointless.  It is simply not possible to extol the virtues of "craft" without practising it.  I'm going to remain delivering projects and livin' the dream.&lt;/sup&gt;&lt;/p&gt;</content></entry><entry><title>Try Things</title><updated>Tue Jul 23 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/07/23/try-things/"></link><id>urn:yobriefca-se:feed:post:Try Things</id><content type="html">&lt;p&gt;I've always been a tinkerer.  I value understanding different things. Even if the net result is "eugh that's a terrible idea". I enjoy challenging my assumptions and opinions because being proven wrong means I've learnt something and that's good.  And while I may never use the language or tech I've tinkered with there may well be principles or practises I can apply to whatever I'm doing presently. Generalism is in my blood.  But tinkering can be a lonely old game, finding like minded people wanting to tinker with the same things with the same availability is a thankless task.&lt;/p&gt;
&lt;p&gt;It's time to try something new - it's time to Try Things.  Try Things is a hands-on meet up format aimed at the technically curious.&lt;/p&gt;
&lt;h2&gt;How It Works&lt;/h2&gt;
&lt;p&gt;&lt;img alt="Try Things Flow" src="/images/blog/trythings.png?1381339182" /&gt;&lt;/p&gt;
&lt;p&gt;Try Things is a relatively informal concept but the general flow of an event is outlined below.  It is a technically focused event so a computer is somewhat essential for attendees.&lt;/p&gt;
&lt;h3&gt;Pre Event&lt;/h3&gt;
&lt;p&gt;First things first you'll need a backlog of topics/suggestions.  Write an app, use &lt;a href="http://trello.com"&gt;Trello&lt;/a&gt;, index cards, suggestion box, email, it doesn't matter really.  Topics can be anything really,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An emerging language like Elixir or Clojure&lt;/li&gt;
&lt;li&gt;A new database technology like RethinkDB or Datomic&lt;/li&gt;
&lt;li&gt;A new build tool like SBT &lt;/li&gt;
&lt;li&gt;An architectural concept like Single Page Applications&lt;/li&gt;
&lt;li&gt;An idea like mobile time tracker&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Kicking off the Event&lt;/h3&gt;
&lt;p&gt;Duration is entirely open but a good 3-4 hour window provides a perfect buffer, though if you are a particularly effective driver then I'm sure 2 hours is great.&lt;/p&gt;
&lt;p&gt;Once people are gathered and settled one of the previously submitted topics are selected at random.  If you want to turn the randomness down a tad you can also use a bit of consensus voting.  First randomly picked topic to get a majority thumbs up is your topic for the event.&lt;/p&gt;
&lt;h3&gt;Challenge Setting&lt;/h3&gt;
&lt;p&gt;Depending on the topic there may be goals, challenges or questions that the participants may have.  Perhaps different people want to look at different aspects of the topic, maybe they want to approach the topic in a different way or perhaps they simply thirst for competition.  Before the main part of the event kicks off people should quickly share what they know about the topic, raise any interesting questions and highlight any learning goals they want to get out of it.  These should be recorded in some manner to allow the participants to reflect back after they've finished hacking.&lt;/p&gt;
&lt;h3&gt;The Hack&lt;/h3&gt;
&lt;p&gt;Once people are happy and if necessary divided into teams everyone gets to the hacking.  Its important to keep the communication open at this point as you don't want people going back into their caves during this time.  This is supposed to be a social thing remember.  A few things to promote the social side,&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Have a vocal facilitator doing the rounds and highlighting interesting discoveries&lt;/li&gt;
&lt;li&gt;Scheduled coffee breaks to allow people to lift their head above the screen and flex their vocal chords&lt;/li&gt;
&lt;li&gt;Team rotation&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Wrap Up&lt;/h3&gt;
&lt;p&gt;Once the hacking has come to some satisfactory conclusion or designated deadline it's time to share what we've learnt.  This can be simple show and tells or checking our progress against the initial objectives/challenges.  &lt;/p&gt;
&lt;p&gt;The end result isn't supposed to be a black and white "yeah I'll use this thing again", it's supposed to be a shared learning experience and provide an avenue for people to work on things they may not get a chance to in their daily work.&lt;/p&gt;
&lt;h2&gt;The Next Step&lt;/h2&gt;
&lt;p&gt;I'm intend to run a few Try Things sessions over the next few months in London and Belfast (dates and locations still TBD) so if this seems like your sort of thing and would be interested in attending, or if you have any suggestions (or maybe you just like the potential of free beer) - please let me know on Twitter (&lt;a href="http://twitter.com/kouphax"&gt;@kouphax&lt;/a&gt;)&lt;/p&gt;</content></entry><entry><title>So Unbecoming</title><updated>Wed Jul 17 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/07/17/so-unbecoming/"></link><id>urn:yobriefca-se:feed:post:So Unbecoming</id><content type="html">&lt;blockquote&gt;&lt;p&gt;The post has been updated to accomodate some of the excellent adivce from &lt;a href="https://twitter.com/rolandkuhn"&gt;Roland Kuhn&lt;/a&gt; via his &lt;a href="https://gist.github.com/rkuhn/6061663"&gt;Gist&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I've been playing around with &lt;a href="http://akka.io/"&gt;Akka&lt;/a&gt; lately for a component I've been working on. The component, a simple remote file sychroniser will essentially batch download files from URLs returned by a remote API call. The specifics aren't important right now but thats the gist. The component is scheduled to run every 3 hours (using the Akka scheduler) but sometimes the entire download process can actually take longer than 3 hours and I don't want to end up thrashing the remote API for little benefit - so I wanted a fail safe to ensure a new download process would only start if the previous one had finished.&lt;/p&gt;&lt;p&gt;So I've got Akka and some kind of co-ordination requirement. There were 3 possible options,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Make use of a global &lt;code&gt;var&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Use Akkas FSM (Finite State Machine), or,&lt;/li&gt;
  &lt;li&gt;Use an Actors &lt;code&gt;become&lt;/code&gt; &amp;amp; &lt;code&gt;unbecome&lt;/code&gt; methods&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;We'll start with some akka system boilerplate (with scheduler) and look at the options&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="c1"&gt;// create the overarching actor system that will manage our application&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;system&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;akka&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ActorSystem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"devtracker"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// import our execution context&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;system.dispatcher&lt;/span&gt;

&lt;span class="c1"&gt;// set up a scheduler to sync the registry&lt;/span&gt;
&lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scheduler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;millis&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// try and start a download&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Along with this we'll have an actor that performs the download and notifies &lt;em&gt;something&lt;/em&gt; when it is finished.&lt;/p&gt;&lt;h2&gt;The Global &lt;code&gt;var&lt;/code&gt;&lt;/h2&gt;&lt;p&gt;Simplest appraoch would be to just use a flag,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="c1"&gt;// create the overarching actor system that will manage our application&lt;/span&gt;
&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;system&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;akka&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ActorSystem&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"devtracker"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// import our execution context&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;system.dispatcher&lt;/span&gt;

&lt;span class="nd"&gt;@volatile&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;isDownloading&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

&lt;span class="c1"&gt;// set up a scheduler to sync the registry&lt;/span&gt;
&lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scheduler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;millis&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;isDownloading&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actorOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Downloader&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nc"&gt;Download&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then somewhere in our &lt;code&gt;Downloader&lt;/code&gt; actor we'd set global isDownloading flag to &lt;code&gt;true&lt;/code&gt; then, once the (synchronous) download process is complete, back to &lt;code&gt;false&lt;/code&gt; again.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Downloader&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Actor&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Download&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isDownloading&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
      &lt;span class="c1"&gt;// perform download&lt;/span&gt;
      &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isDownloading&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Ugh mutable state, am I rite?!?. It's no fun having to manage shared mutable state (even if it is currently just only boolean flag). This is especially when you're using a system that aims to abstract away the concerns of concurrency. &lt;/p&gt;&lt;p&gt;Lets not forget that I've also broken the whole encapsulation of the actor model by explicity accessing &lt;code&gt;Application&lt;/code&gt; and that just seems dirty. Of course you could keep refining this unitl you ended up with an actor that isolated the &lt;code&gt;var&lt;/code&gt; and other actors passed messages via the Actor System (or its Event Bus) but, well, thats already kind of been done for us in Akka.&lt;/p&gt;&lt;h2&gt;The Akka FSM (Finite State Machine)&lt;/h2&gt;&lt;p&gt;The rules we described above are pretty much describing a very basic state machine. The downloading component is either in an &lt;strong&gt;idle&lt;/strong&gt; or &lt;strong&gt;downloading&lt;/strong&gt; state and we can only initiate a download if the component is idling.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/fsm.png" alt="REALLY Finite State Machine" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Akka &lt;a href="http://doc.akka.io/docs/akka/snapshot/scala/fsm.html"&gt;provides&lt;/a&gt; a basic FSM implementation which is essentially an actor with a neat DSL for defining states and transitions. We can model our download case like so,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DownloadCoordinator&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Actor&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;FSM&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;State&lt;/span&gt;, &lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="n"&gt;startWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Go&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;goto&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Downloading&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
 
  &lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Downloading&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Event&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Finish&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;goto&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Idle&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
 
  &lt;span class="n"&gt;onTransition&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Idle&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Downloading&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actorOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Downloader&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nc"&gt;Go&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
 
  &lt;span class="n"&gt;initialize&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The code above tells us that we have 2 states &lt;code&gt;Idle&lt;/code&gt; and &lt;code&gt;Downloading&lt;/code&gt;. When we receive a &lt;code&gt;Go&lt;/code&gt; event (a simple case object) in the Idle state we move into the &lt;code&gt;Downloading&lt;/code&gt; state. When we receive a &lt;code&gt;Finish&lt;/code&gt; event in the &lt;code&gt;Downloading&lt;/code&gt; state we go back to &lt;code&gt;Idle&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Finally, on the transition of &lt;code&gt;Idle&lt;/code&gt; to &lt;code&gt;Downloading&lt;/code&gt; we tell our downloader actor to do its thing.&lt;/p&gt;&lt;p&gt;When the download completes our actor can simply tell it &lt;code&gt;sender&lt;/code&gt; it is finished&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Downloader&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Actor&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Go&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// perform download&lt;/span&gt;
      &lt;span class="n"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nc"&gt;Finish&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We don't need to directly tell the sender but here this is acceptable. The level of decoupling and indirection is entirely up to you.&lt;/p&gt;&lt;p&gt;Finally our scheduler simply asks the co-ordinator to &lt;code&gt;Go&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;coordinator&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;actorOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;DownloadCoordinator&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scheduler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;millis&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;hours&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;coordinator&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nc"&gt;Go&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now if the scheduler ticks while a download is active it just gets ignored (though you can optionally handle it in the &lt;code&gt;whenUnhandled&lt;/code&gt; block of the FSM actor)&lt;/p&gt;&lt;p&gt;I'll come back this implementation near the end of the post.&lt;/p&gt;&lt;h2&gt;Become/Unbecome&lt;/h2&gt;&lt;p&gt;Do you find the FSM implmentation a bit wordy for something that only really has two states? There is a lot of extra stuff going compared to the boolean flag appraoch. Well actually actors are capable of being their own FSM without the need of the FSM trait.&lt;/p&gt;&lt;p&gt;Akkas Actors support a pattern of swapping out the actors message handler for another receiver via the &lt;code&gt;become&lt;/code&gt;/&lt;code&gt;unbecome&lt;/code&gt; methods. We can implement our download co-ordinator with alot les code like so.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Downloader&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Actor&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
 
  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;context._&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="n"&gt;become&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// do nothing&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
 
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;download&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;future&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// perform download&lt;/span&gt;
    &lt;span class="n"&gt;unbecome&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And thats it. This entire thing replaces both the co-ordinator actor and downloader actor. Our scheduler remains roughtly the same except we call this actor instead of our co-ordinator. &lt;/p&gt;&lt;p&gt;So whats happening? Well when we receive &lt;strong&gt;any&lt;/strong&gt; message (for the first time for example) we call &lt;code&gt;download&lt;/code&gt; which begins the download process. Then our actors &lt;code&gt;become&lt;/code&gt;s something else - the thing it becomes is a cold, uncaring machine - doing nothing to any message it gets. When the download completes the &lt;code&gt;unbecome&lt;/code&gt; puts the actor back into its initial (idle) state.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;So what did I personally end up using? I went with the FSM approach. Why? Well I think, even with the basic nature of our needs, the FSM approach is actually more understandable. Comparing my two solutions (which may or may not be good practise, I'm new to Akka) I find the &lt;code&gt;become&lt;/code&gt;/&lt;code&gt;unbecome&lt;/code&gt; approach to be rather cryptic - sure there are less lines to take in but thats a crappy metric to measure quality by. That said reaching for a fully fledged FSM strategy for every situation is probably going to grow out of control - so it may not always be the best case. Know thine weapons.&lt;/p&gt;</content></entry><entry><title>Scala - For Fun &amp; Profit</title><updated>Fri Jun 28 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="/presentations/scala-for-fun-and-profit.pdf"></link><id>urn:yobriefca-se:feed:post:Scala - For Fun &amp; Profit</id><content type="html"></content></entry><entry><title>Elsewhere</title><updated>Wed Jun 05 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/06/05/elsewhere/"></link><id>urn:yobriefca-se:feed:post:Elsewhere</id><content type="html">&lt;p&gt;Oh hey hi, this post is a "personal" post. It doesn't involve any code or analogies to writing software (that I'm aware of) so yeah.....&lt;/p&gt;&lt;p&gt;Many many years ago (well about 9 or 10) I went through a period of "being a singer/song-writer". There was no intention of making money or even playing live - I can barely speak to people I &lt;strong&gt;don't&lt;/strong&gt; know let alone "perform" in front of them (though I can waffle about technical stuff Ad nauseam - oh the irony) - I just felt compelled to record some noise.&lt;/p&gt;&lt;h2&gt;Experiment #1&lt;/h2&gt;&lt;p&gt;The first time I did it was for my sister, a born and bred muso, as a birthday present - she had moved to London a few years ago and I wrote a song about her being always "busy".&lt;/p&gt;&lt;p&gt;&lt;iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F95763666"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;The quality is clearly terrible, the song is clearly terrible, I am clearly terrible. But dear readers - hello - this is me. Was me.&lt;/p&gt;&lt;p&gt;Anyway. I quite liked the process of recording - one maybe, two takes, no editing. I hated hearing my voice and shambling guitar but I did like the fact something existed that I created.&lt;/p&gt;&lt;h2&gt;Experiment #2&lt;/h2&gt;&lt;p&gt;After that I did a little experiment. The idea was this&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Drink for an hour&lt;/li&gt;
  &lt;li&gt;Record an hour of music (one take)&lt;/li&gt;
  &lt;li&gt;Drink for an hour&lt;/li&gt;
  &lt;li&gt;Record an hour of music (one take)&lt;/li&gt;
  &lt;li&gt;5 and 6 and 7 and 8 follow a similar pattern.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;&lt;iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F6460430"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;That was awesome because it was, for me, kind of wild and cool and if I ever put any thought into anything I hate it.&lt;/p&gt;&lt;h2&gt;Experiment #3&lt;/h2&gt;&lt;p&gt;Also out of the previous process I got enough rough recording to do an Extended Play of music I genuinely like to this day.&lt;/p&gt;&lt;p&gt;&lt;iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F6459707"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;This was a cathartic post - I did things, and yeah I do things.&lt;/p&gt;</content></entry><entry><title>Culture Bubbles Up</title><updated>Sat Jun 01 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/06/01/culture-bubbles-up/"></link><id>urn:yobriefca-se:feed:post:Culture Bubbles Up</id><content type="html">&lt;p&gt;As an organisation grows the way by which culture is formed and played out changes. In companies with smaller numbers - culture is more organic, typically people are like-minded and share a similar goal. As a company grows and diversifies (different products, projects or locations) culture becomes more broken down or siloed across the projects or locations. Typically speaking companies of this size share a common structure - top-down whereby key strategic decisions come from the top down.&lt;/p&gt;&lt;p&gt;Culture is different, culture comes from the people that interact with the company the most - the lower levels of the org chart were head count is greater and interaction is more inward. Project teams typically form their own unique cultures but these aren't reflected at a more broader company level and are often short lived as people move around the company. &lt;/p&gt;&lt;p&gt;But culture, at an organisation level, is a key differentiator for a company. Culture defines the type of people that work in your organisation. This isn't news to anyone. Companies know this, but it's how these companies deal with this determines what culture is actually formed.&lt;/p&gt;&lt;p&gt;For IT companies, whose core is that of technical folk, &lt;strong&gt;technical&lt;/strong&gt; culture plays an important role. The right technical culture typically leads to the right technical people. Sure the company BBQs and Christmas Dinners are all well and good but motivated technical people want to learn and share beyond what they get on a project.&lt;/p&gt;&lt;p&gt;In my travels I've come to group companies into three distinct bucket categories when it comes to how they support or react to technical culture,&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/culture.001.png" alt="Culture Support" _="_" /&gt;&lt;/p&gt;&lt;h2&gt;Prohbitive&lt;/h2&gt;&lt;p&gt;Some companies attempt to stimulate technical culture the same way they run their business - by pushing mandates from on high. Forcing culture is doomed from the offset. For example - I know of a large IT company that wanted to hire more developers. The powers that be had heard that developers like hackathons so they decreed that the company shall run an internal hack event to promote their engineering division. It was a marketing ploy and like all marketing work it was handled by marketing. Marketing picked the problem to solve , marketing picked the participants (Solution &amp;amp; Technical Architects that represented the brand) and wrote the story before the event took place. That was the last anyone heard of it - I'm not even sure if the event even happened. To most people that hear this story and hear the name of the company it's of little surprise. &lt;/p&gt;&lt;p&gt;The point is you can't create culture that defies how you allow people to work. &lt;/p&gt;&lt;h2&gt;Permissive&lt;/h2&gt;&lt;p&gt;Better companies are more permissive, they provide financial support and/or time for internal initiatives. This is much better than trying to force anything but it still has its problems. I've already mentioned that culture flows in a different direction to strategic company decisions so being permissive is only part way there. An employee asking for support to run an event can feel like going against the grain and if it happens at all still needs to be justified exactly like all business decisions (whats the return on investment, how do we measure the success etc.). Problem is culture is so fuzzy and unpredictable its hard to come up with an answer that isn't just nonsense. This all creates friction and cultural change is hard enough without extra impediments. &lt;/p&gt;&lt;h2&gt;Proactive&lt;/h2&gt;&lt;p&gt;The best IT companies are not only permissive but senior level people are proactive and open about supporting a &lt;strong&gt;technical&lt;/strong&gt; culture. Rather than the &lt;em&gt;management of culture&lt;/em&gt; that happens in permissive companies, great companies &lt;em&gt;lead culture&lt;/em&gt;. They instil a "go do" attitude in people rather than "ask first". They do this by being proactive in letting people know what they can do, they see culture forming and actively push that up, they allow failure. Rather than seeking funding for something, funds are made available up front. If someone decided to run an impromptu hack session on a new technology after work and expensed a couple of pizzas there would be no questions asked. I'm a firm believer in the old adage of "It's better to seek forgiveness than to ask for permission" and great companies usually are too. When you have a culture that the people directly affected by that culture can control, you'll have happy people. Not only that but you'll have the right type of happy people.&lt;/p&gt;&lt;h2&gt;Personal Experience&lt;/h2&gt;&lt;p&gt;My current employer, who are actively growing, recently published some material to highlight their internal culture and the thing that struck me about this material was that two of our culture differentiators as a company was &lt;strong&gt;hackathons&lt;/strong&gt; and &lt;strong&gt;CodeSlice&lt;/strong&gt; (a monthly out-of-hours tech meet up) and I'm rather proud to admit that both of those initiatives came from myself and a bunch of other ne'er-do-wellers (&lt;a href="http://willhamill.com"&gt;Will Hamill&lt;/a&gt;, &lt;a href="https://twitter.com/jonnyhni"&gt;Jonny Holmes&lt;/a&gt; &amp;amp; &lt;a href="https://twitter.com/lukemcneice"&gt;Luke McNeice&lt;/a&gt;). All it took was a few short emails to our CTO and we were in business. That was 2 years ago and since then various parties have been championing CodeSlice on a near monthly basis (which spawned similar in other locations), ran 4 internal hacks (which also spawned others in other locations), been key organisers at 2 external hack events and had teams attend a number of other hack events (notably the &lt;a href="http://nhshackday.com/"&gt;NHS Hack Days&lt;/a&gt;). You can't buy that sort of enthusiasm but you can nurture it and let it grow.&lt;/p&gt;</content></entry><entry><title>Disciplined Agile Delivery</title><updated>Mon May 27 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/05/27/disciplined-agile-delivery/"></link><id>urn:yobriefca-se:feed:post:Disciplined Agile Delivery</id><content type="html">&lt;p&gt;Thanks to &lt;a href="http://www.liberty-it.co.uk/"&gt;Liberty&lt;/a&gt; I had the opportunity to attend a morning session by &lt;a href="http://www.ambysoft.com/"&gt;Scott Ambler&lt;/a&gt; around &lt;a href="http://www.disciplinedagiledelivery.com/"&gt;Disciplined Agile Delivery&lt;/a&gt;. &lt;/p&gt;&lt;p&gt;The selling point around DAD seems to be that it offers a more complete view of both project and organisational delivery than what Scrum does. The argument being that Scrum gives you a framework within which you can execute project delivery through sprints but doesn't itself address what happens before (where did the product backlog come from) or after (how are we actually releasing the product to production). So Scrum teams start adopting other practises to roll their own, more complete, process. Of course this sort of approach should really be done by "Process Experts" and as most people are not "Process Experts" the resulting process is typically sub-optimised. DAD suggests that rather than starting at the baseline and optimising to the middle, why not just start at the middle?&lt;/p&gt;&lt;p&gt;This is somewhat analogous to the old libraries vs frameworks debate in the technical world. Frameworks make decisions for you and as such are great when venturing into a new technology space but as you become an "expert" in that area the helpfulness of frameworks diminishes and they can become rather restrictive. At this point "experts" begin to craft their own solutions typically utilising a small number of libraries. I like to consider my self well versed in development of solutions and as such I typically avoid big frameworks and favour smaller, more lean bespoke development.&lt;/p&gt;&lt;p&gt;In this sense DAD is a framework, it gives you everything, and in comparison Scrum could be considered a library, giving you a small set of tools and as such I'm inclined to tend toward rolling my own process utilising various methodologies and processes. This has worked for my projects.&lt;/p&gt;&lt;p&gt;But here's the rub - While I may be a "technical expert" (tending toward the use of libraries over frameworks) I am by no means a "Process Expert". My current experiences have been with smaller scale projects and a bespoke process will fit well at that scale without much expert knowledge. We do, however, need to move beyond that; toward proper organisational change and without "Process Experts" the approach of mashing some principles and methodologies together will result in a sub-optimised process tending toward failure.&lt;/p&gt;&lt;p&gt;The process aspect of large scale software delivery is my "new technology space" and as such it may be prudent to investigate a large focused framework until such times as we can adapt and move beyond its limitations. Will it be DAD? Will it be the Scaled Agile Framework? Perhaps it will be like the carefully governed but organically grown Spotify style model. I don't know yet, but I do know its time to start moving toward using agile in the large and I doubt a slapdash approach will be enough until I developed a broader understanding into larger organisational challenges.&lt;/p&gt;</content></entry><entry><title>trello-printer - Print cards from Trello boards</title><updated>Fri May 24 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/trello-printer/"></link><id>urn:yobriefca-se:feed:post:trello-printer - Print cards from Trello boards</id><content type="html"></content></entry><entry><title>The Old "Agile is for Developers" Myth</title><updated>Tue May 07 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/05/07/the-old-agile-is-for-developers-myth/"></link><id>urn:yobriefca-se:feed:post:The Old "Agile is for Developers" Myth</id><content type="html">&lt;p&gt;There is a common misconception among people who are just starting out on the agile path that the agile model favors developers and downplays other roles. While it is true that agile puts a greater focus and added responsibility on developers it also broadens the definition of developer from the niche of "people who write code" role to a more umbrella term of "people who deliver software".&lt;/p&gt;&lt;p&gt;Another comment I heard being made, admittedly in jest, is that agile is "Disneyland for Developers" and honestly this couldn't be further from the truth. In fact agile methods typically expect people involved in delivery to be MORE responsible for delivery. Developers are no longer just mindless code monkeys banging out tasks against pseudo-code written in a document. No, developers are expected to &lt;strong&gt;DEVELOP a working solution&lt;/strong&gt;. This includes analysis, design, architecture, development &amp;amp; testing. All of this is expected to be done with one eye on the "big picture", they are expected to understand WHY they are doing something from, both, a technical and business perspective, they are expected to be at least understand how their code is deployed and often expected to be able to deploy it. You don't have &lt;em&gt;the Java guys&lt;/em&gt;, &lt;em&gt;the Web guys&lt;/em&gt; or &lt;em&gt;the DBAs&lt;/em&gt; - no, a single person in a team should at least be capable of driving out a vertical slice of functionality from infrastructure to database to services to HTML and JavaScript. Of course the slice doesn't have to be, nor will be, perfect right now. There will always be space for specialists to come in and make their areas better, more secure, faster, whatever needs done. But at the very least when all but one person on the team catches some rare exotic disease on a team bonding night out that person can still deliver &lt;strong&gt;valuable&lt;/strong&gt; working software until the rest of the team recovers.&lt;/p&gt;&lt;p&gt;Ultimately though the agile methods that are technically focused are about ensuring quality of delivery. Delivered features should be &lt;strong&gt;"done"&lt;/strong&gt; rather than thrown together an handed over a test team to let them find the issues - a common side effect of time constrained, fixed scope, phased delivery. That practice is unacceptable even in plan based or phased models and regardless of the project approach a development team can and should be making use of practices often associated with agile.&lt;/p&gt;&lt;p&gt;But other roles necessary in the delivery of a project also benefit greatly from agile approaches. Just think of some of the things these other roles would do within more traditional project delivery models,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;Who has to spend days or even weeks manually running the same few hundred scripted tests every few months to avoid regressions and assert correctness of the system?&lt;/em&gt; Not the developers thats for sure. Testers are often required to execute manual scripted tests which are slow, prone to error and have a relatively low ROI compared to other testing techniques. Manual tests check for issues you know will break your system, but its the unknown issues that typically kill your solution. Automation of these tests (by developers and testers) gives faster more frequent feedback and allows testers to actually do interesting, rewarding, more valuable testing such as Exploratory Testing. Testers should not be there to break things but to ensure we build the right thing right in the first place.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Who spends weeks going through multiple versions of documents that seldom get updated and go out of date shortly after sign off?&lt;/em&gt; Again, not typically the developers. Business Analysts and Solution Architects spend a long time producing documents that create a contract for all parties involved. Often, though, they are only pulled out during "disputes" around agreed functionality (between either developers &amp;amp; tester or customers &amp;amp; analysts). If that happens though, then who is actually reading these things properly? No matter how rigorous you make documents there will always be the problem of interpretation. You can't misinterpret a running solution.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Who is expected to "manage expectations" when delivery is slipping beyond the scheduled dates?&lt;/em&gt;. Long running projects that pin all their plans on up front estimates will slip. It's inevitable unless you've managed to overestimate everything but then I'd wonder why you are doing the work and not your more aggressive competitor. When this happens it's typically the job of the manager and senior technical folks to inform the customer in the best way possible, to "manage their expectations" (shudder). No one wants to do that. In typical agile models visibility is a key differentiator (another way to ensure quick and frequent feedback) which removes the need to for big bang disappointment. When things change or slip you know about them much quicker and can course adjust, the impact is significantly reduced.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;All these (arguably wasteful) practices are typically the responsibility of people in non-developer roles. Now that we have reduced or removed these practices does that mean we've made the roles redundant? Not at all - as with the developer role increasing in scope so does the manager, analyst and tester. Testers no longer just "test", they have a more important function - assuring that what they test is the right thing, they've become crucial to analysis. Analysts also need to understand aspects of project management and managers themselves can move to a more hands-off sales and account management role as delivery is managed by the team itself. What we will see is a reduction of specific titles on projects, most people no longer fit those niche titles.&lt;/p&gt;&lt;h2&gt;Facilitators &amp;amp; Implementors&lt;/h2&gt;&lt;p&gt;So, yes, agile approaches certainly favour working software over volumes of documentation describing what the system &lt;strong&gt;should&lt;/strong&gt; do but heck - so does common sense. &lt;/p&gt;&lt;p&gt;Agile approaches flatten the role structure across the delivery of a project. You end up with umbrella terms like facilitators (who ensure the right thing is being built) and implementors (who ensure the thing is being built right). &lt;/p&gt;&lt;p&gt;You go from this,&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/traditional-view.png" alt="Traditional View" _="_" /&gt;&lt;/p&gt;&lt;p&gt;To something more like this,&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/agile-view.png" alt="High-level Agile View" _="_" /&gt;&lt;/p&gt;&lt;p&gt;If you wanted to map these intentionally nebulous terms back to typical roles in a more traditional model you have,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Facilitators&lt;/strong&gt; - Business Analysts, Solution Architects, Customers, Stakeholders, Users, Testers, Project Managers, Legal etc.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Implementors&lt;/strong&gt; - Developers, Testers, DBAs, Designers&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Its clear to see where the commonplace agile roles fall as well - Product Owner and Scrum Master would be &lt;strong&gt;facilitators&lt;/strong&gt; and the team would be the &lt;strong&gt;implementors&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Already, even at this high level you're getting overlaps and in many ways everyone plays their part as both a &lt;strong&gt;facilitator&lt;/strong&gt; and an &lt;strong&gt;implementor&lt;/strong&gt; but its generally obvious what a persons primary function is and you can plan around that accordingly.&lt;/p&gt;&lt;p&gt;You don't need fancy, lofty titles because you have a single purpose. Titles only force attribution of responsibility to particular people, they enforce specialism and defensive attitudes. Titles shouldn't affect the delivery of a solution.&lt;/p&gt;&lt;p&gt;So, no, "agile" is not just for developers, it's for the facilitators, the stakeholder &amp;amp; the implementors, it's an all-encompassing first step on the road to refocusing our goals from following a process to delivering the right thing quickly and painlessly.&lt;/p&gt;</content></entry><entry><title>webbit-metrics - A Metrics based Admin Server for Webbit</title><updated>Thu May 02 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/webbit-metrics/"></link><id>urn:yobriefca-se:feed:post:webbit-metrics - A Metrics based Admin Server for Webbit</id><content type="html"></content></entry><entry><title>Micro Service Architecture</title><updated>Mon Apr 29 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/04/29/micro-service-architecture/"></link><id>urn:yobriefca-se:feed:post:Micro Service Architecture</id><content type="html">&lt;p&gt;Micro Service Architecture is an architectural concept that aims to decouple a solution by decomposing functionality into discrete services. Think of it as applying many of the principles of &lt;a href="http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)"&gt;SOLID&lt;/a&gt; at an architectural level, instead of classes you've got services.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/micro-service-architecture.png" alt="Micro Service Architecture" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Conceptually speaking MSA is not particularly difficult to grasp but in practice it does raise many questions. How do these services communicate? What about latency between services? How do you test the services? How do you detect and respond to failure? How do you manage deployments when you have a bunch of interdependencies? So lets expand on some of these throughout this post and see if MSA really is worth the effort.&lt;/p&gt;&lt;h2&gt;Anatomy of a Micro Service&lt;/h2&gt;&lt;p&gt;First things first what actually &lt;strong&gt;is&lt;/strong&gt; a micro service? Well there really isn't a hard and fast definition but from conversations with various people there seems to be a consensus that a micro service is a simple application that sits around the 10-100 LOC mark. Now I realise that line count is an atrocious way to compare implementations so take what you will from that. But they are small, micro even. This means you're not going to find hundreds of tiny services built on top of large frameworks, it's simply not practical. No, simplicity and lightweightyness &lt;sup&gt;(not a real word)&lt;/sup&gt; is the order of the day here. Small frameworks like &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt;, &lt;a href="https://github.com/webbit/webbit"&gt;Webbit&lt;/a&gt;, &lt;a href="http://twitter.github.io/finagle/"&gt;Finagle&lt;/a&gt; &amp;amp; &lt;a href="http://www.senchalabs.org/connect/"&gt;Connect&lt;/a&gt; do just enough to allow you to wrap your actual code in a thin communication layer.&lt;/p&gt;&lt;p&gt;In terms of a footprint these services will be small, you're potentially going to run a lot of them on the same machine so you don't want to be holding on to memory or resources that you aren't intending to use. Once again simple libraries over large frameworks will win out, you'll also find less of a reliance on 3rd party dependencies.&lt;/p&gt;&lt;p&gt;This decoupling at a service level also offers another interesting option. We've pushed a lot of the old application complexity down to infrastructure level. We are no longer bound to a single stack or language. We can play to the strengths of any stack or language now. It's entirely possible to have a system built out with a myriad of languages and libraries, though as we will touch on later this is a double edged sword.&lt;/p&gt;&lt;p&gt;You're also not going to find any true micro service based architectures that are hosted in an application server, that kinds of defeats the point. To this end micro services self host, they grab a port and listen. This means you'll lose any benefits your typical enterprise application server may bring and your service will need to provide some of the more essential ones (instrumentation, monitoring etc.).&lt;/p&gt;&lt;h2&gt;Communication&lt;/h2&gt;&lt;p&gt;This is an interesting one. How do your services communicate? There really isn't a single answer for this, even in a single solution. The most basic approach across the board would be to expose all services over HTTP and pass JSON back and forth. Service discovery (how one service knows where to find another) can be as simple as putting the endpoint details into a configuration file (or simply hardcoded).&lt;/p&gt;&lt;p&gt;You may discover, in certain circumstances, the cost of serialising and deserialising JSON payloads through an entire &lt;em&gt;transaction&lt;/em&gt; is causing bottlenecks. Perhaps JSON isn't suitable at all (binary formats) and so you'll want to look at different protocols, like &lt;a href="https://code.google.com/p/protobuf/"&gt;Protobuf&lt;/a&gt;&lt;/p&gt;&lt;p&gt;But hardcoded urls kind of imply a certain strict coupling and A-B-C workflow. In a layered application this makes sense but moving into a service based architecture means you're not bound by this (potential) constraint. Some communications between services can be completely decoupled, in fact some services could publish events or data blindly. They can just throw it into the ether (or at least a message bus) and maybe one day some service can come along and start listening. Alternatively maybe parts of your system could operate in a batch/offline process, feeding off a queue and flipping bits many hours later.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/micro-service-architecture-comms.png" alt="Micro Service Architecture Comms" _="_" /&gt;&lt;/p&gt;&lt;p&gt;A micro service approach gives you this kind of flexibility without major architectural rework.&lt;/p&gt;&lt;h2&gt;Monitoring &amp;amp; Metrics&lt;/h2&gt;&lt;p&gt;Components of a monolithic, layered solution don't typically fail silently - it either fails to compile or throws exceptions when something is wrong (unless you go out of your way and silently swallow exceptions like some insane person). In a service based approach a service might drop off the face of the earth and it's very easy for no other service to notice (especially in a pub/sub model). This means it's imperative that services are properly monitored and orchestrated. In fact just knowing a service is alive is seldom good enough. Is it providing enough business benefit? Is it even being used anymore? Is it acting as a bottleneck for reliant transactions?&lt;/p&gt;&lt;p&gt;Monitoring is always important but more so in a service based architecture where failure is often less obvious. Good examples of this from the JVM world are &lt;a href="http://metrics.codahale.com/"&gt;Metrics&lt;/a&gt; and &lt;a href="https://github.com/twitter/ostrich"&gt;Ostrich&lt;/a&gt; these libraries not only allow for collection of metrics but provide a means of integrating and reporting that data into other services like &lt;a href="http://www.nagios.org/"&gt;Nagios&lt;/a&gt; or &lt;a href="http://ganglia.sourceforge.net/"&gt;Ganglia&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Testing&lt;/h2&gt;&lt;p&gt;There is nothing particularly special about testing services in a micro service based system but the point I want to raise here is that you've reduced the need to have a full suite of tests for every service. Because a service does one thing, and should do it well the risk of introducing a system breaking bug is significantly reduced thanks to the natural behavior of a service based system. Now writing tests for the sake of it, or just in case, holds little to no value but still require the same amount of care or attention during refactoring.&lt;/p&gt;&lt;p&gt;I'm not saying &lt;em&gt;don't&lt;/em&gt; test but I am recommending you think before you use the old "No. of Tests" as a value of code quality.&lt;/p&gt;&lt;p&gt;In fact lets go one step further and change the way we test entirely. You've got monitoring in place, at any given time you can know what services are broken and recover. But also why not monitor key business metrics on each service and use them as indicators to know if we've broken something. This is commonly known as a production immune system. If conversions are dropping, or sales have halted then you can make it so your system rolls back to a known stable state and gives you nasty looks until you fix the problem. The same approach can be used for A/B testing and Dark Launching new features and services.&lt;/p&gt;&lt;p&gt;Micro Service Architecture actually makes a whole lot of sense when it comes to ensuring you have a stable system at any scale.&lt;/p&gt;&lt;h2&gt;Applications of Micro Service Architecture&lt;/h2&gt;&lt;p&gt;MSA has a number of interesting applications. Obviously green field solutions of a given size is an obvious use of this approach. Though I say "given size" because the cost incurred at an infrastructure level required to support micro services could outweigh the benefits and, for small systems, a simple web app would be totally acceptable.&lt;/p&gt;&lt;p&gt;Micro Services also have an interesting application in large legacy systems. Working with legacy code is risky at best. For systems that have been running for decades there is a chance there is limited knowledge in how the systems actually works internally. Working with this code can be like working with a house of cards and a simple mistake in one place can have adverse affects elsewhere. These systems are typically mission critical so mistakes can be costly. So just don't touch it. The micro service approach lets you do this. Rather than diving into the legacy code base, write a small service that does what you need (yes you may be duplicating legacy code) and proxy service calls through it (e.g. via nginx/apache etc or purely within code).&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/micro-service-architecture-proxy.png" alt="Micro Service Architecture Proxy" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Now imagine doing this regularly as part of your maintenance and support program. You'll be turning off the old system bit by bit and perhaps eventually be able to turn it off completely.&lt;/p&gt;&lt;h2&gt;Issues&lt;/h2&gt;&lt;p&gt;Micro Service based systems aren't without their problems of course. For one there is a greater need to better understand non-development areas. In days of old before DevOps was a broadly misunderstood marketing term developers would deliver working systems over the wall to operations who would deploy it to production and everything would fail. Of course this was the ops team fault, don't they know anything? Jeez. It was entirely possible for a developer to know only code, running the entire application from their IDE without thinking about how this would work in real life. MSA forces the developers hand to actually be more responsible and actually get involved in scripting of deployments and understanding integration requirements etc.&lt;/p&gt;&lt;p&gt;Another issue that I touched on earlier is the fact, left unchecked you could have hundreds of small services written in hundreds of languages. Simply managing that sort of estate of languages and platforms with their different installations, package managers and requirements can stop a project dead in its tracks.&lt;/p&gt;&lt;p&gt;An anti-pattern has emerged from the MSA approach - often called the Nano-Service architecture. These are systems built on services that are so small that duplication of effort is prevalent and it becomes almost impossible to reason about any particular area of a system easily.&lt;/p&gt;&lt;h2&gt;Macro Post for Micro Services&lt;/h2&gt;&lt;p&gt;The irony of a large wall of text to introduce the concept of micro services is not lost on me but there is lots to consider. Micro Service based systems can produce scalable, fault tolerant systems on fairly low cost hardware. MSA promotes good practices of keeping things lightweight, emerging systems and business monitoring but as with everything should be approached with a rational mindset and solid understanding of what you &lt;strong&gt;need&lt;/strong&gt; to achieve.&lt;/p&gt;</content></entry><entry><title>Rice</title><updated>Sat Apr 20 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/04/20/rice/"></link><id>urn:yobriefca-se:feed:post:Rice</id><content type="html">&lt;p&gt;Rice. A swamp grass that we consume, awesome. But how do you prepare rice? You could,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Buy it in pre-prepared microwave packets&lt;/li&gt;
  &lt;li&gt;Buy a rice cooker&lt;/li&gt;
  &lt;li&gt;Make it yourself using some water and a suacepan&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Theres probably more but that would detract from my carefully constructed metaphor (I'm not drunk, honest). So whats the point? Well lets think about the pros and cons of each.&lt;/p&gt;&lt;h2&gt;Buying In&lt;/h2&gt;&lt;p&gt;You can buy these little pouches of rice that you pop in the microwave for 3 minutes and &lt;strong&gt;bing&lt;/strong&gt; you've got hot steamy rice. Sort of. It's never perfect. The work involved in taking a perishable food stuff and making it "less" perishable, packaging it and shipping it around the world kind of degrades the product a bit. You ultimately have to trade off quality for convenience. You can certainly add ingredients to the rice to rejuvenate it but its never quite same and lets be honest the packet size is always either too big or too small and you end up wasting some. &lt;/p&gt;&lt;h2&gt;Using a Rice Cooker&lt;/h2&gt;&lt;p&gt;Rice cookers typically cook rice really well but they are an investment, you may only use it occasionally, some of them are a pain to clean and you've always got to find space for it in your kitchen (if you had a fancy tool for every type of food you'd need a much bigger kitchen). Plus these things don't really scale very well. They typically make a certain size and if you want more than that you need to start doing it in batches or invest in another rice cooker. &lt;/p&gt;&lt;h2&gt;Make it Yourself&lt;/h2&gt;&lt;p&gt;Rice can be a pain in the ass to cook, it sounds simple. Too little water and it burns and sticks to the bottom of the pan, too much and it boils over leaving starchy water everywhere. You can under-cook it, or wore overcook it into a stogy mess. If you get distracted or try to do too many other things its easy for things to go bad and you'll be left with a mess that needs cleaning up. Then theres the problem of judging the right amount of rice to cook. Eventually though, after a spot of trial and error, cooking rice without fancy tools becomes rather easy and predictable.&lt;/p&gt;&lt;h2&gt;A Carefully Constructed Metaphor&lt;/h2&gt;&lt;p&gt;I did mention previously that this was a metaphor and I wasn't lying. I've been mulling over this analogy for a while (while I was cooking rice for real). In software you've got similar approaches to delivery,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;You can buy an off-the-shelf solution that accomplishes most things&lt;/li&gt;
  &lt;li&gt;You can use a framework with all the bells and whistles&lt;/li&gt;
  &lt;li&gt;You can roll your own solution&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The all have their own trade-offs that align well with the rice metaphor. &lt;/p&gt;&lt;p&gt;Off the shelf products give you a certain level of convenience provided you are willing to live with that you are often compromising user experience to satisfy the view of a "typical user" that the product must adhere to to appeal to wide user base. And yes you can typically add your own ingredients through extra development work but it's never quite right.&lt;/p&gt;&lt;p&gt;By using a large framework you've got accept a certain amount of upfront complexity for the future promise of making the delivery of solutions easier. Much like the rice cooker, frameworks will invariably bring with them a slew of features that you'll never use. However there will always be the need to manage and cope with that extra complexity and often the cost of this complexity (setting up boilerplate for a new feature, writing a bunch of configuration to allow the framework to do it's magic etc.) could be greater than the value a small feature provides. Features users want will be deferred because of decisions they did not have a say in.&lt;/p&gt;&lt;p&gt;Finally the idea of making your own rice, or using a minimal set of common tools to deliver the right amount features. Yes it may seem initially daunting, you'll probably get it wrong a few times (do too much or not enough) but eventually you'll build up enough knowledge and skill that you can see that reliance on heavy abstractions is often more of a burden than a help. While rolling your own may appear as unnecessary risk - taking this approach in small steps is significantly less risky than pinning all your hopes in a frameworks sales pitch or the promise of buy-and-run COTS products. &lt;/p&gt;&lt;p&gt;Building your own stuff has the added future benefit of letting you make more informed decisions. Clearly there is no one right answer for every situation - sometimes you may just benefit from firing up a ready rolled WordPress site, or, perhaps Middleman or Jekyll would fit in well with the publishing strategy of the project, but sometimes a simple &lt;code&gt;Makefile&lt;/code&gt; with a bunch of &lt;code&gt;sed&lt;/code&gt; calls will do the job. &lt;/p&gt;&lt;p&gt;Finally the simple act of actually trying build your own solutions will give you greater clarity when you need to answer questions around build vs buy or technology choices. Just because frameworks, tools and products allow you to be ignorant of many things it doesn't mean you should be.&lt;/p&gt;</content></entry><entry><title>Hardening Sprints: Just Say No?</title><updated>Wed Apr 17 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/04/17/hardening-sprints-just-say-no/"></link><id>urn:yobriefca-se:feed:post:Hardening Sprints: Just Say No?</id><content type="html">&lt;p&gt;I was recently asked by a PM in my company if "hardening sprints" are allowed in agile projects and I gave the very pragmatic answer of "you do what needs to be done". I left the conversation not feeling right though and I've been thinking about the question ever since. To be honest my gut instinct is that, with certain exceptions, these sort of sprints are a smell and their origins are firmly rooted in phased/waterfall delivery. I'll address the &lt;em&gt;"with certain exceptions"&lt;/em&gt; caveat a bit later but lets first dive into the idea of hardening sprints.&lt;/p&gt;&lt;h2&gt;What is a hardening sprint?&lt;/h2&gt;&lt;p&gt;&lt;img src="/images/blog/hardening-sprints.png" alt="We'll Just Squeeze the other bricks in when we're done with the house" _="_" /&gt;&lt;/p&gt;&lt;p&gt;A hardening sprint is a timebox or sprint reserved at the end of a group of sprints (usually prior to release) to allow testing or hardening of the release. This could mean extra testing, different types of testing, refactoring, reviews etc. It brings with it the rather bizarre concept of "Done Done" - i.e. features that have been delivered in previous sprints may be "Done" but not "Done Done". So it may be coded and "kind-of" working and "kind-of" tested and people are "kind-of" happy with it and yeah its "kind-of" done but we'll wait until the end to know if we are really done.&lt;/p&gt;&lt;h2&gt;So What?&lt;/h2&gt;&lt;p&gt;It strikes me as "kind-of" odd that this sort of thing happens without people raising an eyebrow. For one thing there is no such thing as "Done Done" and if we don't admit it I fear that we'll see "Done Done Done" pushing its way into our process vocabulary. &lt;/p&gt;&lt;p&gt;The next thing that strikes me - there is absolutely no data available to predict how long this "hardening" process should take. If we take it at face value the term implies a single sprint. How can we be sure whatever unpredicatable stuff comes up during that sprint can actually be addressed and adequatley resolved in that sprint? Or, taking it to the other extreme (indefinite amount of sprints) how do we know when to stop - afterall nothing is ever perfect.&lt;/p&gt;&lt;h2&gt;Undoing All The Good Work?&lt;/h2&gt;&lt;p&gt;So we've spent X amount of sprints refining our approach to delivery, continually improving and learning and now we've thrown the project into this huge dark pit of uncertainty and "hardening". Surely I'm not alone in thinking this sounds somewhat wrong.&lt;/p&gt;&lt;p&gt;Another risk is that this kind of strucutre will create is a potential reduction in ongoing quality. It could be argued that hardening sprints, much like the old phased/waterfall approach to delivery, removes a certain amount of responsibility from the delivery team. Now there is less of a desire for developers to apply as much rigour to their code as they may have - afterall &lt;em&gt;"the testers will find the bugs so why waste my time being thorough?"&lt;/em&gt;. But if you are deferring your user or penetration testing a few months down the line there is always this notion that the team can just &lt;em&gt;"throw it in and see what comes out in the wash"&lt;/em&gt;. &lt;/p&gt;&lt;h2&gt;"With Certain Exceptions"&lt;/h2&gt;&lt;p&gt;Of course, like every rule, there may be exceptions. One obvious exception is external penetration testing. There can be considerable cost and time assoicated with getting a third party specialist in and its certainly not viable to do it every sprint. So defering a full pen testing cycle until near the release is an acceptable and often necessary exception. But that doesn't mean the team should throw all care about security out the window - the goal of the penetration testing should be to validate that there are no vulnerabilities not to discover them.&lt;/p&gt;&lt;h2&gt;Next Time Gadget... Next Time&lt;/h2&gt;&lt;p&gt;So are "hardening sprints" a good idea? I'm inclined, as negative as it may be, to start with "no" and take it from there, afterall its better to start with discovering &lt;strong&gt;why we can't&lt;/strong&gt; do certain things inside of a sprint rather than assume &lt;strong&gt;we can't&lt;/strong&gt; and carry on ignorant of the potential benefits.&lt;/p&gt;</content></entry><entry><title>Teams or People?</title><updated>Sun Apr 07 23:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/04/07/teams-or-people/"></link><id>urn:yobriefca-se:feed:post:Teams or People?</id><content type="html">&lt;p&gt;A team is much more than a group of people thrown together for the period of a project. This is quite common in a utilisation based business model where downtime is viewed, on the balance sheets at least, as money wasted. I'd call that myopic. &lt;/p&gt;&lt;p&gt;Putting aside other arguments for a moment (downtime is quite often good for allowing motivated people to learn and grow) lets think about &lt;a href="http://en.wikipedia.org/wiki/Tuckman%27s_stages_of_group_development"&gt;Tuckman's Stages of Group Development&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/tuckmans.png" alt="Half Baked Tuckmans Model" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Tuckmans model suggests that teams go through various stages of development, as denoted above. During these phases various things happen,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Forming&lt;/strong&gt; - the team comes together shares their history and begins to develop an understanding of the problem to be solved.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Storming&lt;/strong&gt; - after the gentle introduction people begin finding their place in the team. Dominant parties fight for control, less dominant sniff out their leaders and the power struggle kicks off.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Norming&lt;/strong&gt; - the power struggle is over and people understand their place in the team. Less impediments occur through communication.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Performing&lt;/strong&gt; - the team has gelled and is working effectively, everyone is in the role they need to be and everyone trusts each other.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;There is a final stage - &lt;strong&gt;Adjourning&lt;/strong&gt; where the team, as a collective whole, part ways - but I'll get to that later.&lt;/p&gt;&lt;p&gt;What does the y-axis in this pseudo-graph represent? I guess the obvious answer is team productivity but given the many factors that can affect team productivity/efficacy this isn't a realistic metric. No, the y-axis in this case reflects the teams &lt;strong&gt;focus on their goal&lt;/strong&gt; of delivering the solution. In this situation it makes sense that a "team" become a staple unit of delivery vs the old resource rumble of "whoever is free". Short-lived teams of randomly selected individuals are always going to be operating at sub-optimal levels due to the very nature of team dynamics. Now here's the rub - more often than not this performance is nigh on impossible to quantify. In fact if "people as units of delivery" has been a thing for a while you'll find people making excuses for inefficiencies by front-loading of estimates. What happens then in sales and bids, how can a company who's estimates are derived due to sub-optimal team output remain competitive but keep their process the same? One dubious tactic would be for sales team gently massaging numbers and rates to make them more appealing. This makes the entire delivery process from sales to final delivery of a solution highly inefficient and pressured and we haven't even touched on the evils of potential overselling and micromanaging!&lt;/p&gt;&lt;p&gt;So - the &lt;strong&gt;team a a unit of delivery&lt;/strong&gt; makes sense IMHO, but it's not without it's problems. Moving to a team-based model requires a significant jump in company culture, accounting, sales and, if you'll let me use the word, resourcing. There are potentially huge visible risks in long stretches of downtime for teams. Arguably though large visible, almost predictable risks, are significantly better than unquantifiable consistent loses right? At least you can mitigate them.&lt;/p&gt;&lt;h2&gt;Adjourning&lt;/h2&gt;&lt;p&gt;A team can't stay a team forever. People &lt;a href="http://en.wikipedia.org/wiki/Bus_factor"&gt;get hit buses&lt;/a&gt; or worse get bored and a long lived team that refuses to change is simply another silo of knowledge that methodologies like agile try and crack open.&lt;/p&gt;&lt;p&gt;But &lt;strong&gt;adjouring&lt;/strong&gt; the team isn't about throwing the core team to the wind. Too much team churn and you end up back a square one. Too little team churn and you've got the silo problem. Team members should be rotated to allow knowledge to propagate (inwards and outwards from the original teams perspective). However this clearly brings its own problems (is a person who's constantly rotated really ever part of a team) and the balance needs to be struck over time. &lt;/p&gt;&lt;p&gt;Spotify is decent example for this sort of cross-team knowledge sharing and I'd recommend checking out &lt;a href="https://dl.dropbox.com/u/1018963/Articles/SpotifyScaling.pdf"&gt;Scaling Agile @ Spotify&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;The team as a unit of delivery&lt;/em&gt; is certainly more suited to product based companies or large, long running projects because it feels more natural. In a services based outfit or consultancy house being able to ship people about at will is &lt;em&gt;easier&lt;/em&gt; but thats not to say its actually beneficial. While it brings its own unique challenges team-based resourcing can allow organisations and project to greater realise their true potential.&lt;/p&gt;</content></entry><entry><title>The 'New' Value Triangle</title><updated>Sat Mar 30 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/03/30/the-new-value-triangle/"></link><id>urn:yobriefca-se:feed:post:The 'New' Value Triangle</id><content type="html">&lt;p&gt;&lt;img src="/images/blog/new-value-triangle.png" alt="New Value Triangle" _="_" /&gt;&lt;/p&gt;&lt;p&gt;A projects delivery effectiveness can be affected by many things. There is, however, a baseline trade off on three core values and being aware of this trade off can help you make better decisions. The diagram above highlight these core values,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;New Domain&lt;/strong&gt; - this can be a new problem domain or new challenges within an understood domain. It may also be considered a &lt;em&gt;new domain&lt;/em&gt; if a large proportion of team have limited or no experience in it. (e.g.coming into the health sector for the first time or possibly new legislation coming into effect).&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;New Technology&lt;/strong&gt; - New technology can be either mandated (integration work) or just by virtue of the fact technology is always evolving. There are always new (and potentially) improved ways to solve common problems in solutions.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;New People/Team&lt;/strong&gt; - New people or teams occur when a large proportion of a team has never worked together or are changed between phases. Depending on the type of organisation you work within you may never work with the same person twice or for very short periods of time. This is common within utilisation focused service companies for example.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;The "new" value triangle proposes that, &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Given a new project you must only accept up to a maximum of two of the "new" trade offs. &lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Obviously the less the better but there will invariably be challenges outside of the project that will have an impact on these values. Having to cope with all three simultaneously will seriously impede successful delivery of a project and must be avoided at all costs.&lt;/p&gt;&lt;p&gt;Its worth noting that the term "new" when looking at each trade-off &lt;strong&gt;on its own merit&lt;/strong&gt; isn't necessarily a negative thing. New blood on the team can inject fresh spirit and allow knowledge to propagate beyond the boundary of the team, new technology can speed up delivery of more robust solutions and a new domain can bring considerable business value. But at scale or combined these "new" values can become a hindrance on successful delivery. &lt;/p&gt;&lt;p&gt;Another consideration is that this is a &lt;strong&gt;general rule&lt;/strong&gt;. You are not always going to be in a situation that mandates such trade-offs. Or perhaps you are in a situation that seemingly mandates &lt;strong&gt;all&lt;/strong&gt; of them? It's possible, with careful thought, to slowly transition through all the "new" values over time but at any given point its always wise to keep tabs on what you are trading off to ensure you aren't holding back on the effectiveness of delivery.&lt;/p&gt;&lt;p&gt;So you've got to know when to trade off certain values and when you do what mediation patterns could be applied to prevent unnecessary waste&lt;/p&gt;&lt;h2&gt;Standardise Technology (__NPT/ND__)&lt;/h2&gt;&lt;p&gt;When a team (potentially new to each other) is kicking of a project in a new problem domain (to the team or the organisation at large) there is usually the greatest business risk and as such the team need to be ready for surprises as, and when, they happen.&lt;/p&gt;&lt;p&gt;One of easiest ways to achieve this is to reduce risk on the technology front. Standardising and using the most boring, non-shiny, well understood technologies removes any requirement for the team to learn new paradigms and principles while trying to wrap their collective minds around a new domain and potentially &lt;a href="http://en.wikipedia.org/wiki/Tuckman%27s_stages_of_group_development"&gt;getting up to speed&lt;/a&gt; as an effective team.&lt;/p&gt;&lt;h2&gt;Commoditise Solutions (__NT/NPT__)&lt;/h2&gt;&lt;p&gt;From an organisational perspective when you find yourself consistently in a well known domain there is a risk you can begin producing extremely similar systems time and time again. This is isn't going to look terrible on the balance sheet but if you have a high team churn there is a lot time being wasted in getting people up to speed that could be avoided.&lt;/p&gt;&lt;p&gt;This is probably a very good opportunity to commoditise your solution, turn it into a product and sell it. That way you can establish a baseline consistency via a core team and established technology stack and focus even more on the adding true business value.&lt;/p&gt;&lt;h2&gt;Iterate &amp;amp; Experiment (__ND/NT__)&lt;/h2&gt;&lt;p&gt;Given an established team entering into new domains and technologies the simplest way to reduce risk is to &lt;em&gt;iterate &amp;amp; experiment rapidly&lt;/em&gt;. If the organisation you work within delivers bespoke software its very likely that the work you do will be within a &lt;em&gt;new domain&lt;/em&gt; or at least a &lt;em&gt;new/different subdomain&lt;/em&gt; of a common domain you typically deliver into. When introducing new technology as well it's vital that teams work well and can begin delivering quickly and iteratively to understand both the new domain and any potentially how new technologies fit.&lt;/p&gt;&lt;p&gt;Having a largely consistent team as your unit of delivery allows us to focus on delivering the right tools to tackle the right problems which adds significantly more business value than the other trade-offs. Of course - changes are perfectly acceptable &amp;amp; encouraged as long as they aren't wiping out a large group of the core team in one fell swoop.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;At any point in any project there will be trade-offs. Many of these trade-offs can be boiled down to something similar to this value triangle and knowing strategies for dealing with these situations allows the project to optimise for the most effective approach.&lt;/p&gt;</content></entry><entry><title>Uploading Files with Play! 2 </title><updated>Mon Feb 25 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/play-uploads/"></link><id>urn:yobriefca-se:feed:post:Uploading Files with Play! 2 </id><content type="html">&lt;p&gt;&lt;a href="https://github.com/yobriefcasts/006-play-file-uploads"&gt;Source&lt;/a&gt;&lt;/p&gt;&lt;p&gt;A quick look at how to achieve file uploads with Play! 2 including other form fields and uploading more than one file&lt;/p&gt;</content></entry><entry><title>play-reactivemongo</title><updated>Mon Feb 18 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/play-reactivemongo/"></link><id>urn:yobriefca-se:feed:post:play-reactivemongo</id><content type="html">&lt;p&gt;&lt;a href="https://github.com/yobriefcasts/005-play-reactivemongo"&gt;Source&lt;/a&gt; | &lt;a href="https://github.com/zenexity/Play-ReactiveMongo"&gt;Official Site&lt;/a&gt;&lt;/p&gt;&lt;p&gt;ReactiveMongo - reactive, asynchronous and non-blocking Scala driver for MongoDB.&lt;/p&gt;&lt;p&gt;In this episode we will retrofit the app from episode 003 to use Mongo, ReactiveMongo and the play-reactivemongo plugin.&lt;/p&gt;</content></entry><entry><title>ReactiveMongo</title><updated>Mon Feb 11 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/reactivemongo/"></link><id>urn:yobriefca-se:feed:post:ReactiveMongo</id><content type="html">&lt;p&gt;&lt;a href="https://github.com/yobriefcasts/004-reactivemongo"&gt;Source&lt;/a&gt; | &lt;a href="http://reactivemongo.org"&gt;Official Site&lt;/a&gt;&lt;/p&gt;&lt;p&gt;ReactiveMongo is an asynchronous, non-blocking MongoDB driver written in Scala. This screencast will move quickly through the basic concepts that define ReactiveMongo and then show examples of the various basic operations you'd expect from a MongoDB driver&lt;/p&gt;</content></entry><entry><title>Starter for 10: Play! 2 - Part Three</title><updated>Mon Feb 04 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/starter-for-10-play-2-part-three/"></link><id>urn:yobriefca-se:feed:post:Starter for 10: Play! 2 - Part Three</id><content type="html">&lt;p&gt;&lt;a href="https://github.com/yobriefcasts/003-introducing-play-2/"&gt;Source&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Part three of the Introducing Play! 2 Series. Play! 2 is a Java/Scala framework based on a lightweight, stateless, web-friendly architecture for highly-scalable applications. This episode will introduce us to the concepts of&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Play Forms for binding request data&lt;/li&gt;
  &lt;li&gt;View Helpers, and,&lt;/li&gt;
  &lt;li&gt;Form Validation&lt;/li&gt;
&lt;/ul&gt;</content></entry><entry><title>Starter for 10: Play! 2 - Part Two</title><updated>Sun Jan 27 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/starter-for-10-play-2-part-two/"></link><id>urn:yobriefca-se:feed:post:Starter for 10: Play! 2 - Part Two</id><content type="html">&lt;p&gt;&lt;a href="https://github.com/yobriefcasts/003-introducing-play-2/tree/spike-1"&gt;SOURCE&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Play! 2 is a Java/Scala framework based on a lightweight, stateless, web-friendly architecture for highly-scalable applications. In this episode we will look at&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Creating Controllers &amp;amp; Actions&lt;/li&gt;
  &lt;li&gt;Creating read-only views&lt;/li&gt;
  &lt;li&gt;Routes and URL matching&lt;/li&gt;
&lt;/ul&gt;</content></entry><entry><title>Screencasts with a JVM flavour</title><updated>Mon Jan 21 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2013/01/21/screencasts-with-a-jvm-flavour/"></link><id>urn:yobriefca-se:feed:post:Screencasts with a JVM flavour</id><content type="html">&lt;p&gt;Today sees the inaugural launch of a new project/passion of mine - &lt;a href="http://yobriefcasts.tv"&gt;yobriefcasts.tv&lt;/a&gt;. There is a launch post about it on the &lt;a href="http://yobriefcasts.tv"&gt;site&lt;/a&gt; as well as the first in a series of screencasts around Play! 2 (more being edited as you read this no doubt). Initially I never set out to focus on the world of the JVM but after building a list of potential topics and doing some research there is just so much there and so little audio/visual coverage of it that I think it deserves some attention.&lt;/p&gt;&lt;p&gt;As it stands right now there is an [optional] price associated with the productions - you can stream everything to your hearts content or you can pay a minimal sum to get a high quality download of the screencast - you can do both if you want. It took a while to come to an acceptable price point and structure because I have an end goal here - I want to feed any money earned back into the project to get better equipment and thusly produce better quality screencasts. Futhermore I'd eventually like to turn this into a (mostly) full time gig and move to a subscription model with frequent (weekly) releases - but thats a long way away yet - for now I want to see if I can build something people want and enjoy.&lt;/p&gt;&lt;p&gt;But hopefully the site can speak for itself - I've already got an extensive list of releases coming up over the next while so stay tuned, follow us on &lt;a href="http://twitter.com/yobriefcasts"&gt;twitter as @yobriefcasts&lt;/a&gt; and enjoy the &lt;a href="http://yobriefcasts.tv"&gt;site&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Starter for 10: Play! 2 - Part One</title><updated>Mon Jan 21 00:00:00 UTC 2013</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/starter-for-10-play-2-part-one/"></link><id>urn:yobriefca-se:feed:post:Starter for 10: Play! 2 - Part One</id><content type="html">&lt;p&gt;Play! 2 is a Java/Scala framework based on a lightweight, stateless, web-friendly architecture for highly-scalable applications. In this episode we will look at&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Introduction to Play! 2,&lt;/li&gt;
  &lt;li&gt;Downloading &amp;amp; Installing Play!&lt;/li&gt;
  &lt;li&gt;Using PVM (Play Version Manager) to Install Play!&lt;/li&gt;
  &lt;li&gt;A brief tour of the command line interface.&lt;/li&gt;
&lt;/ul&gt;</content></entry><entry><title>Trello Cards: Materialising Trello Boards</title><updated>Fri Dec 28 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/12/28/trello-cards-materialising-trello-boards/"></link><id>urn:yobriefca-se:feed:post:Trello Cards: Materialising Trello Boards</id><content type="html">&lt;blockquote&gt;&lt;p&gt;TL;DR I ported &lt;a href="https://github.com/psd/pivotal-cards"&gt;pivotal-cards&lt;/a&gt; to &lt;a href="http://yobriefca.se/trello-cards"&gt;trello-cards&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I recently did a spot of work with the awesome and brilliant people of the &lt;a href="http://digital.cabinetoffice.gov.uk/category/gds/"&gt;Government Digital Service&lt;/a&gt; and being big fans of getting stuff done they make use of whiteboards and index cards to visualise the flow of work on the various projects. Thats all well and good but it makes reporting to senior managers, stakeholders and remote people a bit difficult (and for the uber paranoid - what about auditing!!!). To that end this flow of work was also mirrored online - and in GDS's case the tool of choice was usually &lt;a href="http://pivotaltracker.com"&gt;Pivotal Tracker&lt;/a&gt;. In order to bridge the gap between the online and "real" one of the smart chaps at GDS wrote &lt;a href="https://github.com/psd/pivotal-cards"&gt;pivotal-cards&lt;/a&gt; which lets you generate printable index cards from you pivotal board. Lovely stuff&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.flickr.com/photos/psd/7160723862/" title="Pivotal Cards by psd, on Flickr"&gt;&lt;img src="http://farm8.staticflickr.com/7223/7160723862_ef5d8e59a7.jpg" width="500" height="442" alt="Pivotal Cards" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Anyway - I spent some time today porting this little project to Trello. I have been using Trello more and more on recent projects but always miss the physicality of real index cards and I'm usually strapped for time to start writing them out myself.&lt;/p&gt;&lt;p&gt;So here it is - &lt;a href="http://yobriefca.se/trello-cards"&gt;trello-cards&lt;/a&gt;, I've tried to keep it pretty much 1:1 with pivotal-cards but there are some &lt;code&gt;TODO&lt;/code&gt;s still outstanding - the current card make up looks a bit like this.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/cardmocks.png" /&gt;&lt;/p&gt;&lt;p&gt;So basically follow the instructions on the &lt;a href="http://yobriefca.se/trello-cards/"&gt;project page&lt;/a&gt; and hopefully it should all work out. If it doesn't - &lt;a href="https://github.com/kouphax/trello-cards"&gt;FORK IT 'N FIX IT&lt;/a&gt; or &lt;a href="https://github.com/kouphax/trello-cards/issues"&gt;complain&lt;/a&gt; and I'll fix it for you :) &lt;/p&gt;&lt;p&gt;Some other points worth noting,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Currently tested on Chrome 25 (OSX)&lt;/li&gt;
  &lt;li&gt;Could do with some basic tests around it&lt;/li&gt;
  &lt;li&gt;Currently no markdown support for descriptions&lt;/li&gt;
  &lt;li&gt;Story points use the &lt;a href="https://chrome.google.com/webstore/detail/jdbcdblgjdpmfninkoogcfpnkjmndgje?utm_source=chrome-ntp-icon"&gt;Trello Scrum Chrome Extension&lt;/a&gt; convention of &lt;code&gt;(POINTS)&lt;/code&gt; at the start of the card name/title. If you use that plugin then good for you, if not you can still make use of the points convention&lt;/li&gt;
  &lt;li&gt;Tasks are derived from the first checklist on the card&lt;/li&gt;
&lt;/ul&gt;</content></entry><entry><title>Eg. Play 2.1 + Scala + Guice</title><updated>Tue Dec 18 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/12/18/eg-play-2-dot-1-plus-scala-plus-guice/"></link><id>urn:yobriefca-se:feed:post:Eg. Play 2.1 + Scala + Guice</id><content type="html">&lt;blockquote&gt;&lt;p&gt;TL;DR Here is some &lt;a href="https://github.com/kouphax/play21guice"&gt;sample code&lt;/a&gt; of how to use Play 2.1s GlobalSettings#getControllerInstance feature with Google Guice.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I've had the chance to take Play 2.1 RC 1 out for a spin recently and in a bid to make my project a bit more testable I decided to go ahead and give dependency injection a bit of a spin again. While things like the cake pattern and general dynamic composition are alright I've never been happy with the code that I produce using them. So I decided to give &lt;a href="https://code.google.com/p/google-guice/"&gt;Guice&lt;/a&gt; a blast as it seemed very much aligned with how I approached DI in the .NET world.&lt;/p&gt;&lt;p&gt;My main need was to create instances of controllers and given Plays appraoch in the past (singleton &lt;code&gt;object&lt;/code&gt;s for controllers) 2.1 new &lt;code&gt;GlobalSettings#getControllerInstance&lt;/code&gt; feature was what I needed.&lt;/p&gt;&lt;p&gt;First things first we need to add a project dependency to &lt;a href="https://code.google.com/p/google-guice/"&gt;Guice&lt;/a&gt; and while we could just add the Guice dependencies directly I went with a library that offered slightly neater syntax in Scala - &lt;a href="https://github.com/sptz45/sse-guice"&gt;SSE-Guice&lt;/a&gt;. In short this means instead of writing this sort of noisy code&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;bind&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;classOf&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Service&lt;/span&gt;&lt;span class="o"&gt;]).&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;classOf&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;ServiceImpl&lt;/span&gt;&lt;span class="o"&gt;]).&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;classOf&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We get to write it like this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;bind&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Service&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;ServiceImpl&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Singleton&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So lets add the reference to the &lt;code&gt;Build.scala&lt;/code&gt; file (assuming we just generated a our Play project) for sse-guice&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;appDependencies&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;jdbc&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;anorm&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;

  &lt;span class="c1"&gt;// Add your project dependencies here,&lt;/span&gt;
  &lt;span class="s"&gt;"com.tzavellas"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"sse-guice"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"0.7.0"&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next up we need to modify the Application controller a bit&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;It needs to take a dependency&lt;/li&gt;
  &lt;li&gt;It needs to bo be instantiable (e.g. not an &lt;code&gt;object&lt;/code&gt; - this might be doable but the default approach in Guice seems to conflict with Plays approach to defining an instantiable controller)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So we want to go from this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Action&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Some Message"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="nd"&gt;@Inject&lt;/span&gt;&lt;span class="o"&gt;()(&lt;/span&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;messeger&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Messeger&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Action&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messeger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getMessage&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A few things of note here&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;We use the &lt;code&gt;@Inject()&lt;/code&gt; attribute here to mark this constructor as our default one (need by Guice to satisfy constructor injection)&lt;/li&gt;
  &lt;li&gt;Application is now a class. Small change (a Levenshtein Distance of 6 even!) big difference. Rather than having a singleton we now have something we can construct when we want (even in tests) and set it up really simply. Construction and lifecycle can be handled via our DI library easily.&lt;/li&gt;
  &lt;li&gt;We are passing in some sort of dependency - in this case it's a simple string barfer (&lt;code&gt;Messeger&lt;/code&gt; is a trait) and the impl looks like this&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Messeger&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getMessage&lt;/span&gt; &lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;MessegerImpl&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Messeger&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getMessage&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"The Best Super Message"&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then we need to create our Guice module that will wire up our dependencies,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.tzavellas.sse.guice.ScalaModule&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleModule&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ScalaModule&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;configure&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bind&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Messeger&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;toInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MessegerImpl&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;ScalaModule&lt;/code&gt; declaration is a Scala-fied version of Guices &lt;code&gt;AbstractModule&lt;/code&gt; with some helper and whatnot. Guice applies some common sense to wiring so we don't need to declare each controller type here etc.&lt;/p&gt;&lt;p&gt;Finally we need to start the DI mechanism and provide a way to instantiate controllers. We do this via the Play feature of &lt;code&gt;GlobalSettings&lt;/code&gt;. In our &lt;code&gt;app&lt;/code&gt; package we just put a &lt;code&gt;Global&lt;/code&gt; object and set it up as needed&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Global&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;GlobalSettings&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;lazy&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;injector&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Guice&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;createInjector&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleModule&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getControllerInstance&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;controllerClass&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;injector&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getInstance&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;controllerClass&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We use a &lt;code&gt;lazy&lt;/code&gt; val to instantiate the &lt;code&gt;injector&lt;/code&gt; with our &lt;code&gt;SimpleModule&lt;/code&gt; and override the &lt;code&gt;getControllerInstance&lt;/code&gt; method of our &lt;code&gt;GlobalSettings&lt;/code&gt; class to use Guices injector.&lt;/p&gt;&lt;p&gt;Oh one last thing. We need to tell Play what controllers we want to use this feature (it's not an all or nothing thing which I guess is good). We do this by marking the route declaration with an &lt;code&gt;@&lt;/code&gt; in the &lt;code&gt;routes&lt;/code&gt; file. So it becomes this (notice the &lt;code&gt;@&lt;/code&gt;)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="text"&gt;# Home page
GET     /     @controllers.Application.index
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running the app gives us exactly what we expect but why stop there? We need to test this stuff right? Cool. Testing is made easier - we can simply make use of the various helpers in Play like &lt;code&gt;FakeApplication&lt;/code&gt; and &lt;code&gt;FakeRequest&lt;/code&gt;,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationSpec&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Specification&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s"&gt;"Using FakeApplication"&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"get the default message"&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;running&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FakeApplication&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FakeRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GET&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
        &lt;span class="n"&gt;contentAsString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;must&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The Best Super Message"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or we can create a custom &lt;code&gt;Global&lt;/code&gt; object with a &lt;code&gt;FakeApplication&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationSpec&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Specification&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;DummyMessenger&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Messeger&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getMessage&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Dummy Message"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="s"&gt;"Using FakeApplication"&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;DummyGlobal&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;play&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GlobalSettings&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getControllerInstance&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DummyMessenger&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;asInstanceOf&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="s"&gt;"get the an injected message"&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;running&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FakeApplication&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;withGlobal&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DummyGlobal&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;home&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FakeRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GET&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;
        &lt;span class="n"&gt;contentAsString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;must&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Dummy Message"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And even lighter we can just test the controller as if it was just another class,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationSpec&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Specification&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;DummyMessenger&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Messeger&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getMessage&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Dummy Message"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="s"&gt;"Using direct controller testing"&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"return the dummy message"&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;DummyMessenger&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;()(&lt;/span&gt;&lt;span class="nc"&gt;FakeRequest&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
      &lt;span class="n"&gt;contentAsString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;must&lt;/span&gt; &lt;span class="n"&gt;contain&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Dummy Message"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Hopefully this gives you a good starter for using the new Play 2.1 &lt;code&gt;getContollerInstance&lt;/code&gt; feature along with Guice.&lt;/p&gt;</content></entry><entry><title>Neo4J Talk: Using Graphs to Analyse Public Spending on International Development</title><updated>Wed Dec 12 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/12/12/neo4j-talk-using-graphs-to-analyse-public-spending-on-international-development/"></link><id>urn:yobriefca-se:feed:post:Neo4J Talk: Using Graphs to Analyse Public Spending on International Development</id><content type="html">&lt;p&gt;Better late than ever, right? So a few weeks ago I presented a bit of a case study on my use of Neo4J in a UK government project focused on increasing transparency around how the UK disburses foreign aid. This post is a collection of all the useful resources mentioned in the talk.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://speakerdeck.com/kouphax/neo4j-a-case-study"&gt;Slides on SpeakerDeck&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://skillsmatter.com/podcast/nosql/using-graphs-to-analyse-public-spending-on-international-development"&gt;Video on Skills Matter&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Plenty of acronyms were thrown about and here are the useful links to some of the key ones.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://digital.cabinetoffice.gov.uk/category/gds/"&gt;GDS (Government Digital Service)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.dfid.gov.uk/"&gt;DFID (Department for International Development)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;IATI (International Aid Transparency Initiative) - &lt;a href="http://iatiregistry.org/"&gt;Registry&lt;/a&gt; and &lt;a href="http://iatistandard.org"&gt;Standard&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;OIPA (Open IATI Parser and API) - &lt;a href="http://oipa.openaidsearch.org/api/v2/"&gt;Open Aid Site&lt;/a&gt; and &lt;a href="https://github.com/openaid-IATI/OIPA-V2"&gt;Source&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content></entry><entry><title>MongoDB Basics</title><updated>Thu Dec 06 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/mongo.pdf"></link><id>urn:yobriefca-se:feed:post:MongoDB Basics</id><content type="html"></content></entry><entry><title>Neo4J: A Case Study</title><updated>Wed Nov 28 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/neo4j.pdf"></link><id>urn:yobriefca-se:feed:post:Neo4J: A Case Study</id><content type="html"></content></entry><entry><title>Vertical vs Horizontal Thinking</title><updated>Sat Nov 17 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/11/17/vertical-vs-horizontal-thinking/"></link><id>urn:yobriefca-se:feed:post:Vertical vs Horizontal Thinking</id><content type="html">&lt;p&gt;I'm not quite sure were this ramble is going to lead, it's been a while since I've felt like writing something that hasn't been clearly defined by my half conscious brain but lets take the journey anyway shall we?&lt;/p&gt;&lt;p&gt;This is going to be loosely related to agility, not necessarily the "agile method or process" but more simply about adapting to change and embracing transparency in a software project. More often than not this can be simply referred to as "common sense" and it's a great thing. &lt;/p&gt;&lt;p&gt;But I digress,&lt;/p&gt;&lt;p&gt;In a vertical world you deliver features by implementing vertical spikes through each layer of the system architecture. It doesn't have to be right, but it does need to be good enough for now. Sure it may not deliver the best user experience or maybe there are more HTTP requests or database calls than necessary but it's useable. Its shippable. &lt;/p&gt;&lt;p&gt;In the horizontal world layers of the architecture are tackled by (possibly pseudo) specialists and delivered en masse. This is very typical of the Big Design Upfront, Plan Based, Waterfall approach to delivery but its visible in a lot of projects in various guises and it's rather natural to many people.&lt;/p&gt;&lt;p&gt;Now, on the surface, getting specialists to deliver what they know best and then smash them altogether actually sounds great. You know your DB person is going to produce a wonderful hyper-normalised schema for you, your UI/UX person will deliver a gorgeous mockup of the site and your middle layer app people will deliver the best truly ReSTful service layer known to man. But then you spend an insane amount of time (or many many little periods of time) doing the actual integration and making this vision a reality. The testing, the regressions, the edge cases the performance hits and sacrifices through integrating perfect works of horizontal art. Best case you get it done eventually, worst case someone else gets it done by layering hack upon hack at integration points just to get it working. &lt;/p&gt;&lt;p&gt;Now imagine your DB person gets eaten by a badger, or your UI person decides they've had enough and goes to live in a free-love commune and drink the spiked kool-aid? I've overly dramatised the &lt;a href="http://en.wikipedia.org/wiki/Bus_factor"&gt;Bus Factor&lt;/a&gt; here but the point is still made. You've lost siloed knowledge forever and you need to spend time building it up again, and time and quality slip.&lt;/p&gt;&lt;p&gt;Finally - projects that deliver horizontally are, more often than not, projects that have limited customer/stakeholder visibility. Sometimes even less for end-users. So delivering a mockup of the UI and a DB schema that can be exported as a nice UML diagram is enough to satisfy people that work is on-going and going well. Even if it isn't. It's not a lie but it is a false impression, and it's an impression that doesn't work for long especially if you have shorter release cycles with ongoing customer interaction. A page of beautiful buttons that don't do anything will create an impression that you are nearly done and can easily generate disappointment at subsequent demos. Is it worth it?&lt;/p&gt;&lt;p&gt;Don't get me wrong it is very possible to successfully deliver software in this manner, on time and on budget but so many things must fall into place and all the stars must be aligned that it's a rather big risk, but a risk that people seem to ignore.&lt;/p&gt;&lt;p&gt;So I'm saying vertical is better and there are many reasons for that and I'll touch on some of them here but there is one very fundamental reason it's better. Working and thinking vertically allows you to create a shippable product early. It's not all done, far from it. It probably looks like ass as well but still it's usable enough to get the job done, right? Imagine using a new product that looked amazing but only had 3 working features and the rest of the buttons and knobs didn't do anything. Wow factor is for kids and sales men.&lt;/p&gt;&lt;p&gt;Why else is vertical thinking better?&lt;/p&gt;&lt;p&gt;For one everyone on the team works across all layers of the solution. You don't have huge silos of knowledge contained in a single brain of some crazy bus magnet. Sure you may involve specialists to ensure you're not doing it totally wrong but at least you'd be working WITH them and picking up parts of the vast knowledge they may have.&lt;/p&gt;&lt;p&gt;By delivering something "good enough for now" you actually reduce waste. True you may re-write a few things to get it right but you haven't spent hours crafting perfection only to tear it apart. Even better the first iteration may make users realise - "Actually you know what? That super whizzy button thing - we don't need it". Waterfall attempts to "manage change" because change in waterfall is scary expensive. Embrace it early and often and it much less costly.&lt;/p&gt;&lt;p&gt;Finally, the solution evolves rather than being dragged into existence from a 189 page document of business rules and textual assumptions. This allows you to experiment and try things, throw them away or discover a better way of doing things. You can adapt the solution at anytime to suit the changing nature of the environment it lives in.&lt;/p&gt;&lt;p&gt;Vertical thinking is a proven pattern in many other aspects of the software world. TDD for example. Rather than writing reams of code and testing it manually later, you create a solution bit by bit and adapt it fearlessly as you go. UI designers never just bang out one design and leave it there. Smaller feedback loops are essential to flow and success and vertical thinking gives you this. Break the habit of wowing people and replace it with the habit of delivering - it's a better, longer lasting feeling of win.&lt;/p&gt;</content></entry><entry><title>Technology Techsmology</title><updated>Mon Nov 05 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/11/05/technology-techsmology/"></link><id>urn:yobriefca-se:feed:post:Technology Techsmology</id><content type="html">&lt;p&gt;There is an old adage, &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Projects don't fail because of technology decisions&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;The younger me would have got all uppity and defensive when he heard this statement. As a developer/architect type whose main goal is to make key technology decisions on a project (not to mention as a person who likes to grandiously consider themselves a craftsman) this does seem rather belittling. Are you telling me all my decisions are fairly irrelevant? I'll just stay in bed then shall I? &lt;/p&gt;&lt;p&gt;But you know what? The statement is also completely true. &lt;/p&gt;&lt;p&gt;But hey, wait - do you want to know something else? Yes? Good. &lt;/p&gt;&lt;p&gt;The world of technology is not black and white, projects aren't either a success or a failure, it's simply not a binary system. Projects may not fail, but they could be late. They could be well over budget. They could have code bases so toxic that you need to hire contractors to do the work because no one wants to touch it. They could be so overly laden in patterns and complex architecture that even small changes take weeks or months to implement (and who foots the bill for that?). &lt;/p&gt;&lt;p&gt;Technology decisions can indirectly ruin the morale of your staff, muddy the relationship with your customers and generate really high turnover of staff in your company (which may be a good fit for some companies but you can never foster excellence if you don't nurture talent). &lt;/p&gt;&lt;p&gt;This statement can also be used as a weapon - a weapon to excuse bad decisions and hold back change (both good and bad, in short it stiffles learning). People cling to technologies that they used in the past, for fear of more pain with other technologies. They shoehorn their existing skillset into problem domains that aren't a good fit. When this happens, at the extreme you end up with specialists that simply can't adapt to shifts in technology fast enough.&lt;/p&gt;&lt;p&gt;So yeah, projects don't fail because of technology decisions. But you know what - if your dentist turned round to you with an evil grin and a rusty chisel and &lt;a href="http://en.wikipedia.org/wiki/Law_of_the_instrument"&gt;Maslow's Hammer&lt;/a&gt; citing - "No surgery ever went wrong because of a lack of anesthetic" - you're not likely to get in to that chair and open wide. And even if you did (maybe, you're drunk, maybe people keep telling you this dentist is the best, whatever) the experience would be painful, recovery time would be longer and subsequent treatments would probably be more painful. A rather shambling metaphor I admit but I write what my brain dictates - I have not choice in the matter.&lt;/p&gt;&lt;p&gt;Finally, I fully accept that many of these decisions are out of our control. Customers, especially big ones, generally have the final say, they've potentially invested a lot of time on a certain stack and have a lot of legacy (read: code that still works) solutions in play. True, but one of our goals is to at least inform these people of the alternatives and let them make an informed decision - maybe not today but soon enough the cost of developing and maintaining new systems built on out-of-support JDKs will come under scrutiny and they'll need to consider alternatives, alternatives that you've already enlightened them about.&lt;/p&gt;</content></entry><entry><title>Neo4J Talk in November</title><updated>Fri Nov 02 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/11/02/neo4j-talk-in-november/"></link><id>urn:yobriefca-se:feed:post:Neo4J Talk in November</id><content type="html">&lt;p&gt;Its been rather quiet around here of late, truth be told I've been rather busy with my work (this peak is a rather large one). Being busy for me usually implies I'm not getting bored on cookie cutter, by the numbers web apps, being busy means I'm doing new interesting stuff. I've been doing a bit of work with the UK Government, specifically DFID (Department for International Developement) around the nobel goal of transaprency and open data. The project involves interrogating a lot of highly connected data in weird and wonderful ways which can mean only one thing - Graph Databases. We tried the relational database route early on and it was causing too much pain to get the data we wanted,&lt;/p&gt;
&lt;div style="text-align:center; margin-bottom:15px;"&gt;
  &lt;img src="/images/joins.jpg" /&gt;
&lt;/div&gt;&lt;p&gt;So having recently been enlightened by &lt;a href="https://twitter.com/jimwebber"&gt;Jim Webbers&lt;/a&gt; talk on &lt;a href="http://neo4j.org/"&gt;Neo4J&lt;/a&gt; I decided to try the Graph Database appraoch and it paid off, in spades. Then one humble brag on Twitter later and I get asked to present a case study on Neo4J at the London User Group - and I'm stoked. This is the first talk I've ever given that I don't have to pretend to know what I'm talking about - awesome.&lt;/p&gt;&lt;p&gt;If you're in London on the 28th of November come along and see how I've (probably incorrectly) used Neo4J.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;CASE STUDY: using graphs to analyse public spending on international development
Wednesday, November 28, 2012
6:30 PM To 7:30 PM

Skills Matter
The Skills Matter eXchange
116-120 Goswell Road
London EC1V7DP
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href="http://www.meetup.com/graphdb-london/events/87069112/" data-event="87069112" class="mu-rsvp-btn"&gt;RSVP&lt;/a&gt;&lt;/p&gt;
&lt;script&gt;!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s); js.id=id;js.async=true;js.src="https://secure.meetup.com/890357987448484755600/script/api/mu.btns.js?id=9e8dku34o6mqp5knqbjlk6lfvj";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","mu-bootjs");&lt;/script&gt;</content></entry><entry><title>Learning Resources for Scala, Play! and Mongo</title><updated>Fri Oct 05 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/10/05/learning-resources-for-scala-play-and-mongo/"></link><id>urn:yobriefca-se:feed:post:Learning Resources for Scala, Play! and Mongo</id><content type="html">&lt;p&gt;I've been asked a lot recently for some resources on various technologies, namely Scala, Play! (2.0) and Mongo so I thought I'd share the wealth (so to speak).&lt;/p&gt;&lt;p&gt;Got any good resources that I haven't listed here? Let me know.&lt;/p&gt;&lt;h2&gt;Scala&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.scala-lang.org/"&gt;Official Site&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://vimeo.com/40255430"&gt;Screencast&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.scala-lang.org/node/1305"&gt;Various Links from the official site&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://scalatron.github.com/"&gt;Scalatron&lt;/a&gt; - learn Scala by programming a game&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://twitter.github.com/scala_school/"&gt;Scala School&lt;/a&gt; - Twitters learning resource&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://typesafe.com/resources/book/scala-for-the-impatient"&gt;Scala for the Impatient&lt;/a&gt; - excellent "lite" version of a great book.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://typesafe.com/resources/videos"&gt;Scala Videos&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://www.coursera.org/course/progfun"&gt;Coursera Course&lt;/a&gt; - Scala course on Coursera by Martin Odersky. Only good for five weeks or so but during that time anyone can sign up and download the video lectures and materials.&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Play!&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.playframework.org/"&gt;Official Site&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.playframework.org/documentation/2.0.3/ScalaHome"&gt;Official Docs&lt;/a&gt; - Excellent resource&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://github.com/playframework/Play20/tree/master/samples/scala"&gt;Scala Code Samples&lt;/a&gt; - Some nice examples from the official Github repo.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://yobriefca.se/blog/2012/05/08/starter-for-10-scala/"&gt;Scala, Play! and Mongo&lt;/a&gt; - Blog post on writing a simple app using all those technologies.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.artima.com/articles/play2_scala_squeryl.html"&gt;Getting Started with Play! 2, Scala and Squeryl&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.jamesward.com/2012/02/21/play-framework-2-with-scala-anorm-json-coffeescript-jquery-heroku"&gt;Play! 2 Getting Started&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Mongo&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.mongodb.org/"&gt;Official Website&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://try.mongodb.org/"&gt;Try Mongo&lt;/a&gt; - A Tiny MongoDB Browser Shell (mini tutorial included)&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://openmymind.net/mongodb.pdf"&gt;Little Book of Mongo&lt;/a&gt; (&lt;a href="http://openmymind.net/mongodb.epub"&gt;epub&lt;/a&gt;) - Book about Mongo&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://mongly.com/"&gt;Mongly&lt;/a&gt; - Various tutorials and essays on MongoDB&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://speakerdeck.com/u/kouphax/p/mongodb"&gt;Presentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content></entry><entry><title>StackMob: Baas meets PaaS</title><updated>Thu Sep 13 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/stackmob.pdf"></link><id>urn:yobriefca-se:feed:post:StackMob: Baas meets PaaS</id><content type="html"></content></entry><entry><title>StackMob Lightning Talk</title><updated>Wed Sep 12 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/09/12/stackmob-lightning-talk/"></link><id>urn:yobriefca-se:feed:post:StackMob Lightning Talk</id><content type="html">&lt;p&gt;So I finally found an opportunity to deliver a quick lightning talk on &lt;a href="http://stackmob.com"&gt;StackMob&lt;/a&gt; at our monthly CodeSlice sessions in work. Knocked it up today over lunch and presented it tonight. Would like to pull this one out into a more in depth and interactive look... just waiting for someone to ask :)&lt;/p&gt;&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/49405807?title=0&amp;amp;byline=0&amp;amp;portrait=0&amp;amp;color=ffffff" width="600" height="450" frameborder="0" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt;&lt;/p&gt;
&lt;script async="async" class="speakerdeck-embed" data-id="5051d07ffe08c30002031d18" data-ratio="1.3333333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;</content></entry><entry><title>Securely Serving Static Files on Heroku</title><updated>Mon Aug 20 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/08/20/securely-serving-static-files-on-heroku/"></link><id>urn:yobriefca-se:feed:post:Securely Serving Static Files on Heroku</id><content type="html">&lt;p&gt;More than once I've had a need to host what is essentially static content on Heroku. For example when building a simple internal web app for my company that used an existing API or Parse. Invariably in this situation I have also needed to secure this app - nothing to drastic, no need for multiple user accounts or roles or any such nonsense (we generally run our non-essential systems on the honour system). Basic auth is more than enough.&lt;/p&gt;&lt;p&gt;So, static content plus basic auth. Heroku itself doesn't offer basic auth as an option (why would it?) and so you'd need to roll it into your app. Once I hosted my static stuff inside a Scala/Play! app, once a Sinatra app and the other a Rails app - variety is the spice of life. But each time I did it I felt a little dirty - it was noticeably slower serving this content from within an app and it seemed rather wasteful. So, in a bid to stamp out technical debt, I've ripped out all those web frameworks an replaced them with good old Apache.&lt;/p&gt;&lt;p&gt;Now anyone with any knowledge of Apache will already be able to deduce the steps required here - but I want to note it for posterity, for myself, when I forget, in about a week.&lt;/p&gt;&lt;h2&gt;BuildPack&lt;/h2&gt;&lt;p&gt;First things first you'll want to get your hands on a custom &lt;a href="https://devcenter.heroku.com/articles/buildpacks"&gt;Heroku BuildPack&lt;/a&gt; - these are the things that prepare your app instance with all the software and support it needs to run.&lt;/p&gt;&lt;p&gt;I recommend you fork and clone the &lt;a href="https://github.com/pearkes/heroku-buildpack-static"&gt;heroku-buildpack-static&lt;/a&gt; buildpack available from pearkes on GitHub.&lt;/p&gt;&lt;h2&gt;.htpassword&lt;/h2&gt;&lt;p&gt;Next you need to generate your &lt;code&gt;.htpasswd&lt;/code&gt; file that will serve as your store for your Basic Auth credentials. The two simplest ways to generate one of these is to either use &lt;a href="http://www.htaccesstools.com/htpasswd-generator/"&gt;htaccesstools.com's htpasswd generator&lt;/a&gt; or generate on via the command line.&lt;/p&gt;&lt;p&gt;The simplest command to generate one is this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;htpasswd -cb &amp;lt;password_file&amp;gt; &amp;lt;username&amp;gt; &amp;lt;password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;htpasswd -cb .htpasswd james password
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Will generate a file in the current directory called .htpasswd (the standard name for these files) with a single entry of username = james and password = password.&lt;/p&gt;&lt;h2&gt;Updating the BuildPack&lt;/h2&gt;&lt;p&gt;Either run the above command in the &lt;code&gt;heroku-buildpack-static/conf&lt;/code&gt; folder or move your &lt;code&gt;.htpasswd&lt;/code&gt; file into the that folder.&lt;/p&gt;&lt;p&gt;Next up you want to configure apache to authenticate against that generate password file. Adding the following lines to the &lt;code&gt;httpd.conf&lt;/code&gt; file in the &lt;code&gt;heroku-buildpack-static/conf&lt;/code&gt; will lock all assets under your apps site down,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="apache "&gt;    &lt;span class="nb"&gt;AuthType&lt;/span&gt;      Basic
    &lt;span class="nb"&gt;AuthName&lt;/span&gt;      &lt;span class="s2"&gt;"Authentication Required"&lt;/span&gt;
    &lt;span class="nb"&gt;AuthUserFile&lt;/span&gt;  &lt;span class="s2"&gt;"/app/apache/conf/.htpasswd"&lt;/span&gt;
    &lt;span class="nb"&gt;Require&lt;/span&gt;       valid-user
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This block should be added to the configuration section starting &lt;code&gt;&amp;lt;Directory /&amp;gt;&lt;/code&gt; i.e.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="apache "&gt;  &lt;span class="nt"&gt;&amp;lt;Directory&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nb"&gt;Options&lt;/span&gt; FollowSymLinks
      &lt;span class="nb"&gt;AllowOverride&lt;/span&gt; &lt;span class="k"&gt;None&lt;/span&gt;

      &lt;span class="nb"&gt;AuthType&lt;/span&gt;      Basic
      &lt;span class="nb"&gt;AuthName&lt;/span&gt;      &lt;span class="s2"&gt;"Authentication Required"&lt;/span&gt;
      &lt;span class="nb"&gt;AuthUserFile&lt;/span&gt;  &lt;span class="s2"&gt;"/app/apache/conf/.htpasswd"&lt;/span&gt;
      &lt;span class="nb"&gt;Require&lt;/span&gt;       valid-user

      &lt;span class="nb"&gt;Order&lt;/span&gt; deny,allow
      &lt;span class="nb"&gt;Deny&lt;/span&gt; from &lt;span class="k"&gt;all&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/Directory&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You'll notice the &lt;code&gt;AuthUserFile&lt;/code&gt; points to the correct absolute path for our custom .htpasswd file. If you called it anything else please update this line accordingly.&lt;/p&gt;&lt;p&gt;Commit and push that to your forked repo.&lt;/p&gt;&lt;h2&gt;Using the BuildPack&lt;/h2&gt;&lt;p&gt;Finally we need to tell Heroku to use the custom buildpack. If you have yet to create an app then you can specify the buildpack during creation,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;heroku create &amp;lt;app name&amp;gt; --buildpack &amp;lt;url to your repo&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you have created you app already you can add the buildpack through an additional config setting&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;heroku config:add BUILDPACK_URL=&amp;lt;url to your repo&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Push your new or updated site to Heroku and watch in awe as your site is that bit more secure.&lt;/p&gt;</content></entry><entry><title>Dependencies in JavaScript: Constructor, Setter or Global</title><updated>Mon Aug 20 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/08/20/dependencies-in-javascript-constructor-setter-or-global/"></link><id>urn:yobriefca-se:feed:post:Dependencies in JavaScript: Constructor, Setter or Global</id><content type="html">&lt;p&gt;There has been an interesting conversation on Twitter over the last number of hours between @derickbailey and a number of other people and it all started with this tweet,&lt;/p&gt;
&lt;blockquote class="twitter-tweet tw-align-center"&gt;&lt;p&gt;anyone have links to articles on why setter injection is evil? /cc &lt;a href="https://twitter.com/jbogard"&gt;&lt;s&gt;@&lt;/s&gt;&lt;b&gt;jbogard&lt;/b&gt;&lt;/a&gt; &lt;a href="https://twitter.com/jeremydmiller"&gt;&lt;s&gt;@&lt;/s&gt;&lt;b&gt;jeremydmiller&lt;/b&gt;&lt;/a&gt; &lt;a href="https://twitter.com/jflanagan"&gt;&lt;s&gt;@&lt;/s&gt;&lt;b&gt;jflanagan&lt;/b&gt;&lt;/a&gt; &lt;a href="https://twitter.com/chadmyers"&gt;&lt;s&gt;@&lt;/s&gt;&lt;b&gt;chadmyers&lt;/b&gt;&lt;/a&gt;&lt;/p&gt;— derickbailey (@derickbailey) &lt;a href="https://twitter.com/derickbailey/status/237658824163614720" data-datetime="2012-08-20T21:14:13+00:00"&gt;August 20, 2012&lt;/a&gt;&lt;/blockquote&gt;
&lt;script src="//platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt;&lt;p&gt;The conversation has been around using DI and IoC in JavaScript (yes I know they are not one in the same, and I know a Container is not a necessary part of DI or IoC thank you very much). Simple answer is this - good patterns have a place in any language provided they aren't abused mercilessly. But lets get back to the discussion - why would you use constructor injection over setter injection? Is setter injection evil? Obviously no strategy is actually evil per-say - just used in the wrong place at the wrong time. So lets go back to the first question - constructor injection vs. setter injection.&lt;/p&gt;&lt;h2&gt;Constructor Injection or Setter Injection&lt;/h2&gt;&lt;p&gt;The simplest answer to this was one I gave @derickbailey earlier,&lt;/p&gt;
&lt;blockquote class="twitter-tweet tw-align-center" data-in-reply-to="237664796298854401"&gt;&lt;p&gt;&lt;a href="https://twitter.com/derickbailey"&gt;&lt;s&gt;@&lt;/s&gt;&lt;b&gt;derickbailey&lt;/b&gt;&lt;/a&gt; resolve mandatory dependenciesvia constructor injection, optional dependencies via setter.  This is a common convention.&lt;/p&gt;— James Hughes (@kouphax) &lt;a href="https://twitter.com/kouphax/status/237792703826522112" data-datetime="2012-08-21T06:06:13+00:00"&gt;August 21, 2012&lt;/a&gt;&lt;/blockquote&gt;
&lt;script src="//platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt;&lt;p&gt;This little bit of knowledge I've had since living down in the guts of Spring MVC about 5 years ago and it makes a lot of sense regardless of how those dependencies are getting in there.&lt;/p&gt;&lt;h2&gt;Fail Early&lt;/h2&gt;&lt;p&gt;When your code goes into an invalid state you want to fail as quickly as possible, this makes debugging much easier and prevents an avalanche of unexpected behaviour. Using constructor injection (and asserting the existence of dependencies) gives you this ability.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyThingy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;	
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;
		
		&lt;span class="c1"&gt;// rest of my code&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;magic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;beepboop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;thing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MyThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// FAIL FAIL FAIL&lt;/span&gt;
	&lt;span class="nx"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you tried to achieve the same thing with setter injection you would be failing much later in the proceedings,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyThingy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setRequiredA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setRequiredB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;	
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;
		
		&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;magic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;beepboop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;

	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;thing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MyThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; 
	&lt;span class="nx"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// FAIL FAIL FAIL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now imagine that the last two lines are miles apart in code-lines, you've gone and lost all context when debugging. Sure in this trivial example it would be fairly easy to resolve this but if you've gone and lost the plot and created a heavily nested object graph you'll be crying before they day is out.&lt;/p&gt;&lt;p&gt;Now setter injection does have it's place - for &lt;strong&gt;optional&lt;/strong&gt; dependencies,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyThingy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;	
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;
		
		&lt;span class="c1"&gt;// rest of my code&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setOptionalC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;optionalC&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;optionalC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;optionalC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;optionalC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;optionalC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"doing a thing..."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
		&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;magic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;beepboop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;thing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MyThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

	&lt;span class="nx"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setOptionalC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
	&lt;span class="nx"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs "doing a thing..."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So the extension of the rule is that &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Anything injected via &lt;strong&gt;setter injection&lt;/strong&gt; needs to be either defaulted initially or checked for existence before use.&lt;/p&gt;
&lt;/blockquote&gt;&lt;h2&gt;Readability&lt;/h2&gt;&lt;p&gt;By following this convention, heck by following any agreed convention, you get better visibility of the expected behaviour of your code. You know what the object needs to get the job done. That feels like a poor argument but I think it still warrants a mention here.&lt;/p&gt;&lt;h2&gt;Over Engineering and Deep Graphs&lt;/h2&gt;&lt;p&gt;Obviously if you've over-designed your classes or it has grown out of control this approach becomes difficult to manage. I've seen something akin to this in some projects,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;CoordinatorOfAllTheThings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;....)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Someone has decided to make every bit of functionality it's own little class or module and so the dependency graph has become incredibly broad. In fact this might suggest they are creating a deep graph as well and simply passing the dependencies down the chain. &lt;strong&gt;If you have a lot of dependencies then there is something wrong with the object you are creating&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;For fine grained, inter-object communication dependency injection is fine but when you are scaling up to more coarse, inter-module or component communication an evented or message based approach is almost always a better option.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;	&lt;span class="c1"&gt;// rest of previous code&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="nx"&gt;EventStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"doneathing"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;EventStream&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
		&lt;span class="nx"&gt;trigger&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
		&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;OtherThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requiredD&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="nx"&gt;EventStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"doneathing"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;requiredD&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;thing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;other&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;OtherThing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"stuff done"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
	
	&lt;span class="nx"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs "stuff done"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is a rather contrived example but it should server a point. &lt;/p&gt;&lt;h3&gt;The Global Issue&lt;/h3&gt;&lt;p&gt;You'll notice the EventStream object in the code above is global, you'll also notice I have skipped part of this posts title (... or Global). Lets come to that now. It is possible to achieve the same results as constructor/setter injection using global scope,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyThingy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;	
		&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="s2"&gt;"Missing Dependency"&lt;/span&gt;
		
		&lt;span class="c1"&gt;// rest of my code&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;magic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;global&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;beepboop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;thing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MyThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// FAIL FAIL FAIL&lt;/span&gt;
	&lt;span class="nx"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So rather than inject the necessary dependencies in we are relying on the fact they exist in global scope. This works fine in some situations. The EventStream object above is a global hub that can be used throughout the application. I'm ok with that. The risk of global scope is that it's easy for objects to be redefined by other actors in your system so existence or behaviour is never guaranteed. So use sparingly.&lt;/p&gt;&lt;h2&gt;Moptional&lt;/h2&gt;&lt;p&gt;The other alternative approach would be to make everything Moptional. That is, anything mandatory should be instantiated with a default and provide setter injection for changing default behaviour.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyThingy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;DefaultRequiredA&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;DefaultRequiredB&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
		
		&lt;span class="c1"&gt;// rest of my code&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setRequiredA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setRequiredB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
		&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="nx"&gt;MyThingy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
		&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredA&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;magic&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiredB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;beepboop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
	
	&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;thing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MyThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
	&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setRequiredB&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;CustomRequiredB&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
	&lt;span class="nx"&gt;thing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You kind of get the best of worlds here - not having to worry about default behaviour but have the ability to override this (for example in testing). But then you need to think about this. Why are you providing this sort of strategy? Are you simply hiding all implementation for an object but providing setters and getter to simply aid testing? If that's the case - STOP. Your code should be testable but you should not change your code to simply aid testing. Perhaps its time to go back and re-think your strategy? Also, for testing - it's entirely possible, given the dynamic nature of JavaScript, to redefine the internal dependencies prior to them being instantiated in your object anyway. Don't add features or behaviour that you don't actually need to deliver the solution.&lt;/p&gt;&lt;h2&gt;Wrap Up&lt;/h2&gt;&lt;p&gt;This was playing over and over on my ride into work this morning (I cycle I have a lot of thinking time) and I wanted to get it out there. Hopefully be useful to people asking some questions on this topic.&lt;/p&gt;&lt;p&gt;Oh and IoC &lt;strong&gt;Containers&lt;/strong&gt; in JavaScript...... don't.&lt;/p&gt;&lt;p&gt;My $0.02&lt;/p&gt;</content></entry><entry><title>Backend as a Service - Part One</title><updated>Tue Aug 07 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/08/07/backend-as-a-service-part-one/"></link><id>urn:yobriefca-se:feed:post:Backend as a Service - Part One</id><content type="html">&lt;p&gt;I recently wrote an article for &lt;a href="http://cloudspring.com/"&gt;CloudSpring&lt;/a&gt; (a part of the &lt;a href="http://www.sitepoint.com/"&gt;SitePoint&lt;/a&gt; family) around what you can expect and what you should think about when going for one of the "Backend as a Service" offerings on the market. &lt;/p&gt;&lt;p&gt;The article is &lt;a href="http://cloudspring.com/backend-as-a-service-part-1/"&gt;now live&lt;/a&gt;.&lt;/p&gt;&lt;h3&gt;&lt;a href="http://cloudspring.com/backend-as-a-service-part-1/"&gt;CloudSpring: Backend as a Service Part 1&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Part 2, the one where I look at whats actually out there, will be ready within the next week.&lt;/p&gt;</content></entry><entry><title>Fear of Code</title><updated>Sat Aug 04 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/08/04/fear-of-code/"></link><id>urn:yobriefca-se:feed:post:Fear of Code</id><content type="html">&lt;p&gt;More silly wishy washy nonsense - take what you will.&lt;/p&gt;&lt;p&gt;Recently Thomas Fuchs has this to say on twitter,&lt;/p&gt;
&lt;blockquote class="twitter-tweet"&gt;&lt;p&gt;As a programmer, you should be afraid of code, and eliminate as much as possible.&lt;/p&gt;— Thomas Fuchs (@thomasfuchs) &lt;a href="https://twitter.com/thomasfuchs/status/230871987881390081" data-datetime="2012-08-02T03:45:45+00:00"&gt;August 2, 2012&lt;/a&gt;&lt;/blockquote&gt;
&lt;script src="//platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt;&lt;p&gt;Its great, I love this stuff and it got me thinking and I thought why not put my thoughts up here, right? After all this is what this place is all about.&lt;/p&gt;&lt;p&gt;Rather than describe the emotion as fear ("be afraid of code") i'd posit that, as a programmer, you should be respectful of code. Rather than eliminate as much as possible you should conserve it as much as possible - don't write it in the first place. &lt;/p&gt;&lt;p&gt;A while back I posted something similar on Twitter - I can't find the tweet right now but it went a little something like this,&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Adding code is great, removing code is greater but not writing the code in the first place is greatest&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;As a developer it is very easy to fall into the trap of creating some sort of highly reusable complex masterpiece that is going to change the way we write code forever. But down that road, madness lies. Desigining too much, adding too many features, creating too many abstractions, handling every single edge case, writing too many tests - all this stuff is just extra complexity, extra code that simply muddies the solution and makes it harder for someone to understand the solution. More code doesn't solve the problem faster, more code doesn't make things easier, more code doesn't help.&lt;/p&gt;&lt;p&gt;In other words - writing too much code, regardless of quality is just a selfish act. That goes for 3rd party code too.&lt;/p&gt;&lt;p&gt;Quit it.&lt;/p&gt;</content></entry><entry><title>expect.kt - Testing Library for Kotlin</title><updated>Tue Jul 24 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/24/expect-dot-kt-testing-library-for-kotlin/"></link><id>urn:yobriefca-se:feed:post:expect.kt - Testing Library for Kotlin</id><content type="html">&lt;p&gt;In a bid to delve into the &lt;a href="http://kotlin.jetbrains.org/"&gt;language&lt;/a&gt; a bit further than a rake of tiny console apps I decided to do something "real" with Kotlin. With that in mind - welcome &lt;a href="https://github.com/kouphax/expect.kt"&gt;expect.kt&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;It started purely as a learning venture but I think there is a certain usefulness in the library so its probably worth sharing :). Now it's worth noting that kotlin already has a number of testing statements that you can achieve a lot of this stuff with - this library gives you a few things over and above these statements &lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Alternative syntax. Testing is quite a personal thing and some people like to write things in different ways.&lt;/li&gt;
  &lt;li&gt;Descriptive assertions. Some would say &lt;code&gt;expect(2).toBePositive()&lt;/code&gt; reads better than &lt;code&gt;assertTrue(2 &amp;gt; 0)&lt;/code&gt; or &lt;code&gt;expect(2 &amp;gt; 0) { true }&lt;/code&gt;. Again its all about taste, and variety is the spice of life.&lt;/li&gt;
  &lt;li&gt;Chained assertions. See later&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Rather than some campaign to replace the inbuilt expectations and assertions this library intended to complement them.&lt;/p&gt;&lt;h2&gt;So what is expect.kt?&lt;/h2&gt;&lt;p&gt;Expect.kt is a set of (growing) assertions/expectations that you can use in your kotlin based tests. It gives you a number of ways to assert your expectations and gives you quite descriptive methods in which to do it.&lt;/p&gt;&lt;h3&gt;Expect Syntax&lt;/h3&gt;&lt;p&gt;The expect syntax is the default way to assert things,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="n"&gt;tests&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.junit.Test&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;test&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;kotlin.expectations.*&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;StringExpectationTests&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;test&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;testHelloWorld&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
		&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toStartWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hell"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
	&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Most of that is boilerplate JUnit testy things but the key line is&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toStartWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hell"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Reads quite nicely IMHO.&lt;/p&gt;&lt;h3&gt;Should Syntax&lt;/h3&gt;&lt;p&gt;The should syntax offers a more fluent syntax if you want to call it that, if we wanted to write the same test above using the should syntax we could do this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;should&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hell"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So rather than wrap your target with an expect you get and extension function to fluently assert it.&lt;/p&gt;&lt;h3&gt;Chaining&lt;/h3&gt;&lt;p&gt;Sometimes making a single assertion on a result isn't enough and rather than write &lt;code&gt;assertTrue(...)&lt;/code&gt; multiple times you can use extpect.kt's chaining support.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;&lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toStartWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hell"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;and&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toEndWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"rld"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or, with the should syntax,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;should&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hell"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;and&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"rld"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's a bit more readable than expect after expect, right?&lt;/p&gt;&lt;p&gt;So thats it for now - there are a number of assertions implemented already on the useful types (Strings, Ints, Doubles, Dates, Booleans for example) and there are more planned on the way. Currently you'll be wanting to build from source (or copy/paste what you need).&lt;/p&gt;&lt;p&gt;Finally - A shout out to .NETs &lt;a href="http://fluentassertions.codeplex.com/"&gt;FluentAssertions&lt;/a&gt; where a lot of the assertions found their origin!&lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/kouphax/expect.kt"&gt;expect.kt&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>expect.kt: Fluent assertions for Kotlin</title><updated>Fri Jul 20 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="http://yobriefca.se/expect.kt"></link><id>urn:yobriefca-se:feed:post:expect.kt: Fluent assertions for Kotlin</id><content type="html"></content></entry><entry><title>Kotlin: Nullable Extensions</title><updated>Tue Jul 17 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/17/kotlin-nullable-extensions/"></link><id>urn:yobriefca-se:feed:post:Kotlin: Nullable Extensions</id><content type="html">&lt;p&gt;Haskell doesn't have if statements - that blew my mind the first time my old Scala mentor told me it. &lt;code&gt;if&lt;/code&gt;s just don't work with a strong function approach. Plus you know what - &lt;code&gt;if&lt;/code&gt;s are rather ugly especially when you need to wrap ALL THE THINGS in a null check. Scala has a type called &lt;code&gt;Option&lt;/code&gt;. An &lt;code&gt;Option&lt;/code&gt; is essentially a collection of none or one instances of a type. If everything returned an &lt;code&gt;Option&lt;/code&gt; nothing would be null and you don't need to do any null checks (they are much more powerful than I am making out but for now this is all that is necessary).&lt;/p&gt;&lt;p&gt;Kotlin doesn't have the &lt;code&gt;Option&lt;/code&gt; type but it does have two things that allow you to act like it does,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Nullable types - e.g. &lt;code&gt;String&lt;/code&gt; vs &lt;code&gt;String?&lt;/code&gt; , and,&lt;/li&gt;
  &lt;li&gt;Nullable Extension Functions&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Remember the brief definition of an &lt;code&gt;Option&lt;/code&gt; - it's a list of 0 or 1 instances of a particualr type and as such we can treat it a bit like a list. Now once we get functional with our badselves this makes null handling unnecessary. We can use all those nice little functional methods that you get with collections - map, forEach, filter, fold etc.&lt;/p&gt;&lt;p&gt;This means you're less inclined to use &lt;code&gt;if&lt;/code&gt;s and, more importantly, less inclined to use mutable variables which is great for building performant, scalable soltions.&lt;/p&gt;&lt;p&gt;Lets take a really contrived example to show you what I mean. OImagine this bit of code....&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;   &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;authenticate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;View&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
   
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auth&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
   		&lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;login&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid Username or Password"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
   		&lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auth&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;

   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We want to authenticate a user and return a certain view. Now yes we could refactor this down into something more bitsize but this is a very common state to find code - so bare with me for demonstration purposes.&lt;/p&gt;&lt;p&gt;&lt;code&gt;authenticate&lt;/code&gt; returns a user if the username and password was correct otherwise it returns null. With our nullable extensions (by importing &lt;code&gt;kotlin.nullable.*&lt;/code&gt;) we can reduce this into something more Scala-like,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;index&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;login&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Invalid Username or Password"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So what we have done here is basically take an object (the user), if it exists, and transform it to another object which we return. Otherwise we return something different. This is much simpler and avoids having to create a mutable var (yes we could have avoided that in the other example but in many cases its very difficult to achieve this). In fact I am not even a fan of the elvis operator there (&lt;code&gt;?:&lt;/code&gt;) and have a Scala-like syntax &lt;a href="https://github.com/JetBrains/kotlin/pull/100"&gt;pull request&lt;/a&gt; open for getOrElse. A small syntactic change that I think allows you create a much neater, chainable block of code.&lt;/p&gt;&lt;p&gt;Thats only the tip of the iceberg on Nullable Extensions - those of you with a functional mind will be able to put these collection-like extensions to great use I reckon. Not quite Scalas &lt;code&gt;Option&lt;/code&gt; type but good enough at providing the common features for me.&lt;/p&gt;</content></entry><entry><title>Kotlin: Extension Functions</title><updated>Sun Jul 15 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/15/kotlin-extension-functions/"></link><id>urn:yobriefca-se:feed:post:Kotlin: Extension Functions</id><content type="html">&lt;p&gt;In any language (that supports the concept) extension functions are nothing more than syntactic sugar. Instead of writing,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;StringUtils.encrypt(mystring, "salt")
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Extension functions allow you to write,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;mystring.encrypt("salt")
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Once you start moving into the world of chaining things get much cleaner.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;encrypted = StringUtils.encrypt(mystring, "salt")
encoded = StringUtils.encode(encrypted, "UTF-8")
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;or worse,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;StringUtils.encode(StringUtils.encrypt(mystring, "salt"), "UTF-8")
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I've seen this done a few times, it happens. But with sugary sweet extension functions (Extension Methods in C#, Implicit Conversions in Scala) you could just do this,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;mystring.encrypt("salt").encode("UTF-8")
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Much nicer.&lt;/p&gt;&lt;p&gt;Kotlin, unlike crusty old Java, supports extension functions. Here is how you'd implement the methods above&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;salt:&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;):&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;StringBuffer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="o"&gt;)?.&lt;/span&gt;&lt;span class="na"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;encoding:&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ByteArray&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And we can use these, as you'd expect, like so,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;args:&lt;/span&gt; &lt;span class="n"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;mystring&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james"&lt;/span&gt;
    &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;encoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mystring&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"salt"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So what identified those functions as Extension Functions? Simple - the prefixing of the method name with the type &lt;code&gt;String.encode&lt;/code&gt;. No need to create implicit wrappers (Scala), no need to create loads of static classes (C#). And yes it's possible to use generics in these methods as well,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;getOrElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;t:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This can be applied to any nullable type.&lt;/p&gt;&lt;h2&gt;Literal Extension Functions&lt;/h2&gt;&lt;p&gt;An interesting little feature that Kotlin has that makes it a bit different is the use of function literal extension functions. Basically where you can declare a function as a literal (a lambda method if you will) you can also make that literal an extension function,&lt;/p&gt;&lt;p&gt;So we could take our methods we wrote above and bring them right into our &lt;code&gt;main&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;args:&lt;/span&gt; &lt;span class="n"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;encrypt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.(&lt;/span&gt;&lt;span class="nl"&gt;salt:&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;StringBuffer&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;append&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="o"&gt;)?.&lt;/span&gt;&lt;span class="na"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()}&lt;/span&gt;
    &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;encode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.(&lt;/span&gt;&lt;span class="nl"&gt;encoding:&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
    
    &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;mystring&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james"&lt;/span&gt;
    &lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="n"&gt;encoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mystring&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;encrypt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"salt"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;encode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This may seem quite unnecessary at first glance but it become quite powerful when creating DSLs - as demonstrated in the docs around the &lt;a href="http://confluence.jetbrains.net/display/Kotlin/Type-safe+Groovy-style+builders"&gt;Groovy Style Builders&lt;/a&gt;&lt;/p&gt;&lt;p&gt;So Kotlin has extension functions that don't require a load of boilerplate (C#) or rather odd implicit syntax (Scala) - cool, right?&lt;/p&gt;</content></entry><entry><title>Kotlin: Here's What I Think (For Now)</title><updated>Sat Jul 14 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/14/kotlin-heres-what-i-think-for-now/"></link><id>urn:yobriefca-se:feed:post:Kotlin: Here's What I Think (For Now)</id><content type="html">&lt;blockquote&gt;&lt;p&gt;Before I go shooting my mouth off here - a word of warning - I haven't really done anything with Kotlin. I've read the docs, played with the &lt;a href="https://github.com/dodyg/Kotlin101"&gt;Kotlin 101 samples&lt;/a&gt; and written a few small console type apps. So take what you want from this post.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Want to hear something zany? There actually are people - real living people, with a fully working brain, many much smarter than me - that are "happy" with Java. &lt;/p&gt;&lt;p&gt;Seriously.&lt;/p&gt;&lt;p&gt;I made a move from Java to .NET about 5 years ago and have dipped back in a few times over that period. Trying new languages is a sure fire way, for some at least, to make you realise Java is not a language that makes coding fun, or productive. Java put me right of the JVM. Any language that makes woking with collections (something you'll do alot) so long winded and noisy deserves to be shunned. But this isn't a Java rant - the JVM is great but it's just marketed badly because of Java.&lt;/p&gt;&lt;p&gt;But all the JVM-hate that Java instilled in me 5 years ago; the last 6 months of working with Scala has replaced with love. So I've been looking around the JVM world and discovered &lt;a href="http://kotlin.jetbrains.org/"&gt;Kotlin&lt;/a&gt; a new JVM based language from JetBrains. Its much closer to Java than what Scala or Clojure is (intentionally).&lt;/p&gt;&lt;p&gt;One main thing that Kotlin brings to the table is pragmatism. Without trying to belittle Kotlin - it's almost like CoffeeScript for Java (yep kind of like what &lt;a href="http://www.eclipse.org/xtend/"&gt;Xtend&lt;/a&gt; is. Kotlin removes all those annoying necessities in Java that make your code verbose (checked exceptions, semi-colons, null reference checks etc.). It also adds things that make your life much easier (extension methods, functional collection manipulation etc.). &lt;/p&gt;&lt;p&gt;When you write Ruby for the first time you notice that many things just work out like you'd expect, the syntax is quite natural. The same cannot be said for Java - there is just way too much boilerplate. Kotlin helps reduce that boilerplate. So, by extension, it could be said that Kotlin makes you more productive. A person who knows Java will find Kotlin no challenge to start using - the syntax is nicely aligned, just without the noise.&lt;/p&gt;&lt;p&gt;Many people, when trying to get their team to adopt Scala, use the "you can use Scala like Java til you get used to it" mantra. I don't agree with that. Academically it's true but the minute you start working with other peoples Scala code - you've just gotta know Scala and when they have a slew of Implicits thrown in your Scala as Java stuff goes out the window. Kotlin, on the other hand, has some of the nice functional patterns from Scala but none of the "magic-to-a-newcomer" features that can make grown men weep. Now the Kotlin docs themselves even go as far as stating &lt;/p&gt;
&lt;blockquote&gt;
  &lt;pre class="highlight"&gt;&lt;code&gt;If you are happy with Scala, you probably don't need Kotlin.
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;&lt;p&gt;But I think they are selling themselves short - I think plenty of people use Scala but could easily use Kotlin in its place and find themselves in a better place (easier upskilling, less cryptic codebase).&lt;/p&gt;&lt;p&gt;Anyway this is a bit of a ramble, what I'm saying is - Kotlin has some really rather nice features (Groovy Like DSL Builders, Pattern Matching, the &lt;code&gt;when&lt;/code&gt; expression, lambdas, Kotlin to JavaScript compilation(!) etc.) and people happy with either Java OR Scala should give it a go.&lt;/p&gt;</content></entry><entry><title>Azure Website Workflow for Bosses</title><updated>Wed Jul 11 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/11/azure-website-workflow-for-bosses/"></link><id>urn:yobriefca-se:feed:post:Azure Website Workflow for Bosses</id><content type="html">&lt;blockquote&gt;&lt;p&gt;So consider my indefinite hiatus suspended definitely. I suck at quitting! But we wont dive into the whys and wherefores around that now.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I've a confession to make, the "new" Azure, I like it. Yeah I do, s'nice. I like the way it blends PaaS with IaaS (though I do feel that adding 1st class node.js and PHP support is a bit of a tacked on marketing ploy). So you dont have the "Add-On" model you get from Appharbor and Heroku but thats fine because this blending of "as a Services" gives you an alternate approach. For a start you can just use the external services without them having to be "Add-Ons". Sure you lose out on the nice automatic integration points for managing your services but it's not the end of the world and what happens when you want something that isn't available as an Add-On or worse not available as a cloudy SaaS thing? Well I guess you need to go off to EC2 or similar and host your own (assuming you want to stick with the cloudy infrastructure). But with Azure you also get another option - you want to use Mongo or some huge enterprisey platform that isn't SaaSified? Stick it on an Azure VM. Everything managed under one cloud provider. Thats such a nice thing to have at your disposal.&lt;/p&gt;&lt;p&gt;But thats not the point of this post. Most posts out there cover a workflow for Azure WebSites that make use of the portal to provision new sites (and then usually &lt;code&gt;git&lt;/code&gt; to deploy) but that's weak sauce (the portal part at least - gits still aces). No, no no - You want hyper-productivity, you want automated provisioning, you want to be hip; node.js hip; and you want to feel like a boss hanging out on your command line while other "lesser people" click stuff with a silly little rodent. I just read your mind, didn't I? Well then - you got that with Azure.&lt;/p&gt;&lt;h2&gt;Setting Up&lt;/h2&gt;&lt;p&gt;First things first you need to install a few things.&lt;/p&gt;&lt;h3&gt;Installing node.js + NPM&lt;/h3&gt;&lt;p&gt;&lt;a href="http://nodejs.org/#download"&gt;Download&lt;/a&gt; &lt;/p&gt;&lt;p&gt;The tools are built and deployed in node via NPM, the node package manager so just run the one click installer and you'll be all setup.&lt;/p&gt;&lt;h3&gt;Installing Azure CLI Tools&lt;/h3&gt;&lt;p&gt;Install these via NPM so fire up your terminal/command prompt/whatever and type&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;npm install azure -g
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then let NPM install all the dependencies and what not (the &lt;code&gt;-g&lt;/code&gt; option just tells NPM to install the tools globally so they are accessible from the command line)&lt;/p&gt;&lt;h3&gt;Adding your account&lt;/h3&gt;&lt;p&gt;Next up you need to tell azure what account to use so fire this command into you command line&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;azure account download
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You'll then be taken to the Azure website and it should (assuming you're logged in already) download a &lt;code&gt;publishsettings&lt;/code&gt; file. Next up you need to import this file into your azure toolchain.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;azure account import YOUR_DOWNLOADED_FILE.publishsettings
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;After some scrolling text you'll be all set up and ready to deploy. You'll be asked to delete the file you downloaded - probably best to do this :)&lt;/p&gt;&lt;h2&gt;Creating the app&lt;/h2&gt;&lt;p&gt;OK then lets create an app - what we create is irrelevant here so lets just create the default ASP.NET MVC website and use that as our sample app. Remeber though, if you're a cutting edge awesome-addict to switch from .NET 4.5 to 4 or else you'll get some issues when deploying your site to Azure.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt; : The next few steps can be condensed, thanks to the feedback from Glenn Block (@gblock), &lt;/p&gt;&lt;blockquote class="twitter-tweet" data-in-reply-to="224800579388575744"&gt;&lt;p&gt;@&lt;a href="https://twitter.com/kouphax"&gt;kouphax&lt;/a&gt; oh dude, you don't need all those steps! Do "azure site create my site --git" It will create the repo for you and add the remote!&lt;/p&gt;— Glenn Block (@gblock) &lt;a href="https://twitter.com/gblock/status/224855192800215040" data-datetime="2012-07-16T13:17:10+00:00"&gt;July 16, 2012&lt;/a&gt;&lt;/blockquote&gt; &lt;script src="//platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt; This sets up your git repo during creation of the site, adds the remote endpoint and even generates a .gitignore file (which you need to edit yourelf for .NET projects. WIN!!!!&lt;p&gt;Anyway.... on with the old post.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Once you've created you app and saved it then you'll want to intialise an empty repo at the root of the solution,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then we need to create a site on Azure we can push to.&lt;/p&gt;&lt;h2&gt;Creating a new site&lt;/h2&gt;&lt;p&gt;Back to the command line now,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;azure site create SITE_NAME
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If we execute this, again at the root of our solution, it will do a number of things,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Validate the request (site name availability and what not)&lt;/li&gt;
  &lt;li&gt;Create a new site on Azure&lt;/li&gt;
  &lt;li&gt;Add a new git &lt;code&gt;remote&lt;/code&gt; to your &lt;code&gt;.git\config&lt;/code&gt; file&lt;/li&gt;
&lt;/ol&gt;&lt;h2&gt;Pushing site&lt;/h2&gt;&lt;p&gt;As I've said the last command actually added a remote repo to our git configuration so if we do the usual gitty stuff (you'll want to, for neatness sake, set up your &lt;code&gt;.gitignore&lt;/code&gt; but we can skip this for brevity),&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;git add -A
git commit -m "Boring commit message"
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we push,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;git push azure master
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Done!&lt;/p&gt;&lt;h2&gt;Viewing your site&lt;/h2&gt;&lt;p&gt;You could just browse to your new site but thats just not hip enough, hell no. Instead lets launch it from the command line...&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;azure site browse
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That will launch our new site without having to touch a silly old, out of date rodent.&lt;/p&gt;&lt;h2&gt;Wrapping up&lt;/h2&gt;&lt;p&gt;Ok so thats the basic workflow for using CLI tools to manage Azure sites. You can do this with most of the Azure services (VMs, Certs, Cloud Services etc.) aswell.&lt;/p&gt;&lt;p&gt;So apart from making you look all hackery working in the terminal it does have other uses, specifically around the fact terminal command can be easily executed in a headless automated environment. Think of creating new sites as part of your CI's deployment pipeline, or as part of some disaster recovery process. The possibilities are endless... heck even write your own UI for managing sites or deploying new sites/VM/whatever!&lt;/p&gt;&lt;p&gt;So lets see - &lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;CLI hackery coolness... ✓&lt;/li&gt;
  &lt;li&gt;Node.js hipster bragging rights... ✓&lt;/li&gt;
  &lt;li&gt;Azure website created and deployed with about 6 commands... ✓&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Not bad for a few minutes work!&lt;/p&gt;</content></entry><entry><title>Technology in the Cloud</title><updated>Wed Jul 11 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/11/technology-in-the-cloud/"></link><id>urn:yobriefca-se:feed:post:Technology in the Cloud</id><content type="html">&lt;p&gt;The Cloud. As much as the term has been pummelled into meaningless marketing sputum by the guys in expensive suits and fake smiles it does exist and is distinct from a typical "throw all your stuff in a data center" setup.&lt;/p&gt;&lt;p&gt;The Cloud is distinct, as well, in terms of how you think about your solution architecture. The typical old school approach would be to just create big monoliths to process your data but that doesn't work out so well in the cloud. Of course the considerations for the cloud aren't new, hell no - things like SOA have been around for an age, but if you want to harness the benefits of the cloud as the internet intended you will be narrowing your options. No more just cobbling common bits together and crossing your fingers. &lt;/p&gt;&lt;p&gt;So fine - architecting for the cloud has some special considerations but thats not what I want to talk about - there are better people than me to cover that.&lt;/p&gt;&lt;p&gt;What I want to talk about is use of technology within that architecture. I've never been a fan of the one-size-fits-all-painful-or-not technology stack driven almost solely by a need to avoid unnecessary education of developers. And, now I feel the cloud is strengthening my view that a default stack is more trouble than it's worth. More importantly it's really adding momentum to the whole polyglot architecture, or simply using the right tools for the job.&lt;/p&gt;&lt;p&gt;Let me explain. Any default stack, by definition, needs to cater to many user cases and as such brings a certain amount of sacrifice with it. Sacrifices come in many forms but usually in performance, memory footprint and/or verbose configuration. In the cloud you are, in essence, in a Pay-As-You-Go model, every clock cycle and every bit of storage costs. Now that heavy ORM with its less than ideal queries and slow relationship mapping is starting to cost you money EVERY request. In fact, it's probably backed by that storage heavy relational database for all your non-relational data. Same for that rather large web framework you've got there.... every time to deal with a spike and scale that badboy out and you're having to use medium instances rather than small. That SOAP based webservice that requires triple the bandwidth to service a request... ugh you get the point.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;In the cloud the default stack can easily bleed money.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I guess this is less about the default stack and more about just using the right tools to get the job done in the most effective manner - afterall you're paying for this stuff now. You kind of were before but in such a coarse grained manner it didn't matter - it does now.&lt;/p&gt;&lt;p&gt;In summary - the cloud has started to make technology decisions more accountable and we need to ensure that those decisions aren't costing our stakeholders more money than necessary.&lt;/p&gt;&lt;p&gt;PS.&lt;/p&gt;&lt;p&gt;I'm not going to sit here and preach about the which specific technologies to use but come on - if your storing Session State in a Oracle database, switch to Riak, or Redis. If you're building a nice scalable API then use something like Scala which promotes immutable state and concurrency out of the box over something like vanilla Java (or perhaps just a better framework Spring-WS vs Dropwizard for example). JSON over XML or a binary format (Protobuf for example) for machine to machine communication. A small node.js web site over ASP.NET Webforms for simple sites. The list goes on. That is to say consider this advice against the other factors driving technical decisions - dont JUST go for the smallest, lightest tools because it MIGHT be cheaper. Do the research.&lt;/p&gt;&lt;p&gt;Finally, perhaps you've got another IT team needing to support this new solution? Bring them along this journey too, educate them as you educate yourself, dont just throw the finished thing over the wall with a 1 week handover and a pat on the back.&lt;/p&gt;&lt;p&gt;Don't be afraid - it's a super fun world we live in!&lt;/p&gt;</content></entry><entry><title>Agile Practices</title><updated>Sun Jul 08 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/agile-practices.pdf"></link><id>urn:yobriefca-se:feed:post:Agile Practices</id><content type="html"></content></entry><entry><title>Ruby by Example</title><updated>Sun Jul 08 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/ruby.pdf"></link><id>urn:yobriefca-se:feed:post:Ruby by Example</id><content type="html"></content></entry><entry><title>Git Basics</title><updated>Sun Jul 08 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/git-basics.pdf"></link><id>urn:yobriefca-se:feed:post:Git Basics</id><content type="html"></content></entry><entry><title>Ruby on Rails</title><updated>Sun Jul 08 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/rails.pdf"></link><id>urn:yobriefca-se:feed:post:Ruby on Rails</id><content type="html"></content></entry><entry><title>Quitsies, Well Sort Of</title><updated>Mon Jul 02 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/02/quitsies-well-sort-of/"></link><id>urn:yobriefca-se:feed:post:Quitsies, Well Sort Of</id><content type="html">&lt;blockquote&gt;&lt;p&gt;You know when you start to question why you write these things it's probably time to quit, or at least take a break. &lt;/p&gt;&lt;p&gt;~ me, just there now&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;This more of an FYI to anyone who may read this blog than a horrible diatribe about how I am wasting my life with meaningless exercises or technologies I'll never get to use to a bunch of people that don't really care, what would be the point? Plus - it's probably not even true. &lt;/p&gt;&lt;p&gt;So rather than pottering on about cool and interesting technologies that will make everyones (read, developers) life infinitely better 100% guaranteed I've decided to devote my time to, &lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Working on some startup-py type things that have been milling around the back of my head for some time, and,&lt;/li&gt;
  &lt;li&gt;Generally getting stuff done&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Yep, like a hipster band, this little testament to bike-shedding is going on indefinite hiatus.&lt;/p&gt;</content></entry><entry><title>[Quick and Dirty, Super Ugly,] Starter for 10 - Dropwizard</title><updated>Mon Jul 02 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/07/02/quick-and-dirty-super-ugly-starter-for-10-dropwizard/"></link><id>urn:yobriefca-se:feed:post:[Quick and Dirty, Super Ugly,] Starter for 10 - Dropwizard</id><content type="html">&lt;p&gt;So once again I'm writing Java without a gun to my head, and once again I'm finding it enjoyable. Don't get me wrong Java as a language feels very long winded, old and cumbersome but the frameworks and stuff that exist around it actually make those issues, less prominent, so much so I think I could easily live with the noise that Java brings.&lt;/p&gt;&lt;p&gt;Anywho.&lt;/p&gt;&lt;p&gt;&lt;a href="http://dropwizard.codahale.com/"&gt;Dropwizard&lt;/a&gt; is a framework for creating very simple ReSTful Web Services in Java. Not just that but it also bundles some nice best practise in with it as well. Things like pluggable health checks, logging, configuration, content negotiation etc. And, because it's fairly lightweight its fast. Very fast.&lt;/p&gt;&lt;p&gt;In fact you can think of Dropwizard as a collection of pre-existing tools glued together with some simple features to create a cohesive stack for HTTP+JSON web services. You've got &lt;a href="http://www.eclipse.org/jetty/"&gt;Jetty&lt;/a&gt;, &lt;a href="http://jersey.java.net/"&gt;Jersey&lt;/a&gt;, &lt;a href="http://jackson.codehaus.org/"&gt;Jackson&lt;/a&gt;, &lt;a href="http://metrics.codahale.com/"&gt;Metrics&lt;/a&gt;, &lt;a href="http://logback.qos.ch/"&gt;Logback&lt;/a&gt;, &lt;a href="http://www.jdbi.org/"&gt;JDBI&lt;/a&gt; and more. Dropwizard give you the glue that binds these along with a simple pipeline that makes use of these technologies.&lt;/p&gt;&lt;h2&gt;Simple Todo&lt;/h2&gt;&lt;p&gt;I had intended to do something a little more substantial but I am about to make a small annoucemnet that has kind of effectivley stopped me expanding this app much further. So lets create a simple Todo App.&lt;/p&gt;&lt;h3&gt;pom.xml&lt;/h3&gt;&lt;p&gt;As with most Java projects lets start with the &lt;code&gt;pom.xml&lt;/code&gt; which is effectively our project definition. I'll not show you my entire POM, at least not on the first date, but let me reveal the important parts.&lt;/p&gt;&lt;p&gt;Beause it's awesome I want to include WebJars so I add my repository,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="xml"&gt;    &lt;span class="nt"&gt;&amp;lt;repositories&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;repository&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;webjars&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;url&amp;gt;&lt;/span&gt;http://webjars.github.com/m2&lt;span class="nt"&gt;&amp;lt;/url&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/repository&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/repositories&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next I add my dependencies,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Dropwizard Core which has everything you'll need for this&lt;/li&gt;
  &lt;li&gt;jQuery from the WebJar repository&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="highlight"&gt;&lt;code class="xml"&gt;    &lt;span class="nt"&gt;&amp;lt;dependencies&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;com.yammer.dropwizard&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;dropwizard-core&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;0.4.2&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;com.jquery&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;jquery&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.7.2-1&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/dependencies&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I also add the &lt;a href="http://maven.apache.org/plugins/maven-shade-plugin/"&gt;shade plugin&lt;/a&gt; which will let me build a Fat Jar (a jar with all my dependencies and what not in it).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="xml"&gt;    &lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.maven.plugins&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-shade-plugin&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.6&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;createDependencyReducedPom&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/createDependencyReducedPom&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;filters&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;filter&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;artifact&amp;gt;&lt;/span&gt;*:*&lt;span class="nt"&gt;&amp;lt;/artifact&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;excludes&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;exclude&amp;gt;&lt;/span&gt;META-INF/*.SF&lt;span class="nt"&gt;&amp;lt;/exclude&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;exclude&amp;gt;&lt;/span&gt;META-INF/*.DSA&lt;span class="nt"&gt;&amp;lt;/exclude&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;exclude&amp;gt;&lt;/span&gt;META-INF/*.RSA&lt;span class="nt"&gt;&amp;lt;/exclude&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/excludes&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/filter&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/filters&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;phase&amp;gt;&lt;/span&gt;package&lt;span class="nt"&gt;&amp;lt;/phase&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;shade&lt;span class="nt"&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;transformers&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;transformer&lt;/span&gt; &lt;span class="na"&gt;implementation=&lt;/span&gt;&lt;span class="s"&gt;"org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;transformer&lt;/span&gt; &lt;span class="na"&gt;implementation=&lt;/span&gt;&lt;span class="s"&gt;"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;mainClass&amp;gt;&lt;/span&gt;se.yobriefca.TodoService&lt;span class="nt"&gt;&amp;lt;/mainClass&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;/transformer&amp;gt;&lt;/span&gt;
                    &lt;span class="nt"&gt;&amp;lt;/transformers&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Configuration&lt;/h3&gt;&lt;p&gt;Dropwizard has the ability to map configuration options (from a YML or JSON file) into concrete classes that can be accessed by your services. So first I created a todo.yml at the root of the project (beside the pom.xml) with some basic configuration&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="xml"&gt;http:
    rootPath: "/app/*"
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This configuration tells Dropwizard that my services are located at a url starting with &lt;code&gt;/app&lt;/code&gt; - eg &lt;code&gt;/app/todo&lt;/code&gt;. This is necessary as I want to create an asset later that maps to the root url &lt;code&gt;/&lt;/code&gt; that is the default of Dropwizard. At this point I haven't needed any of my own configuration but if I did I could create a POJO that maps to proeprties in this file. The &lt;a href="http://dropwizard.codahale.com/getting-started/#creating-a-configuration-class"&gt;official guide&lt;/a&gt; has more on this. For now I will create an empty &lt;code&gt;Configuration&lt;/code&gt; class in case I want to extend it later (yeah I know YAGNI YAGNI KISS YAGNI - shutuppayourface!)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="n"&gt;se&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;yobriefca&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.yammer.dropwizard.config.Configuration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TodoConfiguration&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;Configuration&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Service&lt;/h3&gt;&lt;p&gt;The service is the core class in our solution. It's the thing that has the &lt;code&gt;main&lt;/code&gt; as an entry point. The thing that "set us up the resouces" and registers healthchecks and controls our service lifecycle. You'd think there would be loads of code here? Right? Wrong!&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="n"&gt;se&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;yobriefca&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.yammer.dropwizard.Service&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.yammer.dropwizard.bundles.AssetsBundle&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.yammer.dropwizard.config.Environment&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;se.yobriefca.resources.TodoResource&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TodoService&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TodoConfiguration&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TodoService&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;TodoService&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;super&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"todo"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;addBundle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;AssetsBundle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/assets"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;addBundle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;AssetsBundle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/public"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/public/"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TodoConfiguration&lt;/span&gt; &lt;span class="n"&gt;configuration&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Environment&lt;/span&gt; &lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;environment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addResource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;TodoResource&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So all services extend &lt;code&gt;Service&lt;/code&gt; and have a &lt;code&gt;Configuration&lt;/code&gt; type (FYI these config types can be nested so its not as restrictive as I first thought). Basically what we have is a &lt;code&gt;main&lt;/code&gt; method that create an instance of our service and runs it. Upon creation we add two bundles to the service. A bundle is bascially a bit of functionality and in this case we create two &lt;code&gt;AssetBundles&lt;/code&gt; which can be used to serve static content (embedded resources) upon request to a specific URL (think things like CSS, JavaScript, HTML etc.) You'll notice I am mapping 2 things bundles here and this is because I need to map to the WebJars directroy as well. So when I got to &lt;code&gt;localhost/index.html&lt;/code&gt; it will match the url pattern in the first AssetsBundle and look for a file &lt;code&gt;src/main/resources/assets/index.html&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Finally we have the &lt;code&gt;initialize&lt;/code&gt; method that allows us to decare mutliple resources and healthchecks against our environment. We also get handed our configuration class we decalred in advance.&lt;/p&gt;&lt;h3&gt;A bit about healthchecks&lt;/h3&gt;&lt;p&gt;Healthchecks are little classes that perform some sort of test on a part of your solution. For example we could have a database connectivity check. These healthchecks are made available on the &lt;a href="http://dropwizard.codahale.com/manual/core/#health-checks"&gt;admin section that is bound to another port&lt;/a&gt; when your app starts. &lt;/p&gt;&lt;p&gt;Now I didn't define any for this example and for that I feel absolutely terrible. Seriously I kind of feel sick thinkging about it. And whats worse Dropwizard kindly reminds me everytime I start the app - using LOTS of exclamantion marks and CAPS!!! IRL you should really not follow my example here.&lt;/p&gt;&lt;h3&gt;Resources&lt;/h3&gt;&lt;p&gt;Resources are resources as they are identified in ReSTy things. For example a &lt;code&gt;User&lt;/code&gt; is a resource or an &lt;code&gt;Order&lt;/code&gt; etc. These things will respond to HTTP requests matched against the URL and the HTTP Method used. In my example I have defined a resource &lt;code&gt;Todo&lt;/code&gt; that can be used to list, create and delete (I didn't need anything else for this).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="nd"&gt;@Path&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/todos"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Produces&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;APPLICATION_JSON&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TodoResource&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Lists&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newArrayList&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nd"&gt;@GET&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Collection&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;todos&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@POST&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Todo&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Valid&lt;/span&gt; &lt;span class="n"&gt;Todo&lt;/span&gt; &lt;span class="n"&gt;todo&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;todos&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;todo&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;todo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@DELETE&lt;/span&gt;
    &lt;span class="nd"&gt;@Path&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/{id}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Todo&lt;/span&gt; &lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathParam&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;LongParam&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;Todo&lt;/span&gt; &lt;span class="n"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;todoCopy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Lists&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newArrayList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;todos&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;todos&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;clear&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// loop past all items in the list and replace the changed one&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Todo&lt;/span&gt; &lt;span class="nl"&gt;element:&lt;/span&gt; &lt;span class="n"&gt;todoCopy&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;()){&lt;/span&gt;
                &lt;span class="n"&gt;todos&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;todo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Again not a great deal of code and what code that is there is fairly straightforward - some would say boilerplate. We use annotations to map our HTTP Methods &lt;code&gt;@GET&lt;/code&gt;, &lt;code&gt;@POST&lt;/code&gt; and &lt;code&gt;@DELETE&lt;/code&gt; as well as a &lt;code&gt;@Path(...)&lt;/code&gt; annotation to specify what url this resource resonds to. Another nice feature we can see annotated here is we get both validation (via the @Valid annotation which is based on Hibernates Validation framework) and JSON model binding via Jackson and model annotations (get to that in a minute).&lt;/p&gt;&lt;p&gt;For this simple demo I am storing a list of Todos in an ArrayList in memory (top of the class).&lt;/p&gt;&lt;h3&gt;Model&lt;/h3&gt;&lt;p&gt;The model is, bar all the annotations to enable serialisation, fairly POJO-like,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Todo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Not So Universally Unique!&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;AtomicLong&lt;/span&gt; &lt;span class="n"&gt;NSUUID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;AtomicLong&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nd"&gt;@JsonProperty&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@NotEmpty&lt;/span&gt;
    &lt;span class="nd"&gt;@JsonProperty&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@JsonCreator&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@JsonProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NSUUID&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;incrementAndGet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getContent&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I've put this in more for completeness as there is little involved in this.&lt;/p&gt;&lt;h3&gt;index.html&lt;/h3&gt;&lt;p&gt;I am not going to create some elaborate over architected front end here - just a few lines of JavaScript (ignoring the rather large jQuery library included of course) and not styling.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="html"&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Demo&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"task"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"addTask"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Add Task&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"todos"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;'text/javascript'&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'/public/jquery.min.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;// load and replace the list&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getJSON&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/app/todos"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
          &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#todos"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
              &lt;span class="s2"&gt;"&amp;lt;li&amp;gt;&amp;lt;input type='checkbox' data-id='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s2"&gt;"'/&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
              &lt;span class="s2"&gt;"&amp;lt;/li&amp;gt;"&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="c1"&gt;// manage adding a new task&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#addTask"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
          &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;         &lt;span class="s2"&gt;"/app/todos"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;        &lt;span class="s2"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;        &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#task"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
            &lt;span class="nx"&gt;contentType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/json; charset=utf-8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;dataType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s2"&gt;"json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;     &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;textStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jqXHR&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
              &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#todos"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="s2"&gt;"&amp;lt;li&amp;gt;&amp;lt;input type='checkbox' data-id='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s2"&gt;"'/&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s2"&gt;"&amp;lt;/li&amp;gt;"&lt;/span&gt;
              &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;

        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#todos"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;delegate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"input[type=checkbox]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"click"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
          &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;chk&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"li"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="nx"&gt;id&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;chk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

          &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;         &lt;span class="s2"&gt;"/app/todos/"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;        &lt;span class="s2"&gt;"DELETE"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;contentType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/json; charset=utf-8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;dataType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="s2"&gt;"json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;     &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;textStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;jqXHR&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
              &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
                &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;css&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="s2"&gt;"text-decoration"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"line-through"&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
                &lt;span class="nx"&gt;chk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
              &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And thats it. Oh wait I also added a boot command that peforms the maven packing and starts the jar.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;mvn package;java -jar target/todo-0.1.jar server todo.yml
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Just run &lt;code&gt;./boot&lt;/code&gt; (assuming you've chmod'd the file to be executable) and you'll have, assuming I (or you) haven't made any mistakes, a Todo service running on port 8080 (with admin/diagnostics stuff on port 8081).&lt;/p&gt;&lt;p&gt;As always the &lt;a href="https://github.com/kouphax/dropwizard-todo"&gt;example code is available on Github&lt;/a&gt;. Sorry for the rushed nature of this post, I'll hopefully keep tweaking the example on Github till it's a bit nicer - perhaps even with tests and comments (YEAH RIGHT!!!!)&lt;/p&gt;&lt;p&gt;In short - Dropwizard - simple ReST services, very little config, very nice.&lt;/p&gt;</content></entry><entry><title>Super Basic Trello API Wrapper for Scala and Play</title><updated>Sun Jun 24 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/06/24/super-basic-trello-api-wrapper-for-scala-and-play/"></link><id>urn:yobriefca-se:feed:post:Super Basic Trello API Wrapper for Scala and Play</id><content type="html">&lt;p&gt;So I am currently doing a bit of work with using Trello as a kind of data store and decided to write a litle app in &lt;a href="http://playframework.org"&gt;Play!&lt;/a&gt; (the Scala variety of course). There didn't appear to be a library out there to handle some of the basic Trello stuff so over the past 20 minutes I put one together.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;play.api.libs.ws.WS&lt;/span&gt;

&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Trello&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"https://api.trello.com"&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)*)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:_&lt;/span&gt;&lt;span class="kt"&gt;*&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)*)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:_&lt;/span&gt;&lt;span class="kt"&gt;*&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)*)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:_&lt;/span&gt;&lt;span class="kt"&gt;*&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;put&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)*)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:_&lt;/span&gt;&lt;span class="kt"&gt;*&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)*)&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;WS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;
    &lt;span class="o"&gt;})).&lt;/span&gt;&lt;span class="n"&gt;withQueryString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"token"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="kt"&gt;*&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Yep it's very basic and all but the &lt;code&gt;get&lt;/code&gt; is untested (it's all I've needed so far). But in the spirit of shipping early this is a good start. The class makes use of Play!s uberflexible &lt;code&gt;WS&lt;/code&gt; class and returns &lt;code&gt;Promise[Request]&lt;/code&gt; so you can use it like so (just an example)&lt;/p&gt;&lt;p&gt;A simple API object to wrap the Trello class instance to load cards and map them into my expected objects&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Api&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Trello&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KEY&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TOKEN&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;CardFormat&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Format&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Card&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;reads&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;JsValue&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Card&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Card&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="o"&gt;\&lt;/span&gt; &lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;as&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
      &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="o"&gt;\&lt;/span&gt; &lt;span class="s"&gt;"due"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;as&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;writes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Card&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;JsValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NotImplementedException&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;getPastTalks&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/1/lists/_____"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"cards"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"open"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"card_fields"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"desc,name,due"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt; &lt;span class="o"&gt;\&lt;/span&gt; &lt;span class="s"&gt;"cards"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;JsArray&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cards&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cards&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;as&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Card&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And calling this bad boy from a controller.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Action&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Async&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;Api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getPastTalks&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;talks&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;talks&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So - lots of work to do around this - I want to move away from a thin wrapper to something a bit more robust with strong classes for the different entites. &lt;/p&gt;&lt;p&gt;Also if you want to find out where to get the API keys etc. go read the decent &lt;a href="https://trello.com/docs/"&gt;API docs from Trello&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Two Hidden Gems in Play 2's Template Engine</title><updated>Sat Jun 16 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/06/16/two-hidden-gems-in-play-2-s-template-engine/"></link><id>urn:yobriefca-se:feed:post:Two Hidden Gems in Play 2's Template Engine</id><content type="html">&lt;p&gt;Poking around some &lt;a href="http://www.playframework.org/"&gt;Play 2&lt;/a&gt; &lt;a href="http://www.playframework.org/documentation/2.0.1/Samples"&gt;samples&lt;/a&gt; today I came across some little bits of sugar. I haven't seen mention of them in other places (they must be well hidden) but they are two very common things I've implemented in less elegant ways in the past. That was reason enough for me to throw a quick post up about them.&lt;/p&gt;&lt;p&gt;Prepare to have your minds blown. Well not blown - gently nudged probably.&lt;/p&gt;&lt;h2&gt;String.when&lt;/h2&gt;&lt;p&gt;&lt;code&gt;"somestring".when(predicate: =&amp;gt; Boolean)&lt;/code&gt; allows you conditionally spit out the value of the string it is called on (otherwise returning and empty string).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="s"&gt;"returned_string"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;// returns "returned_string"&lt;/span&gt;
&lt;span class="s"&gt;"nonreturned_string"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// returns "" (empty string)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So in the context of views what is this good for? Lots of things but the most clever use for this little function is conditional css. Imagine you've got a block of HTML that represents a message area. These messages can be either good or bad and so we need to add the css class of &lt;code&gt;message-bad&lt;/code&gt; depending on an argument passed in to the view. A very quick and dirty way of handling this would be as such&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="o"&gt;@(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;successful&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="-Symbol"&gt;'message&lt;/span&gt; &lt;span class="nd"&gt;@if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;successful&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt; &lt;span class="s"&gt;"message-bad"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nd"&gt;@message&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Grand, fine. It's a bit noisy but it works. &lt;code&gt;when()&lt;/code&gt; makes this a bit neater IMHO.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="o"&gt;@(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;successful&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="-Symbol"&gt;'message&lt;/span&gt; &lt;span class="o"&gt;@(&lt;/span&gt;&lt;span class="s"&gt;"message-bad"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;when&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;successful&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nd"&gt;@message&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Yep the line is the same length but it looks more like a single block of conditional code, it flows much better and, I think at least, is much more readable.&lt;/p&gt;&lt;h2&gt;Date.format("&amp;lt;date-format&amp;gt;")&lt;/h2&gt;&lt;p&gt;&lt;code&gt;new java.util.Date().format(pattern: String)&lt;/code&gt; allows you to specify how a date should be formatted in the rendered HTML. It's essentially shorthand for the noise-fest that is &lt;code&gt;SimpleDateFormat&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SimpleDateFormat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;In the past I've added transient properties to models for displaying the date, or written my own methods on those models. Both of those approaches should have had me taken out into a field and shot. &lt;code&gt;format&lt;/code&gt; makes things much more straightforward,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="o"&gt;@(&lt;/span&gt;&lt;span class="n"&gt;importantDate&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;java.util.Date&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nd"&gt;@importantDate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"dd-MM-yyyy"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And the output will be along the lines of "29-01-2013" or whatever the date was.&lt;/p&gt;&lt;p&gt;OK so neither of these are ground breaking or earth shattering (wait they mean the same thing really dont they???) but they are neat to enough to warrant a shout-out here. Anyone else have any little nuggets in Play 2 they want to share?&lt;/p&gt;</content></entry><entry><title>MonoTouch - C# + iOS = Good Times</title><updated>Fri Jun 08 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/monotouch.pdf"></link><id>urn:yobriefca-se:feed:post:MonoTouch - C# + iOS = Good Times</id><content type="html"></content></entry><entry><title>.NET On a Diet</title><updated>Fri Jun 08 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/dotnet-on-a-diet.pdf"></link><id>urn:yobriefca-se:feed:post:.NET On a Diet</id><content type="html"></content></entry><entry><title>Monotouch: C# + iOS = Good Times</title><updated>Wed Jun 06 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/06/06/monotouch-c-number-plus-ios-equals-good-times/"></link><id>urn:yobriefca-se:feed:post:Monotouch: C# + iOS = Good Times</id><content type="html">&lt;h2&gt;Slides&lt;/h2&gt;
&lt;script async="async" class="speakerdeck-embed" data-id="4fd0fb533bb27c0022030674" data-ratio="1.3333333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;&lt;h2&gt;Code&lt;/h2&gt;&lt;p&gt;&lt;a href="https://github.com/kouphax/monotouch-samples-ndc2012"&gt;https://github.com/kouphax/monotouch-samples-ndc2012&lt;/a&gt;&lt;/p&gt;&lt;h2&gt;Description&lt;/h2&gt;&lt;p&gt;MonoTouch allows you to harness all those C# skills and most of those handy .NET/Mono libraries to create truly native apps for iOS with a sprinkle of cross-platform support. But why not just use objective-c you cry? Choice is nice, so is C# and so is the range of libraries available for mono!&lt;/p&gt;&lt;p&gt;In this talk I will dive in to what makes MonoTouch so compelling for .NET developers wanting to break into iOS development and even how these skills can be harnessed to create cross platform native applications for iOS, Android and WP7 through a shared codebase.&lt;/p&gt;</content></entry><entry><title>.NET on a Diet</title><updated>Wed Jun 06 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/06/06/dot-net-on-a-diet/"></link><id>urn:yobriefca-se:feed:post:.NET on a Diet</id><content type="html">&lt;h2&gt;Slides&lt;/h2&gt;
&lt;script async="async" class="speakerdeck-embed" data-id="4fd0fb8d3bb27c001f0309b7" data-ratio="1.3333333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;&lt;h2&gt;Description&lt;/h2&gt;&lt;p&gt;There are plenty of framework options in .NET. We have the big mainstream web frameworks like ASP.NET WebForms and MVC, the ORM abstractions and DataMappers in the form of Entity Framework, NHibernate and iBatis and services such as WCF requiring large amounts of careful configuration to get right. Unfortunately these frameworks can get in the way, abstractions breakdown and leak and eventually we end up fighting against the frameworks limitations.&lt;/p&gt;&lt;p&gt;This session will take a look at the feature-fit lightweight alternatives and how they can be used to compose a technology stack that is cruft-free and fit for purpose.&lt;/p&gt;</content></entry><entry><title>Mobile, How Do I Develop Thee? Let Me Count the Ways (Long Version)</title><updated>Wed Jun 06 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/06/06/mobile-how-do-i-develop-thee-let-me-count-the-ways-long-version/"></link><id>urn:yobriefca-se:feed:post:Mobile, How Do I Develop Thee? Let Me Count the Ways (Long Version)</id><content type="html">&lt;h2&gt;Slides&lt;/h2&gt;
&lt;script async="async" class="speakerdeck-embed" data-id="4fcf26728a589b001f0040e8" data-ratio="1.3333333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;&lt;h2&gt;Description&lt;/h2&gt;&lt;p&gt;Mobile development is a bit like writing web apps back in the browser war days - full of decision anxiety, volatility and platform specific issues. There is no one true answer yet most people think there should be. This talk will cover the popular(ish) options from native all the way to web solutions (and all the mashup magic in-between). Oh yes and some opinion as well.&lt;/p&gt;&lt;p&gt;This is the longer more up to date version of the previous talk.&lt;/p&gt;</content></entry><entry><title>Mobile, How Do I Develop Thee? Let Me Count the Ways</title><updated>Wed Jun 06 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/mobile-how-do-i-develop-thee.pdf"></link><id>urn:yobriefca-se:feed:post:Mobile, How Do I Develop Thee? Let Me Count the Ways</id><content type="html"></content></entry><entry><title>5 Years</title><updated>Fri May 25 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/05/25/5-years/"></link><id>urn:yobriefca-se:feed:post:5 Years</id><content type="html">&lt;p&gt;So I am sitting here, here being London - 315(ish) miles away from my family, halfway through the &lt;a href="http://nhshackday.com"&gt;NHS Hack Day&lt;/a&gt; and having a bit of a personal crisis. I often have these little episodes but this one has been languishing in my dusty old head for a good while - refusing to go away, nagging me every time I have a chance to ponder things. It's the same old thing - why am I doing any of this stuff I do? I don't generally get a great amount of opportunity in work to really stretch my creative urges. That sort of thing happens on my own time and therefore given my other responsibilities (my wife, family and lifes associated activities) is done under a fairly tight deadline. Add this to the fact I bore easily of things that aren't really benefiting people immediately - I tend to flit, easily, between little unfinished projects. But wait.... I did it again. You see back there, about 2 sentences ago I just sidelined my entire life. Thrown between two looming brackets I relegated my wife and family to a byline yet again. Oy vey.&lt;/p&gt;&lt;p&gt;And this brings me, neatly enough, to the point of this entire ramble.&lt;/p&gt;&lt;p&gt;Five years ago I married my awesome wife and in that time I've done a heap of things - relocated twice, had two kids bought a house, got promoted, pissed everyone off, seen a few countries and bought/traded three cars (which get smaller every time oddly enough). And through all this hopping around my wife has been the one true constant. She thinks, most of the time, I don't care about her - after all I'm in London participating in a hackathon on our 5th wedding anniversary because I stupidly didn't put two and two together and notice the clash. So yeah it may damn well seem like she comes in a close second to my hobby/career stuff (you know the stuff I already mentioned was pulling into crisis mode). But it's not true. I can't help the fact I am "emotionally retarded", as my sister once called it, but perhaps once in a while I really should let her know that she means more than she thinks she does. And here it is. A little bit of emotional letting if you will.&lt;/p&gt;&lt;p&gt;Dear Emma - happy anniversary, I hope we have plenty more, thank you for putting up with me, you're aces.&lt;/p&gt;</content></entry><entry><title>Starter for 10 - Scala, Play 2 and Mongo</title><updated>Mon May 07 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/05/07/starter-for-10-scala-play-2-and-mongo/"></link><id>urn:yobriefca-se:feed:post:Starter for 10 - Scala, Play 2 and Mongo</id><content type="html">&lt;p&gt;This post is going to cover the whole "getting up an running" for a simple Scala stack - namely Scala, Play 2 and Mongo. 2012 has been the year of the polyglot for me. I've been working on projects with .NET, Node.js, Ruby and Scala in the mix. I've seen how Rails can really boost developer productivity (compared to the likes of ASP.NET MVC or other, less opinionated, frameworks) but I must say - In terms of developer productivity, at least for me, nothing has come close to the Scala/Play/Mongo combo. I hadn't touched functional programming since SML in university but I found Scala allowed me to express myself in a very terse manner. Combine that with Play and its straightforward, uncomplicated approach to building web apps and Mongo with its schemaless nature then its productivity++ all the way.&lt;/p&gt;&lt;p&gt;It's worth pointing out right away that James Ward already has a &lt;a href="http://www.jamesward.com/2012/02/21/play-framework-2-with-scala-anorm-json-coffeescript-jquery-heroku"&gt;similar post&lt;/a&gt; and a &lt;a href="https://github.com/jamesward/play2torial/blob/master/JAVA.md"&gt;(currently Java only) tutorial&lt;/a&gt;. These are much better but they dont cover the Mongo/Caabah/Salat side of things (not that it is particularly difficult) so at least I've got a leg to stand on.&lt;/p&gt;&lt;h2&gt;Installing&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;Versions used in this post&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;Scala (2.9.1)&lt;/li&gt;
    &lt;li&gt;Play (2.0.1)&lt;/li&gt;
    &lt;li&gt;Mongo (2.0.4)&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;&lt;p&gt;I'm going to assume that you are capable of installing software (by following basic instructions) so I won't waste your time covering this. Here are some links to kick you off,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.scala-lang.org/downloads"&gt;Scala&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.playframework.org/download"&gt;Play 2&lt;/a&gt; (it just goes on your &lt;code&gt;PATH&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.mongodb.org/downloads"&gt;Mongo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Alternatively if you are on OSX - &lt;a href="http://mxcl.github.com/homebrew/"&gt;Homebrew&lt;/a&gt; has got everything you need at the time of putting this together.&lt;/p&gt;&lt;h2&gt;Generating your Application&lt;/h2&gt;&lt;p&gt;Play is a command line tool and can be used to generate your basic app skeleton,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;play new &amp;lt;project name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Just fill in the project name and you will be guided through the basic generation of the project, e.g.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash"&gt;--&amp;gt; play new sampleapp
        _            _
  _ __ | | __ _ _  _| |
| &lt;span class="s1"&gt;'_ \| |/ _'&lt;/span&gt; | &lt;span class="o"&gt;||&lt;/span&gt; |_|
|  __/|_|&lt;span class="se"&gt;\_&lt;/span&gt;___|&lt;span class="se"&gt;\_&lt;/span&gt;_ &lt;span class="o"&gt;(&lt;/span&gt;_&lt;span class="o"&gt;)&lt;/span&gt;
|_|            |__/

play! 2.0.1, http://www.playframework.org

The new application will be created in /Users/kouphax/Projects/temp/sampleapp

What is the application name?
&amp;gt; sampleapp

Which template &lt;span class="k"&gt;do &lt;/span&gt;you want to use &lt;span class="k"&gt;for &lt;/span&gt;this new application?

  1 - Create a simple Scala application
  2 - Create a simple Java application
  3 - Create an empty project

&amp;gt; 1

OK, application sampleapp is created.

Have fun!
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Indeed - fun you shall have.&lt;/p&gt;&lt;h2&gt;Dependencies&lt;/h2&gt;&lt;p&gt;Working with Mongo directly is all well and good but it can be made a bit simpler with a few dependencies. Namely &lt;a href="http://api.mongodb.org/scala/casbah/2.0/"&gt;Casbah&lt;/a&gt; and &lt;a href="https://github.com/novus/salat"&gt;Salat&lt;/a&gt;. Casbah is the official Scala Toolkit for MongoDB and Salat allows you to map to and from the generic Mongo object classes into more concrete case classes.&lt;/p&gt;&lt;p&gt;To add the dependencies you need to update the &lt;code&gt;Build.scala&lt;/code&gt; file in the generated &lt;code&gt;project&lt;/code&gt; directory. Firstly you need to update the &lt;code&gt;appDependencies&lt;/code&gt; to create the dependency on the new two libraries,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;appDependencies&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"com.mongodb.casbah"&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="s"&gt;"casbah"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"2.1.5-1"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;"com.novus"&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="s"&gt;"salat-core"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"0.0.8-SNAPSHOT"&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then you'll need to add the &lt;code&gt;resolvers&lt;/code&gt; to the &lt;code&gt;main&lt;/code&gt; project declaration,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PlayProject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;appName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;appVersion&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;appDependencies&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mainLang&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SCALA&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;
  &lt;span class="n"&gt;resolvers&lt;/span&gt; &lt;span class="o"&gt;++=&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"repo.novus snaps"&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="s"&gt;"http://repo.novus.com/snapshots/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;UPDATE: Since this article is a bit old but still getting plenty of hits I have noticed that people are having issues resolving dependencies on salat and casbah - this is because of a reporsitory change. Simple fix just add the following line to the resolvers Seq above,&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="s"&gt;"casbah"&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="s"&gt;"https://oss.sonatype.org/content/groups/scala-tools/"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;&lt;p&gt;This should be enough to get you going with the rest of this article however there will be some major changes expected in Scala 2.10 that may well fundamentally alter the need to use Salat etc. Be warned.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;And thats the project set up with all its dependencies ready to hack on. So lets drive a spike through this stack and see what it looks like shall we? Yes why not, tallyho old bean!&lt;/p&gt;&lt;h2&gt;The Model &amp;amp; Data Access&lt;/h2&gt;&lt;p&gt;First things first lets create our model. So we are going to write a very basic registration app. People "register" on the site, for fun or something, I dunno as our stakeholders - I just write the code.&lt;/p&gt;&lt;p&gt;Our model is super simple,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;confirm&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;  &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;realName&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now to the interesting part. To persist this in a Mongo data store we need to make use of the two libraries we imported previously,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.mongodb.casbah.Imports._&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.novus.salat._&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.novus.salat.global._&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Registrations&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;registrations&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MongoConnection&lt;/span&gt;&lt;span class="o"&gt;()(&lt;/span&gt;&lt;span class="s"&gt;"sampleapp"&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="s"&gt;"registrations"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="n"&gt;registrations&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grater&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;asObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="n"&gt;toList&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;registration&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;registrations&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;grater&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;].&lt;/span&gt;&lt;span class="n"&gt;asDBObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;registration&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;First things first we create a connection using the &lt;code&gt;MongoConnection&lt;/code&gt; class and grabs the &lt;code&gt;registrations&lt;/code&gt; collection from the &lt;code&gt;sampleapp&lt;/code&gt; store. Now it's worth noting that provided Mongo is up and running we don't actually need to create either the store or the collection - the act of acting on a collection is enough to create both.&lt;/p&gt;&lt;p&gt;Next up I defined two methods &lt;code&gt;all&lt;/code&gt; and &lt;code&gt;create&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code&gt;all&lt;/code&gt; simply pulls out all the collection data using Casbah, performs a map over the collection (which is a collection of &lt;code&gt;MongoDBObject&lt;/code&gt;/&lt;code&gt;DBObject&lt;/code&gt;s) and uses the &lt;code&gt;grater&lt;/code&gt; object provided by Salat to map from these generic objects to our &lt;code&gt;Registration&lt;/code&gt; case class.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;create&lt;/code&gt; simply adds an element (cast from a &lt;code&gt;Registration&lt;/code&gt; class to an acceptable &lt;code&gt;DBObject&lt;/code&gt;) to the collection.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Now isn't that a nice terse data access layer? Yes it is. You want to make changes? Go ahead - the fear of change is gone or much reduced compared to the typical approach.&lt;/p&gt;&lt;h2&gt;Controllers&lt;/h2&gt;&lt;p&gt;Next up we want to add a register controller and update the existing &lt;code&gt;Application&lt;/code&gt; controller.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Application&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Action&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Registrations&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;all&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The only difference here is the &lt;code&gt;Registrations.all&lt;/code&gt; so we can list the registrations in the UI. Lets jump over the the register controller&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Register&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;registrationForm&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Form&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
      &lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nonEmptyText&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nonEmptyText&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"confirm"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nonEmptyText&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
      &lt;span class="s"&gt;"realName"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;
    &lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unapply&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;verifying&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Passwords must match"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;confirmation&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;confirmation&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;})&lt;/span&gt;
  &lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Action&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nc"&gt;Ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;registrationForm&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;register&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Action&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;registrationForm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bindFromRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fold&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;BadRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="o"&gt;)),&lt;/span&gt;
      &lt;span class="n"&gt;registration&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Registrations&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;registration&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;Redirect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;()).&lt;/span&gt;&lt;span class="n"&gt;flashing&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"message"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"User Registered!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;OK thats a lot to take in so lets break it down a bit.&lt;/p&gt;&lt;h3&gt;The Form&lt;/h3&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="n"&gt;registrationForm&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Form&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;mapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nonEmptyText&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nonEmptyText&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"confirm"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nonEmptyText&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"realName"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;
  &lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unapply&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;verifying&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Passwords must match"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Registration&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;confirmation&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;confirmation&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;})&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The Form concept in Play is a bit like a ViewModel of sorts. It defines validation and mapping rules and allows you to pass a structured object between the controller and the view (inluding error information etc.). In fact you aren't even obliged to bind this to a class, they can exists as themselves. My example above shows you how to bind to and from our &lt;code&gt;Registration&lt;/code&gt; class as well as some field and crossfield/global validation.&lt;/p&gt;&lt;h3&gt;Model Binding&lt;/h3&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="n"&gt;registrationForm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bindFromRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fold&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;BadRequest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="o"&gt;)),&lt;/span&gt;
  &lt;span class="n"&gt;registration&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Registrations&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;registration&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;Redirect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;()).&lt;/span&gt;&lt;span class="n"&gt;flashing&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
      &lt;span class="s"&gt;"message"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"User Registered!"&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This little functional approach allows us to bind a request to our &lt;code&gt;registrationForm&lt;/code&gt; and react to invalid input. In the case of an invalid bind the first argument into &lt;code&gt;fold&lt;/code&gt; is executed which returns a bad request with an instance of the form. In the case of the happy path we simply push our &lt;code&gt;registration&lt;/code&gt; instance into the data store and redirect to the &lt;code&gt;index&lt;/code&gt; action along with a flash message.&lt;/p&gt;&lt;h3&gt;Flash&lt;/h3&gt;&lt;p&gt;Flash is like a carefully curated session variable that exists only for the next request. This allows you to pass messages or some very transient data between requests. This is also accessible in the view which is handy.&lt;/p&gt;&lt;h2&gt;Routes&lt;/h2&gt;&lt;p&gt;Routes in Play are written into the &lt;code&gt;config/routes&lt;/code&gt; file and this is used as a basis to generate reverse routes (this gives is us statically typed routes via the &lt;code&gt;routes&lt;/code&gt; object).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;#&lt;/span&gt; &lt;span class="nc"&gt;Routes&lt;/span&gt;
&lt;span class="k"&gt;#&lt;/span&gt; &lt;span class="nc"&gt;This&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="n"&gt;defines&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="n"&gt;routes&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Higher&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt; &lt;span class="n"&gt;routes&lt;/span&gt; &lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;#&lt;/span&gt; &lt;span class="o"&gt;~~~~&lt;/span&gt;

&lt;span class="k"&gt;#&lt;/span&gt; &lt;span class="nc"&gt;Home&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;
&lt;span class="nc"&gt;GET&lt;/span&gt;     &lt;span class="o"&gt;/&lt;/span&gt;                           &lt;span class="n"&gt;controllers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;
&lt;span class="nc"&gt;GET&lt;/span&gt;     &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;                   &lt;span class="n"&gt;controllers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Register&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;
&lt;span class="nc"&gt;POST&lt;/span&gt;    &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;                   &lt;span class="n"&gt;controllers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Register&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;

&lt;span class="k"&gt;#&lt;/span&gt; &lt;span class="nc"&gt;Map&lt;/span&gt; &lt;span class="n"&gt;static&lt;/span&gt; &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="n"&gt;folder&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;assets&lt;/span&gt; &lt;span class="nc"&gt;URL&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
&lt;span class="nc"&gt;GET&lt;/span&gt;     &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;assets&lt;/span&gt;&lt;span class="cm"&gt;/*file               controllers.Assets.at(path="/public", file)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So I added two new routes for register that allows us to view and submit our registration form.&lt;/p&gt;&lt;h2&gt;The Views&lt;/h2&gt;&lt;p&gt;Right lets do something a little neat - remember I mentioned James Ward earlier? Well he curates a little project called &lt;a href="http://webjars.github.com"&gt;webjars&lt;/a&gt; which allows us to add client side resources as managed dependencies to our project. So lets prettify our UI with a bit of &lt;a href="http://twitter.github.com/bootstrap/"&gt;Twitter Bootstrap&lt;/a&gt; love.&lt;/p&gt;&lt;p&gt;First thing we want to do is update our &lt;code&gt;Build.scala&lt;/code&gt; file with webjars resolver and our bootstrap dependency&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;appDependencies&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"com.github.twitter"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;  &lt;span class="s"&gt;"bootstrap"&lt;/span&gt;  &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"2.0.2"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="c1"&gt;// other deps...&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PlayProject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;appName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;appVersion&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;appDependencies&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mainLang&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SCALA&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;resolvers&lt;/span&gt; &lt;span class="o"&gt;++=&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"webjars"&lt;/span&gt;          &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="s"&gt;"http://webjars.github.com/m2"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// other resolvers...&lt;/span&gt;
  &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Then in our &lt;code&gt;main.scala.html&lt;/code&gt; we can reference the bootstrap assets in the normal way,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="html"&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;media=&lt;/span&gt;&lt;span class="s"&gt;"screen"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"@routes.Assets.at("&lt;/span&gt;&lt;span class="na"&gt;stylesheets&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;bootstrap&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="err"&gt;")"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Index (index.scala.html)&lt;/h3&gt;&lt;p&gt;Here we simply list the current registered people,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="html"&gt;@(registrations: List[Registration])(implicit flash: Flash)
@main("Super Simple Sample") {
  @if(flash.data.contains("message")){
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"alert alert-success"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;@flash.get("message")&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  }
  &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"@routes.Register.index"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn btn-primary"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Register&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;table&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"table table-bordered"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;thead&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Username&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;th&amp;gt;&lt;/span&gt;Real Name&lt;span class="nt"&gt;&amp;lt;/th&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/thead&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;tbody&amp;gt;&lt;/span&gt;
      @registrations.map { registration =&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;tr&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;@registration.username&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;td&amp;gt;&lt;/span&gt;@registration.realName&lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
      }
    &lt;span class="nt"&gt;&amp;lt;/tbody&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/table&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The interesting thing about Play 2 views is that they are just Scala. Yep neat. In fact any .NETters will feel quite at home as they are reminiscent of Razor views too.&lt;/p&gt;&lt;h3&gt;Register (register.scala.html)&lt;/h3&gt;
&lt;pre class="highlight"&gt;&lt;code class="html"&gt;@(registrationForm: Form[models.Registration])(implicit flash: Flash)

@import play.api.i18n._
@import views.html.helper._

@input(field: Field, label: String, fieldType: Symbol = 'text) = {
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"control-group @if(field.hasErrors) {error}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"control-label"&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"@field.id"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;@label&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"controls"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"@fieldType.name"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"@field.value"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"@field.id"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      @if(field.hasErrors){
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"help-inline"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          @Messages(field.error.head.message)
        &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
      }
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
}

@main("Super Simple Sample") {
  @form(action = routes.Register.register, 'class -&amp;gt; "form-horizontal") {
    &lt;span class="nt"&gt;&amp;lt;fieldset&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;legend&amp;gt;&lt;/span&gt;Registration&lt;span class="nt"&gt;&amp;lt;/legend&amp;gt;&lt;/span&gt;
      @registrationForm.globalError.map { error =&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;'row'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"alert alert-error"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;@error.message&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      }
      @input(registrationForm("name"), "Username")
      @input(registrationForm("password"), "Password", 'password)
      @input(registrationForm("confirm"), "Confirm Password", 'password)
      @input(registrationForm("realName"), "Real Name")
    &lt;span class="nt"&gt;&amp;lt;/fieldset&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"btn"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Register"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;You can see from the source there that I've crafted my own helper function to keep things nice and DRY and avoid having to write out the twitter bootstrap required structure over and over.&lt;/p&gt;&lt;h2&gt;Running and Wrap Up&lt;/h2&gt;&lt;p&gt;Right about now the solution is done so with a simple &lt;code&gt;play run&lt;/code&gt; you should be able to register and view current registrations. Excellent. If you want to skip everything and just get my code you can clone the &lt;a href="https://github.com/kouphax/scala-sampleapp"&gt;Github Repo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;OK so it's not the most useful app in the world - that was never the intention. But look at the code - it's very terse and the time required to write this post was considerably more than the time required to implement the solution. Joy!&lt;/p&gt;</content></entry><entry><title>The Little Sacrifices</title><updated>Sat Apr 28 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/28/the-little-sacrifices/"></link><id>urn:yobriefca-se:feed:post:The Little Sacrifices</id><content type="html">&lt;p&gt;Late night ramblings... &lt;/p&gt;&lt;p&gt;Productivity can plateau very quickly - especially in a technical process. No matter how good you get at the process or approach, if you keep doing the same thing you can only get so good at it or do it so fast. It's possible, however, by sacrificing some of your precious time to go beyond that plateau. There are things we, the developers, do as part of our development process that are so small and insignificant that we do them without thinking&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Clearing down the values in LocalStorage when debugging a web app&lt;/li&gt;
  &lt;li&gt;Copying assets from one folder to another (aka the macgyver deploy)&lt;/li&gt;
  &lt;li&gt;Navigating to &lt;code&gt;http://localhost:8000&lt;/code&gt; or &lt;code&gt;9000&lt;/code&gt; or some other port&lt;/li&gt;
  &lt;li&gt;Typing &lt;code&gt;rake generate preview&lt;/code&gt; into the terminal about 20 times an hour&lt;/li&gt;
  &lt;li&gt;These are but a few things I've had to consider today alone&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Generally these things are so small we'd just do them by hand, right? But if you take the time to automate these processes or even just refine them in some way it can do wonders for you productivity going forward.&lt;/p&gt;&lt;p&gt;For example the clearing of LocalStorage in Chrome - spent a few minutes writing a bookmarklet that you can execute with 1 click. The macgyver deploy? Write a rake/make/jake/psake/cake task for it. Better yet use &lt;a href="https://github.com/guard/guard"&gt;Guard&lt;/a&gt; or &lt;a href="https://github.com/mynyml/watchr"&gt;Watchr&lt;/a&gt; and perform the deploy on save! Navigatin to various different localhost ports? Try my &lt;a href="http://yobriefca.se/blog/2012/04/03/alfred-hack-for-web-developers/"&gt;alfred script&lt;/a&gt; to minimise the keystrokes. &lt;code&gt;rake generate preview&lt;/code&gt; - alias that bad boy to &lt;code&gt;rgp&lt;/code&gt;! Save yourself those extra keystrokes - you might need them later.&lt;/p&gt;&lt;p&gt;Listen - specifics aren't important here. You'll have your own small repetitive tasks and you'll be able to find your own ways to automate, improve or refine them - but this is the point. You probably don't. At least not as often as you should. Losing a second here and there on one task means very little in the small. In the large, across many of these tasks, well thats a different story. It's hard to justify spending 10 or 20 minutes on something to automate a task that takes 5 seconds to run but even if the time doesn't repay for a while then the fact you have less of a cognitive shift between your primary task and this secondary one should allow you stay in the highly effective, productive mindset to get the job done. &lt;/p&gt;&lt;p&gt;So give it a go, even as a thought experiment - think about the habitual development tasks you do and how you could automate them - you could well suprise yourself (like I have been recently).&lt;/p&gt;</content></entry><entry><title>Strapping Young Lad v0.1.2 Released</title><updated>Thu Apr 19 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/19/strapping-young-lad-v0-dot-1-2-released/"></link><id>urn:yobriefca-se:feed:post:Strapping Young Lad v0.1.2 Released</id><content type="html">&lt;p&gt;I have just released version 0.1.2 of &lt;a href="https://github.com/kouphax/strapping-young-lad"&gt;Strapping Young Lad&lt;/a&gt;. Now we are getting to something that works a bit better across different platforms (now I just have to prove it to you). This release sees the inclusion of multiple token replacement. For example if I create a &lt;code&gt;.ladconfig&lt;/code&gt; in the source repo with an array of tokens&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;token: ['__NAME__', '__NAMESPACE__', '__AUTHOR__']
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The user will be prompted to specify the values for these tokens before the engine goes off and does the replacement. The &lt;code&gt;__NAME__&lt;/code&gt; token is a bit special - you still need to specify the project name as part of the command line call so if SYL come across this token in the the list of configured tokens it will allow you to default that value without having to type it again, e.g.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;lad git@github.com:strapping-young-lad/Loam.git laddy

  Cloning git repository                OK
  Loading configuration                 OK
  Parsing tokens and stuff              
    name (laddy): 
    namespace: test.lad
    author: kouphax
                                        OK
  Processing files                      OK
  Processing directories                OK
  Moving project files                  OK

  Done processing 137 file(s)
  Done processing 22 directories(s)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice I didn't need to specify the name of the project again and then I was prompted for the second and third token values. Simple stuff. &lt;/p&gt;&lt;h2&gt;Next up&lt;/h2&gt;&lt;p&gt;So there are a few outstanding efforts to go in now. Namely push some more templates into the SYL repo and spin up a site that lets you search said templates and document all the little features of SYL. They will come in time.&lt;/p&gt;&lt;p&gt;This is the life!&lt;/p&gt;</content></entry><entry><title>Infoshare 2012 Talk</title><updated>Thu Apr 19 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/19/infoshare-2012-talk/"></link><id>urn:yobriefca-se:feed:post:Infoshare 2012 Talk</id><content type="html">&lt;h2&gt;Slides&lt;/h2&gt;
&lt;script async="async" class="speakerdeck-embed" data-id="4f911c9791b203001f00cc2b" data-ratio="1.3333333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/41132110?title=0&amp;amp;byline=0&amp;amp;portrait=0" width="500" height="275" frameborder="0" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;h2&gt;Resources&lt;/h2&gt;&lt;p&gt;I mentioned a lot of technologies during my talk so here are the ones I mentioned&lt;/p&gt;&lt;h3&gt;Web Frameworks&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://jquerymobile.com/"&gt;jQuery Mobile&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.sencha.com/products/touch"&gt;Sencha Touch&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Design&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://codiqa.com/"&gt;Codiqa&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.sencha.com/products/architect/"&gt;Sencha Architect 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Web Containers&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://phonegap.com/"&gt;Phonegap&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://incubator.apache.org/cordova/"&gt;Cordova&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Cross Platform&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://xamarin.com/monotouch"&gt;Monotouch&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://xamarin.com/monoforandroid"&gt;Monodroid&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.anscamobile.com/corona/"&gt;Corona&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://build.phonegap.com/"&gt;Phonegap:Build&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.appcelerator.com/platform/titanium-sdk"&gt;Titanium&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Server Platforms&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://parse.com/"&gt;Parse&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.kinvey.com/"&gt;Kinvey&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Cloud Services&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.flurry.com/"&gt;Flurry&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://urbanairship.com/"&gt;Urban Airship&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Testing &amp;amp; Debugging&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://pivotal.github.com/jasmine/"&gt;Jasmine&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://www.lesspainful.com/"&gt;Less Painful&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.testingwithfrank.com/"&gt;Frank&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://people.apache.org/~pmuellr/weinre/"&gt;Weinre&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Beta Testing&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="https://testflightapp.com/"&gt;Testflight&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.hockeyapp.net/"&gt;Hockey&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Comments&lt;/h2&gt;&lt;p&gt;So how did it go? From my perspective it could have been better - I'm not sure why started "ummmmming" all the time - I had plenty to say (and I think I talked too fast as well). I just started in a semi-flustered state for reasons unknown. I seen a few decent comments on twitter so I guess it was fairly well received thats what matters. If anyone has any other observations, no matter how harsh, let me know.&lt;/p&gt;&lt;p&gt;One more note - Gdasnk is awesome - the technical people are so engaged and enthusiastic, the place itself is really interesting and I managed try my new favourite shot - &lt;a href="http://www.drinksmixer.com/drink10324.html"&gt;Mad Dog&lt;/a&gt;. Thanks for the opportunity Gdasnk and Infoshare. Hopefully I can back here and spend more time in the city soon.&lt;/p&gt;&lt;p&gt;Na zdrowie&lt;/p&gt;</content></entry><entry><title>Strapping Young Lad - Project Template Token Replacer and Bootstrapper</title><updated>Mon Apr 16 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/16/strapping-young-lad-project-template-token-replacer-and-bootstrapper/"></link><id>urn:yobriefca-se:feed:post:Strapping Young Lad - Project Template Token Replacer and Bootstrapper</id><content type="html">&lt;p&gt;Today I have released the super simple, rough-as-hell, barely &lt;a href="http://en.wikipedia.org/wiki/Minimum_viable_product"&gt;MVP&lt;/a&gt; &lt;a href="https://rubygems.org/gems/lad"&gt;Strapping Young Lad&lt;/a&gt;(&lt;a href="https://github.com/kouphax/strapping-young-lad"&gt;Source&lt;/a&gt;). It's fulfils a need I keep having but never really fixing. &lt;/p&gt;&lt;h2&gt;Evelvator Pitch&lt;/h2&gt;&lt;p&gt;SYL, or lad for short, will take a git repo, clone it and replace a bunch of tokens in it with the values you've supplied. &lt;/p&gt;&lt;h2&gt;Say Wha?&lt;/h2&gt;&lt;p&gt;This means you can create a bunch of base solution/project types for any language and SYL will simply do some basic file/folder renaming and file content replacing for a given token. For example doing the following,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;lad /path/to/git/repo MyNewProject
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Will do the following things,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Clone the repo at &lt;code&gt;path/to/git/repo&lt;/code&gt; into a temporary folder&lt;/li&gt;
  &lt;li&gt;Remove the &lt;code&gt;.git&lt;/code&gt; folder from the repo&lt;/li&gt;
  &lt;li&gt;Check for the existence of a &lt;code&gt;.ladconfig&lt;/code&gt; in the freshly cloned repo&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
  &lt;li&gt;If one exists it loads the config setting&lt;/li&gt;
  &lt;li&gt;Otherwise it falls back to the defaults&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
  &lt;li&gt;Replace all instances of the configured token (eg. &lt;code&gt;__NAME__&lt;/code&gt;) in all files, directories and file contents with the project name (in our case &lt;code&gt;MyNewProject&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;Copy the new folder over to your current working directory (&lt;code&gt;./MyNewProject&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;What this allows you to do is have a standard project template for pretty much anything (.NET, Ruby, Scala - listen I mean anything - if it a collection of folders and files in a git repo then you set) and create a new instance with a few keystrokes - saving you time and potential RSI.&lt;/p&gt;&lt;p&gt;SYL was inspired by &lt;a href="https://github.com/chucknorris/warmup"&gt;WarmuP&lt;/a&gt; which is itself a gem but it requires you to be running .NET which for a lot of my requirement isn't going to cut it.&lt;/p&gt;&lt;p&gt;If you want to see what a typical template project looks like try &lt;a href="https://github.com/amirrajan/Loam"&gt;Amir's Loam repo&lt;/a&gt;. This is a WarmuP project but works just fine. With SYL you can create a &lt;code&gt;.ladconfig&lt;/code&gt; file to override some of the conventions - get the source README linked above.&lt;/p&gt;&lt;h2&gt;Whats Next&lt;/h2&gt;&lt;p&gt;Yep it's pretty damn basic right now but it satifies most of my needs right now. There are a few things I want to add for myself but I didn't release this for ME, I am pretty sure this will be of use to someone in some community in some shape or form. With that in mind here are some features I think would take this to the next level,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Specifiying multiple tokens e.g. &lt;code&gt;__NAME__&lt;/code&gt; and &lt;code&gt;__NAMESPACE__&lt;/code&gt; and &lt;code&gt;__YEAR__&lt;/code&gt; etc. etc.&lt;/li&gt;
  &lt;li&gt;Post processing actions (e.g. &lt;code&gt;bundle install&lt;/code&gt; or &lt;code&gt;npm install&lt;/code&gt; or &lt;code&gt;sbt compile&lt;/code&gt; or something like that)&lt;/li&gt;
  &lt;li&gt;A catalog - I have a &lt;a href="https://github.com/strapping-young-lad"&gt;github organisation&lt;/a&gt; with no repos setup right now but this would be a brilliant place to hold a load of templates compatible with SYL, don't ya think? Then I could roll a site like rubygems or npmjs.org and make the projects searchable etc. If you build it, they will come. Maybe, who knows.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;So if you like it - HIGHFIVE - if not - it's all good. Now go break it and criticise me. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;So we'll criticise him, because he can take it.... [The Dark Knight, Approx.]&lt;/p&gt;
&lt;/blockquote&gt;</content></entry><entry><title>Scala - A Tragedy in Two Parts - Part 1</title><updated>Thu Apr 12 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/12/scala-a-tragedy-in-two-parts-part-1/"></link><id>urn:yobriefca-se:feed:post:Scala - A Tragedy in Two Parts - Part 1</id><content type="html">&lt;p&gt;The tragedy being we don't use it all the time!&lt;/p&gt;&lt;p&gt;This presentation is the first part in the 2 part series "Scala - A Tragedy in Two Parts". In this presentation I go fast and dive into a range of Scala code snippets pointing out some of the weird and wonderful features of the language.&lt;/p&gt;&lt;h2&gt;Recorded Session&lt;/h2&gt;&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/40255430?title=0&amp;amp;byline=0&amp;amp;portrait=0" width="500" height="375" frameborder="0" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt; &lt;/p&gt;&lt;p&gt;&lt;a href="http://vimeo.com/40255430"&gt;Scala - A Tragedy in Two Parts - Part 1 - The Language&lt;/a&gt; from &lt;a href="http://vimeo.com/kouphax"&gt;James Hughes&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Slides&lt;/h2&gt;
&lt;script async="async" class="speakerdeck-embed" data-id="4f86b1fcd1325b0022019eea" data-ratio="1.3333333333333333" src="//speakerdeck.com/assets/embed.js"&gt;&lt;/script&gt;</content></entry><entry><title>Scala - A Tragedy in Two Parts</title><updated>Thu Apr 12 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/scala.pdf"></link><id>urn:yobriefca-se:feed:post:Scala - A Tragedy in Two Parts</id><content type="html"></content></entry><entry><title>handsfread - Hands Free Page Scrolling Bookmarklet</title><updated>Tue Apr 10 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="http://handsfread.herokuapp.com/"></link><id>urn:yobriefca-se:feed:post:handsfread - Hands Free Page Scrolling Bookmarklet</id><content type="html"></content></entry><entry><title>Alfred Hack for Web Developers</title><updated>Mon Apr 02 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/02/alfred-hack-for-web-developers/"></link><id>urn:yobriefca-se:feed:post:Alfred Hack for Web Developers</id><content type="html">&lt;p&gt;I've been a fan of &lt;a href="http://www.alfredapp.com/"&gt;Alfred&lt;/a&gt; for some time and use it to make pretty much everything a keystroke away. I've thrown a very small little extension that reduces the time to jump between console and newly started web app. Generally during development we need to run our app jump to &lt;code&gt;localhost:PORT&lt;/code&gt; and make sure it's working. If, like me, you have a lot of different apps and platforms (Scala, Jekyll, Rails, Sinatra, Express to name a few) then &lt;code&gt;PORT&lt;/code&gt; becomes the one thing you always need to type and the browsers auto predict is generally not that helpful in this situation.&lt;/p&gt;&lt;p&gt;Alfred to the rescue here, just create a script as shown below and then running alfred and typing &lt;code&gt;l 9000&lt;/code&gt; you can launch your default browser at &lt;code&gt;http://localhost:9000&lt;/code&gt;. Very simple but saves me heaps of time at the minute.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/alfredscript.png" alt="Screenshot" _="_" /&gt;&lt;/p&gt;</content></entry><entry><title>Rails is not a gateway drug</title><updated>Mon Apr 02 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/02/rails-is-not-a-gateway-drug/"></link><id>urn:yobriefca-se:feed:post:Rails is not a gateway drug</id><content type="html">&lt;blockquote&gt;&lt;p&gt;WARNING!!! - a negative opinion on Rails lies ahead. Everything here needs to be taken with an &lt;strong&gt;IMHO&lt;/strong&gt; and, if you feel so inclined, tell me to go mind my own business.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I know I'm stepping into very dangerous territory here but I've come to the very solid conclusion that &lt;a href="http://rubyonrails.org/"&gt;Rails&lt;/a&gt;, Ruby's heavily opinionated web framework, is not the gateway drug people claim. I've heard the claims that if you give &lt;a href="http://rubyonrails.org/"&gt;Rails&lt;/a&gt; a try, coming from some other web stack on any other language, you'll fall in love with Ruby and Rails and write a big long blog post about how you are leaving .NET (or Java or whatever) behind. I really dont get it.... the only feeling that Rails leaves me with - having been using it in anger for about a month now - is frustration. But before you drive rusty nails into my hands and feet let me at least explain where I am coming from.&lt;/p&gt;&lt;h2&gt;Previous attempts&lt;/h2&gt;&lt;p&gt;I've been binging on Ruby for the last few months but this hasn't been my first time - it's been my only successful time but certianly not my first. A year previous I had given Ruby/Rails a shot and it never stuck (in fact not once but at least twice). Coming from a predominantly .NET/ASP.NET MVC background I decided the best approach would be to cut my teeth on a medium sized web based project and therefore jumped right into Rails. It only took a short time before the whole effort felt like a slog full of weird semi-magic behaviour. I gave up.&lt;/p&gt;&lt;h2&gt;Doing things differently&lt;/h2&gt;&lt;p&gt;This time around I decided to do things a little differently - rather than run with a web project I just tinkered with the language and eventually tried porting a &lt;a href="http://yobriefca.se/blog/2012/03/12/sentamentalizer/"&gt;.NET project over to Ruby&lt;/a&gt;. Fair bit of Ruby code and a small &lt;a href="http://sinatrarb.com"&gt;sinatra&lt;/a&gt; app later - it clicked. I got the whole Ruby love affair, it was lovely and I was three lines into my &lt;em&gt;"F**k you .NET, you're worse than Java and I hate you and you smell"&lt;/em&gt; blog post (not factual!!) when I realised I needed to give Ruby more time to bed in and, out of fairness, I had to at least experience Rails. &lt;/p&gt;&lt;h2&gt;Present Day&lt;/h2&gt;&lt;p&gt;And with that I discovered that Rails had been my problem. I find it incredibly frustrating. I don't want to dive into bashing Rails, thats not my intent, - I'm sure people find it really productive - but it's not for me. So here is a very high level executive summary of the problems I've had with Rails&lt;/p&gt;&lt;p&gt;&lt;strong&gt;The Command Line Interface&lt;/strong&gt; &lt;/p&gt;&lt;p&gt;The CLI stuff has just a little bit too many features and options that make it a difficult thing to approach without extensive research. When I watched some of the &lt;a href="http://railscasts.com/"&gt;Railscasts&lt;/a&gt; I found myself going "wait, HOW did he do that?" more than once. Choice is good but surely Rails is supposed to be easy for the beginner? Yet I feel the default options are not the ones I would have picked. You know what - I want to be hand-held once in a while, I'm proud to admit that.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Too Much Magic&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Rails is very heavily convention based but often there is simply too much convention and I've been left baffled a few times. Some of the convention is well documented and some of it less so. When a convention is not obvious it becomes magic and it really feels Rails has a lot of "magic". Thats not something I want. To be honest I'd rather write the same 2/3 lines of boilerplate if it meant that, to a new developer, they could understand the convention better. Take this very trivial example,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby "&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And the same in ASP.NET MVC&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
	&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
	&lt;span class="p"&gt;{&lt;/span&gt;
		&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
	&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To me that one line &lt;code&gt;return View()&lt;/code&gt; tell me that this action will return a View, and now I can infer the convention of &lt;code&gt;home/index.cshtml&lt;/code&gt; that wee bit easier. When you start adding in more conventions, a little bit of extra code makes all the difference. Conventions don't scale unless clearly marked IMHO.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Bloatyness&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;When you generate scaffold in a Rails app, as a typical new developer would, you end up with a awful lot of files. If I generate a new entity for example this is what I am greeted with,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="bash "&gt;--&amp;gt; rails generate scaffold MyEntity name:String age:Int                         !6580
      invoke  active_record
      create    db/migrate/20120403130542_create_my_entities.rb
      create    app/models/my_entity.rb
      invoke    test_unit
      create      &lt;span class="nb"&gt;test&lt;/span&gt;/unit/my_entity_test.rb
      create      &lt;span class="nb"&gt;test&lt;/span&gt;/fixtures/my_entities.yml
       route  resources :my_entities
      invoke  scaffold_controller
      create    app/controllers/my_entities_controller.rb
      invoke    slim
      create      app/views/my_entities
      create      app/views/my_entities/index.html.slim
      create      app/views/my_entities/edit.html.slim
      create      app/views/my_entities/show.html.slim
      create      app/views/my_entities/new.html.slim
      create      app/views/my_entities/_form.html.slim
      invoke    test_unit
      create      &lt;span class="nb"&gt;test&lt;/span&gt;/functional/my_entities_controller_test.rb
      invoke    helper
      create      app/helpers/my_entities_helper.rb
      invoke      test_unit
      create        &lt;span class="nb"&gt;test&lt;/span&gt;/unit/helpers/my_entities_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/my_entities.js.coffee
      invoke    scss
      create      app/assets/stylesheets/my_entities.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Don't get me wrong - it's nice to be given all these things for free, but it's ultimatley stuff I end up ripping out anyway. In .NET there is a running joke that the "Empty ASP.NET MVC Project" Template is anything but. To me this is the same issue except it's something that I'd have to clean up everytime I use the &lt;code&gt;generate scaffold&lt;/code&gt; command. This seems worse to me.&lt;/p&gt;&lt;h2&gt;Current Thoughts&lt;/h2&gt;&lt;p&gt;I'm not sure were Rails is supposed to stand. I hear it's great for beginners or for big sites but I couldn't recommend it for either right now. I realise I should really give it more time before rejecting it entirely but I feel that would be more time wasted. Rails doesn't make me more productive, at least not right now... perhaps once I repeat the same steps over and over and they become natural I'll see a greater boost in productivity (Ruby on the other hand felt natural and great right away). Perhaps if I shun the generators and roll my own stuff then I'll be more productive - but then why not use another framework that comes without all the gubbins I don't need? Sinatra is nice and simple and I can build upon it rather than breakling it apart. &lt;a href="http://ramaze.net/"&gt;Ramaze&lt;/a&gt; seems like a great "next step up" from Sinatra but without as much (perceived) bloat as Rails. I've also followed the recent &lt;a href="http://www.kickstarter.com/projects/1397300529/railsapp"&gt;Kickstarter debacle&lt;/a&gt; from Yehuda - and I agree with him and his mission.&lt;/p&gt;&lt;p&gt;So what I am trying to say is - if you are a person wanting to give the Ruby world a fair shake - DONT start on Rails. Start simpler and later, if you want, when you've been throughly sold on the Ruby way, try Rails to see what you think. Start with Ruby, port some code, write a Sinatra app, watch the &lt;a href="http://railscasts.com/"&gt;Railscasts&lt;/a&gt; as they are amazing then consider immersing yourself in Rails.&lt;/p&gt;&lt;h2&gt;Educate Me&lt;/h2&gt;&lt;p&gt;Finally - educate me. I'm not some sort of oracle proclaiming the death of a framework that thousands of people love. I'm not going to make anyone stop and think "I've been doing wrong all my life - screw Rails". If I'm wrong - tell me. If you think I need to give Rails another look - tell me why, tell me how. If you care - show me what I'm missing.&lt;/p&gt;</content></entry><entry><title>Government Digital Service</title><updated>Mon Apr 02 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/gds.pdf"></link><id>urn:yobriefca-se:feed:post:Government Digital Service</id><content type="html"></content></entry><entry><title>Setting the Selected Option in a Select with Selenium in Scala</title><updated>Sun Apr 01 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/04/01/setting-the-selected-option-in-a-select-with-selenium-in-scala/"></link><id>urn:yobriefca-se:feed:post:Setting the Selected Option in a Select with Selenium in Scala</id><content type="html">&lt;p&gt;Almost a perfect bit of alliteration there in that title - right? Anyway I've been doing a bit of work with bare-metal Selenium stuff in Scala recently and one of the things that kept me stumped for a while was setting the selected &lt;code&gt;option&lt;/code&gt; for a particular &lt;code&gt;select&lt;/code&gt; element.&lt;/p&gt;&lt;p&gt;I looked around the interwebs and didn't really find anything - now I'm not saying it isn't out there, I am certain it is, but I couldn't find it. So I decided to document how I did it in case any lone sole is stuck in a similar situation...&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala"&gt;  &lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;findElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;By&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"year"&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;
    &lt;span class="n"&gt;findElements&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;By&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tagName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"option"&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;
    &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;
    &lt;span class="n"&gt;setSelected&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So here I am simply finding the second &lt;code&gt;option&lt;/code&gt; of the "year" &lt;code&gt;select&lt;/code&gt; element and setting it selected. I know it looks obvious now but trust me I was scratching my head for an age on this one.&lt;/p&gt;&lt;p&gt;I hope it helps some one, eventually :)&lt;/p&gt;</content></entry><entry><title>Behaviour Driven (Development|Design) in .NET</title><updated>Wed Mar 28 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="/presentations/bdd-in-dotnet.pdf"></link><id>urn:yobriefca-se:feed:post:Behaviour Driven (Development|Design) in .NET</id><content type="html"></content></entry><entry><title>Done Spoked at DevWeek</title><updated>Tue Mar 27 23:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/27/done-spoked-at-devweek/"></link><id>urn:yobriefca-se:feed:post:Done Spoked at DevWeek</id><content type="html">&lt;p&gt;So I made it through my talk at DevWeek which might sound a little odd but it took a whole bottle of cough syrup to do it. Not having been sick for most of the winter my body decided a day before DevWeek to go into shut down. I spent Tuesday in bed with very little voice. Thankfully the head colds hold on my vocal chords relented this morning and I think I managed to project as best I could. &lt;/p&gt;&lt;p&gt;So initial thoughts of how it went? I honestly think I'm getting better at this presenting thing so that must be a good thing, unless I was TERRIBLE before. The 2 main annoyances that have been raised thus far have been&lt;/p&gt;&lt;h2&gt;1. Too Short&lt;/h2&gt;&lt;p&gt;I did my usual "talk really fast and finish earlier than planned" thing I sometimes do. But there was plenty of discussion afterwards which I tought was interesting. FWIW when I present this to myself it takes longer. My internal monologue must be slower than the way I speak. Apologies to anyone that felt it was "too short".&lt;/p&gt;&lt;h2&gt;2. Not enough Visual Studio&lt;/h2&gt;&lt;p&gt;I opted to play it safe end embed code into my slides rather than code live - I didn't go unnoticed and I had a few comments that "moar codez" would have been good. Totally agree. &lt;/p&gt;&lt;h2&gt;Other Stuff&lt;/h2&gt;&lt;p&gt;I opted to change my slide deck between submitting them for print and presenting. I did it out of desire to devlier a better, more structured, talk (my first set being quite short as I was going for a "free-form" type of presentation). Probably not the most professional approach to do this but at least I can say my heart was in the right place :) Here are offending articles BTW&lt;/p&gt;
&lt;div style="max-width:500px;margin:0 auto;"&gt;
	&lt;script src="http://speakerdeck.com/embed/4f719e7aca692c002101c187.js"&gt;&lt;/script&gt;
&lt;/div&gt;&lt;p&gt;Code that was presented is available, as always, in/on my &lt;a href="https://github.com/kouphax/bddindotnet-code"&gt;Github Repo&lt;/a&gt;. Thanks to all who attended and any other feedback you have throw it my way so I can get better at this talking malarky.&lt;/p&gt;&lt;p&gt;Also thanks to the folks at DevWeek for the opportunity - hopefully you'll have me back at some point.&lt;/p&gt;</content></entry><entry><title>Slim Generator for Octopress</title><updated>Thu Mar 22 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/22/slim-generator-for-octopress/"></link><id>urn:yobriefca-se:feed:post:Slim Generator for Octopress</id><content type="html">&lt;p&gt;So I've been overhauling my web site, which is based on &lt;a href="http://octopress.org"&gt;Octopress&lt;/a&gt;, lately and have had to jump into the working of Octopress/Jekyll. It's obviously not the most complicated system in the world but hey it was new to me.&lt;/p&gt;&lt;p&gt;Anyway my current setup features a lot of html based pages (the default markdown approach didn't fit due to styling needs) and it was getting rather noisy. So I wrote, as there didn't seem to be one out there, a &lt;a href="http://slim-lang.com/"&gt;Slim&lt;/a&gt; generator. And here it is, step back it's VERY COMPLEX....&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="ruby "&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Jekyll&lt;/span&gt;
  &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'slim'&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SlimConverter&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Converter&lt;/span&gt;
    &lt;span class="n"&gt;safe&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;priority&lt;/span&gt; &lt;span class="ss"&gt;:low&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;ext&lt;/span&gt; &lt;span class="o"&gt;=~&lt;/span&gt; &lt;span class="sr"&gt;/slim/i&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;output_ext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="s2"&gt;".html"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;begin&lt;/span&gt;
        &lt;span class="ss"&gt;Slim&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="ss"&gt;:Template&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt;
      &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;StandardError&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
        &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"!!! SLIM Error: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Just drop this file (call it anything) into your plugins folder in Jekyll/Octopress and any file with the &lt;code&gt;slim&lt;/code&gt; extension will use this converter to generate HTML. No more noisy HTML for me!&lt;/p&gt;</content></entry><entry><title>rbenv and CodeRunner</title><updated>Thu Mar 22 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/22/rbenv-and-coderunner/"></link><id>urn:yobriefca-se:feed:post:rbenv and CodeRunner</id><content type="html">&lt;p&gt;You know &lt;a href="http://krillapps.com/coderunner/"&gt;CodeRunner&lt;/a&gt; right? If not maybe you should go check it out. It's and aptly named little tool for running code snippets in various languages much like what &lt;a href="http://www.linqpad.net/"&gt;LinqPad&lt;/a&gt; does for the .NET folks. I've been using it extensivley recently to test out little Ruby and Scala snippets. Thing is though - I hit a snag. By default CodeRunner will run the system ruby executable (1.8 in OSX) and I generally use &lt;a href="https://github.com/sstephenson/rbenv"&gt;rbenv&lt;/a&gt; along with version 1.9.whatever so some of my scripts started behaving a little... odd.&lt;/p&gt;&lt;p&gt;But hey it's an easy fix,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Open your CodeRunner Preferences&lt;/li&gt;
  &lt;li&gt;Select the Languages tab&lt;/li&gt;
  &lt;li&gt;Select Ruby in the Language List&lt;/li&gt;
  &lt;li&gt;Change the run command from &lt;code&gt;ruby $filename&lt;/code&gt; to &lt;code&gt;~/.rbenv/shims/ruby $filename&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Before&lt;/h2&gt;&lt;p&gt;![](/images/blog/coderunnerruby/before.png)&lt;/p&gt;&lt;h2&gt;After&lt;/h2&gt;&lt;p&gt;![](/images/blog/coderunnerruby/after.png)&lt;/p&gt;&lt;p&gt;Blam - global rbenv version is now the goto version for CodeRunner. OK so it won't work with rbenv locals but I am sure you can tweak it to make it work (I haven't needed it so never looked).&lt;/p&gt;</content></entry><entry><title>Tinkering with Spark: A Micro Web Framework for Java</title><updated>Tue Mar 20 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/20/tinkering-with-spark-a-micro-web-framework-for-java/"></link><id>urn:yobriefca-se:feed:post:Tinkering with Spark: A Micro Web Framework for Java</id><content type="html">&lt;div style="text-align:center;margin-bottom:20px;"&gt;
&lt;img src="/images/blog/spark_logo_blue.png" style="border:none; box-shadow:none; -webkit-box-shadow:none;" /&gt;
&lt;/div&gt;&lt;p&gt;You can't call yourself a language these days without having your own &lt;a href="http://sinatrarb.com"&gt;Sinatra&lt;/a&gt; clone. Not even, it seems, if you are Java. I know - it sounds odd that Java, the language that turns XML into stacktraces, has a micro web framework of it's own - &lt;a href="http://sparkjava.com"&gt;Spark&lt;/a&gt;. I'm well aware as of JRE 1.6 has a built in &lt;code&gt;HttpServer&lt;/code&gt; class but there is such as thing as &lt;strong&gt;too close&lt;/strong&gt; to the metal :) Also Sparks logo is fairly epic.&lt;/p&gt;&lt;p&gt;&lt;a href="http://sparkjava.com"&gt;Spark&lt;/a&gt;'s site has a great little piece about the "why" and that is what stuck with me. Especially this bit,&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Its intention is to be used by Java developers that want or are required to develop in pure Java. &lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I know a few people who are "required" to develop in pure Java and anything that might make their lives easier would be welcomed. Being completely out of loop with Java since before Maven made an appearance I had a lot of catching up to do just to get the project running. In fact I went with SBT as it made things a lot easier for me and my mavenless brain. I did have to tinker with the dependencies in the &lt;code&gt;ivy2&lt;/code&gt; directory due to a &lt;a href="https://github.com/perwendel/spark/pull/15"&gt;known issue&lt;/a&gt; but if I can do it anyone interested in this can to - if not give me a shout.&lt;/p&gt;&lt;h2&gt;App.java&lt;/h2&gt;&lt;p&gt;A Spark app simply uses &lt;code&gt;main&lt;/code&gt; as an entry point. Within the method you can call certain methods to set up routes and filters and whatnots that your app will respond to. For example&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;static&lt;/span&gt; &lt;span class="n"&gt;spark&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Spark&lt;/span&gt;&lt;span class="o"&gt;.*;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;spark.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.io.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Standard Java stuff here, bringing in some of the necessary &lt;code&gt;spark&lt;/code&gt; resources,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;    &lt;span class="n"&gt;before&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nd"&gt;@Override&lt;/span&gt;
      &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;authenticated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;queryParams&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"secret"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;authenticated&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
          &lt;span class="n"&gt;halt&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Incorrect password, hacker alert, hacker alert!!!!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Spark allows us to define filters that run before and after requests. It is also possbile to restrict them to certain routes or simply catch all routes - for example my filter only matches the "/" route. A couple of other things are worth mentioning here,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The &lt;code&gt;Request&lt;/code&gt; object gives you access to many things you would expect i.e. the query parameters. It gives you so much more than this but I'll leave the &lt;a href="http://www.sparkjava.com/readme.html#title2"&gt;docs&lt;/a&gt; to describe this.&lt;/li&gt;
  &lt;li&gt;Just like in Sinatra it is possible to simply &lt;code&gt;halt&lt;/code&gt; the request chain at any time. Here I am halting execution unless the request conforms to my very strict and highly secure authentication scheme.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="highlight"&gt;&lt;code class="java "&gt;    &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nd"&gt;@Override&lt;/span&gt;
      &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// extract the name from the request&lt;/span&gt;
        &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;queryParams&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// set the response type&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text/html"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// return some html&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"&amp;lt;DOCTYPE html&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"&amp;lt;html&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"  &amp;lt;head&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"  &amp;lt;/head&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"  &amp;lt;body&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"    &amp;lt;h1&amp;gt;Hello "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;", Spark here.  Howrya?&amp;lt;/h1&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"  &amp;lt;/body&amp;gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
          &lt;span class="s"&gt;"&amp;lt;/html&amp;gt;"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;});&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally I am defining a route ("/") that responds to the GET verb and introspects the query parameters returning a very complex view. Routes can also be parameterized (&lt;code&gt;/user/:name&lt;/code&gt; for example). Even given the slightly verbose nature of Java syntax this example seems clean enough (albeit a rather simple example).&lt;/p&gt;&lt;h2&gt;SBT Build File&lt;/h2&gt;&lt;p&gt;For the curious among you (you got this far so you must be slightly curious... and rather odd) the SBT file required to get this bad boy running is nice and simple,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="scala "&gt;&lt;span class="n"&gt;resolvers&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"Spark Repository"&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="s"&gt;"http://www.sparkjava.com/nexus/content/repositories/spark/"&lt;/span&gt;

&lt;span class="n"&gt;libraryDependencies&lt;/span&gt; &lt;span class="o"&gt;++=&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"spark"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"spark"&lt;/span&gt;  &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"0.9.9.3-SNAPSHOT"&lt;/span&gt; &lt;span class="n"&gt;exclude&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"org.apache.commons"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"commons-io"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A quick &lt;code&gt;sbt run&lt;/code&gt; command and we are running a standalone Spark app (powered by an embedded Jetty server). Aces.&lt;/p&gt;&lt;h2&gt;Mopping Up&lt;/h2&gt;&lt;p&gt;OK so thats the basics of the framework but then again there really isn't much other stuff besides picking a port and redirecting. This means there is some things still missing that would really make this framework really stand out,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;View rendering. Even if it is just plain HTML it would be a big leap forward (turns out an &lt;a href="https://github.com/perwendel/spark/issues/1"&gt;issue&lt;/a&gt; is already open too).&lt;/li&gt;
  &lt;li&gt;Static Content. Would be nice to define a rule for serving static content without having to define a route or filter manually&lt;/li&gt;
  &lt;li&gt;Session support. Again an &lt;a href="https://github.com/perwendel/spark/pull/12"&gt;issue and pull request&lt;/a&gt; is already available for this.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Good work &lt;a href="https://github.com/perwendel/"&gt;perwendel&lt;/a&gt; keep it up :)&lt;/p&gt;&lt;p&gt;As always my code samples are available on &lt;a href="https://github.com/kouphax/spark-tinkering"&gt;Github&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Speaking at NDC Oslo</title><updated>Mon Mar 19 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/19/speaking-at-ndc-oslo/"></link><id>urn:yobriefca-se:feed:post:Speaking at NDC Oslo</id><content type="html">&lt;p&gt;&lt;img src="/images/blog/ndc.png" style="border:none; box-shadow:none; -webkit-box-shadow:none; width:100%;" /&gt;&lt;/p&gt;&lt;p&gt;Mike Cohn, Dan North, Robert C. Martin, Udi Dahan, Gojko Adzic, Billy Hollis, Laurent Bugnion, Venkat Subramaniam, Brad Wilson, Phil Haack, Rob Conery, Greg Young, Zach Holman..... and then me. So someone was drinking when deciding the line up for NDC Oslo right?&lt;/p&gt;&lt;p&gt;Seriously though I've been given one of the best opportunites of my career to talk alongside some of the shining lights of our industry at NDC Oslo so I better not drop the ball. Thank you to whoever gave me the opportunity. But hey if I mess up I've got another 2 shots at it... thats right I'm speaking not once, not twice but thrice.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://ndcoslo.oktaset.com/t-4960"&gt;.NET On a Diet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://ndcoslo.oktaset.com/t-4961"&gt;MonoTouch - C# + iOS = Good Times&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://ndcoslo.oktaset.com/t-4872"&gt;Mobile, How Do I Develop Thee? Let Me Count the Ways&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Still blown away by this, if you can't tell - given the calibre of people talking this is going to be a highlight of the year for sure. Also it couldn't have come at a better time - there is so much I've wanted to say about things like the OSS world in .NET, Mono and mobile development. Now I've got the perfect opportunity.&lt;/p&gt;&lt;p&gt;Now whats this .NET thing I keep hearing about :)&lt;/p&gt;</content></entry><entry><title>sentimentalizer: A Ruby port of Sentan</title><updated>Mon Mar 12 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/12/sentimentalizer-a-ruby-port-of-sentan/"></link><id>urn:yobriefca-se:feed:post:sentimentalizer: A Ruby port of Sentan</id><content type="html">&lt;p&gt;In a bid to be at least 1% less stupid I decided to jump into Ruby. Properly this time, no tinkering with Rails or watching a few videos. So the first thing I did was take &lt;a href="https://twitter.com/#!/martinrue"&gt;@martinrue&lt;/a&gt;'s &lt;a href="https://github.com/martinrue/Sentan"&gt;Sentan&lt;/a&gt; project, fork it, made it run on mono (just because), ported it over to Ruby and renamed it &lt;a href="https://github.com/kouphax/sentimentalizer/"&gt;sentimentalizer&lt;/a&gt; (but keeping all necessary attribution to Martins work in the README of course).&lt;!-- more --&gt; I did this for two reasons,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;I wanted something fairly meaty to actually port over to Ruby in an effort to get a more-than-superficial feel for the language&lt;/li&gt;
  &lt;li&gt;I might have a need for such a service in an upcoming hackathon (though the original .NET version would have been fine too)&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;This is in no way some sort of new project or intended for some production environment.&lt;/p&gt;&lt;p&gt;So what is it? &lt;a href="https://github.com/kouphax/sentimentalizer/"&gt;sentamentalizer&lt;/a&gt; is a quick and dirty &lt;a href="http://en.wikipedia.org/wiki/Sentiment_analysis"&gt;sentiment analysis&lt;/a&gt; tool. With it you can loosley class statements as either positive or negative (and get a breakdown of how that classification was derived). You "train" it with datasets for both negative and positive control statements and then feed it sentences you want analysed. At the minute the engine sits behind a super super super, did I say super already?, simple sinatra app that you submit statements to and it returns a simple JSON response with the sentiment result (negative, positive, neutral) and probability. If you want to train the bitch you've got to change the &lt;code&gt;api.rb&lt;/code&gt; file or update the files in the &lt;code&gt;data\positive&lt;/code&gt; and &lt;code&gt;data\negative&lt;/code&gt; folders.&lt;/p&gt;&lt;p&gt;Plans? Yeah I might poke around some more, make into more of a standalone learning service or something (again another feature I might need soon anyway). But for now - feel free to review my shitey code Rubyists (I assume there are better ways to do things, standard appraoches that I'm not using etc.) and offer suggestions.&lt;/p&gt;&lt;p&gt;Once again thanks to &lt;a href="https://twitter.com/#!/martinrue"&gt;@martinrue&lt;/a&gt; for doing all the hard-work on &lt;a href="https://github.com/martinrue/Sentan"&gt;Sentan&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>Tinkering with minion.js</title><updated>Thu Mar 08 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/08/tinkering-with-minion-dot-js/"></link><id>urn:yobriefca-se:feed:post:Tinkering with minion.js</id><content type="html">&lt;p&gt;Yay a technical post... &lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/gigafied/minion"&gt;minion.js&lt;/a&gt; is a micro framework providing classical inheritance strategy for JavaScript plus many other goodies. It can be used in both browsers and &lt;a href="http://nodejs.org"&gt;node.js&lt;/a&gt;. Now I don't want to get caught up in any of this classical inheritance for JavaScript is an anti-pattern stuff so just take this framework for what it is - a decent way to define both client and server side models for you JavaScripts!&lt;/p&gt;&lt;p&gt;I've already said it has a host of goodies that can be useful for creating a nice flexible set of models and associated helpers. This includes,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Class and inheritance support&lt;/li&gt;
  &lt;li&gt;Modularity&lt;/li&gt;
  &lt;li&gt;A cross model pub/sub mechanism&lt;/li&gt;
  &lt;li&gt;Statics, Singletons, static methods and properties&lt;/li&gt;
  &lt;li&gt;Dependency management&lt;/li&gt;
  &lt;li&gt;Some other stuff I've yet to touch upon...&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The code will more or less speak for itself here so lets dive in (the full suite of tests and sample node.js/express implementation can be found on my &lt;a href="https://github.com/kouphax/minion-tinkering"&gt;GitHub thingy&lt;/a&gt;).&lt;/p&gt;&lt;h2&gt;Classes&lt;/h2&gt;&lt;p&gt;One of the first things I did was try out the whole inheritence thing. I create a Fruit base class and provided a Strawberry implementation on top of it.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;&lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fruit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Fruit&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"minion.Class"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nx"&gt;isInSeason&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inSeason&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;&lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fruit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Strawberry&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fruit.Fruit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;inSeason&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Using the Strawberry class is pretty damn simple. First thing you need to do is a one off simple configuration of minion,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;&lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;configure&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;classPath&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"js"&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This just tells minions dependency manager where to start looking for your class implementations. We can then start using the Strawberry class easily using minions &lt;code&gt;require&lt;/code&gt; method&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;&lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fruit.Strawberry"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Strawberry&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Strawberry&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Static Objects&lt;/h2&gt;&lt;p&gt;Static objects are pre-initialised Singletons in minion.js and that makes a lot of sense. I created a Grocer static by simply extending another minion type &lt;code&gt;Static&lt;/code&gt;.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;&lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fruit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Grocer&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"minion.Static"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"purchase"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buy&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="nx"&gt;sell&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stock&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="nx"&gt;buy&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stock&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;

    &lt;span class="nx"&gt;checkLevels&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Pretty unsuprising but minion will new up a shared instance of this class the first time it is called. You'll see a little hint of the pub/sub mechanism in the &lt;code&gt;init&lt;/code&gt; method as well so lets jump on that.&lt;/p&gt;&lt;h2&gt;Pub/Sub&lt;/h2&gt;&lt;p&gt;So minion has a nice little pub/sub mechanism built right into the classes as well. So lets say a customer buys stuff from the grocer by publishing his/her request - sure beats just taking it without asking!!!&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript "&gt;&lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fruit"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Customer&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;minion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"minion.Class"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;buy&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"purchase"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Minion once again handles all the internal wiring up of this stuff and it just works. Nice.&lt;/p&gt;&lt;h2&gt;The Rest&lt;/h2&gt;&lt;p&gt;There is more to minion.js, I've just given you a very high level view but you should dive a bit deeper by looking at the &lt;a href="https://github.com/gigafied/minion/blob/master/docs/getting_started.md"&gt;official docs&lt;/a&gt;. There is a few bits of handy sugar sprinkled on the minion types (proxy, scoped setTimeout and setInterval). The pub/sub also goes a bit further provinding &lt;code&gt;Notifications&lt;/code&gt; that allow a sort of async callback/response mechanism which can be handy.&lt;/p&gt;&lt;p&gt;Now I've never been a fan of these sorts of Class frameworks - I've never really seen the need but I must admit minion appeared at about the right time. I've been playing with a nice way of structuring &lt;a href="http://mongoosejs.com"&gt;mongoose&lt;/a&gt; Schemas/Classes and I think minion here might be a nice fit.&lt;/p&gt;&lt;p&gt;Anyway check it out. Yay a technical blog with more to come!&lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/gigafied/minion"&gt;minion.js&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/kouphax/minion-tinkering"&gt;Sample Code&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>Taking the Stairs</title><updated>Tue Mar 06 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/03/06/taking-the-stairs/"></link><id>urn:yobriefca-se:feed:post:Taking the Stairs</id><content type="html">&lt;p&gt;Ya'll about ready for another silly analogy? I sure am....&lt;/p&gt;&lt;p&gt;I work on the 6th floor and recently decided to start taking the stairs. Mostly because I've let myself go and I really need to drop a few pounds and those stairs are a bit of a hard slog. But it's worth it. It's funny how much of a better understanding of the building layout I get by just putting in the extra effort and going up the stairs. The stairwell itself is pretty bland but I see extra doors, and I now know were many of doors could lead.&lt;/p&gt;&lt;p&gt;So the analogy... Taking the stairs is a bit like the whole software craftsmanship thing. Sure I could just take the prescribed route, the default stack so to speak, and as with the stairs I'd probably get where I was going for a lot of the time. But you know what? I'd not know much more than the average person. What if that lift was over capacity, out of order or just smelling of urine - I'd be able to manage just fine without getting stranded on a random floor wondering what to do next, if that door I never tried went were I wanted. Imagine the stress involved if I was already late for a meeting. Crickey!&lt;/p&gt;&lt;p&gt;So I put the extra effort in to understand things, and it's already given me a better perspective on things. It's shown me other options, the ability to decide if the what I am doing is a good approach or perhaps there is a quicker, better, healthier route.&lt;/p&gt;&lt;p&gt;How does that grab you?&lt;/p&gt;&lt;p&gt;I really need to get off this analogy trip I'm on FFS!&lt;/p&gt;</content></entry><entry><title>The Estimation 4x4</title><updated>Sun Feb 12 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/02/12/the-estimation-4x4/"></link><id>urn:yobriefca-se:feed:post:The Estimation 4x4</id><content type="html">&lt;blockquote&gt;&lt;p&gt;This post is dedicated to one of the most influential managers I've ever had the pleasure to work with.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I'm of the opinion that the moment you start trying to apply complex mathematical rigour and time to estimation you've entered into a broken system. You're wasting time trying to protect yourself from finger pointing managers or getting a kicking from old school "us or them" lawyer types. None of that adds anything to the project except a big stick to beat people with and make sure they work themselves to death based a a random stab in the dark that is somehow made more realistic by the application if science. Ultimately though if the initial guesswork is wrong, and it's seldom on the nose, the science means sweet FA. So why bother? You're pleasing the wrong people. But then those people hold a lot of power. So as long as we can keep them happy with the minimum of effort life should be sweet.&lt;/p&gt;&lt;h2&gt;The Answer&lt;/h2&gt;&lt;p&gt;Ladies and gentlemen I bring you the &lt;strong&gt;Estimation 4x4&lt;/strong&gt; - guaranteed to knock those super science estimation techniques out of the park.&lt;/p&gt;&lt;h2&gt;The Science Bit&lt;/h2&gt;&lt;p&gt;Got a project to estimate? I've done your homework for you - &lt;strong&gt;4 people, 4 weeks&lt;/strong&gt;. BOOM! Mind Blown! Now get on with delivering that project buddy. Oh wait it's a big ass project - 4 people, 4 months. Oh wait you're limited by deadlines and/or resourcing - adjust as needed (3 people, 5 weeks or 5 people, 3 weeks).&lt;/p&gt;&lt;h2&gt;Balancing Act&lt;/h2&gt;&lt;p&gt;Careful now, 9 women dont make a baby in 1 month so don't assume that scaling one side up to cater for the other will get the same results. Theres a balance that needs addressed. This is real science after all - repsect the rule and the rule will respect you.&lt;/p&gt;&lt;h2&gt;You Gotta Be Kidding Me&lt;/h2&gt;&lt;p&gt;Dont believe me? Try it out next time. Do your pseudo-scientific enterprise-ready finger in the air analysis. But hey see when that project is finished see how close the 4x4 would have brought you to the real result. I bet 9 times out of 10 it was as accurate as your real estimate, except you could have started much sooner....&lt;/p&gt;&lt;p&gt;#winning&lt;/p&gt;</content></entry><entry><title>Caveat Abstractor</title><updated>Thu Feb 09 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/02/09/caveat-abstractor/"></link><id>urn:yobriefca-se:feed:post:Caveat Abstractor</id><content type="html">&lt;p&gt;&lt;em&gt;Ramblings from 30,000ft. The word abstraction is starting to sound odd.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Every time you create an abstraction there is a very good chance you've introduced unnecessary complexity into a solution. It should never be done alone - it should be a team decision. The goal of an abstraction is to simplify but in its untamed natural form an abstraction will only add complexity.&lt;/p&gt;
&lt;!-- more --&gt;&lt;p&gt;As programmers it's easy to fall into the trap whereby we want the code that we write to be the last time anyone ever has to write that code again. We dream of creating reusable works of art that ever other developer will use and never be concerned with the low level implementation ever again.&lt;/p&gt;&lt;p&gt;This is wrong and impossible. An anti-pattern even!&lt;/p&gt;&lt;p&gt;It baffles me that anyone would create an abstraction that they didn't have a need for at the time. It's wasteful and degrades the readability, grokability and all those other good "ilities" we use. Of course I say this like i don't do it myself but I'd be lying to you and myself.&lt;/p&gt;&lt;p&gt;An abstraction, by definition, is an intentional hiding of implementation and knowledge but surely understanding of these things will lead to a higher chance of success in a project? The person that created the abstraction has that understanding but it's like they dont want anyone else to know how it works. They're ready to take that secret to their grave.&lt;/p&gt;&lt;p&gt;When you start to abstract you need to remind yourself you are building a specific solution and not a framework (unless you are of course). Abstractions should be done only within the scope of the project and you should have a strong case to back up its creation (make the decision a team effort). This may sound like overhead but if you find it difficult to justify the creation odds are the abstraction is unnecessary.&lt;/p&gt;&lt;p&gt;Abstractions should not be grand, they should be light and convey their intent clearly by other people (pair programming and peer review will help weed out any verbose abstractions).&lt;/p&gt;&lt;p&gt;If you are creating an abstraction for anything other than DRY, odds are youre probably creating it for YAGNI. You'll start considering irrelevant use cases and having to cater for insane edge cases. This will happen even if you are taking a strong test driven approach - you'll concern youself with the workings of the abstraction rather then the problem you are solving and begin creating endless "what if" tests to cater for all those "0.00000001% chance of happening" edge cases. When this happens youre going to end up in mediocrity. Mediocre abstractions attract more abstraction. Even in a shared codebase bad abstractions create a certain amount of implied ownership to the person that created it. Inevitably someone will come along and create an abstraction around your abstraction (even if they have access to the original source). This in turn will likely become YAMA (Yet Another Mediocre Abstraction) attracting yet more abstraction.... You can guess where I am going with that one... Its abstractions all the way down and everytime you abstract you've decreased system comphension and likely destroyed another future developers soul.&lt;/p&gt;&lt;p&gt;In the same way guns dont kill people, people do - Abstraction dont kill projects, developers do. You've only got yourself to blame (been there done that, will be again). A nice simple abstraction, at the right time (e.g. When needed) can be expressive and helpful but too often we put the abstraction before the need.&lt;/p&gt;&lt;p&gt;So consider abstractions as "guilty until proven innocent" not the other way around. Solve YOUR problem - not every other problem, not until you need to.&lt;/p&gt;</content></entry><entry><title>On Craftsmanship and Understanding, I Guess</title><updated>Tue Jan 31 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/01/31/on-craftsmanship-and-understanding-i-guess/"></link><id>urn:yobriefca-se:feed:post:On Craftsmanship and Understanding, I Guess</id><content type="html">&lt;p&gt;You can stand at the station and wait for the train. You know the one. It's the one that's always late, always packed with people, head down staring at the ground. Or, you can walk the tracks with your own two feet. When you walk the tracks you take in your surroundings, you understand the route and you see the alternatives. You wont beat the train everytime but when you walk the tracks you'll have better stories to tell when you get there.&lt;/p&gt;</content></entry><entry><title>Enterprise Scale</title><updated>Thu Jan 26 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/01/26/enterprise-scale/"></link><id>urn:yobriefca-se:feed:post:Enterprise Scale</id><content type="html">&lt;p&gt;Enterprise. What a broken, misued term. Its meant to sound big, huge even. I guess many years ago it used to mean big, huge, big business serious work.&lt;/p&gt;&lt;p&gt;But when you think about it, its none of those things really, not any more. Even the biggest multi-nationals entire enterprise backbone throughput is probably a small fraction of Lady Gagas Twitter feed. Enterprise is just that - one enterprise. Social is global, its all of us at anytime and that is so much bigger than some old enterprise. So if you try and pimp out your CV by using the term enterprise - the only people who are probably going to care are the managers and execs that think their little boring legacy system is unique and special... Do you really want to be sold that sort of job? All bytes are equal, some are more equal than others - Pah! Some are more mired in unnecessary abstraction, contracts and unread, out-of-date doucmentation but they are still bytes.&lt;/p&gt;</content></entry><entry><title>Using Parse in .NET and WP7</title><updated>Thu Jan 05 00:00:00 UTC 2012</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2012/01/05/using-parse-in-net-and-wp7/"></link><id>urn:yobriefca-se:feed:post:Using Parse in .NET and WP7</id><content type="html">&lt;blockquote&gt;&lt;p&gt;UPDATE: Being completely wrong in assuming WP7 supports &lt;code&gt;dynamic&lt;/code&gt; which apparently it doesn't the current build wont run in WP7. So another item on the list is a version that doesn't rely on &lt;code&gt;dynamic&lt;/code&gt; I guess. Sorry for misleading anyone on that front - I did say it was rough and mostly untested right?&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;&lt;a href="https://www.parse.com/"&gt;Parse&lt;/a&gt; is a site aimed at providing a simple service for pushing and pulling data on mobile devices. You can create applications and store your mobile data there, create users, provide logon/off mechanisms and even achieve push notifications. There are 2 ways to access &lt;a href="https://www.parse.com/"&gt;Parse&lt;/a&gt;&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Via its natively compiled binaries (on iOS and Android)&lt;/li&gt;
  &lt;li&gt;Via a its Rest API (for all non iOS/Android devices and desktops)&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Now I wanted to try and create bindings for the iOS library so I could use it for a Monotouch application I've been playing with but truth be told the whole process confused the hell out of me. Thing was the REST API gave me everything I needed so I started recently to write a dynamic wrapper around the API. Currently built on .NET in Visual Studio (Mono on my list) with a few sanity tests it works well enough to put it out there incase anyone was wanting to do something similar and could extract value from it.&lt;/p&gt;&lt;p&gt;It's pretty early at the minute - in fact all you can do with this first spike is basic object CRUD. No querying yet, no special user management features etc. But they will come. So what could you use it for? Well a number of things,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;WP7 Cloud Storage&lt;/li&gt;
  &lt;li&gt;Monotouch (and Monodroid) storage until someone smarter than me creates proper bindings to the actual iOS frameworks&lt;/li&gt;
  &lt;li&gt;It's an object store so why not use it to store data on any web app?&lt;/li&gt;
  &lt;li&gt;Just for tinkering.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It makes use of &lt;code&gt;dynamic&lt;/code&gt; from .NET 4 so you can kind of pass it arbitrary stuff and get arbitrary stuff back (Parse is schemaless so this makes sense for me). It also means you wont have to create concrete classes for every single thing you want to store.&lt;/p&gt;&lt;p&gt;If you want to run the tests you'll need to sign up for Parse, create an app and update the &lt;code&gt;consts&lt;/code&gt; in &lt;code&gt;describe_Parsely&lt;/code&gt; to point to your new app (and authenticate).&lt;/p&gt;&lt;h2&gt;Sample Uses&lt;/h2&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="c1"&gt;// create your parsley API&lt;/span&gt;
&lt;span class="n"&gt;Parsley&lt;/span&gt; &lt;span class="n"&gt;parsley&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Parsley&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;applicationId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;masterKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// create new class&lt;/span&gt;
&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parsley&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"MyClass"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;CoolProperty&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;  &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// do other cool stuff&lt;/span&gt;
&lt;span class="n"&gt;parsley&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"MyClass"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objectId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;CoolProperty&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="n"&gt;dyanmic&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parsley&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Retrieve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"MyClass"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objectId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;parsley&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objectId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Outstanding Tasks&lt;/h2&gt;&lt;p&gt;Lots. But here is the main ones I see right now.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Compile on Mono and show MonoTouch some Parse love.&lt;/li&gt;
  &lt;li&gt;Samples (for WP7, Monotouch, possibly Monodroid)&lt;/li&gt;
  &lt;li&gt;Add Queries&lt;/li&gt;
  &lt;li&gt;Add User Management&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;It's probably fairly rough right now and could do with some TLC if anyone wants to pitch in? I've given it a name (Parsley) but that in no way implies this is going to be a long running project or anything. I just like naming things. Get it while it's hot, the code is in my &lt;a href="https://bitbucket.org/kouphax/parsley"&gt;usual place&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Pre-Post Update&lt;/h2&gt;&lt;p&gt;Rather than update this blog post I decided to tack this on here. With BitBucket experiencing some issues at present I spent about 10 minutes hacking in the user management features. No tests written yet (oh no I have broken all the rules and must be beaten with oversized crayons) so lets just say that it's very edge, very experimental at the minute. Also bonus - returning a collection of objects is implemented as &lt;code&gt;Query&lt;/code&gt; but again totally untested (fast and loose - it's how I roll son). There be dragons there therebe!&lt;/p&gt;&lt;p&gt;Happy hacking.&lt;/p&gt;&lt;p&gt;Can I have my WP7 MVP now please? &lt;strong&gt;Kidding&lt;/strong&gt;&lt;/p&gt;</content></entry><entry><title>A Nancy Module that Behaves like a Rails Controller... Mother of God</title><updated>Fri Dec 30 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/12/30/a-nancy-module-that-behaves-like-a-rails-controller-dot-dot-dot-mother-of-god/"></link><id>urn:yobriefca-se:feed:post:A Nancy Module that Behaves like a Rails Controller... Mother of God</id><content type="html">&lt;p&gt;If &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; is inspired by Sinatra I thought I'd have a little fun a create an abstract class that created modules that kind of mimic the behaviour of Rails controllers. Code first, ask questions later...&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RailslikeControllerFor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TEntity&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NancyModule&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;TEntity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
&lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;RailslikeControllerFor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TEntity&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"s"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/new"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/(?&amp;lt;id&amp;gt;[\d])"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Show&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/(?&amp;lt;id&amp;gt;[\d])/edit"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Edit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Put&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/(?&amp;lt;id&amp;gt;[\d])"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/(?&amp;lt;id&amp;gt;[\d])"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;Show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;Edit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;Destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;AsView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;StackTrace&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;GetFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;GetMethod&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TEntity&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{0}/{1}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;To use it you just subclass the class and override the necessary methods. Like so.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;QuestionModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;RailslikeControllerFor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Question&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;IQuestionsRepository&lt;/span&gt; &lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;QuestionModule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IQuestionsRepository&lt;/span&gt; &lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;questions&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;AsView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;questions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetAll&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;What the base class does is create the necessary CRUDdy urls with a base route starting with the name of the entity you pass in, pluralised in the easiest way possible. In this case &lt;code&gt;/questions&lt;/code&gt;. The rules match what you wold get for a controller in Rails.&lt;/p&gt;
&lt;table style="width:100%" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr style="background-color: #666;color:#fff;"&gt;
            &lt;th style="padding:4px;font-weight:bold;"&gt;Verb &lt;/th&gt;
            &lt;th style="padding:4px;font-weight:bold;"&gt;Path&lt;/th&gt;
            &lt;th style="padding:4px;font-weight:bold;"&gt;Action &lt;/th&gt;
            &lt;th style="padding:4px;font-weight:bold;"&gt;&lt;/th&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="padding:4px;"&gt;&lt;span class="caps"&gt;GET&lt;/span&gt;     &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;/questions           &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;index    &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;display a list of all questions&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="background-color: #fff;"&gt;
            &lt;td style="padding:4px;"&gt;&lt;span class="caps"&gt;GET&lt;/span&gt;     &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;/questions/new       &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;new      &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;return an &lt;span class="caps"&gt;HTML&lt;/span&gt; form for creating a new question&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="padding:4px;"&gt;&lt;span class="caps"&gt;POST&lt;/span&gt;    &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;/questions           &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;create   &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;create a new question&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="background-color: #fff;"&gt;
            &lt;td style="padding:4px;"&gt;&lt;span class="caps"&gt;GET&lt;/span&gt;     &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;/questions/:id       &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;show     &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;display a specific question&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="padding:4px;"&gt;&lt;span class="caps"&gt;GET&lt;/span&gt;     &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;/questions/:id/edit  &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;edit     &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;return an &lt;span class="caps"&gt;HTML&lt;/span&gt; form for editing a question&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr style="background-color: #fff;"&gt;
            &lt;td style="padding:4px;"&gt;&lt;span class="caps"&gt;PUT&lt;/span&gt;     &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;/questions/:id       &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;update   &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;update a specific question&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="padding:4px;"&gt;&lt;span class="caps"&gt;DELETE&lt;/span&gt;  &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;/questions/:id       &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;destroy  &lt;/td&gt;
            &lt;td style="padding:4px;"&gt;delete a specific question&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;There is a teeny-tiny bit of sugar in the &lt;code&gt;AsView&lt;/code&gt; method that will look up the corresponding view. So the module above would resolve its &lt;code&gt;Index&lt;/code&gt; method to &lt;code&gt;questions/index&lt;/code&gt; and the view engine would find the correct file (eg. for Razor - &lt;code&gt;questions/index.cshtml&lt;/code&gt;).&lt;/p&gt;&lt;p&gt;I've already said it's a bit of fun but I have actually used this at least once - there is probably more that could be added but it'll do for a first spike at least. Any use to anyone?&lt;/p&gt;</content></entry><entry><title>Standing at the cusp of 2011... WTF?</title><updated>Fri Dec 30 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/12/30/standing-at-the-cusp-of-2011-dot-dot-dot-wtf/"></link><id>urn:yobriefca-se:feed:post:Standing at the cusp of 2011... WTF?</id><content type="html">&lt;p&gt;I'm not sure where this post is going to go in terms of content. I'm thinking a bit of full disclosure might be quite cathartic but at the same time do you really want to know the ins and out of my somewhat awful 2011? Compromise - I'll keep it short. Possibly.&lt;/p&gt;&lt;h2&gt;2011 (The Bad Parts)&lt;/h2&gt;&lt;p&gt;2011 was a pretty terrible year if I'm honest. It was the year my wife suffered two miscarriages, I got into my first car accident, stuff kept breaking and I totally dropped the ball in work when I burnt out (I'm certain I'm still feeling the effects of that). Oh and yes on top of all that I was diagnosed with severe depression for which I am now fully medicated. But you know what? I'm not alone - I know a ton of people for whom (look at me I said whom - how posh) 2011 was grim.&lt;/p&gt;&lt;p&gt;So lets cast aside all the personal stuff, we've all had our problems, and let me talk about the burn out. Long and short of it was that I took too much work on and eventually delivered unacceptable results for them all. I have something akin to ADD when it comes to work and unless I am suitably challenged or stimulated my productivity drops to pretty much nil. 2011 was full of time consuming mundane boring work for me and as I tried desperately to pull personal value from the work (or hobbies) I started making mistakes, over committing and generally under achieving. Lesson learned - I need to learn to say no and sometimes I just need to suck up the fact that we can't all be doing the best part of our jobs ALL THE TIME. Sometimes we need to just knuckle down and get on with it. Just not for a full year...&lt;/p&gt;&lt;p&gt;Anyway I became somewhat disillusioned with my employer and, truthfully, Northern Ireland. I like other people to be excited about the stuff I preach but I felt there was a lot of disinterest and empty words. This annoyed me and I guess I just started pushing harder, which I guess made me look quite angry and pushy. This obviously wouldn't help. When I looked outside of my company - Northern Ireland didn't seem that much better. So I felt a bit out of place and I had 2 choices ahead of me.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Up sticks and go somewhere that I felt would be more receptive to my extreme nerdyness and desire to share it.&lt;/li&gt;
  &lt;li&gt;Stick around, my company and Northern Ireland and try and make a difference.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;I chose the second, not being to be hero or anything but for more pragmatic reasons,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;My wife got pregnant again (due soon yay!), the economy is terrible and it would be too much of a risk right now&lt;/li&gt;
  &lt;li&gt;If I stayed in Northern Ireland, or even Ireland, I don't believe there is any other company that would support me (aka let me get away with) the things I do. I'm not the most predictable of sorts. I work weird hours and do strange things. &lt;a href="http://www.kainos.com"&gt;Kainos&lt;/a&gt; have always been there for me and supported me. Having done interviews with people from various levels from various other companies the state of other IT firms makes me seethe with anger.&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;&lt;p&gt;It is better to invest in people and risk them leaving than to not invest in them and risk them staying&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;So I start 2012 with a big challenge set for myself. That sounds about right. The more I think about &lt;a href="http://www.kainos.com"&gt;Kainos&lt;/a&gt;, my employer, deserves more credit than I give it. We have been in a space thats been all about big business types with big enterprisey solutions and delivered well. It's not an area that exactly screams enthusiasm and I think it will take a while to draw people out of that kind of insular thinking. &lt;a href="http://www.kainos.com"&gt;Kainos&lt;/a&gt; certainly seems to be better than most in this area. Now enough gushing about &lt;a href="http://www.kainos.com"&gt;Kainos&lt;/a&gt; (who are hiring BTW)&lt;/p&gt;&lt;h2&gt;2011 (The Good Parts)&lt;/h2&gt;&lt;p&gt;OK OK 2011 wasn't all sob stories and dullness some really cool things happened.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;My CodeSlice initiative in work (meet at least once a month, talk nerdy things, get &lt;a href="http://www.kainos.com"&gt;Kainos&lt;/a&gt; to pay for free pizza) didn't die. In fact in the hands of one of my colleagues, Jonny Holmes, it flourished. Attendence was never HUGE but it was consistent and varied. May long this tradition continue and hopefully we can expand it..&lt;/li&gt;
  &lt;li&gt;I organised and participated in not one but two hackathons (internally). Once again &lt;a href="http://www.kainos.com"&gt;Kainos&lt;/a&gt; covered the costs for our weekend long hack sessions. Turnout was decent (about 8-12 people) with all types turning up - managers, placement students, technical architects etc. The experience was enough for me - which is a clever way of saying the end products weren't as good as envisaged. But we drew so many valuable lessons that the second one was infinitely more successful that the first - live and learn! And, oh lord, the plans I have for the first half of 2012 around hackathons are going to be legendary!&lt;/li&gt;
  &lt;li&gt;I talked, and not just to myself but to real people, real people who were not paid to be there! I talked, outside of work, a total of three times. I know the number isn't huge but it was late on in the year before I decided to get out there. Already topped that number for 2012 and the year hasn't started. Double figures would be good next year thank you.&lt;/li&gt;
  &lt;li&gt;I attended &lt;a href="http://gotocon.com/amsterdam-2011/"&gt;GOTO Amsterdam&lt;/a&gt; were I got to meet a lot of people I have deemed awesome (because they are). Being in rooms with like minded people (who are generally much smarter than me) saying things that make you realise you are not insane for thinking things could be better really strengthens your beliefs!&lt;/li&gt;
  &lt;li&gt;Shared more. All the stuff above I did because I wanted to get my thoughts out there - I wanted to be educated as well as educate. I was happy to look like a fool if it meant I didn't look like a fool later. Thats hard for an introvert but damn it I gave it my best shot. I know people, the one who only know me through my writing on &lt;a href="http://yammer.com"&gt;Yammer&lt;/a&gt; or half-read blog post, think I am a purist (I'm not - perfect is the enemy of better), or angry (not me, frustrated at times but never angry), or opinionated (openly opinionated perhaps) or even annoyed that everyone isn't dancing the happy geek dance like myself (as long as I'm not dancing alone on the dance floor like a sleazy old man stalking girls half his age I'm happy). Thats fine. People reckon I risk detracting from the message I am trying to communicate. Fair enough. Perhaps I need to tone it down sometimes - but I can't see that happening - it's no in my nature. I think it's better that people see me as a purist and take some of my ideas away rather than being half-assed and them not taking anything on board! It's inevitable that when I try and challenge peoples conceptions of technical things, especially in the open, it's going to leave a sour taste in some peoples mouths. But thats how I roll and thats how it should be. Sorry but there is more on the way. But I take as good as I give so come at me bros!&lt;/li&gt;
  &lt;li&gt;Won a years to subscription to &lt;a href="http://tekpub.com"&gt;Tekpub&lt;/a&gt; on Christmas Day thanks Rob 'Santa Clause' Conrey.&lt;/li&gt;
  &lt;li&gt;Put my foot in it a few times. Yeah sounds odd I am classing this as a good thing but Scott Hanselman gave me permission, and I &lt;a href="http://www.hanselman.com/blog/BestPracticesForIndividualContribution.aspx"&gt;quote&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;&lt;p&gt;Chris Sells told me once, If you're not getting in trouble with your boss at least twice a year, you're likely not pushing the envelope hard enough. Two slaps a year might be the cost for 10 successes. If you're not moving forward, well, you're not moving forward.&lt;/p&gt;
&lt;/blockquote&gt;&lt;h2&gt;2012 (More like 20AWESOME! AMIRITE!)&lt;/h2&gt;&lt;p&gt;So enough dwelling in the past - how am I going to make 2012 be the awesomest? Well thats easy - &lt;strong&gt;set some real fecking goals&lt;/strong&gt;. I failed to do that last year (outside of my typical career goals during my annual reviews of course) and ended up coasting in parts. Not next year - hells no. I got me a bucket list (stuff to do before the year dies - not me - unless that comes first - oh god I never thought of that). TO THE LIST!&lt;/p&gt;&lt;h3&gt;2012 Bucket List (In No Particular Order)&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Release a screencast&lt;/li&gt;
  &lt;li&gt;Release a game&lt;/li&gt;
  &lt;li&gt;Appalachian Trail&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://givecampni.org.uk"&gt;GiveCamp NI&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Talk/Present as often as possible&lt;/li&gt;
  &lt;li&gt;Learn a new language&lt;/li&gt;
  &lt;li&gt;Contribute more to OSS&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So let me explain some of these, as I am sure they mean sweet FA to some of you.&lt;/p&gt;&lt;h3&gt;Release a Screencast&lt;/h3&gt;&lt;p&gt;A lot of my blog posts last year were a kind of "Introduction to Technology X" and I think that sort of knowledge can be better shared via video and real coding. Coupled with my desire to go a bit deeper in my technical posts but always getting distracted with other blog posts I think 2012 is the year I try my hand at producing a real life proper screencast (or perhaps a series). With decent production values and what not. The topic? Well I am currently thinking &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; but I need to really get my head around it a lot than I already do - but thats fine. Time will tell on that one.&lt;/p&gt;&lt;h3&gt;Release a Game&lt;/h3&gt;&lt;p&gt;Game programming is an area I've failed to venture into and I feel it is about time. I wan't to do something really simple, something mobiley possibly using &lt;a href="http://impactjs.com/"&gt;ImpactJS&lt;/a&gt; because it interests me. I'd like to release said game into an app store as well - just to watch it get flamed and to say "I've done it"&lt;/p&gt;&lt;h3&gt;Appalachian Trail&lt;/h3&gt;&lt;p&gt;This is a codename for a current unfunded project of mine. The goal is to get a more fuller understanding of the spectrum of mobile development approaches, platforms and technologies. So simply put generate a bunch of super simple (though still useful) app ideas and using every conceivable type of technology from native to web and all in-between deliver decent quality apps across all the major platforms through hackathons, team challenges, school and university outreach, app camps etc..&lt;/p&gt;&lt;p&gt;Sounds awesome doesn't it?&lt;/p&gt;&lt;h3&gt;GiveCamp NI&lt;/h3&gt;
&lt;blockquote&gt;&lt;p&gt;GiveCamp was founded by Microsoft Developer Evangelist, Chris Koenig, in Dallas, Texas in 2007. His vision was to bring together passionate developers and local charitable organisations for an entire weekend, and see what would happen.&lt;/p&gt;&lt;p&gt;In late 2012 GiveCamp is coming to Northern Ireland.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Thats what the &lt;a href="http://givecampni.org.uk/"&gt;official site&lt;/a&gt; says (I should know I put it there). So late next year we will run GiveCamp for the first time in Northern Ireland. A weekend long charity focused public hackathon. It's going to be a belter!&lt;/p&gt;&lt;h3&gt;Talk/Present as often as possible&lt;/h3&gt;&lt;p&gt;I enjoy talking about nerdy things. I got a taste for it last year when I went public (having been talking internally to &lt;a href="http://www.kainos.com"&gt;Kainos&lt;/a&gt; staff for years) and in 2012 I really want to get out there as much as possible. I find it an amazing way for me to learn as well as understand whats going on in the industry. So far I am speaking in &lt;a href="http://dublinalt.net/2011/12/14/january-meetup-calling-all-mobile-devs/"&gt;Dublin in January&lt;/a&gt;, &lt;a href="http://www.devweek.com/"&gt;Devweek in London&lt;/a&gt; and a few other un-announced venues.&lt;/p&gt;&lt;p&gt;BTW if anyone can recommend conferences I should pitch for, or want me to come over and talk about anything in the .NET/JavaScript/Mobie world(s) (see my blog for the kind of thing I know about) - let me know!&lt;/p&gt;&lt;h3&gt;Learn a new language&lt;/h3&gt;&lt;p&gt;Everyone says this one right? Yeah so I spent last year looking at node.js and Ruby and I think it's time to get out of my safe - I'm thinking F# - yeah that sounds sweet!&lt;/p&gt;&lt;h3&gt;Contribute more to OSS&lt;/h3&gt;&lt;p&gt;I head-dived into OSS .NET last year and loved it. Many people see .NET as a very restricted, Microsoft led platform were every modern web-app is MVC3 with Entity Framework and WCF rules the web service world. Thats total bull. .NET has a very vibrant, very active OSS community. There is almost no need to use any MS technology ever. But lets not forget MS have really started to embrace and support OSS in a big way. Lets hope that trend continues!&lt;/p&gt;&lt;h2&gt;In Summary&lt;/h2&gt;&lt;p&gt;Boooo to 2011, Yay to 2012. See you around the same time next year when I have another failful year and promising myself a better one in 2013 :-P&lt;/p&gt;</content></entry><entry><title>Code Coverage and the Forces of Good</title><updated>Tue Dec 27 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/12/27/code-coverage-and-the-forces-of-good/"></link><id>urn:yobriefca-se:feed:post:Code Coverage and the Forces of Good</id><content type="html">&lt;p&gt;Code coverage often does more harm than good. It removes the focus from testing the expected behaviour of the code to just writing tests to make sure all your code is hit (gamifiying the process to its own detriment). People will bend tests, often in a way that no typical use case could, just to make them run as they want. This is unrealistic and paints a false picture of the behaviour of your code.&lt;/p&gt;&lt;p&gt;That being said - code coverage is a tool and it shouldn't be blamed for enabling bad habits (a bad workman always blames hi... well, you know the rest). So can code coverage be applied in a less obvious way? Indeed it can.&lt;/p&gt;&lt;p&gt;Business users invariably end up using things like &lt;a href="http://google.com/analytics"&gt;Google Analytics&lt;/a&gt;, &lt;a href="http://gaug.es"&gt;Gauges&lt;/a&gt; or &lt;a href="http://flurry.com"&gt;Flurry&lt;/a&gt; to discover the most valuable areas of the solution and tailor accordingly. Makes perfect sense, right? So why not use code coverage, during development, as a mechanism to identify redundant areas of code, or discover feature hotspots that could benefit from refactoring or performance tweaking? Why not indeed?&lt;/p&gt;&lt;p&gt;As any system evolves there will be behaviours that simply don't need to exist to satisfy the overall behaviour of the system. Some code and it's related tests will still hang around. It happens and with the right tools you'll be able to see those areas - MightyMoose will show you hit counts for lines of code for example. So by ensuring tests are reviewed as part of your code review process (or refactored during pairing) you'll also get reporting of redundant code or feature hotspots for free.&lt;/p&gt;&lt;p&gt;So if your project is already reporting on code coverage thanks to some great architectural mandate why not at least use it for something more positive?&lt;/p&gt;</content></entry><entry><title>Ugh. He's on about something mobiley again. Lord save us all!</title><updated>Tue Dec 13 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/12/13/ugh-hes-on-about-something-mobiley-again-lord-save-us-all/"></link><id>urn:yobriefca-se:feed:post:Ugh. He's on about something mobiley again. Lord save us all!</id><content type="html">&lt;p&gt;Good timing right? Fresh of the wave of my "lets have a natter about mobile" blog posts HP went and did something good (for a change, right?). After the hash that was made of the TouchPad HP went and open sourced the webOS software. Bundled with this software is &lt;a href="https://developer.palm.com/content/api/dev-guide/enyo.html"&gt;Enyo&lt;/a&gt; a mobile framework for WebKit browsers. Enyo was to be one of the main development platforms for webOS - JavaScript on the front end with node.js services for the backend. The future was bright. Then the TouchPad flopped. Ack well.&lt;/p&gt;&lt;p&gt;On the other hand Enyo is a very nice little framework and in the hands of the Open Source community could be a real winner. I've had a chance to tinker with over the last few days and thought I'd help get the word out by outlining how to write the obligatory by law ToDo app. It is currently somewhat similar to the official &lt;a href="https://developer.palm.com/content/api/dev-guide/enyo/tutorial.html"&gt;FeedReader tutorial&lt;/a&gt; but it could be easily extended to add more features. I recommend you look at the original &lt;a href="https://developer.palm.com/content/api/dev-guide/enyo/tutorial.html"&gt;tutorial&lt;/a&gt; for more info about how the project is structured as there are certain conventions that may seem odd to your typical web framework.&lt;/p&gt;&lt;p&gt;The entire application, which looks like this (yeah yeah it's not the most beautiful thing on the planet - I suck at design).&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/enyo/todo.png" alt="Sample Output for Web View" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Was knocked together with the following bit of code&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="nx"&gt;enyo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"TodoApp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;enyo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VFlexBox&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"PageHeader"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"VFlexBox"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;flex&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;align&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"center"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Todos"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;]},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Button"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;caption&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Clear Complete"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onclick&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clearComplete"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"RowGroup"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"newTask"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Input"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hint&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Enter new ToDo here..."&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Button"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;caption&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Add"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onclick&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"addTask"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Scroller"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;flex&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"list"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"VirtualRepeater"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onSetupRow&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"getTask"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Item"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;layoutKind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"HFlexLayout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;align&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"center"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"taskStatus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"CheckBox"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"margin-right:10px;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"completeTask"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"taskDescription"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
          &lt;span class="p"&gt;]}&lt;/span&gt;
        &lt;span class="p"&gt;]}&lt;/span&gt;
      &lt;span class="p"&gt;]}&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inherited&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nx"&gt;addTask&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;newTask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getValue&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nx"&gt;getTask&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;taskDescription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nx"&gt;completeTask&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetchRowIndex&lt;/span&gt;&lt;span class="p"&gt;()].&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getChecked&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="nx"&gt;clearComplete&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Those of you with some experience in Sencha Touchwill notice that the declarative syntax is quite similar to STs except slightly simpler. So lets break out some of the features that brought this example together.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Button"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;caption&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Clear Complete"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onclick&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"clearComplete"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is one of the component declarations that make up the suite of components that form the "kind" (Enyo speak for component or object). This is somewhat similar to the DOM (COM anyone? Component Object Model) except in JSON form. You can specify a component type (kind) and apply properties (which may or may not do something depending on the kind). Events are also declared inline (see onclick) and map to function names within the main kind declaration. Obviously if this declaration started getting too big and bloated I could decompose it into smaller components all handling their own stuff.&lt;/p&gt;&lt;p&gt;One other thing not highlighted in my demo is that backend service calls can also be defined within this component model and then called programatically. While this felt alien to me initially it is pretty similar to the way inline stores are declared in Sencha - except slightly less engineered.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Scroller"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;flex&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"list"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"VirtualRepeater"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onSetupRow&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"getTask"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Item"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;layoutKind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"HFlexLayout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;align&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s2"&gt;"center"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"taskStatus"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;kind&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"CheckBox"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"margin-right:10px;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"completeTask"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"taskDescription"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;]}&lt;/span&gt;
    &lt;span class="p"&gt;]}&lt;/span&gt;
&lt;span class="p"&gt;]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The scroller definition above has a few nice features. The use of "flex" to manage different screen sizes or viewport resizes and create very nice flexible layout. The VirtualRepeater which is a templated control that generates items from a collection based on the defined template. The Virtual part is useful for large lists and ensures that only the minimum amount of items are rendered (just before they are needed).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inherited&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Create is essentially our component constructor and allows us to set some things up in advance. Notice the use of the &lt;code&gt;inherited()&lt;/code&gt; method - this ensures the superclasses &lt;code&gt;create&lt;/code&gt; method is also called (inheritence chaining)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;this.$&lt;/code&gt; is a handy property. It is essentially a hash of all the named components that are direct and indirect children of the defined component. This makes it very easy to access a certain component if you want to manipulate it.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="javascript"&gt;&lt;span class="nx"&gt;getTask&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;taskDescription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;getTask&lt;/code&gt; is the method responsible for populating an Item template in the repeater for each row. This is where I bind the values of the current collection to the Item kind.&lt;/p&gt;&lt;p&gt;There you have it - yet another ToDo app spat out into the world! Just what we needed :)&lt;/p&gt;&lt;h2&gt;It not all Unicorns and Rainbows&lt;/h2&gt;&lt;p&gt;Yeah Enyo isn't perfect (shock horror). There are some quite annoying and some serious bugs that I have noticed in my hacking.&lt;/p&gt;&lt;h3&gt;Data Association&lt;/h3&gt;&lt;p&gt;When rendering the Repeater I can't see a way to easily attach data items to that item instance. I worked it out eventually by using &lt;code&gt;this.$.list.fetchRowIndex()&lt;/code&gt; but I think you would need to then manage your own data association. Not a big issue but it didn't feel right to me at the time.&lt;/p&gt;&lt;h3&gt;iPad/iPhone issues&lt;/h3&gt;&lt;p&gt;The one big issue I had with the iPad was that text fields often didn't bring the keyboard up (more often than not). Obviously this could be a bit of a deal breaker but there could be a simple fix. Some other minor quirks included scrolling issues when multiple scroll areas existed and choppy animations in the kitchen sink style demo (Sampler).&lt;/p&gt;&lt;p&gt;The example apps display and work on the iPhone but they are super tiny. I am not sure if this is just a lack of correct formatting within my HTML or if Enyo is purely designed for larger screens.&lt;/p&gt;&lt;h3&gt;Events&lt;/h3&gt;&lt;p&gt;At one point I wanted to bind to an event of a component programatically (rather than declaring it inline). I still can;t find a way to do this and I wonder if this could be a bit of hindrence when you get into a truly dynamic application. Thought this could be me being a bit stupid as usual.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Enyo v1.0 is still young in the hands of the OSS community but it's started out strong and could well be a strong competitor to something like Sencha Touch if it keeps up this pace. Hopefully the community embraces it and allows it to grow. It could well be great.&lt;/p&gt;&lt;p&gt;So yes, so far I like it but I've only been hacking a few days. Hopefully I get to spend more time with it over the next few weeks.&lt;/p&gt;&lt;p&gt;The sample project is available on my &lt;a href="https://bitbucket.org/kouphax/enyo-todos"&gt;BitBucket Repo&lt;/a&gt; if you want to tinker.&lt;/p&gt;</content></entry><entry><title>So This Mobile Stuff Then... (Techologies)</title><updated>Mon Dec 05 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/12/05/so-this-mobile-stuff-then-dot-dot-dot-techologies/"></link><id>urn:yobriefca-se:feed:post:So This Mobile Stuff Then... (Techologies)</id><content type="html">&lt;p&gt;Do you have your mobile strategy yet? Do you have your "one true way" to cater for every mobile application need? If not, the world will suddenly end and you'll never get anywhere in the mobile market. Seriously. Well not seriously. Obviously that's a pile of sheep.&lt;/p&gt;&lt;p&gt;To be blunt the best "mobile strategy" is the obvious one. Do whatever suits the project best. Yeah, yeah your company or your presales guys may not like that, no prepackaged waffle to fill document with or spew out in front of customers. The easy route is seldom the best route. Seriously though if your playing it safe with web just in case that iOS project you've gotten involved in goes Android you'll be compromising all the way.&lt;/p&gt;&lt;h2&gt;Just In Case&lt;/h2&gt;&lt;p&gt;Ahhhh good old "just in case". Nothing produces more mediocrity through compromise than "just in case". It's up to you to use your head when it comes to deciding what is the best fit for the project but as long as all the stakeholders are aware of the constraints those choices put on the project then those "what if" scenarios can be dealt with later. We need to stop treating "mobile" as if it was a technology decision. Web vs Native is not the same as saying Entity Framework vs NHibernate. It's so much bigger than that.&lt;/p&gt;&lt;h2&gt;Weighing up the options&lt;/h2&gt;&lt;p&gt;So you got a killer mobile project and you want it to be awesome - just like all your projects. Before we pull out the "Native, Hybrid or Web" question we need to know what is out there. What tools, technologies or languages can we use to make the best solution we can?&lt;/p&gt;&lt;h2&gt;Going Native&lt;/h2&gt;&lt;p&gt;The problem with the native moniker is that people assume it means a different code base per mobile target and therefore crazy expensive, difficult to handle change, and very time consuming but that isn't always the case. It is entirely possible to produce truly native apps without having to have an expansive code base in every language possible.&lt;/p&gt;&lt;h3&gt;Appcelerator Titanium&lt;/h3&gt;&lt;p&gt;&lt;a href="http://www.appcelerator.com/"&gt;Appcelerator's Titanium&lt;/a&gt; is a good example of a platform that provides an abstract API via a common meta language (in it's case JavaScript) that allows you to build directly to native apps for each target platform. You write your code and send it off to the cloud for compilation (AFAIK there currently isn't an in-house option for building but happy to be corrected). So you've got a middleman that you have no control over. What happens if your app gets rejected due to some low level issue in the compiled code? What if the cloud service goes down when you really really gotta build? What if &lt;a href="http://www.appcelerator.com/"&gt;Appcelerator&lt;/a&gt; goes bust? I dunno maybe that's just mostly FUD but these are the questions that customers ask and they are difficult ones to answer. I realise I've lambasted the use of too much "what if"s in decision making but I've come up against these ones and they directly affect the current solution rather than only the future scope. They need addressing and that's difficult.&lt;/p&gt;&lt;p&gt;Having said the a brilliant example of &lt;a href="http://www.appcelerator.com/"&gt;Appcelerators&lt;/a&gt; usefulness is &lt;a href="http://www.wunderlist.com/"&gt;Wunderlist&lt;/a&gt; - available on a ton of mobile and desktop platforms and built with Titanium the whole project shares, AFAIK, a common code base.&lt;/p&gt;&lt;h3&gt;MonoTouch, Monodroid etc.&lt;/h3&gt;&lt;p&gt;&lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt; and its ilk take a slightly different approach to the meta-language. &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt;, for example, isn't, as many believe, a cross-platform tool for building native apps. No, &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt; simply compiles to native code through direct bindings to Objective-C code. You are simply using the C# and the powerful features of the Mono/.NET Base Class Library. In fact it's even possible to bind to third party Objective-C code like Flurry, Three20 etc. (and many are made available by other developers).&lt;/p&gt;&lt;p&gt;The ability to share a common codebase comes from the fact that the same platform is available in MonoDroid (C# and Android) and Silverlight (Windows Phone 7). So sharing comes down to architecture. You can share code provided your code doesn't touch and platform specific code. You can create your own abstraction layer and work it into your build process or you can use MonoMobile.Extensions which gives you a common API already.&lt;/p&gt;&lt;p&gt;So in one way the &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt; stuff is simply the same as writing Objective-C but simply doing it using C#. This is nice for a number of reasons.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Mono has some very nice features like LINQ and Parallel programming.&lt;/li&gt;
  &lt;li&gt;Plenty of nice compatible frameworks - &lt;a href="http://code.google.com/p/sqlite-net/"&gt;sqlite-net&lt;/a&gt;, &lt;a href="https://github.com/timscott/catnap"&gt;catnap&lt;/a&gt;, &lt;a href="http://restsharp.org/"&gt;restsharp&lt;/a&gt; and plenty more.&lt;/li&gt;
  &lt;li&gt;Syntax that is less noisy and easier to read&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;There are of course issues. Assuming most people who use &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt; will come from a .NET background and want to use those skills - when you start you'll realise just how nice Visual Studio is. MonoDevelop isn't terrible, it's just fine, but it's simply not on par with Visual Studio (would you expect it to be?). With the way &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt; works your are kind of stuck with MonoDevelop too. Your average VS2010 developer might easily get frustrated with MonoDevelop. An aesthetic concern but one that can put people off easily. That said XCode is equally as infuriating at times (bugs and quirks abound!).&lt;/p&gt;&lt;p&gt;It's not all roses though. What if Xamarin, the commercial sponsor of &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt;, pull the plug? What if Apple decide, as they tried to before, to ban the use of any intermediary language? These are questions that people will ask but again the immediate and real advantages always trump theoretical risks. One other thing worth thinking about - If you came from a Ruby background for example I'm not sure why you would use &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt; over Obj-C. Will this niche have an affect on Mono* in the long term? Who knows.&lt;/p&gt;&lt;h2&gt;Mixing It Up aka &lt;em&gt;(cough)_Hybrid&lt;/em&gt;(cough)_&lt;/h2&gt;&lt;p&gt;So maybe you're a web guru/ninja/hero/wizard type person and you want to make use of those powers/skills in your mobile development but you want to get market visibility by creating a presence in the app stores. Perhaps you want to have some sort of reuse across platforms but aren't a .NET dev and Titanium isn't appealing. There a number of options in this area already.&lt;/p&gt;&lt;h3&gt;PhoneGap/Callback&lt;/h3&gt;&lt;p&gt;&lt;a href="http://phonegap.com/"&gt;PhoneGap&lt;/a&gt; is easily the most technology in the hybrid space. It is exactly what you expect. A fullscreen UIWebView in which resources embedded in the application package are executed. These resources are given access to native device features such as device access etc. via a JavaScript API that hooks into native function calls. There are wrappers for numerous platforms (iOS, Android, Bada, Blackberry, WP7 etc.) but your code calls the same adaptive API. Things start to veer off course when you need to introduce custom plugins as these are native and so you need to target all your platforms. Not much &lt;a href="http://phonegap.com/"&gt;PhoneGap&lt;/a&gt; can do in that case of course but worth pointing out.&lt;/p&gt;&lt;p&gt;With &lt;a href="http://phonegap.com/"&gt;PhoneGap&lt;/a&gt; it is up to you to create the experience. Even if you want a simple straight forward native look and feel you'll need to simulate it. Results may vary depending on how good you are at that sort of thing. Of course something like Sencha Touch could be used to fake native but again what is native on one platform is weirdly alien on another and if you are targeting one platform only why not just man-up and learn that platform in the long term? At least give yourself that option.&lt;/p&gt;&lt;h3&gt;NimbleKit&lt;/h3&gt;&lt;p&gt;&lt;a href="http://nimblekit.com/index.php"&gt;NimbleKit&lt;/a&gt; is in some ways a bit like MonoTouch meets &lt;a href="http://phonegap.com/"&gt;PhoneGap&lt;/a&gt; . You can use it as you would &lt;a href="http://phonegap.com/"&gt;PhoneGap&lt;/a&gt; as a transparent API the bridges native and Web Technologies but you can also create real life native components such a Navigation Controllers and Tabs. Currently iOS is the only supported platform (more support for iPhone than iPad) though an Android version does exist in beta.&lt;/p&gt;&lt;p&gt;The interesting thing about &lt;a href="http://nimblekit.com/index.php"&gt;NimbleKit&lt;/a&gt; is that it allows you to make use of a range of technologies to create a really native application while at the same time making use of HTML5/CSS3 features to create a more compelling experience that becomes very difficult in native platforms (see my previous charting example in my last post).&lt;/p&gt;&lt;p&gt;Plugins are extremely simple as well. Just register the Obj-C class and call it from JavaScript. Very nice, very simple. This allows third party extensions to be bridged very easily too (Flurry for example).&lt;/p&gt;&lt;p&gt;The slightly annoying thing about &lt;a href="http://nimblekit.com/index.php"&gt;NimbleKit&lt;/a&gt; is that it is currently very quiet. There is still activity but it's certainly not on par with the likes of PhoneGap. There are also some issues around stability. It's not falling over all the time but there are somethings that need to be done in a certain order that aren't properly documented. There is some minor speculation that &lt;a href="http://www.sencha.com/"&gt;Sencha&lt;/a&gt; have acquired &lt;a href="http://nimblekit.com/index.php"&gt;NimbleKit&lt;/a&gt; and I'd welcome that with open arms because I like the philosophy around &lt;a href="http://nimblekit.com/index.php"&gt;NimbleKit&lt;/a&gt; and it could do with some money behind it.&lt;/p&gt;&lt;h2&gt;Options for Using Web Technologies&lt;/h2&gt;&lt;p&gt;Be it hybrid or simply a mobile targeted web site/app there are a ton of options out there. An absolute TON!&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Native look and feel, ultra rich frameworks&lt;/li&gt;
  &lt;li&gt;Truly cross-platform progressively enhancing frameworks&lt;/li&gt;
  &lt;li&gt;Frameworks for creating structure in your apps like MVC, MVVM etc.&lt;/li&gt;
  &lt;li&gt;Frameworks for offering responsive designs&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Mind boggling choices that change and evolve on a near hourly basis. Here are just a few&lt;/p&gt;&lt;h3&gt;Native Look and Feel - Sencha Touch&lt;/h3&gt;&lt;p&gt;&lt;a href="http://www.sencha.com/products/touch/"&gt;Sencha Touch&lt;/a&gt; targets high end iOS and Android devices and creates native-esque UIs (WebKit only). It's can be patchy on Android at times but the results you can get on iOS are very appealing. Version 2.0 is seeing massive improvements in performance across the compatible platforms. People who have experience in ExtJS will really feel at home here - other people will struggle with the short but steep learning curve (as with all &lt;a href="http://www.sencha.com/"&gt;Sencha&lt;/a&gt; frameworks).&lt;/p&gt;&lt;p&gt;Bear in mind that &lt;a href="http://www.sencha.com/products/touch/"&gt;Sencha Touch&lt;/a&gt; is a framework not a product so there will be work involved in getting it to behave exactly how you want but the available architecture options such as the MVC approach and the Data Stores make it very powerful.&lt;/p&gt;&lt;h3&gt;Cross Platform - jQuery Mobile&lt;/h3&gt;&lt;p&gt;&lt;a href="http://jquerymobile.com/"&gt;jQuery Mobile&lt;/a&gt; lives at the opposite end of the mobile framework spectrum. It takes the philosophy of jQuery UI (Progressive Enhancement, accessibility, themeability etc.) and put it into a mobile framework. So you construct a purely functional purely HTML application or site and with data annotations you let &lt;a href="http://jquerymobile.com/"&gt;jQuery Mobile&lt;/a&gt; make the site as rich and functional as it can. This means provided your mobile device can render HTML the site or app will at least be functional. Butt ugly but functional.&lt;/p&gt;&lt;p&gt;This carries a penalty though. Even when used on the top of the line device there are some trade offs that need to be made. The experience is not as polished as it could be. It is possible to tweak it yourself and cater to the high end devices only but there is extra work involved. Again the result is still very nice and generally smooth but it is easy to nitpick little quirks in the navigation or styling.&lt;/p&gt;&lt;h3&gt;MVC Frameworks - Backbone, Spine(Mobile), Sammy etc.&lt;/h3&gt;&lt;p&gt;If you want to roll your own UI entirely but still need to create a scalable and proven foundation &lt;a href="http://backbonejs.org/"&gt;Backbone&lt;/a&gt; and &lt;a href="http://spinejs.com/mobile/index"&gt;Spine&lt;/a&gt; are two very interesting projects. It's worth noting that Sencha Touch has a powerful MVC architecture built into to and it is somewhat similar to these frameworks.&lt;/p&gt;&lt;p&gt;These sort of frameworks offer base classes for your typical project structure - Controllers, Models and Views. They provide the wiring between these abstract classes allowing the developers to focus on the important stuff. The controllers allow you to specify custom routing in your single page apps, the models give you structured persistence and sometimes relationships and the views give you a nicer lifecycle around page components and custom event handling.&lt;/p&gt;&lt;p&gt;It's often said these frameworks take some time to "get" - some people can use them with ease while others just think they introduce unnecessary complexity. I think they come into their own in the mobile app space but I do still struggle with the concepts at times.&lt;/p&gt;&lt;h3&gt;Responsive Design - Skeleton&lt;/h3&gt;&lt;p&gt;There are grid and UI toolkit frameworks out there that are aimed at creating a responsive design. Got a large screen? Make use of the better horizontal space and stack elements horizontally. On a small mobile device with tiny viewport? Stack the elements vertically. &lt;a href="http://getskeleton.com"&gt;Skeleton&lt;/a&gt; is the perfect example of this using media queries to layout a grid system depending on available space. It also has a few nice widgets like Tabs and Buttons. Geared more around the web site rather than the app these frameworks are a handy addition.&lt;/p&gt;&lt;h2&gt;Run out of things to say&lt;/h2&gt;&lt;p&gt;I've covered a lot there but I wanted to get my thoughts out there in the world and hopefully educate and be educated. My tools of choice right now? Well RIGHT NOW I am tinkering with Obj-C and iOS as well as MonoTouch and did some NimbleKit hacking at the weekend. To me this sort of understanding is important and that's why I do it. You may argue that it makes you a jack-of-all-trades but I don't think so - this I merely trying to better understand the mobile ecosystem in the same way I try and grok the entire .NET ecosystem.&lt;/p&gt;&lt;p&gt;To me if you want to embrace mobile you need to understand it properly. If you're just sticking with a particular stack because it's familiar to you you are doing yourself and the project a dis-service. As many articles before me have said - you are not the programming language you code in - you need to embrace the polyglot nature of a good developer and do what is right for that project.&lt;/p&gt;&lt;p&gt;Hope you enjoyed this. That's me for December I think :). I have other things to do.&lt;/p&gt;</content></entry><entry><title>Rolling Your Own PhoneGap with MonoTouch</title><updated>Mon Dec 05 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/12/05/rolling-your-own-phonegap-with-monotouch/"></link><id>urn:yobriefca-se:feed:post:Rolling Your Own PhoneGap with MonoTouch</id><content type="html">&lt;p&gt;&lt;a href="http://phonegap.com/"&gt;PhoneGap&lt;/a&gt; is a clever little idea. Take a transparent native wrapper for a mobile platform, stretch a 100% x 100% web view and provide a JavaScript API that acts as a bridge to native methods that control the device features such as cameras, GPS and Contacts. It brings all those lovely web people into the mobile space. Truth be told PhoneGap isn't powered by flaked unicorn and rainbow essence - it's fairly simple to achieve yourself.&lt;/p&gt;&lt;p&gt;So I was thinking, as I do when I am bored, that it would be super awesome if MonoTouch could do the PhoneGap. Turns out there have been some attempts in the past to create bindings but I barely understand the posts so I felt disappointed. Then curious. Then I started hacking. Then happy. Then bored again. The happy once more. Then slightly frustrated. Finally I started writing this post and I think I feel a bit tired because it's late an I've had a hard day.&lt;/p&gt;&lt;p&gt;Anyway what follows is a very primitive start to what is essentially a PhoneGap, possibly NimbleKit, for MonoTouch. Thats cool becuase not only would you be saying "Screw You Objective-C!!" by using MonoTouch but you'd be sort of saying "Hey MonoTouch - HTML5 just had your mum, twice" as well. Hey I'd use a NimbleKit/PhoneGap-like framework for MonoTouch if it was available and offered a clever way of mixing Native with Web but thats just me. Hence I am writing this post.&lt;/p&gt;&lt;h2&gt;The Recipe&lt;/h2&gt;&lt;p&gt;There are various components that would make up a framework like this.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;A fullscreen Web View (lets call it the webside)&lt;/li&gt;
  &lt;li&gt;A controller that handles requests from the webside (lets call it the nativeside)&lt;/li&gt;
  &lt;li&gt;A JavaScript API on the webside that talks to the nativeside&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Simple enough so lets start proving things.&lt;/p&gt;&lt;h3&gt;The Webside&lt;/h3&gt;&lt;p&gt;Using a simple Single View iPhone Project in MonoDevelop I added some local resources.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;    |
    |_ www
    | |_ index.html
    |
    |_ AppDelegate.cs
    |_ MainViewController.cs
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For now the index.html file has some basic content&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="html "&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello UIWebView&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next up I created a full screen &lt;code&gt;UIWebView&lt;/code&gt; and asked it to point to the &lt;code&gt;index.html&lt;/code&gt; I just created in &lt;code&gt;wwww&lt;/code&gt; folder (which by the way should be set as "Content".&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ViewDidLoad&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ViewDidLoad&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIWebView&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;UIWebView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;RectangleF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;320f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;460f&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// obtain path to actual content file&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NSBundle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MainBundle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PathForResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"www/index"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"html"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// create an address and escape whitespace&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"file:{0}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"%20"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// create url and request&lt;/span&gt;
        &lt;span class="n"&gt;NSUrl&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;NSUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;NSUrlRequest&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;NSUrlRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// load request and add to main ?view&lt;/span&gt;
        &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LoadRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddSubview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running this gives us what we would expect (I hope you expect this otherwise I'd be worried).&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/monotouch-phonegap/capture1.png" alt="Sample Output for Web View" _="_" /&gt;&lt;/p&gt;&lt;p&gt;Webside more or less spiked for now. Next up the nativeside.&lt;/p&gt;&lt;h3&gt;The Nativeside&lt;/h3&gt;&lt;p&gt;So how is the native stuff supposed to respond to requests made via the mysterious JavaScript API from the webside. The approach that makes the most sense here is to set up an internal HttpListener that reacts to web requests. This means we can, thanks to the lack of a Same Origin Policy in UIWebViews via the &lt;code&gt;file://&lt;/code&gt; protocol, make Ajax requests to said listener to do stuff and return appropriate responses.&lt;/p&gt;&lt;p&gt;In the AppDelegate class I created a basic HttpListener that simply returns a simple JSON response of true if a request is made.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;HttpListener&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HandleRequest&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IAsyncResult&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//Get the listener context&lt;/span&gt;
    &lt;span class="n"&gt;HttpListenerContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EndGetContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;//Start listening for the next request&lt;/span&gt;
    &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BeginGetContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;AsyncCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HandleRequest&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;responseBytes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"text/json"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;HttpStatusCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentLength64&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;responseBytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OutputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;responseBytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;responseBytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OutputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;FinishedLaunching&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIApplication&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;NSDictionary&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;UIWindow&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UIScreen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MainScreen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bounds&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;viewController&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JsBridgeViewController&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RootViewController&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewController&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MakeKeyAndVisible&lt;/span&gt; &lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;HttpListener&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Prefixes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://*:30001/"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BeginGetContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;AsyncCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HandleRequest&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;The Final Step - The Bridge&lt;/h3&gt;&lt;p&gt;So we need to talk to this "server" from our JavaScript. Easy stuff - for now we just make a simple ajax call to our endpoint in the &lt;code&gt;index.html&lt;/code&gt; (synchronous for code simplicity).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="html "&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'GET'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'http://127.0.0.1:30001/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;responseText&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Error"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Running this once again reveals that all is sweet...&lt;/p&gt;&lt;p&gt;&lt;img src="/images/blog/monotouch-phonegap/capture2.png" alt="Sample Output for Web View with Nativeside call" _="_" /&gt;&lt;/p&gt;&lt;h2&gt;Now What?&lt;/h2&gt;&lt;p&gt;Well thats about as far as I've actually gotten but this is far enough to satisfy myself that it's possible to talk between the web view and the native stuff. Yeah I know it's not rocket science but I needed to prove this to myself. So whats next? Well I want see what I can do with regards to making real calls to real things, perhaps even venture into NimbleKit territory and generate native elements such as ViewControllers and Tabs - who knows!&lt;/p&gt;&lt;p&gt;&lt;em&gt;(MYSTERIOUS EXIT MUSIC)&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;(FADE TO BLACK)&lt;/em&gt;&lt;/p&gt;&lt;p&gt;To Be Continued...&lt;/p&gt;&lt;p&gt;&lt;em&gt;(EVIL CACKLE HEARD IN THE DISTANCE)&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;(END SCENE)&lt;/em&gt;&lt;/p&gt;</content></entry><entry><title>So this mobile stuff then... (Hybrid)</title><updated>Sat Dec 03 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/12/03/so-this-mobile-stuff-then-dot-dot-dot-hybrid/"></link><id>urn:yobriefca-se:feed:post:So this mobile stuff then... (Hybrid)</id><content type="html">&lt;p&gt;It's fair to say over the past year or so I've dabbled in a bit of mobile development. I've been involved in a number of projects (from a technical perspective),&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Web Based, Tablet Focused Analytics Dashboard (Custom HTML and &lt;a href="http://www.highcharts.com/"&gt;HighCharts&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Web Based, Tablet Focused Data Capture application (&lt;a href="http://www.sencha.com/products/touch"&gt;Sencha Touch&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Hybrid application for a teleco (&lt;a href="http://jquerymobile.com"&gt;jQuery Mobile&lt;/a&gt; and &lt;a href="http://phonegap.com"&gt;PhoneGap&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Mobile Hackathon (&lt;a href="http://jquerymobile.com"&gt;jQuery Mobile&lt;/a&gt; and &lt;a href="http://phonegap.com"&gt;PhoneGap&lt;/a&gt; and &lt;a href="http://parse.com"&gt;Parse&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Hybrid Application for a Bank (&lt;a href="http://www.sencha.com/products/touch"&gt;Sencha Touch&lt;/a&gt; and &lt;a href="http://phonegap.com"&gt;PhoneGap&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Various iPhone and iPad POCs for a health based project (&lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt;, XCode)&lt;/li&gt;
  &lt;li&gt;Technology evaluations (Native Apple Development, &lt;a href="http://parse.com"&gt;Parse&lt;/a&gt;, &lt;a href="http://www.sencha.com/products/touch"&gt;Sencha Touch&lt;/a&gt;], &lt;a href="http://phonegap.com"&gt;PhoneGap&lt;/a&gt;, &lt;a href="http://nimblekit.com/index.php"&gt;NimbleKit&lt;/a&gt;, &lt;a href="http://jquerymobile.com"&gt;jQuery Mobile&lt;/a&gt;, &lt;a href="http://backbonejs.org/"&gt;Backbone&lt;/a&gt;, &lt;a href="http://spinejs.com"&gt;Spine.js&lt;/a&gt;, &lt;a href="http://www.appcelerator.com/"&gt;Appcelerator&lt;/a&gt;, &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt;, &lt;a href="http://android.xamarin.com/"&gt;Monodroid&lt;/a&gt; and plenty more...)&lt;/li&gt;
  &lt;li&gt;Consulted on various projects around the whole "native vs hybrid vs web" thing&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;That Hybrid Word&lt;/h2&gt;&lt;p&gt;One thing I've found is that many clients are using the old "a web app will never be as polished as a native app" argument but TBH that really isn't the case. This is down to a lack of real understanding and abuse of the word in the industry. A bit of hybridism can go along way in creating a rich experience.&lt;/p&gt;&lt;h3&gt;A Rose By Any Other Name...&lt;/h3&gt;&lt;p&gt;Many people break mobile applications into 3 VERY distinct areas,&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Native&lt;/strong&gt; - app installed on the device, written using the default toolkit for that platform.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Hybrid&lt;/strong&gt; - consists of a transparent "native" layer and a Web View into which HTML, JavaScript and CSS is loaded (generally stored within the native package). The nativelayer provides and API for the JavaScript model in the Web View to call. Think PhoneGap.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Web&lt;/strong&gt; - HTML, CSS, JavaScript assets hosted on the web and accessed from a mobile device. Possibly some native-like features such as icons and removing the browser chrome.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;The problem with the hybrid definition there is that it is, well, wrong. There isn't a clear distinction between the native and hybrid and there shouldn't be. If I spun up a Silverlight app that put a Web View in the middle to display some HTML I wouldn't call it hybrid. If I wrote an Android app that used a library written in Scala it wouldn't be hybrid. Or even if I wrote a Monotouch app using some bindings to the &lt;a href="http://www.flurry.com/"&gt;Flurry&lt;/a&gt; framework - nope not hybrid.&lt;/p&gt;&lt;h3&gt;The Charting Example&lt;/h3&gt;&lt;p&gt;A good example of "hybrid" is charting. There aren't a great deal of nice flexible charting libraries for iOS, but there is for JavaScript/HTML5. So some of our projects have made use of HighCharts and Sencha Charting inside a transparent UIWebView to render charts that are flexible and rich. I wouldn't bother pinning the hybrid tag to that app - it's simply making use of whatever technologies helped us get the job done in the best way possible.&lt;/p&gt;&lt;p&gt;I understand the use of hybrid in sales pitches or to simply convey meaning to people who are less technical but the problem is people are starting to develop opinions based on these words alone which is disatorous. I've had customers tell me that they want native because that hybrid stuff is ugly. True some hybrid apps are ugly as sin (Aer Lingus iPhone app for one) but then so are some native ones. It's unfair to collectively throw away and entire type of app because of poorly misunderstood words.&lt;/p&gt;&lt;h2&gt;But, But, But... Reusability&lt;/h2&gt;&lt;p&gt;People use the hybrid term to show that they have thought about portability. Write once, run anywhere. In that sense I guess using hybrid makes sense (but that sort of talk is really part of the sales talk right?). But then it would be just as easy to say that "elements of the solution will be written in a platform agnostic language to minimise code rewrites on different devices" - or something like that. In fact that would be better. That way you cover off more than "hybrid". You've included the things like &lt;a href="http://xamarin.com/monotouch"&gt;MonoTouch&lt;/a&gt; or &lt;a href="http://www.appcelerator.com/"&gt;Appcelerator&lt;/a&gt; as well. You've moved away from implementation specifics at the point where they shouldn't matter anyway! Thats got to be a good thing.&lt;/p&gt;&lt;h2&gt;Ah ha but what about Skills?&lt;/h2&gt;&lt;p&gt;People go "hybrid" for another good reason - because they have no experience in the native platform. I used to be one of those people but I'm not convinced that this is a good reason on its own. If you want to make a compelling experience your limitation should never be your lack of skill. Get skilled. Obj-C isn't as scary as it seems - in fact I'd rather write Obj-C than Java. Dig deep - thats what we've got the internet for - understanding and learning (well that and porn).&lt;/p&gt;&lt;h2&gt;Awareness and Education&lt;/h2&gt;&lt;p&gt;So what am I saying? Well for a start I'm not saying we totally abandon the word hybrid and mock anyone who uses it. Hells no. It's still useful as I've already mentioned above. All I ask is that you make sure you know what you are talking about and that the people you are talking to know what you are talking about. That and don't just take the easy route because it's easier for you.&lt;/p&gt;</content></entry><entry><title>Chutzpah 1.3 Released</title><updated>Sun Nov 27 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/11/27/chutzpah-1-dot-3-released/"></link><id>urn:yobriefca-se:feed:post:Chutzpah 1.3 Released</id><content type="html">&lt;p&gt;I missed the announcement last week the &lt;a href="http://matthewmanela.com/blog/chutzpah-1-3-0-released/"&gt;Chutzpah 1.3 has been released&lt;/a&gt;. This is a big release as it brings support for &lt;a href="http://pivotal.github.com/jasmine"&gt;Jasmine&lt;/a&gt; - my BDD framework of choice for JavaScript.&lt;/p&gt;&lt;p&gt;&lt;a href="http://chutzpah.codeplex.com/"&gt;Chutzpah&lt;/a&gt; is available as a plugin for Visual Studio but more importantly as a command line runner which allows us to integrate JavaScript testing into our continuious integration builds. Excellent stuff!&lt;/p&gt;</content></entry><entry><title>Out with the old...</title><updated>Fri Nov 25 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/11/25/out-with-the-old-dot-dot-dot/"></link><id>urn:yobriefca-se:feed:post:Out with the old...</id><content type="html">&lt;p&gt;... in with the new. So I've started rolling out my new site, expect somethings to be slightly broken as images and whatnot have been moved around. Spent a day or two designing a new site that was similar to the old one but slightly better. Eventually got frustrated at my inability to do any sort of graphical design and went the &lt;a href="http://octopress.org"&gt;Octopress&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Still a few things to tweak, possibly add a splash of colour around the place and give it a bit more life. Also apologies to people who subscribe to my feed - looks like the new feed has pushed all the old posts out as new. But hey they are worth reading twice!&lt;/p&gt;</content></entry><entry><title>Speaking at DevWeek 2012</title><updated>Thu Nov 17 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/11/17/speaking-at-devweek-2012/"></link><id>urn:yobriefca-se:feed:post:Speaking at DevWeek 2012</id><content type="html">&lt;p&gt;&lt;img src="http://www.devweek.com/pix/dwbanner2012c.jpg" style="width:100%" /&gt;&lt;/p&gt;&lt;p&gt;So it's been officially announced that I am talking at &lt;a href="http://devweek.com"&gt;DevWeek&lt;/a&gt; 2012 in London (26th - 30th March). Looking at the names on the billing it's quite weird seeing my name thrown in there, perhaps some one made a mistake :-P&lt;/p&gt;
&lt;!--more--&gt;
&lt;blockquote&gt;&lt;h3&gt;BDD and .NET&lt;/h3&gt;&lt;p&gt;Behaviour Driven Design/Development is an interesting premise, but does it really work? What benefits does it offer and how can we successfully apply its principles in the .NET world? We can answer these questions by reviewing the principles around BDD and diving into the technologies that exist in the .NET world that support these principles.&lt;/p&gt;&lt;p&gt;From low-level unit focused testing technologies all the way up to the more abstract integration and acceptance level suite of specifications, automated testing and continuous integration, we will see that .NET isn't lacking in choice. As a bonus we will address the oft ignored testing of our JavaScript project components using tools such as Jasmine and QUnit.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;The brief was written in haste and sounds a bit boilerplate but I will be giving it my all and really pull off an engaging session. I've been at the attendee end of a few bad sessions and I will do my damnedest not to put anyone through that kind of ordeal.&lt;/p&gt;&lt;p&gt;The &lt;a href="http://www.devweek.com/sessions/"&gt;line-up for DevWeek&lt;/a&gt; looks excellent and the pre/post conference workshops all look stellar - so much choice, I just need to find a way to clone myself.&lt;/p&gt;&lt;p&gt;If you're going - see you there, catch up with me and come throw tomatoes and/or abuse during my session. If you're not going, well sucks to be you then!&lt;/p&gt;</content></entry><entry><title>Wednesday Tip: C# Type aliases</title><updated>Wed Nov 16 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/11/16/wednesday-tip-c-number-type-aliases/"></link><id>urn:yobriefca-se:feed:post:Wednesday Tip: C# Type aliases</id><content type="html">&lt;p&gt;Ever written code that looks quite like this?&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp"&gt;&lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;KeyValuePair&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;KeyValuePair&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I have. Ugly and noisy isn't it! &lt;!--more--&gt; OK you could make it &lt;em&gt;slightly&lt;/em&gt; less noisy by using &lt;code&gt;var&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp"&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;KeyValuePair&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Or I suppose you could ahead and create yet another random type to abstract the implementation out of the way but thats just too much work for something internal to a class.&lt;/p&gt;&lt;p&gt;Turns out you can make use of alises. I've always known about using alises for namespaces to either resolve possible type conflicts or to make code a bit more understandable&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp"&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;CoreWeb&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Web&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;CoreWeb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So this works with namespaces &lt;strong&gt;AND&lt;/strong&gt; types.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp"&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Cache&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;KeyValuePair&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This allows us to make the declaration above in a much nicer manner,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp"&gt;&lt;span class="n"&gt;Cache&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Cache&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;OK so there are way to many uses of the word cache there but you get the gist. Handy to know. Never knew this until today #youlearnsomethingneweveryday&lt;/p&gt;</content></entry><entry><title>Innovation Waffle</title><updated>Wed Nov 09 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/11/09/innovation-waffle/"></link><id>urn:yobriefca-se:feed:post:Innovation Waffle</id><content type="html">&lt;p&gt;There has been a lot of internal chatter at my employer around innovation. A recent internal Hackathon produced some interesting, innovative stuff for using a Kinect in one of our projects and piqued peoples interest. Oddly enough my dad reminded me of a quote the other day.&lt;/p&gt;
&lt;!--more--&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;em&gt;"If I had asked my customers what they wanted they would have said a faster horse"&lt;/em&gt; - Henry Ford&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;Creating a &lt;em&gt;faster horse&lt;/em&gt; is considered &lt;strong&gt;sustaining innovation&lt;/strong&gt; - you're pushing boundaries but you aren't breaking into new markets. Instead, what Ford did was create the &lt;a href="http://en.wikipedia.org/wiki/Ford_Model_T"&gt;Ford Model T&lt;/a&gt;, the first affordable automobile. This &lt;strong&gt;disruptive innovation&lt;/strong&gt; changed everything, breaking into new markets and created value where once no value existed.&lt;/p&gt;&lt;p&gt;To be truly innovative you need both a &lt;strong&gt;need&lt;/strong&gt; and the &lt;strong&gt;technology&lt;/strong&gt; to achieve it. The &lt;strong&gt;need&lt;/strong&gt; doesn't even need to be particularly complex (in Fords case it was the need to go faster), the &lt;strong&gt;technology&lt;/strong&gt; doesn't even need to be revolutionary - just applied in a different manner (in my employers case the Kinect).&lt;/p&gt;&lt;p&gt;But to really succeed in this innovation you need the ability to marry these with something. That something is a &lt;strong&gt;vision&lt;/strong&gt;. Ford realised the need and he was capable of harnessing technology to address that need in such a way as to disrupt the market. This wouldn't have been possible without Fords vision. In fact he didn't even really need to understand the detailed nuances of what the public needed, all he needed to know was the basic premise. Arguably, even if he wasn't the engineer, he could have got someone else to manage the technology. Admittedly this opens up another avenue of discussion - the whole nature vs nurture aspect of innovation - but with a strong enough &lt;strong&gt;vision&lt;/strong&gt; I'm not sure that matters as much.&lt;/p&gt;&lt;h2&gt;Vision is the Glue of Innovation&lt;/h2&gt;&lt;p&gt;It's great to have a &lt;strong&gt;need&lt;/strong&gt; you want to address, it's even better to be able to apply all sorts of &lt;strong&gt;technology&lt;/strong&gt; to that need but odds are it will all come to nought without a strong &lt;strong&gt;vision&lt;/strong&gt;. &lt;strong&gt;Vision is the glue of innovation&lt;/strong&gt;, while you need both need and technology they'll never really stay together without that binding vision.&lt;/p&gt;</content></entry><entry><title>GiveCamp Northern Ireland 2012</title><updated>Thu Nov 03 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/11/03/givecamp-northern-ireland-2012/"></link><id>urn:yobriefca-se:feed:post:GiveCamp Northern Ireland 2012</id><content type="html">&lt;p&gt;&lt;img src="http://givecampni.org.uk/assets/img/givecamp-large-logo.png" alt="GiveCamp" _="_" /&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://givecampni.org.uk/"&gt;http://givecampni.org.uk/&lt;/a&gt; &lt;/p&gt;&lt;p&gt;Aint no backing out now son! In 2012 there is going to be a Northern Ireland based &lt;a href="http://givecamp.org"&gt;GiveCamp&lt;/a&gt;. The "Register Your Interest" placeholder site is now live at &lt;a href="http://givecampni.org.uk/"&gt;http://givecampni.org.uk/&lt;/a&gt; so if everyone can start spreading the word and getting excited that would be super awesome.&lt;/p&gt;&lt;p&gt;Any tips, advice or help at this point is also really welcome.&lt;/p&gt;&lt;p&gt;Let the fun begin....&lt;/p&gt;</content></entry><entry><title>Nancy JSONP Hook</title><updated>Tue Nov 01 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/11/01/nancy-jsonp-hook/"></link><id>urn:yobriefca-se:feed:post:Nancy JSONP Hook</id><content type="html">&lt;blockquote&gt;&lt;p&gt;3 Nov 2011: As of Nancy v0.9 this is part of the core (same logic applies)&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I needed to write a simple JSONP capable service recently and decided to run with &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt;. Turns out Nancy doesn't give us a way to do JSONP so I set about creating something that fitted my use case. The fruits of my very very short labour (thanks to the super-hyper-flexible-powerfulness of the Nancy Pipeline) can be seen here. The full source is available in the &lt;a href="https://github.com/kouphax/nancy-jsonp"&gt;Github repo&lt;/a&gt; and hopefully we can get this pushed into the core of Nancy shortly (or at least part of the official repo one way or another) - just for the geek cred of course :)&lt;/p&gt;
&lt;!--more--&gt;&lt;h2&gt;Implementation Rundown&lt;/h2&gt;&lt;p&gt;I took the same approach as the existing hooks (using the SassAndCoffee one as my implementation pattern) so created a static &lt;code&gt;Hooks&lt;/code&gt; class with an &lt;code&gt;Enable&lt;/code&gt; method that simply adds a new item to the end of the pipeline&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp"&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Enable JSONP support in the application&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;param name="pipeline"&amp;gt;Application Pipeline to Hook into&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Enable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IApplicationPipelines&lt;/span&gt; &lt;span class="n"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pipeline&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AfterRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddItemToEndOfPipeline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PrepareJsonp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The meat of the work lies in the &lt;code&gt;PrepareJsonp&lt;/code&gt; method&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Transmogrify original response and apply JSONP Padding&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;param name="context"&amp;gt;Current Nancy Context&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;PrepareJsonp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NancyContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isJson&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;hasCallback&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"callback"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;HasValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isJson&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hasCallback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// grab original contents for running later&lt;/span&gt;
        &lt;span class="n"&gt;Action&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Stream&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"callback"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// set content type to application/javascript so browsers can handle it by default&lt;/span&gt;
        &lt;span class="c1"&gt;// http://stackoverflow.com/questions/111302/best-content-type-to-serve-jsonp&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"application/javascript"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contents&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// disposing of stream is handled elsewhere&lt;/span&gt;
            &lt;span class="n"&gt;StreamWriter&lt;/span&gt; &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;StreamWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;AutoFlush&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
            &lt;span class="p"&gt;};&lt;/span&gt;

            &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{0}("&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;");"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Let me highlight a few of the key areas here.&lt;/p&gt;&lt;h3&gt;Determine if Response need JSONP Padded&lt;/h3&gt;&lt;p&gt;I decided to take a basic approach to determining if the request should return a JSONP response,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;If the content type of the current response is &lt;code&gt;application/json&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;If there is a query string parameter called &lt;code&gt;callback&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Here is what we need to satisfy that,&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;isJson&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;hasCallback&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"callback"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;HasValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isJson&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;hasCallback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I am sure this could be refined and open to suggestion (perhaps more applicable content types or extra possible configuration for the callback parameter). I wonder if it is a bit safer to restrict requests that are GET requests seeing as that is the only way to do JSONP? Hmmmm.&lt;/p&gt;&lt;p&gt;Next thing I set the content type to &lt;code&gt;application/javascript&lt;/code&gt; as that makes perfect sense&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"application/javascript"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally I wrap the original responses content in the JSONP "padding" (the callback function) and write everything out.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code class="csharp "&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contents&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// disposing of stream is handled elsewhere&lt;/span&gt;
    &lt;span class="n"&gt;StreamWriter&lt;/span&gt; &lt;span class="n"&gt;writer&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;StreamWriter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;AutoFlush&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{0}("&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;writer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;");"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So all in all very simple but damn I'm proud that I got off my arse and contributed to such a stellar community. hopefully someone finds this useful.&lt;/p&gt;</content></entry><entry><title>Tinyweb.FluentSecurity v0.1 Released</title><updated>Tue Oct 18 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/10/18/tinyweb-dot-fluentsecurity-v0-dot-1-released/"></link><id>urn:yobriefca-se:feed:post:Tinyweb.FluentSecurity v0.1 Released</id><content type="html">&lt;blockquote&gt;&lt;p&gt;Slight change here - FluentSecurity is now FluentAuth to avoid any confusion with a similarly named product of which there is some inspiration taken. Anyway on with the original post....&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I have procrastinated long enough on this and it was blocking some of the other work I wanted to get on with. I have just pushed v0.1 to Nuget and launched the &lt;a href="http://yobriefca.se/projects/fluentauth/"&gt;site sans documentation&lt;/a&gt;.&lt;/p&gt;
&lt;!--more--&gt;&lt;p&gt;Not a great deal has changed since my last announcement, well it has but it hasn't been documented as it should be backwards compatible. Just a few more configuration options that I'll explain once I get the documentation rolled out.&lt;/p&gt;
&lt;script type="text/javascript"&gt;
    (function () {
        var nb = document.createElement('script'); nb.type = 'text/javascript'; nb.async = true;
        nb.src = 'http://s.prabir.me/nuget-button/0.2.1/nuget-button.min.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(nb, s);
    })();
&lt;/script&gt;
&lt;pre class="nuget-button"&gt;Install-Package Tinyweb.FluentAuth&lt;/pre&gt;&lt;p&gt;So go check it out and hopefully you'll find it useful - plenty of plans for expansion to other frameworks and some more API changes coming too. Now back to procrastinating.&lt;/p&gt;</content></entry><entry><title>refORM: Death to ORMs in .NET</title><updated>Mon Oct 17 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="/presentations/reform-death-to-orms.pdf"></link><id>urn:yobriefca-se:feed:post:refORM: Death to ORMs in .NET</id><content type="html"></content></entry><entry><title>CoffeeScript the Awesome</title><updated>Mon Oct 10 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="/presentations/coffeescript-the-awesome.pdf"></link><id>urn:yobriefca-se:feed:post:CoffeeScript the Awesome</id><content type="html"></content></entry><entry><title>CoffeeScript the Awesome</title><updated>Sun Oct 09 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/10/09/coffeescript-the-awesome/"></link><id>urn:yobriefca-se:feed:post:CoffeeScript the Awesome</id><content type="html">&lt;p&gt;CoffeeScript - "a little language that compiles into JavaScript" has been making a lot of noise in the web community with its inclusion in Ruby on Rail v3 and its swift rise in popularity on Github.&lt;/p&gt;&lt;p&gt;This session will look at the benefits this language brings over the native JavaScript alternative, the syntactical double rainbow it creates, the problems you'll face and of course how you can actually use it in different technology stacks such as Ruby, node.js, .NET and Java.&lt;/p&gt;
&lt;script src="http://speakerdeck.com/embed/4eb7effc5040b9005100f937.js"&gt;&lt;/script&gt;</content></entry><entry><title>refORM: Death to ORMs in .NET</title><updated>Sun Oct 09 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/10/09/reform-death-to-orms-in-net/"></link><id>urn:yobriefca-se:feed:post:refORM: Death to ORMs in .NET</id><content type="html">&lt;p&gt;Data Access is a difficult area to master. There are plenty of frameworks such as Entity Framework, NHibernate, i/myBatis. These frameworks attempt to make data access simpler but while these products are widely used they can lead to leaky abstractions forcing us to work around constraints of the technology or introduce the nightmare of trying to figure out what is happening deep down in the frameworks internals.&lt;/p&gt;&lt;p&gt;In this session we will look at the various lightweight alternatives and the advantages they offer as well as provide rationale about why the more heavyweight approach isn't always the best approach.&lt;/p&gt;&lt;p&gt;&lt;a href="https://bitbucket.org/kouphax/ddd-data-access/"&gt;Demo Code and Examples&lt;/a&gt;&lt;/p&gt;
&lt;script src="http://speakerdeck.com/embed/4eb7ebb05040b9005400eb4f.js"&gt;&lt;/script&gt;</content></entry><entry><title>DDDNorth Retrospective</title><updated>Sat Oct 08 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/10/08/dddnorth-retrospective/"></link><id>urn:yobriefca-se:feed:post:DDDNorth Retrospective</id><content type="html">&lt;p&gt;So its the day after DDD North and I'm more or less getting packed and ready to do the long boring travel bit home, which is really less travelling and more waiting around. Anyway DDD North was pretty special. Excellent turn out, great location and above all some great talks. My talk (refORM: Death to ORMs in .NET) had a technical problem with the projector which meant the live demo section I wove in since last weeks talk had to be abandoned, shit happens. It also went very fast, the talk was over in about 35 minutes but at least there was plenty of banter and questions - so at least the people seemed to be engaged. Enough about my woes. I'll have the slides and the (super secret) demo code available as soon as get back home.&lt;/p&gt;&lt;p&gt;So what about the rest of the talks?&lt;/p&gt;&lt;h2&gt;Commercial Software Development - Writing Software Is Easy, Not Going Bust is the Hard Bit - Liam Westley&lt;/h2&gt;&lt;p&gt;Liam took us through some of his personal experience of working in the industry as a sole(ish) trader. He highlighted some of the things that you should really be doing to ensure you aren't "wasting time" and raised a very valid point - every time you take a support call you are bleeding money. Makes sense but I initially rebelled against this premise. I had wrongly jumped to the conclusion that if you don't get support calls you don't need a support contract - which in hindsight is pretty stupid of me.&lt;/p&gt;&lt;p&gt;He also highlighted that if the phone does ring, or a support issue is raised then you need to make sure you have all the information at hand. Detailed logging of crucial areas of your system, automated emails or tweets when the system detects something is wrong and generally making sure you don't need to rely on the end user to tell you what has went wrong. Again makes perfect sense. Obviously writing good code that is well tested prior to release is another winner in this area.&lt;/p&gt;&lt;p&gt;In terms of releases he re-iterated the old "release early" mantra. You don't want to waste time writing features that no one is going to use and you aren't really going to know this until users have the product in their hands - the minimum viable product.&lt;/p&gt;&lt;p&gt;All in all a great session, nothing massively new for me here but I love hearing this stuff from people with real experience in it - it really helps solidify my beliefs in good code and early release.&lt;/p&gt;&lt;h2&gt;"The Happy Programmer" - Is It a Myth? - Andy Gibson&lt;/h2&gt;&lt;p&gt;Looking the various aspects of a developers life that determine if it is truly possible to have a "happy developer". A fair amount of audience participation looking at the best office type, the rationale for having the best hardware and all that other stuff.&lt;/p&gt;&lt;h2&gt;Continuous Delivery - Paul Stack&lt;/h2&gt;&lt;p&gt;Certainly the highlight of the day for me. Many people I work with believe that CD is a long way off for many projects but there was so much hard evidence at this talk that debunked this theory. If it can be scripted or automated in anyway it should be. This includes HTML, JS, CSS, and C# code analysis, unit tests, acceptance tests, environment configuration, document generation, Sharepoint config and setup (yep that to), VM rollout, DB upgrades and backups. Everything really.&lt;/p&gt;&lt;p&gt;A great example of this was a massive search engine in Norway that turned their 2 1/2 year release cycle into once every 4 hours. This wasn't some big massive leap, it was done by gradually shortening the release cycle and tightening up the processes bit by bit till they got there.&lt;/p&gt;&lt;p&gt;A good point was raised on this topic later in the evening - CI/CD scripts should be the very first thing you do on a project. Then you build them up gradually as and when you need to. No point in waiting till the end of the first release do this - it'll hurt way too much and never get done.&lt;/p&gt;&lt;h2&gt;The 10 Habits of Highly Effective Programmers - Dennis Doomen&lt;/h2&gt;&lt;p&gt;The last session, and after being up since 4AM I was wrecked. But you know what? Even with the tiredness and the heat of the room I remained engaged. Again a lot of the topics in this talk I was already very familiar with but it was great to get a different perspective and new techniques for achieving them. Also managed to increase my "To Read" list with a few books from this session. Nice way to finish off the day.&lt;/p&gt;&lt;h2&gt;Grok Talks&lt;/h2&gt;&lt;p&gt;Through lunchtime there were a number of quick, informal grok talks. The one I want to mention was on "teaching your kids to code" and showed various tools/games like LightBot and SmallBasic - my son is still a tiny bit young for this sort of thing but he'll probably be forced to do some of this with me being his dad. I'd also have liked to have seen Scratch (from MIT) mentioned as this is a similar technology.&lt;/p&gt;&lt;p&gt;Overall the day was beyond my expectations - plenty of refreshments, goodies, decent food and great people. To be honest I really felt like a small fish presenting though - after seeing the other guys I felt my delivery was decidedly amateur. But hey thats why I am doing this - to get better. Looking forward to the next one! Thanks everyone.&lt;/p&gt;&lt;p&gt;Next up I have GOTOAmsterdam (this Wednesday). Funnily enough I need to find my passport ASAP as 2 minutes after jokingly saying "Wouldn't it be funny if I couldn't find my passport?" to my wife, well, you can probably guess what happened. So not only did I look like a dick in-front of my lovely wife I also managed to put my GOTO conf attendance in jeopardy - hell I really am a dick!&lt;/p&gt;</content></entry><entry><title>DDDBelfast Retrospective</title><updated>Sun Oct 02 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/10/02/dddbelfast-retrospective/"></link><id>urn:yobriefca-se:feed:post:DDDBelfast Retrospective</id><content type="html">&lt;p&gt;So I'm no longer a conference speaker virgin. It was so good I did it twice in the same day. How'd it go? Went good I guess. From a conference stand point it seemed to fly by. There was a kind of laid back atmosphere about the whole thing and the numbers, while lower than expected were high enough to make all the talks feel comfortably occupied. I skipped the first block to get my talk prepared but managed to pull in a few talks after I got my first one over.&lt;/p&gt;&lt;h1&gt;Other Peoples Talks&lt;/h1&gt;&lt;h2&gt;Behavioural Driven Development (BDD) with F# - Phillip Trelford&lt;/h2&gt;&lt;p&gt;Big toss up here as I wanted to attend &lt;a href="http://developerdeveloperdeveloper.com/dddie11/ViewSession.aspx?SessionID=871"&gt;Andrea's Code Kata&lt;/a&gt; as well but this talk won out. It was a decent talk - a nice mix of theory and practise. Interesting to see Phils testing framework, &lt;a href="http://tickspec.codeplex.com"&gt;TickSpec&lt;/a&gt;, in action, I must revisit this again some time soon (a promise I'll probably never keep :( ).&lt;/p&gt;&lt;h2&gt;CQRS - Bringing Elegance to "Normal" Applications - Ashic Mahtab&lt;/h2&gt;&lt;p&gt;The last CQRS talk I attended flew right over my head in terms of complexity. Since then I've done a bit of research and came prepared. I really didn't need to though. Ashic did a really good job of taking something that appears quite complex and conveying the message quite nicely. Interested to see his 250 LOC CQRS framework as well!&lt;/p&gt;&lt;h1&gt;My Talks&lt;/h1&gt;&lt;h2&gt;refORM: Death to ORMs in .NET&lt;/h2&gt;&lt;p&gt;Went quite well. I played it safe in terms of not going for any live coding but I think it would have been much better if I had (and plan to next time week at DDDNorth) as it was more dry than I would have liked. Lots of good discussion after the presentation that I might try and bring in next time too.&lt;/p&gt;&lt;h2&gt;Coffeescript the Awesome&lt;/h2&gt;&lt;p&gt;Slight technical problem resulted in the majority of the code samples being nearly unreadable for a lot of the presentation which was a total fail. I think I flew through the whole thing too - perhaps a bit of live coding would have improved this one too. That said the discussion lasted as long as the talk more or less which is good. Another problem was a number of people came in quite late and missed the first lot of slides which meant I was repeating the content at the end but hey thats fine.&lt;/p&gt;</content></entry><entry><title>Tinyweb Fluent Security</title><updated>Sun Sep 25 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/09/25/tinyweb-fluent-security/"></link><id>urn:yobriefca-se:feed:post:Tinyweb Fluent Security</id><content type="html">&lt;p&gt;I've been working on a little project recently that is using Tinyweb. It's a typical little CRUDdy app and makes use of typical security features in a web app. Tinyweb itself doesn't provide any sort of custom security features out of the box but as it sits on top of ASP.NET you can avail of all the normal membership stuff. Problem is though - the default membership stuff, when working directly with it, is a bit cumbersome. I recently tinkered with a project for ASP.NET MVC called &lt;a href="http://www.fluentsecurity.net/"&gt;FluentSecurity&lt;/a&gt; that added a fluent syntax to configuring security for ASP.NET MVC applications. Taking a healthy dose of inspiration from this project I rolled a lightweight Tinyweb version of my own. I figure this might be useful to someone else so I have now created a new project specifically for this. It's probably totally broken in areas but the areas I've written tests around appear to work. &lt;/p&gt;&lt;p&gt;The source is available in a &lt;a href="https://bitbucket.org/kouphax/tinyweb-fluentsecurity"&gt;BitBucket repository&lt;/a&gt; so feel free to critique. Once I get it tested better, documents written up and generally applied a bit of sipt and polish I'll push it to Nuget and the fun can really begin.&lt;/p&gt;&lt;h1&gt;Tinyweb.FluentSecurity&lt;/h1&gt;&lt;p&gt;FluentSecurity comes as a 2 part solution&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;The &lt;code&gt;Security&lt;/code&gt; class is the main entry point for configuring FluentSecurity and authenticating users&lt;/li&gt;
  &lt;li&gt;The &lt;code&gt;SecurityFilter&lt;/code&gt; class is a Tinyweb filter that performs auth tests on the current request&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Security --&lt;/p&gt;&lt;p&gt;The &lt;code&gt;Security&lt;/code&gt; class provides a central location to configure FluentSecurity. &lt;code&gt;Security&lt;/code&gt; is also responsible for executing the auth tests you pass to it.&lt;/p&gt;&lt;h3&gt;Security.Configure(Action&lt;Configurator&gt; configurator)&lt;/Configurator&gt;&lt;/h3&gt;&lt;p&gt;&lt;code&gt;Configure&lt;/code&gt; lets you define the configuration for you module. It accepts an Action that can be used to define rules for each handler. For example&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;Security.Configure(c =&amp;gt;
{
    c.For&amp;lt;RootHandler&amp;gt;().DenyAnonymousAccess();
    c.For&amp;lt;AdminHandler&amp;gt;().RequireRoles("Admin");
    c.For&amp;lt;UserHandler&amp;gt;().DenyRoles("Admin");
    c.For&amp;lt;SecretHandler&amp;gt;().AllowVerbs(Security.AllowedVerbs.GET | Security.AllowedVerbs.POST);
});

Tinyweb.Init();
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We call &lt;code&gt;Security.Configure&lt;/code&gt; just before we Init Tinyweb (though it can be done at anytime realistically speaking). The configuration block shows off most of the ways you can configure handlers e.g.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code&gt;DenyAnonymousAccess&lt;/code&gt; will prevent, as the name suggests, all anonymous users from accessing this handler&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;RequireRoles&lt;/code&gt; specifes 1..N roles that are required to access this handler&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;DenyRoles&lt;/code&gt; specifies 1..N roles that aren't permitted to access this handler&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;AllowVerbs&lt;/code&gt; restricts the HTTP Verbs that can be used to access this handler (PUT, POST, GET etc.)&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;WithCustomRule&lt;/code&gt; (not listed) allows you to specify a function that can be used to apply a custom rule to each request&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Each method returns the configuration object so it is possible to chain the calls to create more complex rules.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;Security.Configure(c =&amp;gt;
    c.For&amp;lt;EditHandler&amp;gt;()
        .DenyAnonymousAccess()
        .RequireRoles("Author")
        .DenyRoles("Reader")
        .AllowVerbs(Security.AllowedVerbs.POST);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Obviously some combinations will not make sense, in fact AllowedVerbs needs to be tweaked as it may only apply to certain roles (so the combinations don't make sense right now). Currently FluentSecurity doesn't care and it probably never will if you try and create illogical combinations.&lt;/p&gt;&lt;h3&gt;bool Security.Test(RequestContext req, HandlerData d)&lt;/h3&gt;&lt;p&gt;This will run all pre-configured rules against the current request to determine if the request is authorised to continue. It will return a true/false result depengin on whether the rules pass or not.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;bool granted = Security.Test(context, data);
&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;IResult Security.Validate(RequestContext req, HandlerData d)&lt;/h3&gt;&lt;p&gt;Implements the typical SecurityFilter use case. This function will call the &lt;code&gt;Test&lt;/code&gt; method and returns &lt;code&gt;Result.None()&lt;/code&gt; if the test passes (may be configurable in the future) otherwise it executes the &lt;code&gt;Security.OnAccessDenied&lt;/code&gt; function and returns the &lt;code&gt;IResult&lt;/code&gt;. If no &lt;code&gt;OnAccessDenied&lt;/code&gt; is defined &lt;code&gt;Result.None()&lt;/code&gt; will be returned.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;IResult result = Security.Validate(context, data);
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is simply shorthand for now but may be expanded with some custom logic in the near future.&lt;/p&gt;&lt;h3&gt;Security.OnAccessDenied&lt;/h3&gt;&lt;p&gt;This property is used to define a function that can be used to return a result should access been denied. This is done lazily as it allows you access to the &lt;code&gt;RequestContext&lt;/code&gt; and &lt;code&gt;HandlerData&lt;/code&gt; so you could implement per-request handling of the Result (redirects or return URLs etc.)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;Security.OnAccessDenied = (c, d) =&amp;gt; Result.Redirect&amp;lt;AccessDeniedHandler&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;SecurityFilter --&lt;/p&gt;&lt;p&gt;The &lt;code&gt;SecurityFilter&lt;/code&gt; class is simply a Tinyweb filter that performs the request validation for authentication. Nothing special here move along now.&lt;/p&gt;&lt;h1&gt;For Now and Up Next&lt;/h1&gt;&lt;p&gt;So thats all there is for now. Code is, as I've said, available at the &lt;a href="https://bitbucket.org/kouphax/tinyweb-fluentsecurity"&gt;BitBucket repository&lt;/a&gt;. I'm starting to build up some docs and stuff now and maybe tweak and add useful features. If there is anything you would like to see raise it as an &lt;a href="https://bitbucket.org/kouphax/tinyweb-fluentsecurity/issues?status=new&amp;amp;status=open"&gt;issue&lt;/a&gt; and I'll get around to it.&lt;/p&gt;</content></entry><entry><title>Personal Systems of (Dis)Organisation</title><updated>Tue Sep 20 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/09/20/personal-systems-of-dis-organisation/"></link><id>urn:yobriefca-se:feed:post:Personal Systems of (Dis)Organisation</id><content type="html">&lt;p&gt;From the outside people probably regard me as a typically chaotic person. My desk is a mess, I'm not mad keen on the whole shaving thing, my writing is terrible and I am generally not where people expect me to be at any particular time. But there is, more often than not, a workflow that works for me hidden deep down in the chaos. Most people stick to one or two way to organise themselves (Todo Lists, pen and paper, iPad etc.). Not me. I'm still trying to find some sort of process that fits well with me but I'm starting to think that perhaps that actually is my process. I move from different mediums and tools quite a lot. Doing this isn't always easy given some tools just natrually try and lock you in. My current workflow involves a few tools,&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Pen and paper (Work only). Nothing beats its it immediate and short term loggin of free form information. I have strict rules around it though.&lt;/li&gt;
  &lt;li&gt;Blank paper - no lines or binder holes or headers. I guess I subconciouslly view this a restriction perhaps?&lt;/li&gt;
  &lt;li&gt;Black pen (with a bit of red). I've totally gone off blue pen - it makes my notes look messy (they are though). Pencil has been overtaken by the black ink pen these days though I am not sure why&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.wunderlist.com/"&gt;Wunderlist&lt;/a&gt;. List management. Cloud syncing. Free. I moved from &lt;a href="http://culturedcode.com/things/"&gt;Things.app&lt;/a&gt; when I realised they just don't seem to listen to their customers. Cloud synching is essential. Wunderlist offers me a cross platform and web based solution for doing everything I need quickly. I use this for medium-longer term "listable" tasks such as,&lt;/li&gt;
  &lt;li&gt;Project Deliverables&lt;/li&gt;
  &lt;li&gt;General day to day work tasks (though not shopping lists - see later)&lt;/li&gt;
  &lt;li&gt;Things to blog about&lt;/li&gt;
  &lt;li&gt;Wish lists for presentas and books and things (shared with family who also do the same thing)&lt;/li&gt;
  &lt;li&gt;iOS 5's Reminders app. Yeah ANOTHER list manager. This one is for very short time lists such as shopping lists. I find polluting Wunderlist with this sort of list very annoying. I don't use it that often but it's the best app I find for ultra fast entry of lists.&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://trello.com/"&gt;Trello&lt;/a&gt;. The latest addition to my supposed system. It's essentially a multi-level list system. Kind of like a cross between a Kanban board and a Todo list application. I am currently using this for more and more things. Essentially higher level list management that makes more sense to have a pseudo workflow or a board to collect realted lists and collaborate with others. The key to this one is everything becomes more collaborative but it's much less restictive than something like AgileZen.&lt;/li&gt;
  &lt;li&gt;Books (To Read, Reading, Finished)&lt;/li&gt;
  &lt;li&gt;Hackathon Voting (Ideas, Timings, Dates, Objectives etc). Allows a bunch of people to vote and discuss relevant things for the next hackathon. We also use checklists for things we need to prepare in advance etc.&lt;/li&gt;
  &lt;li&gt;House renovations (one list per room). Shared between my wife and myself.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://agilezen.com/"&gt;AgileZen&lt;/a&gt;. Amazing site for Kanban and Scrum. Essentially a Kanban board with enough flexbility/features that means we can tailor the workflow to suit the needs of the project and team. I use this for work projects where feedback on times and bottlenecks is essential. Can't say enough great things about this.&lt;/li&gt;
  &lt;li&gt;Whiteboard - Ultra transient information. I use this during real time &lt;strong&gt;brain thinking&lt;/strong&gt; sessions. An old friend remareked that the process of getting out of your chair and picking up a pen changes the way you think and creates a more open environment for ideas. Generally items on the whiteboard last a couple of minutes. Anything important can be photographed and sent to Evernote or email. I do my best work at a whiteboard. If I am blanking I get up and start doodling - inspiration eventually comes. Currently there is a picture of Silos (pronounced Sigh Loss) - The Evil Data Overlord - makes sense to me. &lt;div style="text-align:center"&gt;&lt;img src="/images/blog/silos.jpg" /&gt;&lt;/div&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="https://www.evernote.com/"&gt;Evernote&lt;/a&gt;. Online storage of notes useful for indexing and searching. To be honest - more information goes into this than comes out but it has saved my neck a few times with information I thought irrelevant. I moved from OneNote because there was no suitable OneNote client for the devices I use. Oh and I own a Mac.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://www.tripit.com/"&gt;TripIt&lt;/a&gt;. I'm travelling a bit more these days and it is always nice to have all the necessary information in one place that actually assists planning by proving maps and schedules. Assistive metasites are the future!&lt;/li&gt;
  &lt;li&gt;Fridge (one of those cold things in your kitchen) - My materialised Kanban board at home that I use for "hobby" activities when I am off work to let my wife know what I am up to and plan accordingly.&lt;/li&gt;
  &lt;li&gt;Calendar - Outlook synched to Google and available on all devices. I don't use this directly but other people do and I follow the instructions people send to me (if I think it's worth my time).&lt;/li&gt;
  &lt;li&gt;Email - Most disorganised but oft used medium for retrieving information (usually once and discarded or archived to never be accessed again)&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;So you see I have got a lot of stuff in a lot of places but luckily important information is exactly where I need it. It may not seem sane or organised but for some reason it is to me - or maybe this is very very normal?&lt;/p&gt;</content></entry><entry><title>Talking at DDD North and Belfast</title><updated>Mon Sep 19 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/09/19/talking-at-ddd-north-and-belfast/"></link><id>urn:yobriefca-se:feed:post:Talking at DDD North and Belfast</id><content type="html">&lt;p&gt;The start of October is going to be a busy time for me - in a good way. The first weekend I'll be doing two talks at &lt;a href="http://developerdeveloperdeveloper.com/dddie11/Schedule.aspx"&gt;DeveloperDeveloperDeveloper in Belfast&lt;/a&gt; and the following week I'll be skipping over to Sunderland (first time there) do do another talk for &lt;a href="http://developerdeveloperdeveloper.com/north/Schedule.aspx"&gt;DeveloperDeveloperDeveloper North&lt;/a&gt;. I've been lucky enough to land the same presentation for both DDD Belfast and DD North which means I have slightly less to prepare. Not that it will be a drop in quality in anyway - no siree.&lt;/p&gt;&lt;p&gt;For the interested folk the two talks are outlined below. The titles and synopsis are fairly childish/over-sensationalised (delete as you see fit) but thats the way I roll. My opinion enjoys being passed around and so my talks will feature a heavy dose of it - with all necessary disclaimers attached. &lt;/p&gt;&lt;p&gt;For both events I'll be covering,&lt;/p&gt;
&lt;blockquote&gt;&lt;h1&gt;refORM: Death to ORMs in .NET&lt;/h1&gt;&lt;p&gt;Data Access is a difficult area to master. There are plenty of frameworks such as Entity Framework, NHibernate, i/myBatis. These frameworks attempt to make data access simpler but while these products are widely used they can lead to leaky abstractions forcing us to work around constraints of the technology or introduce the nightmare of trying to figure out what is happening deep down in the frameworks internals.&lt;/p&gt;&lt;p&gt;In this session we will look at the various lightweight alternatives and the advantages they offer as well as provide rationale about why the more heavyweight approach isn't always the best approach.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;And for DDD Belfast only,&lt;/p&gt;
&lt;blockquote&gt;&lt;h1&gt;CoffeeScript the Awesome&lt;/h1&gt;&lt;p&gt;CoffeeScript - "a little language that compiles into JavaScript" has been making a lot of noise in the web community with its inclusion in Ruby on Rail v3 and its swift rise in popularity on Github.&lt;/p&gt;&lt;p&gt;This session will look at the benefits this language brings over the native JavaScript alternative, the syntactical double rainbow it creates, the problems you'll face and of course how you can actually use it in different technology stacks such as Ruby, node.js, .NET and Java.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;I've never really undertaken sessions of this size before (mostly internal or for a customer) so it's going to be a cracking experience. Hopefully people get as much out of these sessions as I will. I'll have whatever resources up after the events as well.&lt;/p&gt;&lt;p&gt;See you there (maybe).&lt;/p&gt;</content></entry><entry><title>Hackathon #2: Mobile</title><updated>Wed Sep 14 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/09/14/hackathon-number-2-mobile/"></link><id>urn:yobriefca-se:feed:post:Hackathon #2: Mobile</id><content type="html">&lt;p&gt;So as I've already said in a &lt;a href="/blog/2011/08/29/hackathon-retrospective/"&gt;previous post&lt;/a&gt; our first hackathon was well recieved. So why not, we thought, do another one? And indeed why not?&lt;/p&gt;&lt;p&gt;This time we are changing things up. Rather than everyone coming together to bang out a solution we are going to have small teams working on the same project on different mobile platforms. With musical chairs. Yeah you heard right right. Musical Chairs. Kind of.&lt;/p&gt;&lt;p&gt;Heres the deal&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;A number of stations are set up in a room.&lt;/li&gt;
  &lt;li&gt;Each station is set up to host a particular development platform (iOS Native, Android Native, PhoneGap, Web, Windows Phone 7 etc.).&lt;/li&gt;
  &lt;li&gt;Each station will have 2 x Developers assigned to them for pairing and 1 X Technology Expert that will act as an advisory role for that stations technology platform&lt;/li&gt;
  &lt;li&gt;There will be 1 product owner and 1 idea.&lt;/li&gt;
  &lt;li&gt;Each station will produce the solution in the best way possible for that platform&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Now here's the catch. &lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Development will be done in sprints lasting 1 hour. At the end of the sprint the 2 devs at each station get up and move to the next station giving a 5 minute handover to the next team. This keeps happneing until the Hackathon (24hrs this time) is over.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Thats the musical chairs bit. But why? Well this allows people to get some exposure to each platform, see the relevant pain points and above all else learn to work as a team rather than being a bunch of selfish pricks (#joke). Sure the first full cycle will probably be painful for most people but like women who live in the same house - after the first cycle everyone should be roughly in sync (#jokeagain).&lt;/p&gt;&lt;p&gt;It's a grand idea but it's easily broken. We need to prevent that happening. Here's how I think we can do that.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;People&lt;/strong&gt;. People are key to this one. More specifically numbers. We need 3 people per station and 1 product owner. If people drop out we have to start cutting stations, if numbers aren't right some teams will be too big/small and that wont work. We got 12 people last time which dropped to about 6 after 24 hours because some people just couldn't hack the pace. this time people need to commit and stay commited or we fail and that would suck!&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Planning&lt;/strong&gt;. Everything needs to be ready for this one. It's got a mobile focus and that should remain the focus. This means all the backend services need to be available and working upfront. This means user stories need to be defined beforehand and even a few broken down into tasks so devs can get cracking immediatley while the rest are fleshed out. This means stations needs to be up and running with everything installed and working. So lots to do.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Server/Services&lt;/strong&gt;. As I've said these need to be in place and rather than roll our own we are looking at the potentially awesome &lt;a href="https://www.parse.com/"&gt;Parse&lt;/a&gt; as it will have everything we need for all platforms thanks to the minimum of a RESTful API. Still under review but it's looking good so far&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Idea&lt;/strong&gt;. We need an idea that is sufficiently small but also feature rich enough to make the implementations different (device integration etc.). We have some but we need to narrow this down to something that would be beneficial.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Infrastructure&lt;/strong&gt;. All the infrastructure needs to be in place. All dev software installed, source control ready, &lt;a href="http://agilezen.com/"&gt;AgileZen&lt;/a&gt; subscriptions bought, test devices ready blah blah blah&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I'm sure there's more - I mean it's early days.&lt;/p&gt;&lt;p&gt;So that is where we are right now, lots to think about and lots to do. We are aiming for a November/December slot if we can get the people and a weekend they are all free. Excited++.&lt;/p&gt;</content></entry><entry><title>Windows 8</title><updated>Tue Sep 13 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/09/13/windows-8/"></link><id>urn:yobriefca-se:feed:post:Windows 8</id><content type="html">&lt;p&gt;Where WebForms was an attempt to bring the desktop paradigm to web development Windows 8 attempts to do the exact opposite by bringing web to the desktop. More so in fact Windows 8 appears to be effectivley deprecating the desktop idea over time. It's a bold move but trying to cater to every platform is surely a recipe for disaster but you know what I can't help but like it. &lt;/p&gt;&lt;p&gt;I'm not convinced by the Vista v2 arguments either but then I do most of my "poweruser" stuff via a command prompt and thats not going to change - when I hit the desktop I am looking for a nice experience and I feel, so far, I get that with Windows 8.&lt;/p&gt;&lt;p&gt;Being an idiot I managed to grab the ISO without VS11/Blend 5 on it so I am waiting for that one to download before sharing opinions on them (Blend 5 surely must be just like Dreamweaver but I stand to be proven wrong). I am told, however, the first options on the New Project dialog in VS11 are JavaScript projects - I like that. :-)&lt;/p&gt;</content></entry><entry><title>Tinkering with Agility: Honorable Mentions</title><updated>Thu Sep 01 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/09/01/tinkering-with-agility-honorable-mentions/"></link><id>urn:yobriefca-se:feed:post:Tinkering with Agility: Honorable Mentions</id><content type="html">&lt;p&gt;In the process of &lt;a href="/blog/2011/09/01/tinkering-with-agility/"&gt;researching Agility&lt;/a&gt; I needed to throw together a quick serverside stack to accept the RESTful calls from Agility. I went with my current framework de jour &lt;a href="https://github.com/martinrue/Tinyweb"&gt;Tinyweb&lt;/a&gt; and I must admit it was a great call. For more basic info on Tinyweb check out my &lt;a href="/blog/2011/07/18/micro-web-frameworks-in-net-101-tinyweb/"&gt;blog post&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Agility by default routes the RESTful requests to &lt;code&gt;api/model/{id}&lt;/code&gt; and Tinyweb let me create a handler associated with this sort of url by way of areas.&lt;/p&gt;&lt;p&gt;Areas --&lt;/p&gt;&lt;p&gt;Tinyweb has a feature I wasn;t aware of before. It is possible to register a handlers namespace as an "area" (if you have experience with ASP.NET MVC you'll know what that means).&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;Tinyweb.Areas.Add("Agility.Tinkerings.Web.Handlers.Api", "api");
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This means that even though my handler is call &lt;code&gt;TodoHandler&lt;/code&gt; the url would map to the one above without having to sacrifice naming style of the handler or force us to create a custom route variable in each handler that had to map to this area. Flippin clever stuff&lt;/p&gt;&lt;p&gt;WebActivator&lt;/p&gt;&lt;p&gt;Another excellent Nuget package is the &lt;a href="http://nuget.org/List/Packages/WebActivator"&gt;WebActivator&lt;/a&gt; and I have been charmed by it's slickness recently. WebActivator allows you to attribute a class/namespace and specify what code should be run when the application starts. So rather than having to put in a load of lines for configuring different aspects of your stack into &lt;code&gt;Global.asax.cs&lt;/code&gt; you can break them into distinct classes and avoid breaking &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt;SRP&lt;/a&gt; more than you have too. Here is the example I used in the Agility.js source&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;[assembly: WebActivator.PostApplicationStartMethod(typeof(TinywebActivator), "Activate")]
namespace Agility.Tinkerings.Web.Activators
{
    public class TinywebActivator
    {
        public static void Activate()
        {
            Tinyweb.Areas.Add("Agility.Tinkerings.Web.Handlers.Api", "api");
            Tinyweb.Init();
        }
    }
} 
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I don't even have a &lt;code&gt;Global.asax&lt;/code&gt; defined at all.&lt;/p&gt;&lt;p&gt;Feel free to poke around the source to get a better picture of how I threw the backend together. Thanks go to &lt;a href="https://github.com/martinrue/Tinyweb"&gt;Tinyweb&lt;/a&gt; and &lt;a href="http://twitter.com/#!/martinrue"&gt;@martinrue&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>Tinkering with Agility</title><updated>Wed Aug 31 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/08/31/tinkering-with-agility/"></link><id>urn:yobriefca-se:feed:post:Tinkering with Agility</id><content type="html">&lt;p&gt;Having spent a few days tinkering with &lt;a href="http://agilityjs.com/"&gt;agility.js&lt;/a&gt; I have come to the following conclusions&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;It's a bit buggy in places (it is currently on v0.1 so this is to be expected)&lt;/li&gt;
  &lt;li&gt;At first glance code written against &lt;a href="http://agilityjs.com/"&gt;agility.js&lt;/a&gt; looks like a total mess though it appears to be a weird optical illusion because once you grasp the &lt;a href="http://agilityjs.com/"&gt;agility.js&lt;/a&gt; approach the code makes total sense&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://agilityjs.com/"&gt;Agility&lt;/a&gt; would fit well into an existing codebase as much as into a new one. Certainly more so that some of the other frameworks. And finally,&lt;/li&gt;
  &lt;li&gt;I like it&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;You could almost class &lt;a href="http://agilityjs.com/"&gt;agility.js&lt;/a&gt; as a micro MVC framework (and we all know how I like them). I say that because it takes an incredibly simple approach to MVC as well as offering a very small but (IMHO) complete feature set. Unlike &lt;a href="http://knockoutjs.com/"&gt;knockout&lt;/a&gt;, which also makes use of the &lt;code&gt;data-&lt;/code&gt; attributes for model binding (and more), &lt;a href="http://agilityjs.com/"&gt;agility.js&lt;/a&gt; uses the &lt;code&gt;data-bind&lt;/code&gt; attribute for model binding and thats it. &lt;/p&gt;&lt;p&gt;With Agility you have that ability to compose UI's from discrete little self-contained, and/or nested agility objects that behave as standalone MVC implementations. This is &lt;em&gt;kind of&lt;/em&gt; different to the standard appraoch of other MVC-like JS frameworks which generally go for the create a single large MVC architecture and manage components within that architecture. I am not saying this appraoch is forbidden or impossible in the other frameworks - it's just not how a noob would go about things. At least I never did when I first started looking at them.&lt;/p&gt;&lt;p&gt;To play with agility I went ahead and created "yet another Todo app" (&lt;a href="https://bitbucket.org/kouphax/agility-tinkering"&gt;source&lt;/a&gt;) complete with server side persistence and some responsive UI elements (Complete button will vanish when a Todo is completed - wow). I'll not break the entire thing down here but here are some choice cuts (in CoffeeScript cause I am THAT cool)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;todo = $$ 
  model: {}, 
  view:
    format: '''
      &amp;lt;li&amp;gt;
        &amp;lt;span data-bind="description"/&amp;gt;
        &amp;lt;button class="complete-task"&amp;gt;Complete&amp;lt;/button&amp;gt;
        &amp;lt;button class="delete-task"&amp;gt;Delete&amp;lt;/button&amp;gt;
      &amp;lt;/li&amp;gt;
    ''',
  controller: 
    'create': -&amp;gt; 
      @view.$().toggleClass "task-done", @model.get("complete")
    'change:complete': -&amp;gt; 
      @view.$().addClass "task-done"
    'click .complete-task': -&amp;gt;  
      @model.set "complete": true
      @persist $$.adapter.restful, collection:'todos'
      @save()
    'click .delete-task': -&amp;gt;  
      @persist $$.adapter.restful, collection:'todos'
      @erase()
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The code above shows the creation of a Todo entity i.e. a single entry in the list. &lt;code&gt;$$&lt;/code&gt; is the Agility factory method and will create an MVC based entity out of the object passed in. I have decalred a view, a model (albeit empty) and a controller with an assortment of tasks for the entity.&lt;/p&gt;&lt;p&gt;This will give you two-way model binding, event wiring up and even custom events totally for free plus a very simple way to react to events. The controller is essentially the event handler so things like &lt;code&gt;click .delete-task&lt;/code&gt; will bind the handler to the click event of the element with the &lt;code&gt;delete-task&lt;/code&gt; class (the typical full range of selectors are available thanks to jQuery, Zepto integration forthcoming too). Events like &lt;code&gt;create&lt;/code&gt; and &lt;code&gt;change:complete&lt;/code&gt; are custom events given to us by agility and allow us to listen to key points in the lifecycle of the object. In fact &lt;code&gt;change:complete&lt;/code&gt; is a demonstration of something truly awesome (IMHO as usual). The &lt;code&gt;change&lt;/code&gt; event will fire when model properties are changed - the &lt;code&gt;change:&amp;lt;property_name&amp;gt;&lt;/code&gt; will only fire when the &lt;code&gt;complete&lt;/code&gt; property changes. Noice!&lt;/p&gt;&lt;p&gt;My Todo list object then creates instances of the &lt;code&gt;todo&lt;/code&gt; object above after pulling the info from the server. It uses agility's inheritance strategy to to create them from the &lt;code&gt;todo&lt;/code&gt; prototype I pass in&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;todos = $$
  model: {},
  view: 
    format: '''
      &amp;lt;div&amp;gt;
        &amp;lt;input type="text" id="newItem" /&amp;gt;
        &amp;lt;button class="add-task"&amp;gt;Add&amp;lt;/button&amp;gt;
        &amp;lt;ol/&amp;gt;
      &amp;lt;/div&amp;gt;
    '''
  controller:
    'click .add-task': -&amp;gt; 
      item = @view.$('#newItem').val()
      if item isnt ""            
        newTodo = $$ todo, description: item
        newTodo.persist $$.adapter.restful, collection:'todos'
        newTodo.save()
        @empty()
        @gather todo, 'append', 'ol'
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next I append the &lt;code&gt;todos&lt;/code&gt; object to the document (which renders it and wires things up)&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;$$.document.append todos
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Persistance is provided as a plugin of sorts with ability to provide your own adapters. You get a RESTful ajax adapter with Agility. I use this to provide persistence methods on my objects and let agility set that up.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;todo.persist $$.adapter.restful, collection:'todos'
todos.persist()
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;There does seem to be a bug with the factory method and persitance enabled object forcing me to call &lt;code&gt;persist&lt;/code&gt; prior to any sort of persitence operation but hopefully we will see that resolved soon.&lt;/p&gt;&lt;p&gt;Finally I fetch from the server and wipe the awesome sauce from my lips and beard.&lt;/p&gt;
&lt;pre class="highlight"&gt;&lt;code&gt;todos.gather todo, 'append', 'ol'
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I've went on more than I wanted to but hey it's worth it. Hopefully this has helped clarify agility to a few people or at least got people intrigued. Feel free to pull down the &lt;a href="https://bitbucket.org/kouphax/agility-tinkering"&gt;source&lt;/a&gt; and have a go yourself otherwise pop on over to the &lt;a href="http://agilityjs.com/"&gt;Agility.js&lt;/a&gt; site and read the excellent docs.&lt;/p&gt;</content></entry><entry><title>Hackathon Retrospective</title><updated>Sun Aug 28 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/08/28/hackathon-retrospective/"></link><id>urn:yobriefca-se:feed:post:Hackathon Retrospective</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;So my first ever Hackathon (48hr) is done and dusted and has been for over a week (lazy!!). All in all it was a great experience and plenty of stuff was learnt - we got some technology evaluation (good and bad), ways we could streamline the process and things that burnt time way too fast.&lt;/p&gt;&lt;p&gt;This event was intended as a kind of dry run to see if there is any benefit in Kainos running more events of this nature. Perhaps external ones for charity or educational endeavor. Does that mean I'll be doing it again? Hope so. I guess the best question to ask then is what did I want to get out of it. Well I went in a bit open minded - mainly I wanted to have the experience but I also wanted to try and get some of the techs I have been tinkering with and see how they faired in a semi-real life environment. I think I actually wanted to have produced something really usable too but perhaps I was expecting too much? I mean what we produced wasn't a total failure but it needs a lot of work.&lt;/p&gt;&lt;h1&gt;The End Result&lt;/h1&gt;&lt;p&gt;We ended up producing a web app for giving continuous feedback to colleagues. The idea was to allow people to give and ask for feedback as well as facilitating a more formal career review workflow. Social appraisals as it were.&lt;/p&gt;&lt;p&gt;&lt;img src="/images/appraisr.png" alt="" style="width:100%;" /&gt;&lt;/p&gt;&lt;p&gt;We had some basic features such as integrated Windows Authentication and user information look up etc, autocomplete on peoples name, pretty relative time output and some commenting features. So yeah it's pretty sparse but hey it's a start. Whats more interesting is the process and things we observed.&lt;/p&gt;&lt;h1&gt;The Story&lt;/h1&gt;&lt;p&gt;The team was a mix of Technical Architects, placement students, software and senior software engineers and even a project manager we had a few no shows and a few people who intended to drop in for a while and leave again. We also had a person who thought going out the night before was a good idea - it wasn't (hahaha sucker). But hey at least he showed up. I have no problem with people not turning up as long as they let someone know but not turning up and not telling me…. bad form folks.&lt;/p&gt;&lt;p&gt;Anyway the majority of us gathered in our office at 9ish on Saturday morning and started pitching ideas to attempt. We ended up with a decent pile of good ideas and started aggressively throwing ideas in the bin until, after some debate, we ended up with the one we decided to run with. After that we started thrashing out user stories as a group and ended up with a decent amount. The whole process, including getting food, took about 3 or 4 hours. We then all got stuck into various tasks, I started getting the build server functioning, a few people sat with the product owner and started fleshing out the user stories, a few people looked at the domain model and some people started writing acceptance tests. This was the bit that cost us a lot of time. Rather than each person or group working on vertical slice through the system we went horizontally and started arguing over things that never even made it into the end solution. Everyone fell into their comfort zone cause thats the way Kainos projects typically run - old school waterfall for the most part. So yeah no &lt;span class="caps"&gt;REAL&lt;/span&gt; work actually got done, people were just keen to start and I didn't really pull it under control until it was just too late . It would have been nice to have a Scrum Master type person who was overseeing everything too. I was too busy doing my own horizontal slice :-S.&lt;/p&gt;&lt;p&gt;So things went on like this for a while, a lot of "getting things in place" and some people had to drop off. Eventually we hit a point when we realised that our rate of work was nowhere near what it should be and something needed to change. At about this point everyone but me decided to get some shut eye and I slipped my headphones in, played some Bon Iver and got stuck into fleshing out what we already had. This formed the basis of the work the next day (this was about 4AM on Sunday morning) when the remaining team (5 left out of the initial 12) kicked of the final leg. Due to our earlier stumble there wasn't very much for our product owner/tester to do for most of the day which was a shame. Eventually we ended up with what you seen above (finishing up at about 6PM on Sunday night). The Sunday was less frantic, lots of pairing, lots of trying new technologies and was so much more productive&lt;/p&gt;&lt;h1&gt;The Technologies&lt;/h1&gt;&lt;p&gt;We used quite an interesting array of technologies&lt;/p&gt;&lt;ul&gt; &lt;li&gt;&lt;span class="caps"&gt;ASP&lt;/span&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt; &lt;span class="caps"&gt;MVC&lt;/span&gt; 3&lt;/li&gt; &lt;li&gt;&lt;span class="caps"&gt;SQL&lt;/span&gt; Server Compact 4 (due to issues with &lt;span class="caps"&gt;SQL&lt;/span&gt; Server on the last leg)&lt;/li&gt; &lt;li&gt;&lt;a href="http://www.toptensoftware.com/petapoco/"&gt;PetaPoco&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://jashkenas.github.com/coffee-script/"&gt;CoffeeScript&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://backbonejs.org/"&gt;Backbone.js&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://jqueryui.com/"&gt;jQueryUI&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="https://github.com/dradovic/MigSharp"&gt;Mig#&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="https://github.com/JamesKovacs/psake"&gt;Psake&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://www.nunit.org/"&gt;NUnit&lt;/a&gt; which I have discovered is a massive impediment to writing tests after the silky smooth experience of &lt;a href="http://www.nspec.org/"&gt;NSpec&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://should.codeplex.com/"&gt;Should.Fluent&lt;/a&gt;&lt;/li&gt; &lt;li&gt;Git&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In terms of supporting technologies we used the awesome AgileZen for our Kanban board and HipChat for some shared chatting (we had Lync but it's inability to persist conversations is a deal breaker in my book). Finally, we used TeamCity as our build and continuous integration server which was a much much better experience than CC.&lt;span class="caps"&gt;NET&lt;/span&gt;&lt;/p&gt;&lt;h1&gt;Hackathon Review&lt;/h1&gt;&lt;p&gt;I guess my story above probably sounds more negative than it really was cause thats the way I write. It wasn't - it was great craic. We held a review post-hackathon with everyone involved and the following point were raised.&lt;/p&gt;&lt;h2&gt;Likes&lt;/h2&gt;&lt;ol&gt; &lt;li&gt;Experience with new technologies e.g. CoffeeScript, PetaPoco&lt;/li&gt; &lt;li&gt;Experience with things such as user stories, kanban etc.&lt;/li&gt; &lt;li&gt;People got to do things that they just don?t get the chance to do. E.g. Andrew was designing screens.&lt;/li&gt; &lt;li&gt;Pair programming&lt;/li&gt; &lt;li&gt;Great craic!&lt;/li&gt;&lt;/ol&gt;&lt;h2&gt;Dislikes&lt;/h2&gt;&lt;ol&gt; &lt;li&gt;People didn't know what to do when there were bottlenecks&lt;/li&gt; &lt;li&gt;Lunches and dinners were taken at different times. This affected communication.&lt;/li&gt; &lt;li&gt;People thought they weren't productive with no/little sleep.&lt;/li&gt; &lt;li&gt;Tester/Product Owner wasn't used as much as they could've been on the second day.&lt;/li&gt; &lt;li&gt;Facilities room has a motion sensor on the lights&lt;/li&gt; &lt;li&gt;A yoga mat is not good for sleeping on&lt;/li&gt;&lt;/ol&gt;&lt;h2&gt;Improvements&lt;/h2&gt;&lt;ol&gt; &lt;li&gt;Add an equivalent of a Scrum master to the team.&lt;/li&gt; &lt;li&gt;Agree an idea prior to the hackathon so it doesn't eat into dev time.&lt;/li&gt; &lt;li&gt;Create a couple of use cases going into the hackathon so the dev team don't have to wait on the business people&lt;/li&gt; &lt;li&gt;Set up infrastructure prior to the hackathon&lt;/li&gt; &lt;li&gt;Choose a few technologies before hand so that people can learn a bit about them before. e.g. the software for source control can be decided in advance.&lt;/li&gt; &lt;li&gt;Make sure people aren't going to the shops/getting food at different times&lt;/li&gt; &lt;li&gt;Clone James for tech leader/troubleshooter/developer&lt;/li&gt; &lt;li&gt;Have &lt;strong&gt;less&lt;/strong&gt; team work. We found that the entire team was focused on some tasks that didn't require it because, well, teamwork is great.&lt;/li&gt; &lt;li&gt;Focus on core functionality instead of e.g. logins which can be done later. Or prep login functionality before.&lt;/li&gt; &lt;li&gt;Use hackathon specific machines&lt;/li&gt; &lt;li&gt;If there's another weekend hackathon, people should go get some sleep to rest eyes and minds!&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;So all in all it was a great learning experience and I don't think anyone said "I'm never doing that again" which was a result and I am certain we will attempt something else in a similar vein and take on board all those improvements to produce something that changes the world or something. So many things to try…. what you all doing next weekend :-P&lt;/p&gt;</content></entry><entry><title>Life (in IT) Lesson #5372: Learning To Say No, Keep Having Fun</title><updated>Tue Aug 16 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/08/16/life-in-it-lesson-number-5372-learning-to-say-no-keep-having-fun/"></link><id>urn:yobriefca-se:feed:post:Life (in IT) Lesson #5372: Learning To Say No, Keep Having Fun</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;In case you haven't noticed I didn't blog for nearly a whole month. Thats not like me. Sure, I have my troughs (and peaks) but in terms of my overall social activity I may as well have been mostly dead. My lame excuse? I've been busy. Yeah just like everybody else I've been working more than usual - but I've been taught a valuable lesson. &lt;strong&gt;Fun&lt;/strong&gt; is key.&lt;/p&gt;&lt;p&gt;Over the last month or so I've been gradually building up a collection of in-progress projects - just like Pokemon. I was taking new ones on even when it felt risky and assuring people I could get the work done. My hubris cost me, and others. I spread myself too thin, I left no time for contingency and eventually I ended up, and this is really the best term for it so excuse the language, "fucking over" one of the projects at the worst possible time I can imagine. I've always prided myself on the fact I can always stay on top of an extremely large amount of work and satisfy all the stakeholders. I guess now I know my limit and I even have an indicator well before this threshold is met that I need to start waving a red flag - when it stops being &lt;strong&gt;fun&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;I enjoy the vast majority of my work, but obviously there are some aspects that less than &lt;strong&gt;fun&lt;/strong&gt; (writing documents, estimating etc.)&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;. What started happening was I was doing more &lt;strong&gt;non-fun&lt;/strong&gt; work that &lt;strong&gt;fun&lt;/strong&gt; work (in fact I was doing very little &lt;strong&gt;fun&lt;/strong&gt; stuff at all). This made me less productive, my heart wasn't fully in it and so I wasn't 100% focused. I was less able to predict and plan accordingly and as things started happening on one project (on-site customer meetings etc.) another project had to take the hit and eventually one had to give. The &lt;strong&gt;fun&lt;/strong&gt; factor also meant I was taking on extra work in the vain hope that it might be more &lt;strong&gt;fun&lt;/strong&gt;, which upon reflection, is pretty damn stupid. Oh and lets not forget the fact I was holding off talking to people because of some insane belief that there was a good chance that things would look much better in "another hour or so". Seldom was this the case of course so it mean't that I wasn't raising flags early enough and people felt I was being aloof.&lt;/p&gt;&lt;p&gt;So what will I be doing in future? Well I now have a rule and I will see how it goes. Simply put,&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;I will only take on more work if I am currently having &lt;strong&gt;fun&lt;/strong&gt; and the new work will not push me into the &lt;strong&gt;not fun&lt;/strong&gt; work spectrum. If I am &lt;strong&gt;not having fun&lt;/strong&gt; I can only take on more work when I have cleared some of my work and am back having &lt;strong&gt;fun&lt;/strong&gt;. The closer I get to the &lt;strong&gt;not fun&lt;/strong&gt; end of the spectrum the sooner and more frequent communication with stakeholders needs to be.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Simple enough but it seems to hold up so far.&lt;/p&gt;&lt;ul&gt; &lt;li&gt;Give me too much &lt;strong&gt;fun&lt;/strong&gt; work and it stops being fun because there is too much pressure.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt; &lt;li&gt;Give me a single &lt;strong&gt;not fun&lt;/strong&gt; job that isn't fun and I will only accept it provided I can work on my own projects to re-address the &lt;strong&gt;fun&lt;/strong&gt; ratio (blogging about experiences, related technologies etc.)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This probably sounds quite selfish and, well, of course it is. Nobody should expect me to just do whatever they say&lt;sup class="footnote" id="fnr2"&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt; without at least making the deal appealing to me or at least allowing me a certain amount of free time to draw my own benefits from the situation. For example I accept a certain amount of &lt;strong&gt;not fun&lt;/strong&gt; work because I am permitted to do a lot of my own &lt;strong&gt;fun&lt;/strong&gt; work to address the balance.&lt;/p&gt;&lt;p&gt;I think I've left this open enough for personal interpretation and flexibility but strict enough that there is a foundation here to aid in making decisions. If I had applied this rule previously I wouldn't have taken the extra work (even though it seemed appealing at the time - it was &lt;strong&gt;fun&lt;/strong&gt; and I got paid overtime for it) and both myself and the project would be in better shape. But hey how can you ever learn if you never make mistakes?&lt;/p&gt;&lt;p&gt;Is the word &lt;strong&gt;fun&lt;/strong&gt; starting to sound really weird to anyone else?&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; I realise some people &lt;span class="caps"&gt;LOVE&lt;/span&gt; writing documents and estimating - I don't. They are just a necessary part of the job that I live with.&lt;/p&gt;&lt;p class="footnote" id="fn2"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; There is only one person that &lt;span class="caps"&gt;CAN&lt;/span&gt; - my wife and thats like the law. The company I work for is great at trying to keep their workforce happy and I am in no way implying that they forced work on me. They didn't.&lt;/p&gt;</content></entry><entry><title>An Opinionated Project Structure</title><updated>Mon Aug 15 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/08/15/an-opinionated-project-structure/"></link><id>urn:yobriefca-se:feed:post:An Opinionated Project Structure</id><content type="html">
&lt;p&gt;Choice and flexibility are good, in fact they a super smashing awesome.  Sort of.  I mean if you go over the top and offer people "all the choices" they are going to get all stressed and probably procrastinate until someone else makes the decision for them.  Also if you give them so much flexibility they will probably do things differently each time - again that is not a great thing either - for many things in life and IT you shouldn't have to make a conscious decision (it's probably waste of time in this case).&lt;/p&gt;&lt;p&gt;I've recently been reviewing my companies default project structure and it's full of choice and flexibility and while this can cater for the vast array of different project types in our company it also imposes a certain level of cognitive-tax prior to kicking off any development to get the solution in a position that can be farmed out to the team.  It imposes this tax across &lt;span class="caps"&gt;ALL&lt;/span&gt; projects even the 99% of the time typical project, with their typical project structure.  It's not a costly tax but it's unnecessary.  Why not just push it onto the non-standard projects who will likely need to spend time setting things up correctly anyway?  Suck it, weird projects!  Thats what I say.&lt;/p&gt;&lt;p&gt;To this end I've been trying to build a fairly opinionated project structure (along with some handy tools during the development stage).  Right now it's in the early stages and open to abuse/commentary (in fact I'd welcome it, please) but I thought it makes sense to share it&lt;sup id="fnr1" class="footnote"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt; - for the benefit of myself and others (or selfishly unselfish as I like to call it).&lt;/p&gt;&lt;h1&gt;Folder Structure&lt;/h1&gt;&lt;p&gt;I didn't want to break from tradition here.  There is exists a fairly standard project structure that is common across many languages and environments and rather than create a clever, verbose structure I reckon it's probably best to stick with the tried and tested approach.  That way new developers, regardless of background, shouldn't have to wrestle with heavily nested folders or obscure names for folders (SolutionSource vs src) when navigating our solution.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;|_ &amp;lt;MySolution&amp;gt;
    |_ &amp;lt;lib&amp;gt;
   |_ repositories.config
    |_ &amp;lt;src&amp;gt;
    |_ &amp;lt;test&amp;gt;
 |_ .gitignore   
  |_ default.ps1  
  |_ MySolution.sln
 |_ nuget.config
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Most of this will be fairly self-explanatory&lt;sup id="fnr2" class="footnote"&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt; but whats the harm in a little clarification?&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;&lt;code&gt;lib&lt;/code&gt; this is the place either myself or Nuget (see nuget.config and default.ps1) will dump any DLLs, tools or other dependencies required by the solution.  As I will talk about later the only file I ever check in here is the repositories.config and have nuget resolve the rest (still some decisions need made around manually referenced files etc.).&lt;/li&gt;	&lt;li&gt;&lt;code&gt;src&lt;/code&gt; hold all the projects that are actually going to be released as part of the final solution.  This folder doesn't contain any of the test projects you create for the solution - no point in muddyinbg the waters.&lt;/li&gt;	&lt;li&gt;&lt;code&gt;test&lt;/code&gt; holds all the solutions projects that we create for running tests (unit, integration etc) to validate the solution.&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;Special Files&lt;/h1&gt;&lt;p&gt;My default project comes with a number of files that help make developing and building the solution form scratch as easy as it should be.  These files are open to be tweaked to suit the needs of the project as and when necessary.&lt;/p&gt;&lt;h2&gt;repositories.config&lt;/h2&gt;&lt;p&gt;This is a file that is used by &lt;a href="http://nuget.org"&gt;Nuget&lt;/a&gt; to resolve the location of all the package configuration files associated with the projects in a solution (packages.config).  When I start this file is empty but as dependencies are added via &lt;a href="http://nuget.org"&gt;Nuget&lt;/a&gt; this file is updated to the location of packages.config files within each project (which in turn is used to resolved the necessary &lt;a href="http://nuget.org"&gt;Nuget&lt;/a&gt; packages that need to be downloaded).  I include this file along with a download task in my &lt;a href="https://github.com/JamesKovacs/psake"&gt;psake&lt;/a&gt; build file (see later) so that we we don't have to check-in all those, potentially large, dlls and tools into our &lt;acronym title="Version Control System"&gt;&lt;span class="caps"&gt;VCS&lt;/span&gt;&lt;/acronym&gt; - it's slow and annoying.  As a bonus when using some VCS's (e.g. git) this file will ensure the lib folder is committed to source control even thought it is technically empty - keeping our project structure intact.&lt;/p&gt;&lt;h2&gt;.gitignore&lt;/h2&gt;&lt;p&gt;Used to ignore certain generated and user specific files when using the git version control system.  Keeps things nice and clean and reduces annoying unnecessary conflicts.  As with most solutions out there I ignore the following patterns by default,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;bin
obj
*.suo
*.csproj.user
*.cache
lib/*/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;nuget.config&lt;/h2&gt;&lt;p&gt;I'll jump onto the meat of the files, default.ps1, shortly but first as you can probably guess by now I assume the use of &lt;a href="http://nuget.org"&gt;Nuget&lt;/a&gt;.  It's there, it's great and thats all I want to say on that matter.  &lt;code&gt;nuget.config&lt;/code&gt; is a simple file that tells &lt;a href="http://nuget.org"&gt;Nuget&lt;/a&gt; (even within Visual Studio) to put all it's downloaded files into the lib directory rather than the default &lt;code&gt;packages&lt;/code&gt; folder.  Everyone understands what &lt;code&gt;lib&lt;/code&gt; is, less people - packages.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;repositoryPath&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;repositoryPath&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;default.ps1&lt;/h2&gt;&lt;p&gt;I've decided to run with &lt;a href="https://github.com/JamesKovacs/psake"&gt;psake&lt;/a&gt; to coordinate and perform my builds.  MSBuild, while useful, is ridden with sickeness I call &lt;span class="caps"&gt;XML&lt;/span&gt; (&lt;span class="caps"&gt;BTW&lt;/span&gt; everyone else calls it &lt;span class="caps"&gt;XML&lt;/span&gt; too &lt;span class="caps"&gt;AFAIK&lt;/span&gt;).  It's a beast to get right.  &lt;a href="https://github.com/JamesKovacs/psake"&gt;Psake&lt;/a&gt; on the other hand is pure script.  It's more succinct and you can even debug it - how awesome is that?  So &lt;code&gt;default.ps1&lt;/code&gt; is my &lt;a href="https://github.com/JamesKovacs/psake"&gt;psake&lt;/a&gt; based build file.  What does it look like currently?  Glad you asked.....&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;# --&lt;/span&gt;
&lt;span class="c"&gt;#                     S C R I P T   P R O P E R T I E S &lt;/span&gt;
&lt;span class="c"&gt;# --&lt;/span&gt;
&lt;span class="n"&gt;Properties&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nv"&gt;$solution&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Get-ChildItem&lt;/span&gt; &lt;span class="p"&gt;*.&lt;/span&gt;&lt;span class="n"&gt;sln&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;
  &lt;span class="nv"&gt;$solutionname&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$solution&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$solution&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LastIndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I assume that the majority of projects are based on a single solution held at the root of our folder structure (as described above).  So I dynamically grab the name and relative path of the solution file for use later.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;# --&lt;/span&gt;
&lt;span class="c"&gt;#                      H E L P E R   F U N C T I O N S&lt;/span&gt;
&lt;span class="c"&gt;# --&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nb"&gt;Write-LineBreak&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="nb"&gt;Write-Host&lt;/span&gt; &lt;span class="s2"&gt;"-"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Set of helper functions currently only provides a simple function to print out a flowerboxing line to make output a bit cleaner.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;# Set the default framework (3.5 by default)&lt;/span&gt;
&lt;span class="nv"&gt;$framework&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'4.0'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Assume that we are all living in the present (as much as possible) so switch to .&lt;span class="caps"&gt;NET&lt;/span&gt; 4.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;# Declare default task&lt;/span&gt;
&lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="k"&gt;Default&lt;/span&gt; &lt;span class="n"&gt;-depends&lt;/span&gt; &lt;span class="n"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;Test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By default &lt;a href="https://github.com/JamesKovacs/psake"&gt;psake&lt;/a&gt; should do a &lt;code&gt;Build&lt;/code&gt; and &lt;code&gt;Test&lt;/code&gt;.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;# Resolves all the requires nuget dependecies so they don't need checked in&lt;/span&gt;
&lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="n"&gt;Resolve&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;Push-Location&lt;/span&gt; &lt;span class="s1"&gt;'.\lib\'&lt;/span&gt;
  &lt;span class="nb"&gt;Get-Content&lt;/span&gt; &lt;span class="s1"&gt;'.\repositories.config'&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    
 &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$_&lt;/span&gt; &lt;span class="o"&gt;-match&lt;/span&gt; &lt;span class="s1"&gt;'path="(.*)"'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  
      &lt;span class="nb"&gt;Write-LineBreak&lt;/span&gt;
     &lt;span class="nb"&gt;Write-Host&lt;/span&gt; &lt;span class="s2"&gt;"Resolving Dependencies for "&lt;/span&gt; &lt;span class="n"&gt;-nonewline&lt;/span&gt;
     &lt;span class="nb"&gt;Write-Host&lt;/span&gt; &lt;span class="nv"&gt;$matches&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'\'&lt;/span&gt;&lt;span class="p"&gt;)[-&lt;/span&gt;&lt;span class="n"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;-ForegroundColor&lt;/span&gt; &lt;span class="n"&gt;Green&lt;/span&gt;
      &lt;span class="nb"&gt;Write-LineBreak&lt;/span&gt;
 
      &lt;span class="n"&gt;nuget&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="nv"&gt;$matches&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
   
      &lt;span class="nb"&gt;Write-LineBreak&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nb"&gt;Pop-Location&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Resolve is the function mentioned earlier that finds the location of the repositories.config files and asks &lt;a href="http://nuget.org"&gt;Nuget&lt;/a&gt; to resolve all the external dependencies of the solution.  This should be done when you have pulled from a repository that included new dependencies.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;# Build the first solution file you find&lt;/span&gt;
&lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="n"&gt;Build&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  
  &lt;span class="nb"&gt;Write-LineBreak&lt;/span&gt;
  &lt;span class="nb"&gt;Write-Host&lt;/span&gt; &lt;span class="s2"&gt;"Building Solution "&lt;/span&gt; &lt;span class="n"&gt;-nonewline&lt;/span&gt;
  &lt;span class="nb"&gt;Write-Host&lt;/span&gt; &lt;span class="nv"&gt;$solutionname&lt;/span&gt; &lt;span class="n"&gt;-ForegroundColor&lt;/span&gt; &lt;span class="n"&gt;Green&lt;/span&gt;
  &lt;span class="nb"&gt;Write-LineBreak&lt;/span&gt;
      
  &lt;span class="n"&gt;Exec&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;msbuild&lt;/span&gt;   &lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="n"&gt;quiet&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;&lt;span class="n"&gt;Rebuild&lt;/span&gt;  &lt;span class="p"&gt;}&lt;/span&gt;
  
  &lt;span class="nb"&gt;Write-LineBreak&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Performs a basic, quiet build of the default solution.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;# Run the project tests&lt;/span&gt;
&lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="n"&gt;Test&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Executes all the tests in the solution.  Currently empty I intend to flesh this out with a default unit testing approach.&lt;/p&gt;&lt;h1&gt;Baby Steps&lt;/h1&gt;&lt;p&gt;So this is just the beginning and I hope this evolves based on observations made in our real world projects rather than a bunch of nice to haves.  It will also be nice to automate the creation of projects and solutions but thats certainly for another time.  Recommendations, comments and all that stuff welcome.  I'll try and get this basic structure up on Github too.&lt;/p&gt;&lt;p id="fn1" class="footnote"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; I mean, it has been a while since I blogged so it's about time I get back to it.  That said this isn't a "filler post".  I'm not doing this under some random contract.  Thought I wish I was - imagine that - how fun.&lt;/p&gt;&lt;p id="fn2" class="footnote"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; Cracker idea, right?  Who'd have thought it??! I'm a total genius and you can bow at my feet at will please.  kthnxbai.&lt;/p&gt;&lt;p id="fn3" class="footnote"&gt;&lt;a href="#fnr3"&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt; All subfolders in lib - as mentioned in the previous section.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET 101: Tinyweb</title><updated>Sun Jul 17 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/07/17/micro-web-frameworks-in-net-101-tinyweb/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET 101: Tinyweb</id><content type="html">
&lt;p&gt;Tinyweb takes a slightly different view than the other web frameworks I have talked about.  In fact it takes a fairly opinionated approach to the way your structure your applications code and forces you to think in terms of resource endpoints rather then big monolithic modules or controllers.  It's certainly true that projects based on the other frameworks such as Nancy and Jessica can be architected in such a way but Tinyweb ensures that you don't start cutting corners and making allowances for lazy code by simply not providing the ability to do it!&lt;/p&gt;&lt;h2&gt;Getting Started - Hello World&lt;/h2&gt;&lt;p&gt;Lets get Tinyweb first from good old reliable Nuget,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt; &lt;span class="n"&gt;Tinyweb&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
 &lt;p&gt;With Tinyweb installed we can go ahead and create a &lt;code&gt;RootHandler&lt;/code&gt; this name is the one exception to the Tinyweb handler naming convention which I'll touch on in a minute.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RootHandler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IResult&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In our handler I also defined the a &lt;code&gt;Get&lt;/code&gt; method which returns an IResult this will act as our endpoint.  One last thing before we magic up a web page is to initialise Tinyweb via our &lt;code&gt;Global.asax.cs&lt;/code&gt; so Tinyweb can do its bootstrapping discovery voodoo stuffs.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Global&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;HttpApplication&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Application_Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Tinyweb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
 &lt;p&gt;Et voila! Run the app and once again we have a hello world.&lt;/p&gt;&lt;h2&gt;Tinyweb Features&lt;/h2&gt;&lt;p&gt;Even in that small Hello World example there are plenty of Tinyweb features but you'll also notice that Tinyweb really doesn't get in the way at all.&lt;/p&gt;&lt;h3&gt;Handlers&lt;/h3&gt;&lt;p&gt;Each Tinyweb Handler represents a single resource endpoint.  With the exception of &lt;code&gt;RootHandler&lt;/code&gt; the &lt;span class="caps"&gt;URL&lt;/span&gt; endpoint for each handler is inferred from the name of the handler.  For example a handler named &lt;code&gt;HelloHandler&lt;/code&gt; will react to &lt;code&gt;/hello&lt;/code&gt;, &lt;code&gt;HelloWorldHandler&lt;/code&gt; will react to &lt;code&gt;/hello/world&lt;/code&gt;.  You can see from these examples that casing of the handler name is important in terms of the url endpoint generation.&lt;/p&gt;&lt;p&gt;The next thing about handlers is that they respond to 4 methods each one corresponding to the 4 main Http Verbs - &lt;code&gt;Get()&lt;/code&gt;, &lt;code&gt;Post()&lt;/code&gt;, &lt;code&gt;Put()&lt;/code&gt; and &lt;code&gt;Delete&lt;/code&gt; - the only requirement of these is that they return an &lt;code&gt;IResult&lt;/code&gt;&lt;/p&gt;&lt;h3&gt;Model Binding and Arguments&lt;/h3&gt;&lt;p&gt;Model binding is also provided by handlers actions - just pass in an object and Tinyweb will do it's best to bind request values to this object (accepts primitives, collections and plain C# objects).&lt;/p&gt;&lt;p&gt;Alternatively if you want to do some work under the hood you can make sure the &lt;code&gt;RequestContext&lt;/code&gt; is passed in by simply passing it instead.  This gives you access to the bowels of the request to do with what you please.&lt;/p&gt;&lt;h3&gt;Advanced Routes&lt;/h3&gt;&lt;p&gt;It's also possible to override the default routing convention by declaring a handler level variable called &lt;code&gt;route&lt;/code&gt; of type &lt;code&gt;Route&lt;/code&gt;.  This example shows how we can override handlers default route &lt;code&gt;/hello/world&lt;/code&gt; and go with the more understandable "/helloworld".&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorldHandler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Route&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"helloworld"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IResult&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Route Parameters&lt;/h3&gt;&lt;p&gt;In the spirit of RESTFulness you may also want to accept parameters as part of your &lt;span class="caps"&gt;URL&lt;/span&gt; and we can do that as well with this &lt;code&gt;route&lt;/code&gt; class as well as optionally suppling default values for parameters,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorldHandler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Route&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello/{name}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// OR WITH A DEFAULT VALUE FOR PARAMS&lt;/span&gt;
    &lt;span class="n"&gt;Route&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello/{name}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IResult&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A powerful little approach.&lt;/p&gt;&lt;h3&gt;Results&lt;/h3&gt;&lt;p&gt;All Tinyweb results (e.g. what the response returns) implement the &lt;code&gt;IResult&lt;/code&gt; interface so it's simple enough to implement your own custom result type though it is probably unnecessary for the most part as Tinyweb offers a range of result types straight away via the &lt;code&gt;Result&lt;/code&gt; classes static methods.&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;String&lt;/li&gt;	&lt;li&gt;File&lt;/li&gt;	&lt;li&gt;Json&lt;/li&gt;	&lt;li&gt;&lt;span class="caps"&gt;XML&lt;/span&gt;&lt;/li&gt;	&lt;li&gt;JsonOrXml (returns either Json or &lt;span class="caps"&gt;XML&lt;/span&gt; depending on the request headers)&lt;/li&gt;	&lt;li&gt;Html&lt;/li&gt;	&lt;li&gt;Redirect (to a specific handler or &lt;span class="caps"&gt;URL&lt;/span&gt;)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Tinyweb also offers a &lt;code&gt;View&lt;/code&gt; class that can render views written with Spark and Razor so rendering a view can be as simple as&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorldHandler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Route&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello/{name}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"World"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IResult&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Razor&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hello.cshtml"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Filters&lt;/h3&gt;&lt;p&gt;Filters allow us to intercept requests both before and after they are processed on both a per handler and global level.&lt;/p&gt;&lt;p&gt;Each handler can optionally contain an &lt;code&gt;After&lt;/code&gt; and/or a &lt;code&gt;Before&lt;/code&gt; method that will, not surprisingly, be called after and before each handler request (I'll let you guess which one does which) e.g.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RootHandler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Before&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Before Executed"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IResult&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;After&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"After Executed"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alternatively if you want the same before or after filter applied across all handlers you can create a Filter class by creating a class appended with the word &lt;code&gt;Filter&lt;/code&gt;.  We can recreate the same handler above with a global filter for logging,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoggingFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Before&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Before Executed"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;After&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"After Executed"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RootHandler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IResult&lt;/span&gt; &lt;span class="nf"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;Before&lt;/code&gt; and &lt;code&gt;After&lt;/code&gt; methods can also return &lt;code&gt;IResult&lt;/code&gt; objects if you want to work with the actual response.&lt;/p&gt;&lt;h3&gt;Error Handling and Debugging&lt;/h3&gt;&lt;p&gt;Not all of us write flawless code and so sometimes things go south and exceptions start throwing their weight around.  Tinyweb has a global hook that is useful for capturing such errors as they bubble up to the surface.  The TinyWeb class has a static property &lt;code&gt;OnError&lt;/code&gt; that accepts an Action that can be used to handle the exception e.g.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Application_Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Tinyweb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Init&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Tinyweb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OnError&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another useful tool when debugging your app is the &lt;code&gt;Tinyweb.WhatHaveIGot()&lt;/code&gt;.  It's a convenience method that can be used to print out all the matched routes and filters.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;On the surface the focus of Tinyweb may seem only moderately different from other frameworks but after playing with it for a while it become apparent that the opinionated approach really makes you think about your projects structure and &lt;span class="caps"&gt;API&lt;/span&gt;.  I'm certainly of the opinion that this is a damn good thing&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.  Tinyweb gives me just enough framework to do pretty much everything I need - which I would expect from a real micro-framework.  On top of that it's flexible and intuitive and another handy utility in my tool belt.&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; I've already said that it is possible to make the likes of Nancy and Jessica behave in this, either by ensuring that all module adhere to this pattern, or creating an abstract base class that enforces this sort of structure so I am in no way bashing the alternatives.  I like choice.&lt;/p&gt;</content></entry><entry><title>Baler v0.4.1 Released</title><updated>Sat Jul 09 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/07/09/baler-v0-dot-4-1-released/"></link><id>urn:yobriefca-se:feed:post:Baler v0.4.1 Released</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;Tiny release push out to &lt;a href="http://yobriefca.se/projects/baler"&gt;Baler&lt;/a&gt; that provides VS2008 specific project and solution files. No new stuff pushed to Nuget as it is source only this time.&lt;/p&gt;&lt;p&gt;Source available in the &lt;a href="https://github.com/kouphax/baler/"&gt;Github Repo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;More on its way soon!&lt;/p&gt;</content></entry><entry><title>Get Excited and Make Things</title><updated>Tue Jul 05 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/07/05/get-excited-and-make-things/"></link><id>urn:yobriefca-se:feed:post:Get Excited and Make Things</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p style="text-align:center;"&gt;&lt;img src="/images/getexcitedandmakethings.png" style="border:15px solid white;" /&gt;&lt;/p&gt;&lt;p&gt;Not generally a fan of the &lt;a href="http://en.wikipedia.org/wiki/Keep_Calm_and_Carry_On"&gt;Keep Calm and Carry On&lt;/a&gt; parodies but this one pretty much sums up how I feel about my job/career type thingies and well worth a mention. Props to &lt;a href="http://www.flickr.com/photos/blackbeltjones/3365682994/"&gt;Matt Jones&lt;/a&gt; for this one.&lt;/p&gt;</content></entry><entry><title>Debugging NSpec Tests: The DebuggerShim</title><updated>Mon Jul 04 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/07/04/debugging-nspec-tests-the-debuggershim/"></link><id>urn:yobriefca-se:feed:post:Debugging NSpec Tests: The DebuggerShim</id><content type="html">
&lt;p&gt;Busy busy busy.  I've been working away on a lot of projects and not had a lot of time to stick anything up on this blog in a while.  I've got a growing "To Blog" list but no time to do it, woe is me!!!&lt;br /&gt;&lt;img src="/images/woeisme.png" style="float:right;" /&gt;&lt;/p&gt;&lt;p&gt;Enough about my perfectly normal life, well almost.  Some of my current work has me going full tilt with &lt;span class="caps"&gt;TDD&lt;/span&gt; on a little C# project and I decided that &lt;a href="http://nspec.org"&gt;NSpec&lt;/a&gt; would help me with unit testing.  So I went about writing failing tests, writing code, passing tests on and on and on.  Things went along fairly well until I hit a little bit of a wall around a little edge case.  My tests kept failing and I couldn't understand why.  The stack trace made little to no sense and my code looked sound.  The problem I had was that &lt;a href="http://nspec.org"&gt;NSpec&lt;/a&gt;, out of the box at least, doesn't have any nice integration with Visual Studio or TestDriven.&lt;span class="caps"&gt;NET&lt;/span&gt;.  Initially I was using a throw away console application and debugging through it but as you can imagine that was time consuming.&lt;/p&gt;&lt;p&gt;It wasn't long though until I was pointed in the &lt;a href="http://twitter.com/#!/amirrajan/status/87174922782253056"&gt;right direction&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;!-- http://twitter.com/#!/amirrajan/status/87174922782253056 --&gt; &lt;style type="text/css"&gt;.bbpBox87174922782253056 {background:url(http://a0.twimg.com/images/themes/theme1/bg.png) #C0DEED;padding:20px;} p.bbpTweet{background:#fff;padding:10px 12px 10px 12px;margin:0;min-height:48px;color:#000;font-size:18px !important;line-height:22px;-moz-border-radius:5px;-webkit-border-radius:5px} p.bbpTweet span.metadata{display:block;width:100%;clear:both;margin-top:8px;padding-top:12px;height:40px;border-top:1px solid #fff;border-top:1px solid #e6e6e6} p.bbpTweet span.metadata span.author{line-height:19px} p.bbpTweet span.metadata span.author img{float:left;margin:0 7px 0 0px;width:38px;height:38px} p.bbpTweet a:hover{text-decoration:underline}p.bbpTweet span.timestamp{font-size:12px;display:block}&lt;/style&gt; &lt;/p&gt;&lt;div class="bbpBox87174922782253056"&gt;&lt;p class="bbpTweet"&gt;If you want to have debugger support for you NSpec specifications, use this: &lt;a href="http://t.co/6SwZcVL" rel="nofollow"&gt;http://t.co/6SwZcVL&lt;/a&gt; /cc @&lt;a class="tweet-url username" href="http://twitter.com/kouphax" rel="nofollow"&gt;kouphax&lt;/a&gt; @&lt;a class="tweet-url username" href="http://twitter.com/mattflo" rel="nofollow"&gt;mattflo&lt;/a&gt;&lt;span class="timestamp"&gt;&lt;a title="Sat Jul 02 15:04:55 +0000 2011" href="http://twitter.com/#!/amirrajan/status/87174922782253056"&gt;less than a minute ago&lt;/a&gt; via web &lt;a href="http://twitter.com/intent/favorite?tweet_id=87174922782253056"&gt;&lt;img src="http://si0.twimg.com/images/dev/cms/intents/icons/favorite.png" /&gt; Favorite&lt;/a&gt; &lt;a href="http://twitter.com/intent/retweet?tweet_id=87174922782253056"&gt;&lt;img src="http://si0.twimg.com/images/dev/cms/intents/icons/retweet.png" /&gt; Retweet&lt;/a&gt; &lt;a href="http://twitter.com/intent/tweet?in_reply_to=87174922782253056"&gt;&lt;img src="http://si0.twimg.com/images/dev/cms/intents/icons/reply.png" /&gt; Reply&lt;/a&gt;&lt;/span&gt;&lt;span class="metadata"&gt;&lt;span class="author"&gt;&lt;a href="http://twitter.com/amirrajan"&gt;&lt;img src="http://a2.twimg.com/profile_images/1326219901/1613d80124a605829d755d9df0fc8b9e_normal.jpeg" /&gt;&lt;/a&gt;&lt;strong&gt;&lt;a href="http://twitter.com/amirrajan"&gt;Amir Rajan&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;amirrajan&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/div&gt; &lt;!-- end of tweet --&gt;&lt;p&gt;The gist, in full below, provides a simple debugger shim over nspec that allows you to hook into the Visual Studio debugger (through something like TestDriven.&lt;span class="caps"&gt;NET&lt;/span&gt; for example).  I simply right clicked on the shim and select "Run With Debugger" and boom my stupid mistake was displayed to me rather quickly.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;NUnit.Framework&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;NSpec.Domain&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Reflection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;NSpec&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;DynamicBlog.Tests&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [TestFixture]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DebuggerShim&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;        [Test]&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;//the specification class you want to test&lt;/span&gt;
            &lt;span class="c1"&gt;//this can be a regular expression&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;testClassYouWantToDebug&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"describe_Blog"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;//initialize NSpec's specfinder&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;finder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;SpecFinder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;Assembly&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetExecutingAssembly&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Reflector&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; 
                &lt;span class="n"&gt;testClassYouWantToDebug&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            
            &lt;span class="c1"&gt;//initialize NSpec's builder&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;builder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ContextBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;finder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DefaultConventions&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

            &lt;span class="c1"&gt;//this line runs the tests you specified in the filter&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ContextRunner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;builder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ConsoleFormatter&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="n"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Handy until a more concrete solution appears.  One more thing - really enjoying nspec, it seems to be fitting well with the way I work.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET 101: Anna</title><updated>Tue Jun 21 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/21/micro-web-frameworks-in-net-101-anna/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET 101: Anna</id><content type="html">
&lt;p&gt;It's been a while since I visited this subject (at least in my blog) and I've still to record a lot of screencasts around Nancy.  Anyway I thought I'd give a quick mention to &lt;a href="https://github.com/jfromaniello/Anna/"&gt;Anna&lt;/a&gt; that has just come up on my radar.  Now the project is still in the very early stages but it's possibly one of the best uses of &lt;a href="http://msdn.microsoft.com/en-us/data/gg577609"&gt;Rx&lt;/a&gt; and worth a quick look.  Now I am a bit rusty in the old Rx stuff so sorry if I say something stupid.&lt;/p&gt;&lt;h2&gt;Getting Started - Hello World&lt;/h2&gt;&lt;p&gt;&lt;a href="https://github.com/jfromaniello/Anna/"&gt;Anna&lt;/a&gt; aims to be an asynchronous event-driven &lt;span class="caps"&gt;HTTP&lt;/span&gt; server in a similar vein to &lt;a href="http://nodejs.org/"&gt;node.js&lt;/a&gt;.  At it's core is the &lt;code&gt;HttpServer&lt;/code&gt; object that allows us to wire up routes.&lt;/p&gt;&lt;p&gt;There is no configuration required - just install &lt;a href="https://github.com/jfromaniello/Anna/"&gt;Anna&lt;/a&gt;  from the Nuget Package Console via &lt;code&gt;Install-Package Anna&lt;/code&gt; and you are good to go.  Just create a console application with the infamous &lt;code&gt;Program.cs&lt;/code&gt; and a few lines of code gives you the &lt;code&gt;Hello World&lt;/code&gt; solution,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;HttpServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://localhost:1234/"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
        &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReadLine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I added the &lt;code&gt;Console.ReadLine()&lt;/code&gt; to stop the console app exiting and cleaning things up.  Running the app and going to &lt;code&gt;http://localhost:1234&lt;/code&gt; gives you the result.  The &lt;code&gt;HttpServer&lt;/code&gt; object has a number of methods for the various &lt;span class="caps"&gt;HTTP&lt;/span&gt; verbs and these methods all return instances of &lt;code&gt;IObservable&lt;/code&gt; which means you can subscribe to them and apply all those filters, transforms etc. the Rx offers.&lt;/p&gt;&lt;h2&gt;Other Anna Features&lt;/h2&gt;&lt;p&gt;As I said &lt;a href="https://github.com/jfromaniello/Anna/"&gt;Anna&lt;/a&gt;  is still in the early stages so once you grab a route there isn't a &lt;span class="caps"&gt;MASSIVE&lt;/span&gt; amount you can do with but respond.  Responses however are chainable so you can create an almost middleware like stack for requests e.g.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello "&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This gives you the same output as the original Hello World sample.&lt;/p&gt;&lt;p&gt;But from what I can see thats about as far as you can get.  In &lt;a href="http://nodejs.org/"&gt;node.js&lt;/a&gt; there is a framework called &lt;a href="http://senchalabs.github.com/connect/"&gt;Connect&lt;/a&gt; now I'm not sure if &lt;a href="https://github.com/jfromaniello/Anna/"&gt;Anna&lt;/a&gt;  is intending to go in this sort of direction but I think it could be moulded into something like this with a bit of &lt;span class="caps"&gt;TLC&lt;/span&gt;.  The problem is at the minute I don't see any sort of object passed along the subscription chain e.g. a &lt;code&gt;dynamic&lt;/code&gt; bag or something. I don't even know how to easily manage response headers.&lt;/p&gt;&lt;h3&gt;Routes and Route Fragments&lt;/h3&gt;&lt;p&gt;&lt;a href="https://github.com/jfromaniello/Anna/"&gt;Anna&lt;/a&gt;  supports creating different routes including routes with dynamic sections &lt;code&gt;/name/{name}&lt;/code&gt; that are used to populated a &lt;code&gt;UriFragments&lt;/code&gt; property,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hello/{name}"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello {0}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UriArguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Conditional Routes&lt;/h3&gt;&lt;p&gt;Thanks to the power of Rx and Linq it is possible to create conditional routes an create chains that have pre-response checks.  For example we can have a route that only triggers a response if the QueryString has a secret parameter.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;QueryString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"letmein"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;h2&amp;gt;Secret Message&amp;lt;/h2&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;So if you hit &lt;code&gt;http://localhost:1234/&lt;/code&gt; you get Hello World.  Hit that again with the param &lt;code&gt;?/secret=letmein&lt;/code&gt; and you get the super secret message.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Thats about it for now but I think I'll be keeping an eye on this project and see were it goes.  Even though it's still early days I like the concept behind the library and the fresh approach it's taking.  I'll push some of my sample code up to Github and update this post in the next few days.  In the meantime Jose the creator of Anna has &lt;a href="http://joseoncode.com/2011/06/17/event-driven-http-server-in-c-with-rx-and-httplistener/"&gt;some nice examples&lt;/a&gt; of Rx based HttpServer stuff on his blog featuring a long polling example.&lt;/p&gt;</content></entry><entry><title>MicroORMs for .NET: Inserts, Updates &amp;amp; Delete</title><updated>Mon Jun 20 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/20/microorms-for-net-inserts-updates-and-delete/"></link><id>urn:yobriefca-se:feed:post:MicroORMs for .NET: Inserts, Updates &amp;amp; Delete</id><content type="html">
&lt;blockquote class="announce"&gt;This post is the is part of series of posts covering the various microORMs in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world.  I intend to look at how each microORM meets certain data access and manipulation needs.  The current series includes,&lt;ul&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/15/microorms-for-net-obligatory-introduction-post"&gt;Obligatory Introduction Post&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/16/microorms-for-net-syntax-selects"&gt;Syntax - SELECTs&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/18/microorms-for-net-stored-procedures"&gt;Stored Procedures&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/21/microorms-for-net-inserts-updates-and-delete"&gt;Inserts, Updates &amp;amp; Deletes&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;p&gt;Moving swiftly on, if all our solutions only used different way to select data then how would we ever get data into our solution?  So lets look at how these microORMs handle Inserts, Updates and Deletes&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.  These examples will follow on from previous ones so I am only going publish the key lines.  If you want the complete code you can grab them from the &lt;a href="https://github.com/kouphax/microorm-comparison"&gt;GitHub project&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Dapper&lt;/h2&gt;&lt;h3&gt;Insert&lt;/h3&gt;&lt;p&gt;Nothing unexpected here.  More &lt;span class="caps"&gt;SQL&lt;/span&gt;.  Sam Saffron previously pointed me in the direction of &lt;a href="http://code.google.com/p/dapper-dot-net/source/browse/Dapper.Contrib/Extensions/SqlMapperExtensions.cs"&gt;Dapper.Contrib&lt;/a&gt; that has extra extension methods for doing things like &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; but at the time of trying I couldn't get it to work and have an &lt;a href="https://github.com/SamSaffron/dapper-dot-net/issues/8"&gt;open issue&lt;/a&gt; on GitHub.  If I get an update on this I'll post the slightly cleaner syntax.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"INSERT Authors(Username, FullName, CreatedDate) "&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt;
    &lt;span class="s"&gt;"VALUES (@Username, @FullName, GETDATE())"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Update&lt;/h3&gt;&lt;p&gt;Update is very similar to insert.  The nice thing about this approach vs. using POCOs is that you only need to send the delta of the object up where as with the other approaches you may have to fetch the object before updating.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"UPDATE Authors SET FullName = @FullName WHERE Id = @Id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Delete&lt;/h3&gt;&lt;p&gt;Delete is pretty much what you'd expect.  I'll update these examples when I get my problems with Dapper.Contrib sorted but when you are so close to the &lt;span class="caps"&gt;SQL&lt;/span&gt; metal there isn't a massive amount to discuss.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"DELETE FROM Authors WHERE Id = @Id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Massive&lt;/h2&gt;&lt;h3&gt;Insert&lt;/h3&gt;&lt;p&gt;Massive keeps things simple as usual.  &lt;code&gt;Insert&lt;/code&gt; can take POCOs as well as dynamic object (and a number of other types) and it uses the info found in the &lt;code&gt;Authors&lt;/code&gt; DynamicModel class to generate the &lt;span class="caps"&gt;SQL&lt;/span&gt; for you.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As a bonus feature Massive can wrap multiple inserts in a single transaction and perform multiple inserts at once.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InsertMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Update&lt;/h3&gt;&lt;p&gt;Like Dapper, Massive allows us to send only a delta between the old and new object.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;
                                 &lt;span class="p"&gt;{&lt;/span&gt;
                                     &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;
                                 &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Massive also gets extra points for the &lt;code&gt;UpdateMany&lt;/code&gt; that, not suprisingly, behaves like the &lt;code&gt;InsertMany&lt;/code&gt; method mentioned above (except for updates of course).&lt;/p&gt;&lt;h3&gt;Delete&lt;/h3&gt;&lt;p&gt;Ultra simple approach to Deleting specific objects just pass in an id and you're done,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;PetaPoco&lt;/h2&gt;&lt;h3&gt;Insert&lt;/h3&gt;&lt;p&gt;PetaPoco offers 2 main ways to insert your data&lt;sup class="footnote" id="fnr2"&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt;.  You can pass in a plain &lt;span class="caps"&gt;POCO&lt;/span&gt; (or in our case on that uses attributes to normalise the difference between the schema and the C# class),&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DefaultConnectionString"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or you can pass in an anonymous object specifying the table and primary key field,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Authors"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Update&lt;/h3&gt;&lt;p&gt;There are plenty of ways to update data using PetaPoco.  The first one is to pass in your model object and the row will get updated.  I am creating a new object in this example but you could just fetch it from the database as you'd expect,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DefaultConnectionString"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another approach is to just pass up a delta of the changes,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Authors"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alternatively you can simply pass up the delta and specify the primary key value externally,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Authors"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Variety is the spice of life in PetaPoco!&lt;/p&gt;&lt;h3&gt;Delete&lt;/h3&gt;&lt;p&gt;Thanks to the custom attribute on our Author class deleting a record is a matter of passing the Id to the &lt;code&gt;Delete&lt;/code&gt; method,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;ServiceStack ORMLite&lt;/h2&gt;&lt;h3&gt;Insert&lt;/h3&gt;&lt;p&gt;ORMLite is pretty much inline with the other microORMs a simple &lt;code&gt;Insert&lt;/code&gt; method that accepts a &lt;span class="caps"&gt;POCO&lt;/span&gt; or an anonymous object and generates &lt;span class="caps"&gt;SQL&lt;/span&gt; based on the info given.  This can be done in one of two ways,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IDbConnection&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Program&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OpenDbConnection&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IDbCommand&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateCommand&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Or the factory approach discussed in the first article,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;IDbConnectionFactory&lt;/span&gt; &lt;span class="n"&gt;dbFactory&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;OrmLiteConnectionFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;Program&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionString&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SqlServerOrmLiteDialectProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Instance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;dbFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dbCmd&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dbCmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;}));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Update&lt;/h3&gt;&lt;p&gt;ORMLite sticks with one approach for updating a row - passing in a &lt;span class="caps"&gt;POCO&lt;/span&gt; that it can map to a table/row,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;If you really don't want to fetch your row again and only want to send up a delta you'll have to roll your own approach (simple enough though).&lt;/p&gt;&lt;h3&gt;Delete&lt;/h3&gt;&lt;p&gt;ORMLite supports a number of neat ways to delete rows,&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;&lt;code&gt;DeleteById(id)&lt;/code&gt;&lt;/li&gt;	&lt;li&gt;&lt;code&gt;Delete(where_clause, id)&lt;/code&gt;&lt;/li&gt;	&lt;li&gt;&lt;code&gt;Delete(object)&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeleteById&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"Id = @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;7&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Simple.Data&lt;/h2&gt;&lt;h3&gt;Insert&lt;/h3&gt;&lt;p&gt;Guess what Simple.Data's approach is once again very fluent, clean and easy to understand.  OK I don't get Intellisense as the methods are dynamic but I didn't need it anyway.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Update&lt;/h3&gt;&lt;p&gt;Simple.Data has 2 ways of updating.  &lt;code&gt;Update&lt;/code&gt; takes a &lt;span class="caps"&gt;POCO&lt;/span&gt; and maps the id to the specific row,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"james@dapper.net"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alternatively Simple.Data supports the delta approach too using &lt;code&gt;UpdateById&lt;/code&gt;&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UpdateById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Delete&lt;/h3&gt;&lt;p&gt;Finally Simple.Data has 2 ways to delete a record,&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;&lt;code&gt;DeleteById(id)&lt;/code&gt;&lt;/li&gt;	&lt;li&gt;&lt;code&gt;Delete(named_parameters)&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeleteById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;There We Have It&lt;/h2&gt;&lt;p&gt;OK OK I guess this post feels a bit rushed.  I started out throwing lots of details into selects and stuff but this one just isn't up to par.  I slowly discovered that I'd have to spend almost all my spare time covering all the aspects I wanted.  So I cut it back to a kind of "Starter for 10" approach.  Yeah thats right I'm leaving stuff up to you to ask and research :-P.  Anyway in these basic examples no one &lt;span class="caps"&gt;ORM&lt;/span&gt; really stands out too much.  Simple.Data is, as always, lovely and clean and PetaPoco offers a very flexible experience.  I like the ability to send deltas instead of full objects back (in case you hadn't guessed) and I had a few annoyances around updates using Massive and Dapper but I think it was my own stupidity (I'll update when I investigate).  If I had to choose between them I am still tending towards Simple.Data and PetaPoco (probably PetaPoco at this stage) but I do find all of them a much more pleasant experience vs. MyIbatis or NHibernate.&lt;/p&gt;&lt;p&gt;That should be enough to start anyone off on the right foot.&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; Hehehehe IUDs&lt;/p&gt;&lt;p class="footnote" id="fn2"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; You can pass a &lt;span class="caps"&gt;SQL&lt;/span&gt; string or a &lt;span class="caps"&gt;SQL&lt;/span&gt; builder as well but it's fairly straightforward so I'll not cover it here.  See the original &lt;a href="http://yobriefca.se/blog/2011/06/16/microorms-for-net-syntax-selects"&gt;&lt;span class="caps"&gt;SELECT&lt;/span&gt; post&lt;/a&gt; for a &lt;span class="caps"&gt;SQL&lt;/span&gt; and &lt;span class="caps"&gt;SQL&lt;/span&gt; Builder example.&lt;/p&gt;</content></entry><entry><title>Baler v0.4 Released</title><updated>Sun Jun 19 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/19/baler-v0-dot-4-released/"></link><id>urn:yobriefca-se:feed:post:Baler v0.4 Released</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;Just a quick note to say that Baler v0.4 has been released. Nothing new here I have simply compiled it (and the official extensions) against .NET3.5 just to make it more useable for people still stuck on old projects :-P&lt;/p&gt;&lt;p&gt;Everything is on Nuget already so go have fun.&lt;/p&gt;</content></entry><entry><title>Thinking about Hackathons</title><updated>Sat Jun 18 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/18/thinking-about-hackathons/"></link><id>urn:yobriefca-se:feed:post:Thinking about Hackathons</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;div class="update"&gt;&lt;p&gt;This post is a bit of a brain dump so expect some incoherence and repetition. I'm buzzed on coffee and feel like a kid in a toy shop so I just want to get it out there and if anyone has any experience of hackathons feel free to pitch in. In fact please do :)&lt;/p&gt;&lt;p&gt;Also sorry for the blogging overload.&lt;/p&gt;&lt;/div&gt;&lt;h2&gt;Background&lt;/h2&gt;&lt;p&gt;So I recently started walking to and from work. The walk is about 3.5 miles which takes about 50 minutes. This gives me a lot of time to think, even over think but even I bore myself sometimes so I decided to give podcasts a try. I grabbed a load of &lt;a href="http://hanselminutes.com"&gt;Hanselminutes&lt;/a&gt; and &lt;a href="http://herdingcode.com/"&gt;Herding Code&lt;/a&gt; episodes to maybe learn something new. One of the &lt;a href="http://herdingcode.com/"&gt;Herding Code&lt;/a&gt; podcasts really caught my attention - &lt;a href="http://herdingcode.com/?p=310"&gt;109: Harmony Hackathon&lt;/a&gt;. The podcast tells the story of a group of people that got together and spent 48 hours designing and developing a solution for a charity organisation. I've heard of hackathons before but hearing these guys talk about their experiences while using a technology stack that is my bread and butter (.&lt;span class="caps"&gt;NET&lt;/span&gt;) just got me all tingly inside.&lt;/p&gt;&lt;p&gt;I wanted to do this. I know it sounds pathetic but the experience of hashing something out, cutting code and working totally agile without the bureaucracy of management slowing things up&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt; sounds really fun to me. I tend to get bored on long running projects. I really want to try new things in real projects but convincing managers or customers is a near impossible task as the "play it safe" attitude makes a lot more sense to them. Come on guys who cares about money when there is fun to be had?&lt;/p&gt;&lt;p&gt;So anyway this got me thinking. How would I go about this, why would I go about this and how would I get other people to give 24 or 48 straight hours of their life for something like this? I mean I live in Belfast, Northern Ireland not exactly a &lt;span class="caps"&gt;MAJOR&lt;/span&gt; hub of geek activity.&lt;/p&gt;&lt;h2&gt;Hackathon Goals&lt;/h2&gt;&lt;p&gt;So why would we do this? Having clear goals upfront is really going help sell this to people. They will also act as guidance and help drive the entire event to the finish line. There are plenty of reasons, perhaps even contradictory ones,&lt;/p&gt;&lt;ul&gt; &lt;li&gt;A finished product or prototype&lt;/li&gt; &lt;li&gt;Education of technologies and techniques&lt;/li&gt; &lt;li&gt;The experience&lt;/li&gt; &lt;li&gt;Technology evaluation&lt;/li&gt; &lt;li&gt;Networking&lt;/li&gt; &lt;li&gt;Something to put on your CV&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I realise that much of this is done on a per person basis but unless each persons goals are aligned in some way there is a risk the whole thing would fall apart. Personally I think people wanting to leverage it for any sort of networking, CV building or taking unfounded punts on technology need not apply. I am not adverse to a bit of risk taking in the technology stakes but they need to be calculated risks rather than a &lt;em&gt;"ohhh shiny lets use this"&lt;/em&gt; attitude. If it's putting all the other goals at risk it's not worth it.&lt;/p&gt;&lt;ul&gt; &lt;li&gt;A finished product or prototype&lt;/li&gt; &lt;li&gt;Education of technologies and techniques&lt;/li&gt; &lt;li&gt;The experience&lt;/li&gt; &lt;li&gt;&lt;del&gt;Technology evaluation&lt;/del&gt;&lt;/li&gt; &lt;li&gt;&lt;del&gt;Networking&lt;/del&gt;&lt;/li&gt; &lt;li&gt;&lt;del&gt;Something to put on your CV&lt;/del&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I'd be happy working with people who agree with me on at least 2 of the points above. But of course this isn't a hard and fast rule and I remain open for the greater good.&lt;/p&gt;&lt;h2&gt;The Idea&lt;/h2&gt;&lt;p&gt;So what would we do? I don't think it would work very well if we just played around with some technologies and produced a few prototypes. No, addressing a real work problem or need that is of sufficient size and complexity that allows us to drive a spike right through the entire solution gives us something to work towards. Actually producing something that works or at least proves that the idea is viable seems almost mandatory. The thought of walking away after the 24 or 48 hours with something that you can show people is a noble goal. Whether that be for charity, something internal for your company or something for the open source community.&lt;/p&gt;&lt;p&gt;I have plenty of ideas I would go for but this isn't a dictatorship (not at this point anyway) this is only going to work if people feel really involved, after all when the "mid way slump" starts kicking in you're only going to make it through if you &lt;span class="caps"&gt;WANT&lt;/span&gt; it to work and are invested in experience. I guess what I am saying is you'd get people together - potential developers, product owners, investors up front and brainstorm, plan and argue. This brings me on to another point….&lt;/p&gt;&lt;h2&gt;The 7 P's&lt;/h2&gt;&lt;p&gt;Prior Preparation and Planning Prevent Piss Poor Performance. The more I think about this hackathon thing the more I come to the conclusion that it is so much bigger than the 24 or 48 hours of design and development. Like all good adventures you have to go in prepared. What would you need upfront?&lt;/p&gt;&lt;ul&gt; &lt;li&gt;You need your goals clearly defined&lt;/li&gt; &lt;li&gt;You need a few user stories so there is something for people to do while things are getting established&lt;/li&gt; &lt;li&gt;You need your stack and approach mapped out (including source control, &lt;acronym title="Continuous Integration"&gt;&lt;span class="caps"&gt;CI&lt;/span&gt;&lt;/acronym&gt; and Build Server etc.)&lt;/li&gt; &lt;li&gt;The participants need to at least have a fair idea what they are doing&lt;/li&gt; &lt;li&gt;You need to have some sort of rough plan or schedule&lt;/li&gt; &lt;li&gt;You need food, drink, beer, alternative entertainment&lt;/li&gt; &lt;li&gt;You need a venue, a comfortable venue with internet access&lt;/li&gt; &lt;li&gt;Oh and I guess you need to invest in whiteboards. Lots and lots of whiteboards, and markers, and cue cards and possibly deodorant.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;People involved&lt;/h2&gt;&lt;p&gt;Agile makes sense here, of course it does. So it's not just a matter of locking a few devs away with coffee and vitamin pills. No you need product owners at hand, you need someone testing and you need some sort of &lt;span class="caps"&gt;BDFL&lt;/span&gt; (Benevolent Dictator for Life). I guess a &lt;span class="caps"&gt;BDFL&lt;/span&gt; isn't mandatory but a technical lead who has the final say is certainly going to make the decision making process a lot quicker - be that good or bad.&lt;/p&gt;&lt;p&gt;Motivational people are another thing that would come in handy. Having people see something and be like "yeah that's nice, good job" is going to keep those flagging energy levels up.&lt;/p&gt;&lt;p&gt;Developer headcount is another balancing act. You need enough people to make the goals achievable but you also can't have too many people or you risk stamping on each others feet. I guess it depends on the size/complexity of the idea you want to implement but a finger in the air guess I'd say between 4-8 developers with a product owner and a tester (could be the same person) would be a nice number.&lt;/p&gt;&lt;h2&gt;Development Approach&lt;/h2&gt;&lt;p&gt;I've already said agile seems like a perfect fit and I think the whole ecosystem that agile brings in fits well. &lt;acronym title="Test Driven Development"&gt;&lt;span class="caps"&gt;TDD&lt;/span&gt;&lt;/acronym&gt;/&lt;acronym title="Behaviour Driven Design"&gt;&lt;span class="caps"&gt;BDD&lt;/span&gt;&lt;/acronym&gt; almost seems mandatory here - at least some level of it. Checking in code with no tests around it is going to cause a headache when you hit a bug that takes and hour to track down. A strict regime of constant branching, merging and checkins (every 20 minutes or less) along with a fast continuous build system will help discover problems &lt;span class="caps"&gt;ASAP&lt;/span&gt;.&lt;/p&gt;&lt;h2&gt;Atmosphere and Communication&lt;/h2&gt;&lt;p&gt;An entire project condensed into 24/48 hours is going to magnify the pain points and tensions will rise. Techniques such as Pomodoro would come in handy to make sure people aren't burning themselves out too fast. A quiet room and alternative non-computer based entertainment will give people a quick escape on their pomodoro break and a chance to get their head showered. Communication &lt;span class="caps"&gt;NEEDS&lt;/span&gt; to flow so regular standups (every 30 minutes to an hour) are essential to spot problems early and find a way to resolve them quickly. Again having some sort &lt;acronym title="Benevolent Dictator for Life"&gt;&lt;span class="caps"&gt;BDFL&lt;/span&gt;&lt;/acronym&gt; could really help in this situation.&lt;/p&gt;&lt;h2&gt;Where now?&lt;/h2&gt;&lt;p&gt;You know what - someway, somehow I am going to do this. Consider it part of my career bucket list. Something I want to do before I join the great geek scrapheap in the sky. Could I get the people? Yeah I reckon I could. Could I get the sponsorship/support? Most likely. Could I find a suitable idea to run with? Hells yeah. So what is stopping me? Hmmmmmm nothing. &lt;span class="caps"&gt;BRB&lt;/span&gt; got a hackathon to organise……..&lt;sup class="footnote" id="fnr2"&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; Not all managers are created equal but they have a job to do and process, meetings, documents etc. all get in the way.&lt;/p&gt;&lt;p class="footnote" id="fn2"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; Yeah really. No joke. I can but try.&lt;/p&gt;</content></entry><entry><title>MicroORMs for .NET: Stored Procedures</title><updated>Fri Jun 17 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/17/microorms-for-net-stored-procedures/"></link><id>urn:yobriefca-se:feed:post:MicroORMs for .NET: Stored Procedures</id><content type="html">
&lt;blockquote class="announce"&gt;This post is the is part of series of posts covering the various microORMs in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world.  I intend to look at how each microORM meets certain data access and manipulation needs.  The current series includes,&lt;ul&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/15/microorms-for-net-obligatory-introduction-post"&gt;Obligatory Introduction Post&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/16/microorms-for-net-syntax-selects"&gt;Syntax - SELECTs&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/18/microorms-for-net-stored-procedures"&gt;Stored Procedures&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/21/microorms-for-net-inserts-updates-and-delete"&gt;Inserts, Updates &amp;amp; Deletes&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;p&gt;As handy as running straight, fully-tailored SELECTs straight from your app code a lot of the time, especially in the "enterprise" world your &lt;span class="caps"&gt;DBA&lt;/span&gt; is going to insist that all operations go through tightly controlled stored procedures.  It's all for the greater good I'm sure but flipsake it's bloody annoying.&lt;/p&gt;&lt;div style="font-family:&amp;quot;Courier New&amp;quot;, Courier;color:#0b0;background-color:black; padding:1px 20px;font-size:0.9em;margin-left:20px;margin-right:20px;"&gt;&lt;p&gt;&lt;em&gt;You are standing at the edge of a forest.  Ahead of you an old wooden sign sits at a slight angle.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; read sign&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;You squint your eyes as you approach the sign and read the fading writing.  The sign reads&lt;/em&gt;&lt;/p&gt;&lt;p style="padding-left:1em;"&gt;&lt;em&gt;"Welcome to the forest of microORMs.  You have nice eyes.  Have a &lt;span class="caps"&gt;GREAT&lt;/span&gt; day."&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; enter forest&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;You walk, nay skip, into the forest.  A few minutes in you are confronted with a horribly gnarled beast.  The grumpy looking thing seems very out of place in this land of near tranquilty.  As you approach, it speaks&lt;/em&gt;&lt;/p&gt;&lt;p style="padding-left:1em;"&gt;&lt;em&gt;"&lt;span class="caps"&gt;GROAN&lt;/span&gt; &lt;span class="caps"&gt;GRUNT&lt;/span&gt; Control &lt;span class="caps"&gt;GROWL&lt;/span&gt; Best Practise &lt;span class="caps"&gt;WHEEZE&lt;/span&gt; Enterprise Ready &lt;span class="caps"&gt;SNARL&lt;/span&gt;.  Little being... you may not pass any way you please.  That is not how we do things &lt;span class="caps"&gt;GRUMBLE&lt;/span&gt; &lt;span class="caps"&gt;WHEEZE&lt;/span&gt;. No to pass this way meatbag you must, yes must, take this route I have planned for you.  If you need to change your route I must approve it and construct it &lt;span class="caps"&gt;COUGH&lt;/span&gt; &lt;span class="caps"&gt;GRUMBLE&lt;/span&gt;.  There is limited scenery and not too many points to have fun"&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; kill beast&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;I'm afraid I can't do that&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&amp;gt;&amp;gt; fffffffffffffffffffff..&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt;&lt;p&gt;Fear not help is at hand.  So can we call sprocs using our microORMs, and if yes how?  Lets go see shall we?&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;h2&gt;Dapper&lt;/h2&gt;&lt;p&gt;Dapper makes use of an extra parameter in its &lt;code&gt;Query&amp;lt;T&amp;gt;&lt;/code&gt; method to specify a specific command type.  In this way it's simply a matter of passing in the name of the sproc, the parameters and a &lt;code&gt;CommandType.StoredProcedure&lt;/code&gt; as a named parameter.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"GetAuthorById"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;commandType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;CommandType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StoredProcedure&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;First&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And boom thats it.  Same applies for multiple results.&lt;/p&gt;&lt;h2&gt;Massive&lt;/h2&gt;&lt;p&gt;Massive just works.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;dyn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GetAuthorById @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;First&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All I did was swap out the &lt;span class="caps"&gt;SELECT&lt;/span&gt; statement for the name of the sproc and Bobs Your Uncle.  Nice.&lt;/p&gt;&lt;h2&gt;PetaPoco&lt;/h2&gt;&lt;p&gt;PetaPoco has &lt;em&gt;some&lt;/em&gt; support for sprocs.  For my needs in this article it works fine but apparently it doesn't support output parameters &lt;span class="caps"&gt;ATM&lt;/span&gt;.  You need to prepend your call to the sproc with a DB specific command.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"EXECUTE GetAuthorById @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;First&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;EXECUTE&lt;/code&gt; statement is database specific.  If you are using MySql you need to use &lt;code&gt;CALL&lt;/code&gt; instead which kind of starts breaking the abstraction a bit.  I wonder if we will see this support getting more solidified in future releases.&lt;/p&gt;&lt;h2&gt;ServiceStack ORMLite&lt;/h2&gt;&lt;p&gt;At the time of writing this I couldn't see any built in support for sprocs using ORMLite.  Either I am being my typical clueless self or we would just have to do it by hand.  Here is what I came up with,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dbFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dbCmd&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;dbCmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CommandType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CommandType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StoredProcedure&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;dbCmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Parameters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;SqlParameter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"@id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;dbCmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CommandText&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"GetAuthorById"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dbCmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ExecuteReader&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;ConvertTo&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;It's not exactly the worst thing in the world and I guess you could wrap this stuff in an extension method but still I feel spoilt by the other microORMs so far and this was a slight disappointment.  Perhaps someone could either confirm or deny sproc support in ORMLite?&lt;/p&gt;&lt;h2&gt;Simple.Data&lt;/h2&gt;&lt;p&gt;Simple.Data has lovely syntax for this.  You just call it as yet another dynamic method on the database object and Simple.Data just infers it for you.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;GetAuthorById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;First&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yeah thats nice.&lt;/p&gt;&lt;h2&gt;Slightly Shorter and Summing Up&lt;/h2&gt;&lt;p&gt;So I didn't want this one to have too much waffle (bar the odd text adventure here and there).  I was concerned that perhaps calling sprocs might be one of those requirements were the whole microORM stuff starts falling apart but no.  They actually handle the whole thing in a nice, clean manner.&lt;/p&gt;&lt;p&gt;Simple.Data is certainly starting to look more appealing to me and the whole fact that it's not database specific (you've got NoSQL stores and even experimental things such as a Registry adapter - imagine this as part of Powershell).&lt;/p&gt;&lt;p&gt;Anyway some code snippets for this stuff are currently available in the &lt;a href="https://github.com/kouphax/microorm-comparison"&gt;project/series github repo&lt;/a&gt;&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; Not a real game.&lt;/p&gt;</content></entry><entry><title>Baler v0.3 Released</title><updated>Thu Jun 16 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/16/baler-v0-dot-3-released/"></link><id>urn:yobriefca-se:feed:post:Baler v0.3 Released</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;I've just pushed a new version of &lt;a href="http://yobriefca.se/projects/baler"&gt;Baler&lt;/a&gt;. It's current version of 0.3 but semantically speaking it should be v0.2.2 as I haven't changed the &lt;span class="caps"&gt;API&lt;/span&gt;. But hey we all make mistakes :(&lt;/p&gt;&lt;h2&gt;What's New?&lt;/h2&gt;&lt;p&gt;Thanks to a suggestion from &lt;a href="https://github.com/edazdarevic"&gt;edazdarevic&lt;/a&gt; this release reworks the way files are generated.&lt;/p&gt;&lt;p&gt;Originally if a bale wasn't flagged as generated (e.g. after an &lt;span class="caps"&gt;IIS&lt;/span&gt; reset) a new file with a random name would be generated. This mean't that during development a shed load of files would be generated as you changed some other file and restarted your server. This lead to an awful mess in your output folder. Now what happens if a bale isn't flagged as generated the contents of the file are processed and a hash is generated from this. This hash is then used as the filename for that file (appended with the correct extension). If that file exists then the contents haven't changed and are not written out to a new file saving that extra file system write.&lt;/p&gt;&lt;p&gt;In the future I'd like to adopt the approach of simply writing out references to the bales contents during &lt;code&gt;DEBUG&lt;/code&gt;/development mode and only process the bales when in &lt;code&gt;PRODUCTION&lt;/code&gt; mode. But for now I think this is a reasonable stop gap.&lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/edazdarevic"&gt;edazdarevic&lt;/a&gt; has some &lt;a href="https://github.com/kouphax/baler/issues/1"&gt;more ideas&lt;/a&gt; that I plan on trying to integrate but I need more time to think about how to approach it without bloating the code.&lt;/p&gt;</content></entry><entry><title>MicroORMs for .NET: Syntax - SELECTs</title><updated>Wed Jun 15 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/15/microorms-for-net-syntax-selects/"></link><id>urn:yobriefca-se:feed:post:MicroORMs for .NET: Syntax - SELECTs</id><content type="html">
&lt;blockquote class="announce"&gt;This post is the is part of series of posts covering the various microORMs in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world.  I intend to look at how each microORM meets certain data access and manipulation needs.  The current series includes,&lt;ul&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/15/microorms-for-net-obligatory-introduction-post"&gt;Obligatory Introduction Post&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/16/microorms-for-net-syntax-selects"&gt;Syntax - SELECTs&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/18/microorms-for-net-stored-procedures"&gt;Stored Procedures&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/21/microorms-for-net-inserts-updates-and-delete"&gt;Inserts, Updates &amp;amp; Deletes&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;p&gt;The two main things I want to look at in this article are&lt;/p&gt;&lt;ol&gt;	&lt;li&gt;Syntax of the microORM, specifically around,&lt;/li&gt;	&lt;li&gt;Selecting single and multiple results&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;So the most obvious starting point, bar the &lt;a href="../15/microorms-for-net-obligatory-introduction-post/"&gt;boring introduction&lt;/a&gt;, is how you perform selects using these awesome thingies.  To begin with lets get our playground set up.  Lets create a table of authors, lets call it &lt;code&gt;Authors&lt;/code&gt;&lt;sup class="footnote" id="fnr0"&gt;&lt;a href="#fn0"&gt;0&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;Id&lt;/span&gt;          &lt;span class="nb"&gt;int&lt;/span&gt;          &lt;span class="k"&gt;IDENTITY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;Username&lt;/span&gt;    &lt;span class="nb"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;FullName&lt;/span&gt;    &lt;span class="nb"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;250&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;               &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;                   &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

 &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;PK_Authors&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="n"&gt;CLUSTERED&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And populate it with some (meaningless) data&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'0@example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'James Red'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GETDATE&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'1@example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Harry Black'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GETDATE&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="c1"&gt;-- ...CROPPED FOR BREVITY...&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'134@example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Chloe Green'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;GETDATE&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Finally we need to create the equivalent C# object that our results can map to.  Again nothing too crazy here,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Author&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So now the stage is set and it's time for our microORMs to play their part.&lt;/p&gt;&lt;h2&gt;Selecting Single and Multiple Authors&lt;/h2&gt;&lt;p&gt;So lets assume we want to do 2 things -&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;Select a single author with an Id of 1, and,&lt;/li&gt;	&lt;li&gt;Select multiple authors whose username (in this case the users email address) ends with &lt;em&gt;example.com&lt;/em&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Dapper&lt;/h3&gt;&lt;p&gt;Dapper has a single way of querying your database and it offers this via an extension method on the standard &lt;code&gt;SqlConnection&lt;/code&gt; class,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SqlConnection&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;SqlConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ConnectionString&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"SELECT * FROM Authors WHERE Id = @Id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="n"&gt;First&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see I've just opened a normal connection using a ConnectionString property I pulled from my &lt;code&gt;App.config&lt;/code&gt;'s ConnectionString config block.  The magic here lies in the &lt;code&gt;Query&amp;lt;Author&amp;gt;&lt;/code&gt; method.  This method accepts a parameterised &lt;span class="caps"&gt;SQL&lt;/span&gt; string and maps the result back into the object you specified.  Ultra simple, ultra clean.  There are a whole host of other arguments to the &lt;code&gt;Query&amp;lt;T&amp;gt;&lt;/code&gt; method but it would fill this post up pretty fast.&lt;/p&gt;&lt;p&gt;Selecting multiple object simply requires me to change the query to meet the new requirements,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dapperA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"SELECT * FROM Authors WHERE Username LIKE @PartialUsername"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;PartialUsername&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"%example.com"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One of the features I really liked apart from the simplicity of the whole thing was the parameterised &lt;span class="caps"&gt;SQL&lt;/span&gt;.  Many of the frameworks support this but most support it by index based assignment e.g. "&lt;span class="caps"&gt;WHERE&lt;/span&gt; Id=@0".  Thats fine but Dappers use of an anonymous object makes the whole thing that little bit nicer. No need to refactor strings should your arguments move around (it has happened) and it all reads just that tiny bit better as well.&lt;/p&gt;&lt;p&gt;Dapper also offers a non-generic &lt;code&gt;Query&lt;/code&gt; method that returns instances of &lt;code&gt;dynamic&lt;/code&gt; objects but I'm still to be convinced that this is useful when pushing these models into and back out of views etc.  Still it's nice to have for those times that creating another model would be overkill.&lt;/p&gt;&lt;h3&gt;Massive&lt;/h3&gt;&lt;p&gt;Massive is slightly different to the other microORMs in that it only deals with &lt;code&gt;dynamic&lt;/code&gt; objects from queries.  This means if you need concrete models you will have to map them either manually or use a tool to help (such as &lt;a href="http://automapper.codeplex.com/"&gt;AutoMapper&lt;/a&gt;).  Due to my schema (pluarl table names) I use Massive in a slightly different way to most people.  Massive requires your models to extend &lt;code&gt;Massive.DynamicModel&lt;/code&gt; so to keep things clean I tended to naturally create a model that represents my Authors table,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Authors&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Massive&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DynamicModel&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DefaultConnectionString"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;PrimaryKeyField&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Id"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This provides an entry point for configuring how Massive connects to your database.  The &lt;code&gt;DynamicModel&lt;/code&gt; has a constructor that allows us to specify a connection string name that Massive will lookup from the &lt;code&gt;Web/App.config&lt;/code&gt; &lt;code&gt;ConnectionStrings&lt;/code&gt; config section.  Alternatively if you don't supply this Massive will grab the first one it finds.  This class also allows us to specify the table name (in this case inferred from the class name) and properties such as the primary key of the table (as shown above).&lt;/p&gt;&lt;p&gt;Now we have our class defined we can start selecting from the database.  To select a single object we can use the &lt;code&gt;Single&lt;/code&gt; method.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Authors&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You'll notice I've made another step here and that is mapping the &lt;code&gt;dynamic&lt;/code&gt; result to a concrete &lt;code&gt;Author&lt;/code&gt; object.  It's a simple mapping method added to the &lt;code&gt;Authors&lt;/code&gt; object but could be made more generic fairly easiy,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="nf"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Author&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreatedDate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Selecting multiple objects can be done in 2 ways using &lt;code&gt;All&lt;/code&gt; which returns the entire table and then you filter or using the &lt;code&gt;Query&lt;/code&gt; method (preferred unless you really want to return the entire table for refdata or something I guess)&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;IList&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dyn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tbl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"SELECT * FROM Authors WHERE Username LIKE @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;"%example.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="update"&gt;&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;:&lt;/strong&gt; Rob, the creator of Massive, has just pushed &lt;a href="http://wekeroad.com/post/6618062904/changed-my-mind-massive-changed-to-embrace-c"&gt;a new type of query syntax&lt;/a&gt; into Massive.  The new approach makes use of named parameters&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Products&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FindBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;CategoryID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;UnitPrice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I haven't rewritten my stuff in this post but I want to close this post off and move on.  There is plenty more info in the &lt;a href="http://wekeroad.com/post/6618062904/changed-my-mind-massive-changed-to-embrace-c"&gt;original post&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;h3&gt;PetaPoco&lt;/h3&gt;&lt;p&gt;PetaPoco has a number of way of selecting single objects, or more specifically multiple ways of building/manipulating &lt;span class="caps"&gt;SQL&lt;/span&gt; queries.  But before we do that we need to get access to the database.  We do this via the &lt;code&gt;PetaPoco.Database&lt;/code&gt; object which is an &lt;code&gt;IDisposable&lt;/code&gt; wrapper around a Database connection that gives us the PetaPoco magic.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DefaultConnectionString"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// query goes here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="update"&gt;&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;:&lt;/strong&gt; &lt;a href="http://twitter.com/toptensoftware"&gt;Brad Robinson&lt;/a&gt; has informed me that the &lt;code&gt;using&lt;/code&gt; statement is unnecessary&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;You don't need to explicitly dispose the database object unless you've called OpenSharedConnection yourself. The idea is that if you're using an &lt;span class="caps"&gt;IOC&lt;/span&gt; container to instantiate per-http-context instances you can call OpenSharedConnection at the start of the request and a single connection will be used for the entire http request. In practice I've found this not really necessary as most providers do connection pooling anyway. In other words, generally the using clause is not necessary.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;p&gt;The Database constructor takes a name of a defined connection string to use for connecting to the database.  Now we are ready to start writing our query,&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Using Single(id)&lt;/strong&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Using Custom Parameterised &lt;span class="caps"&gt;SQL&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT * FROM Authors WHERE Id = @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="update"&gt;&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;:&lt;/strong&gt; &lt;a href="http://twitter.com/toptensoftware"&gt;Brad Robinson&lt;/a&gt; pointed out that it is also possible to used named parameters as discussed in the Dapper section above &lt;code&gt;@MyNamedParam&lt;/code&gt; instead of &lt;code&gt;@0&lt;/code&gt;.  The niceness just keeps coming with PetaPoco&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Using &lt;span class="caps"&gt;SQL&lt;/span&gt; Builder to Append Statements&lt;/strong&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT * FROM Authors"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"WHERE Id = @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Using &lt;span class="caps"&gt;SQL&lt;/span&gt; Builders Fluent Syntax to Append Statements&lt;/strong&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Builder&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"*"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;From&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Authors"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Id = @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;See what I mean?  Lots of ways.  Now I guess the 3rd and 4th methods will probably seem a bit &lt;acronym title="Over The Top"&gt;&lt;span class="caps"&gt;OTT&lt;/span&gt;&lt;/acronym&gt; for this simple select but think how powerful these would be if we had to conditionally build up our where clause or select statement based on a number of user defined conditions.  Nice.&lt;/p&gt;&lt;p&gt;Selecting multiple objects offers the same sort of variety except we can use either &lt;code&gt;Query&lt;/code&gt; or &lt;code&gt;Fetch&lt;/code&gt;.  The difference between the two is summed up by the PetaPoco site,&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;These are pretty much identical except &lt;code&gt;Fetch&lt;/code&gt; returns a List&amp;lt;&amp;gt; of POCO's whereas &lt;code&gt;Query&lt;/code&gt; uses &lt;code&gt;yield return&lt;/code&gt; to iterate over the results without loading the whole set into memory.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;So what I say about &lt;code&gt;Query&lt;/code&gt; applies to &lt;code&gt;Fetch&lt;/code&gt; as well.  Retrieving multiple objects is done in the same way as selecting a single object in that you can give the method parameterised &lt;span class="caps"&gt;SQL&lt;/span&gt; or use the &lt;span class="caps"&gt;SQL&lt;/span&gt; builder to construct the query.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;authors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"SELECT * FROM Authors WHERE Username LIKE @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;"%example.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So to cover this off a full select using PetaPoco will would look like this.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;PetaPoco&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DefaultConnectionString"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s"&gt;"SELECT * FROM Authors WHERE Id = @0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class="caps"&gt;IMO&lt;/span&gt; The syntax is nice, clean and flexible.  One thing about PetaPoco that is a &lt;span class="caps"&gt;VERY&lt;/span&gt; minor pain point for me is that it assumes singular name for tables which contradicted my earlier decision.  Not to worry PetaPoco uses attributes against the model class to manipulate the standard configuration and I can specify a class level attribute of &lt;code&gt;[PetaPoco.TableName("Authors")]&lt;/code&gt; to put things right with the world.  This also only applies to the queries that use object inference such as &lt;code&gt;Single&amp;lt;T&amp;gt;&lt;/code&gt; and not to the methods that accept &lt;span class="caps"&gt;SQL&lt;/span&gt;.&lt;/p&gt;&lt;h3&gt;ServiceStack ORMLite&lt;/h3&gt;&lt;p&gt;ORMLite from ServiceStack does things a wee bit differently to the rest.  It's a bit less self-contained because it also applies extension methods to the string object as well.  Also like PetaPoco it takes the singular naming convention for Object Name &amp;rarr; Table Name so first things first we need to annotate our &lt;code&gt;Author&lt;/code&gt; model with another attribtue&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[ServiceStack.DataAnnotations.Alias("Authors")]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another thing we need to do once before hand is set the dialect the ORMLite will use.  This is so it knows how to speak to the specific database type we created.  Typically you would put this in something like &lt;code&gt;Global.asax.cs&lt;/code&gt;, &lt;code&gt;Program.cs&lt;/code&gt; or whatever bootstrapper you care to use as it needs done only once.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;OrmLiteConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DialectProvider&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;SqlServerOrmLiteDialectProvider&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we can go ahead and start selecting things like a boss,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IDbConnection&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OpenDbConnection&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IDbCommand&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateCommand&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetById&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And for selecting many&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IDbConnection&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Program&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OpenDbConnection&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IDbCommand&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateCommand&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cmd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"Username LIKE {0}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="s"&gt;"%example.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Alternatively if we want to perform a specific operation on each result we can use the &lt;code&gt;Each&amp;lt;T&amp;gt;&lt;/code&gt; to iterate over the result set rather than simply return it.&lt;/p&gt;&lt;p&gt;As you can see you don't need to specify a full &lt;span class="caps"&gt;SQL&lt;/span&gt; query, just the &lt;span class="caps"&gt;WHERE&lt;/span&gt; clause which makes everything look a little bit neater and avoids repeating unnecessary portions of &lt;span class="caps"&gt;SQL&lt;/span&gt; that could be inferred.&lt;/p&gt;&lt;p&gt;ORMLite's syntax is &lt;em&gt;slighlty&lt;/em&gt; more complex - it feels a bit closer to the metal.  But it's certainly not horribly different that it would put me off using it.&lt;/p&gt;&lt;div class="update"&gt;&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;:&lt;/strong&gt; It seems there is another way to query the database that makes things a bit shorter.  Thanks to mythz (in the comments) for this one.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;removed by original author :(
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I agree that this is a viable alternative and can make for shorter code.  Thanks.&lt;/p&gt;&lt;/div&gt;&lt;h3&gt;Simple.Data&lt;/h3&gt;&lt;p&gt;Finally Simple.Data offers a very clean syntax that mixes dynamic with concrete results.  The first thing you need to do with Simple.Data is get a handle on the Database object that is returned as a special &lt;code&gt;dynamic&lt;/code&gt; object.  There are a number of ways of doing this,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OpenNamedConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DefaultConnectionString"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OpenConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Program&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConnectionString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By using &lt;code&gt;Database.Open()&lt;/code&gt; Simple.Data will look for a connection string called &lt;code&gt;Simple.Data.Properties.Settings.DefaultConnectionString&lt;/code&gt; and use it to create the database instance.  Alternatively you can specify a specific named connection using &lt;code&gt;Database.OpenNamedConnection&lt;/code&gt; or even a raw connection string using &lt;code&gt;Database.OpenConnection&lt;/code&gt;&lt;/p&gt;&lt;p&gt;These methods all return a special &lt;code&gt;dynamic&lt;/code&gt; object that dynamically creates a &lt;span class="caps"&gt;SQL&lt;/span&gt; statement by constructing methods on the fly.  It's best to look at the code for this.  To select a single object we can do this,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Author&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FindById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So whats happening here.  Well firstly we a asking it to look at the Authors table.  Next we are telling it to FindById - but this method doesn't exist. We could easily have said FindByUsernameAndEmail and we would get the results we expected.  This bit of metaprogramming is the magic behind Simple.Data where method signatures are parsed and used to build &lt;span class="caps"&gt;SQL&lt;/span&gt;.  Nice stuff.  I still have a bit of issue working with dynamic objects but in this case my models are concrete so it's really not an issue.&lt;/p&gt;&lt;p&gt;Selecting multiple objects is that same except instead of &lt;code&gt;FindByXXX&lt;/code&gt; we use &lt;code&gt;FindAllByXXX&lt;/code&gt; and as this query returns a &lt;code&gt;SimpleQuery&lt;/code&gt; object we then need to perform a &lt;code&gt;.Cast&amp;lt;Author&amp;gt;&lt;/code&gt; at the end to get back a list of authors.&lt;/p&gt;&lt;p&gt;An alternative and more flexible way of doing queries is using &lt;code&gt;Find&lt;/code&gt; and &lt;code&gt;FindAll&lt;/code&gt;.  These methods let you specify more complex queries.  To implement our &lt;code&gt;LIKE&lt;/code&gt; comparison for our query above we need to use &lt;code&gt;FindAll&lt;/code&gt; like so,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;dynamic&lt;/span&gt; &lt;span class="n"&gt;authors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Simple&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Authors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;authors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;authors&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FindAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;authors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Like&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%.com"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Cast&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This also shows the use of the &lt;code&gt;Cast&amp;lt;T&amp;gt;&lt;/code&gt; mentioned above.  We can supply a variable number of arguments to &lt;code&gt;Find&lt;/code&gt; and &lt;code&gt;FindAll&lt;/code&gt; as well as using different comparison operators.&lt;/p&gt;&lt;h2&gt;That Was a Long One&lt;sup class="footnote" id="fnr2"&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/h2&gt;&lt;p&gt;So yeah not only have I touched on selecting single and multiple object at a high level I also had to add a bit of discussion around setting things up so I apologise for the length of this post.  On the plus side discussing configuration alone for MyBatis and nHibernate would probably take the same length of time.  Thats whats great about these microORMs 99% of the time you don't need all that set up for most projects.  No need getting bogged down in configuration when it's not actually achieving anything.&lt;/p&gt;&lt;p&gt;In terms of the syntax - so far I am quite taken by both Simple.Data and PetaPoco but that could change when I start really pushing things - after all everyone can do selects quite easily.&lt;/p&gt;&lt;p&gt;In terms of dynamic results (e.g. from Massive) I'm still to be convinced on their usefulness without having to always map to concrete classes.  I don't see how this helps refactoring (it looks like it makes it much worse) nor do I see how it works with most validation mechanisms.  But I would love to be proven wrong on this please.&lt;/p&gt;&lt;p&gt;One thing I do know - it's going to take a lot more justification for someone to use a heavyweight &lt;span class="caps"&gt;ORM&lt;/span&gt; in my next project.  While I not willing to declare the death of ORMs like some other people I am certainly leaning in that direction.&lt;/p&gt;&lt;p&gt;Source for this series is slowly evolving in a &lt;a href="https://github.com/kouphax/microorm-comparison"&gt;Github repo&lt;/a&gt; if you feel inspired to have a poke around.&lt;/p&gt;&lt;p class="footnote" id="fn0"&gt;&lt;a href="#fnr0"&gt;&lt;sup&gt;0&lt;/sup&gt;&lt;/a&gt; We can argue about plural or singular data naming conventions some other time, or in the comments!!!&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; I created a load of test data using the handy little tool &lt;a href="http://autopoco.codeplex.com/"&gt;AutoPoco&lt;/a&gt;&lt;/p&gt;&lt;p class="footnote" id="fn2"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; "Thats what she said" &amp;lt;self:facepalm&amp;gt;&lt;/p&gt;</content></entry><entry><title>MicroORMs for .NET: (Obligatory) Introduction (Post)</title><updated>Tue Jun 14 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/14/microorms-for-net-obligatory-introduction-post/"></link><id>urn:yobriefca-se:feed:post:MicroORMs for .NET: (Obligatory) Introduction (Post)</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;blockquote class="announce"&gt;This post is the is part of series of posts covering the various microORMs in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world. I intend to look at how each microORM meets certain data access and manipulation needs. The current series includes,&lt;ul&gt; &lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/15/microorms-for-net-obligatory-introduction-post"&gt;Obligatory Introduction Post&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/16/microorms-for-net-syntax-selects"&gt;Syntax - SELECTs&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/18/microorms-for-net-stored-procedures"&gt;Stored Procedures&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="http://yobriefca.se/blog/2011/06/21/microorms-for-net-inserts-updates-and-delete"&gt;Inserts, Updates &amp;amp; Deletes&lt;/a&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;p&gt;I've been playing, somewhat superficially, with a few MicroORMs in .&lt;span class="caps"&gt;NET&lt;/span&gt; for a while now. While I have yet to use any in a real project I wanted to do a bit of comparison series. It's a bit selfish I guess as I am actually writing this series as a means to get myself up to speed on these frameworks as well as inform others about perils I faced as a newcomer to them as and when I hit those pain points.&lt;/p&gt;&lt;p&gt;To be honest I've never been a fan of the Data Access layer in projects - Databases are fine I guess and the application layer is all good but that thin layer between the two just frustrates me. It's often needlessly over complicated, confusing, hacked together or just plain broken. Most of my experience is with &lt;a href="http://www.mybatis.org/dotnet.html"&gt;iBatis/MyBatis&lt;/a&gt; and it "does the job" but its very fiddly and &lt;span class="caps"&gt;XML&lt;/span&gt; configuration becomes cumbersome for simple tasks. I've also tinkered with other things like Entity Framework and NHibernate but more often than not they just don't feel right.&lt;/p&gt;&lt;p&gt;This make microORMs sound appealing to me for the very same reasons. They give you just enough to get the job done, prefer convention over configuration and generally allow you to get right down to the metal and tweak things using &lt;span class="caps"&gt;SQL&lt;/span&gt; - easily the best &lt;span class="caps"&gt;DSL&lt;/span&gt; for working with databases. Sure they may lack many features but typically these are features you wouldn't really miss if taken away from you.&lt;/p&gt;&lt;h2&gt;The MicroORMs&lt;/h2&gt;&lt;p&gt;There are a number of ORMs currently available for .&lt;span class="caps"&gt;NET&lt;/span&gt; and I am going to look at a few of them in terms of features and syntax.&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;h3&gt;&lt;a href="http://code.google.com/p/dapper-dot-net/"&gt;Dapper&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Dapper was created by Sam Saffron from &lt;a href="http://stackoverflow.com"&gt;Stackoverflow&lt;/a&gt;. Rather than a library Dapper is a single file that you can drop into your project and applies some syntactic sugar to extend your IDbConnection interface.&lt;/p&gt;&lt;h3&gt;&lt;a href="https://github.com/robconery/massive"&gt;Massive&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Like Dapper, Massive is a single file rather than a library and offers a very similar feature set to Dapper except it works exclusively with the .NET4 &lt;code&gt;dynamic&lt;/code&gt; stuff.&lt;/p&gt;&lt;h3&gt;&lt;a href="http://www.toptensoftware.com/petapoco/"&gt;PetaPoco&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;PetaPoco came out of inspiration from Massive and has extended to support real objects as well as some very nice extra features such as T4 templating, configuration via model attributes and other nice stuff.&lt;/p&gt;&lt;h3&gt;&lt;a href="https://github.com/markrendle/Simple.Data"&gt;Simple.Data&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Simple.Data is A lightweight, dynamic data access component that make use of convention and &lt;code&gt;dynamic&lt;/code&gt; to allow users to access database tables via dynamically generated methods.&lt;/p&gt;&lt;h3&gt;&lt;a href="https://github.com/ServiceStack/ServiceStack.OrmLite"&gt;ORMLite&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;ServiceStack.OrmLite is a convention-based, configuration-free lightweight &lt;span class="caps"&gt;ORM&lt;/span&gt; that uses standard &lt;span class="caps"&gt;POCO&lt;/span&gt; classes and Data Annotation attributes to infer its table schema.&lt;/p&gt;&lt;h2&gt;On with the show&lt;/h2&gt;&lt;p&gt;So thats my toys all laid out and ready to be played with. Next step is to look at some of the typical operations for selecting and manipulating data from our database. I hope to have the first real part of this investigation up shortly (or before the decade is out at least).&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; This list is not exhaustive there are a few specialised ones around as well which I may touch on later in the series.&lt;/p&gt;</content></entry><entry><title>Baler v0.2.1 Released and 2 New Extensions</title><updated>Wed Jun 08 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/08/baler-v0-dot-2-1-released-and-2-new-extensions/"></link><id>urn:yobriefca-se:feed:post:Baler v0.2.1 Released and 2 New Extensions</id><content type="html">
&lt;h1&gt;Baler&lt;/h1&gt;&lt;p&gt;I have just released a new version of &lt;a href="http://yobriefca.se/projects/baler"&gt;Baler&lt;/a&gt;.  Version 0.2.1 release adds a new feature for adding custom attributes to bales.  The &lt;code&gt;Attr&lt;/code&gt; function, available as an instance method to all bales, accepts an attribute name and a string value that will be applied to the rendered bale tag.  The best way to demonstrate this is with an example.&lt;/p&gt;&lt;h2&gt;&lt;span class="caps"&gt;CSS&lt;/span&gt; Media Attribute&lt;/h2&gt;&lt;p&gt;When specifying link tags for &lt;span class="caps"&gt;CSS&lt;/span&gt; it is possible to scope that stylesheet to a specific meida type.  For example you could have a stylesheet that modifies your page style for printing e.g.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;link&lt;/span&gt; &lt;span class="nt"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"stylesheet"&lt;/span&gt; &lt;span class="nt"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"core.css"&lt;/span&gt; &lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;media&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"screen"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; 
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;link&lt;/span&gt; &lt;span class="nt"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"stylesheet"&lt;/span&gt; &lt;span class="nt"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"print.css"&lt;/span&gt; &lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;media&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"print"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;The new &lt;code&gt;Attr&lt;/code&gt; method allows you to specify this new attribute like so,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Baler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"core.css"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"media"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"screen"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;AsCss&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Baler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"print.css"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;Attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"media"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"print"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;AsCss&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;"No Value" Attributes&lt;/h2&gt;&lt;p&gt;Some attributes, such as the &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; on script tags do not require a value.  &lt;code&gt;Attr&lt;/code&gt; is capable of dealing with these type of attributes.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;Baler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script.js"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;Attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"async"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;AsJs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Will produce&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"..."&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I am sure there are other uses for it (setting id's or data-* attributes) as it's a fairly flexible little enhancement.&lt;/p&gt;&lt;h1&gt;&lt;span class="caps"&gt;CSS&lt;/span&gt; Media Extensions&lt;/h1&gt;&lt;pre&gt;&lt;code&gt;PM&amp;gt; Install-Package CodeSlice.Web.Baler.Extensions.CssMedia&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The first extension to utilise this new &lt;code&gt;Attr&lt;/code&gt; is the &lt;code&gt;CssMediaExtensions&lt;/code&gt; addon. &lt;code&gt;CssMediaExtensions&lt;/code&gt; provide methods for setting the media type that a stylesheet should render for such as screen or print&lt;/p&gt;&lt;h2&gt;WithMedia()&lt;/h2&gt;&lt;p&gt;The WithMedia Extension allows you to set the media attribute of the rendered &lt;span class="caps"&gt;CSS&lt;/span&gt; tag.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;bale&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(...).&lt;/span&gt;&lt;span class="n"&gt;WithMedia&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"screen"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;AsCss&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;AsCss()&lt;/h2&gt;&lt;p&gt;&lt;code&gt;AsCss&lt;/code&gt; overrides the existing &lt;code&gt;AsCss&lt;/code&gt; method allowing you to pass a media attribute value directly into the render call. For example the above example could simply be written as,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;bale&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(...).&lt;/span&gt;&lt;span class="n"&gt;AsCss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"screen"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h1&gt;Named Bales Extension&lt;/h1&gt;&lt;pre&gt;&lt;code&gt;PM&amp;gt; Install-Package CodeSlice.Web.Baler.Extensions.NamedBales&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This one has been released for a while but I didn't get around to writing about it. TheNamed Bales extension provides a mechanism to declare a Bale definition up front and reuse the same bale throughout the application without having to redefine the contents over and over. For example we can define a bale with 3 scripts and name it&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Baler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"script1.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="s"&gt;"script2.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="s"&gt;"script3.js"&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;NameAs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"base"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This can returns the Bale itself so can be used inline or somewhere like &lt;code&gt;Global.asax&lt;/code&gt;. Next we can render this bundle using &lt;code&gt;AsJs&lt;/code&gt; later in the app like so,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;NamedBales&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"base"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;AsJs&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Currently this doesn?t short circuit Balers internal cache so a second cache check will be made using the internal hashing mechanism. This is either a good thing or a bad thing. But for now it?s a good thing! This does have the side effect of ensuring that any unnamed bales with the same signature as a named bale will still get cached bale.&lt;/p&gt;&lt;h3&gt;NameAs()&lt;/h3&gt;&lt;p&gt;NameAs adds some sugar to the IBale interface allowing us to apply a friendly name to a bale. There is currently no check to see if the bale name is already taken. Existing definitions will be overwritten.&lt;/p&gt;&lt;h3&gt;NamedBales.Get()&lt;/h3&gt;&lt;p&gt;Allows us to retrieve a bale based on a friendly name defined by the developer. Will throw and exception if the bale name doesn?t exist in the cache ? otherwise the bale itself.&lt;/p&gt;&lt;h1&gt;Coming Up&lt;/h1&gt;&lt;p&gt;Still plenty to be done.  Docs are fairly complete but there are a mountain of unit tests to be written still and some extra features to be added (without bloating the code base of course).  I have a few features I'd like to work in but I'll keep that for another day.&lt;/p&gt;</content></entry><entry><title>Baler Site now Live(ish)</title><updated>Sun Jun 05 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/06/05/baler-site-now-live-ish/"></link><id>urn:yobriefca-se:feed:post:Baler Site now Live(ish)</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;I've just pushed the first draft of the new shiny Baler web site into the various tubes that form the basis of the interwebs. I am still missing a logo but why let that hold me back? Anyway feel free to check it out while I add more tests to the project and generally polish things.&lt;/p&gt;&lt;p&gt;&lt;a href="http://yobriefca.se/projects/baler"&gt;Baler Project Site&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>Announcing Baler</title><updated>Mon May 30 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/30/announcing-baler/"></link><id>urn:yobriefca-se:feed:post:Announcing Baler</id><content type="html">
&lt;p&gt;It's very early days (with regards to both code and documentation) but I though I amy as well put it out there.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;a href="http://yobriefca.se/projects/baler/"&gt;Baler&lt;/a&gt; is a .&lt;span class="caps"&gt;NET&lt;/span&gt; Web Resource Bundler. Capable of concatenating and transforming &lt;span class="caps"&gt;CSS&lt;/span&gt; and JavaScript files to lower request count and bandwidth requirements. It works with any .&lt;span class="caps"&gt;NET&lt;/span&gt; Web Framework/View Engine capable of running C# in the view&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;What differentiates &lt;a href="http://yobriefca.se/projects/baler/"&gt;Baler&lt;/a&gt; from other resource bundlers out there is that it aims to provided a bare bones, minimum feature set for building bales (or resource bundle).  The core &lt;a href="http://nuget.org/List/Packages/CodeSlice.Web.Baler"&gt;Baler package&lt;/a&gt; offers the ability to concatenate, render and cache JavaScript and &lt;span class="caps"&gt;CSS&lt;/span&gt; bales.  Thats it.  No minification nor fancy processing nothing.  However &lt;a href="http://yobriefca.se/projects/baler/"&gt;Baler&lt;/a&gt; does have 2 main extensibility hooks though (&lt;code&gt;Before&lt;/code&gt; and &lt;code&gt;After&lt;/code&gt;) which can be leveraged to control how bundles are manipulated.  There are currently 3 extensions for Baler but more are on their way,&lt;/p&gt;&lt;ol&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/CodeSlice.Web.Baler.Extensions.CoffeeScript"&gt;CoffeeScript&lt;/a&gt; - Transforms &lt;a href="http://coffeescript.org"&gt;CoffeeScript&lt;/a&gt; files into JavaScript&lt;/li&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/CodeSlice.Web.Baler.Extensions.Less"&gt;.&lt;span class="caps"&gt;LESS&lt;/span&gt;&lt;/a&gt; - Transforms &lt;a href="http://www.dotlesscss.org/"&gt;.&lt;span class="caps"&gt;LESS&lt;/span&gt;&lt;/a&gt; files into &lt;span class="caps"&gt;CSS&lt;/span&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/CodeSlice.Web.Baler.Extensions.AjaxMinifier"&gt;Ajax Minifier&lt;/a&gt; - Allows for minification of JavaScript and &lt;span class="caps"&gt;CSS&lt;/span&gt; using Microsofts Ajax Minifier&lt;/li&gt;&lt;/ol&gt;&lt;h2&gt;Quick Example&lt;/h2&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CodeSlice.Web.Test.Default" %&amp;gt;
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Import Namespace="CodeSlice.Web.Baler" %&amp;gt;

&lt;span class="cp"&gt;&amp;lt;!DOCTYPE HTML /&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=
      Baler.Build(
        "~/scripts/script1.js",
        "~/scripts/script2.js",
        "~/scripts/script3.js",
      ).AsJs()
    %&amp;gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This example will take 3 source files concatenate them into a random script file and output the necessary &lt;code&gt;&amp;lt;script src='1jdk2ds.js' type='text/javascript'&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;.  Subsequent calls that match this bales signature (the order and contents of the bale) will use the cached script tag and not perform that concatenation again.&lt;/p&gt;&lt;p&gt;As I say it's early days.  I really need to get the docs up to snuff, write plenty of test and add a few more features before I start shouting from the rooftops but at least it's out now and there is no going back :-).&lt;/p&gt;&lt;p&gt;Baler v0.1 is available on &lt;a href="http://nuget.org/List/Packages/CodeSlice.Web.Baler"&gt;Nuget&lt;/a&gt; and &lt;a href="https://github.com/kouphax/baler.git"&gt;Github&lt;/a&gt; and the annotated source code is available &lt;a href="http://kouphax.github.com/baler/CodeSlice.Web.Baler/CodeSlice.Web.Baler/Baler.html" title="Core"&gt;here&lt;/a&gt;, &lt;a href="http://kouphax.github.com/baler/CodeSlice.Web.Baler/CodeSlice.Web.Baler.Extensions.AjaxMinifier/MinificationExtensions.html" title="AjaxMin Extensions"&gt;here&lt;/a&gt;, &lt;a href="http://kouphax.github.com/baler/CodeSlice.Web.Baler/CodeSlice.Web.Baler.Extensions.CoffeeScript/CoffeeScriptExtensions.html" title="CoffeeScript Extensions"&gt;here&lt;/a&gt; and &lt;a href="http://kouphax.github.com/baler/CodeSlice.Web.Baler/CodeSlice.Web.Baler.Extensions.Less/LessExtensions.html" title=".LESS Extensions"&gt;here&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>Turns Out I'm Not Doing my Job Very Well aka Sucks To Be Me</title><updated>Wed May 25 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/25/turns-out-im-not-doing-my-job-very-well-aka-sucks-to-be-me/"></link><id>urn:yobriefca-se:feed:post:Turns Out I'm Not Doing my Job Very Well aka Sucks To Be Me</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;Soooooo anyway I had the day off today and you know what they say - the devil makes work for idle hands. And boy did that devil dude really go to town on me. Earlier this morning I came across a tweet. The sort of tweet that I like. A highly polarising tweet that makes geeks stand up and cheer and makes everyone else feel like we are laughing at them. Anyway heres the tweet,&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;cite&gt;"pragmaticpat: adults are "pro homework" for children, yet frequently do not study their craft outside of m-f, 9-5 #leadbyexample ?"&lt;/cite&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;So anyway I thought, as I was waiting in the car for the wife to finishing shopping or whatever she was doing at the time, I would throw this tweet up to our &lt;a href="http://yammer.com"&gt;Yammer&lt;/a&gt; community and stir up a bit of debate. Things got a bit heated, more than even I would have liked. In fact I think I offended people&lt;sup class="footnote" id="fnr1"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&lt;p&gt;Lets say just I am beyond passionate about my job almost obsessively so in some respects. Being that type of person I often expect everyone else to be just like me and therefore measure people against what I do. I also tend to make statements as if there were 2 types of people in the world. People like me, who go home and do a bit of extra work/research and people who do their 9-5 allocated work and don't care about anything. But hey there's a twist here - I'm not actually stupid - I know the world isn't black and white, us and them, all or nothing. I'm also not as horribly snobby or elitist as I make out (I am a bit but thats just the nature of a technology fanboy - defensive about their passions - suck it).&lt;/p&gt;&lt;p&gt;Anyways enough of the woe is me schtick, it's boring. I just want to say that I understand there are people out there who do their 9-5 and go home and actually have a life beyond the keyboard. I was never implying people need to work 24/7 or they are coasting. I realise I have a bit of a reputation of being an elitist uber-geek and therefore many people may have assumed that was what I was getting at. However I know that in the 9-5 block a lot of people are contributing more that just their time to a single project and are going out of their way to do something decent rather than mediocre. I'm happy for someone to say &lt;cite&gt;"I'm happy with were I am"&lt;/cite&gt; - hell I am sure they've worked hard to get were they are so why not? What I'm not happy with is someone who &lt;strong&gt;expect&lt;/strong&gt; the same kudos as people who go above and beyond just because they show their face everyday. When I retweeted that tweet and even went as far as yamming it I was stating an opinion, an opinion that wasn't directed at any particular people or any particular company for that matter, it was just a blanket statement.&lt;/p&gt;&lt;p&gt;On a similar note I've had the pleasure of seeing plenty of CV's over the last few years as well as interviewing people and I must say the industry has it's fair share of people who are guru's on paper but ask them a question about something that they haven't encountered due to work on a project and they are flummoxed. It's a sad state of affairs to get into that shape. It's a bit like a morbidly obese fitness instructor - they can do their job but neglected to look after themselves - who would want to hire that sort of person? Not many people I'd bet.&lt;/p&gt;&lt;p&gt;So yeah I accept that there are many different types of people. People who want to better the industry as well as themselves. People who want to better themselves and the company they work for. People who just want to better themselves. People who just want to do a super job with the role they are given and finally people who just want to get by&lt;sup class="footnote" id="fnr2"&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt;. But I refuse to accept that everyone should be treated equally in all respects and as long as people can respect that then we can all get along.&lt;/p&gt;&lt;p&gt;So whats the point of all this? I know I have high standards but I also need to learn that not everyone wants the same thing out of life. Or more to the point that other people actually &lt;span class="caps"&gt;HAVE&lt;/span&gt; a life outside of IT. In the past I've had shed loads of opportunity to shine, but I've also been on some long boring pressured projects so I need to realise other people are going through that too. I need to be a bit smarter about this. It's even part of my job description to mentor others so I suck at my job (hence the title). Perhaps I should pull my finger out and ensure people are given opportunities to shine? Yeah thats sounds about right….&lt;/p&gt;&lt;p class="footnote" id="fn1"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; For what it's worth I wasn't asked, request or ordered to write this. As always I am doing this for my own reasons.&lt;/p&gt;&lt;p class="footnote" id="fn2"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; I'm sure you could break it down more.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET - Nancy - Hello World</title><updated>Tue May 24 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/micro-web-frameworks-in-net-nancy-hello-world/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET - Nancy - Hello World</id><content type="html">&lt;p&gt;Part of my Micro Web Frameworks in .NET series. This time looking at Hello World in Nancy &lt;a href="http://nancyfx.org"&gt;nancyfx.org&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET - Jessica - Views</title><updated>Mon May 23 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/micro-web-frameworks-in-net-jessica-views/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET - Jessica - Views</id><content type="html">&lt;p&gt;The fourth part of a series covering the various micro web frameworks in .NET. This part shows you how to serve dynamic views with Jessica &lt;a href="http://jessicafx.org"&gt;http://jessicafx.org&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET 101: Nancy</title><updated>Sun May 22 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/22/micro-web-frameworks-in-net-101-nancy/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET 101: Nancy</id><content type="html">
&lt;p&gt;&lt;a href="htttp://nancyfx.org"&gt;Nancy&lt;/a&gt; is a different beast to &lt;a href="http://jessicafx.org"&gt;Jessica&lt;/a&gt; while on the surface it may appear like a micro web framework, once you scratch the surface it becomes debatable because there is just some much more than a nice &lt;span class="caps"&gt;DSL&lt;/span&gt; for creating lightweight web apps.  In fact this was &lt;a href="http://tombell.org.uk/blog/2011/04/10/why-did-i-create-jessica/"&gt;one of the reasons&lt;/a&gt; Jessica was created,&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Nancy has taken an approach to making many parts of the framework replaceable; if you don?t like the functionality of X you can create your own, and have Nancy use that instead. While this is a respectable goal for a web framework, the focus for Jessica has been to stay as simple and as close to Sinatra as possible. &lt;em&gt;Tom Bell.  Why Did I Create Jessica?&lt;/em&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Anyway we will touch on these aspects a bit later.  Lets get cracking.&lt;/p&gt;&lt;h2&gt;Getting Started - Hello World&lt;/h2&gt;&lt;p&gt;Getting &lt;a href="htttp://nancyfx.org"&gt;Nancy&lt;/a&gt; up and running is simple enough.  Starting with an empty &lt;span class="caps"&gt;ASP&lt;/span&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt; web project&lt;sup id="fnr1" class="footnote"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt; we can use &lt;a href="http://nuget.org/"&gt;NuGet&lt;/a&gt; to install Nancy&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Install&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Package&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Also because we are hosting this in an &lt;span class="caps"&gt;ASP&lt;/span&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt; environment (more on this later) we need to install the appropriate hosting package&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt; &lt;span class="n"&gt;Nancy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hosting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Aspnet&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Next step requires us to add some stuff to the &lt;code&gt;web.config&lt;/code&gt;.  If you've been smart and installed the hosting package from NuGet this steps is done for you but ultimately your minimal &lt;code&gt;web.config&lt;/code&gt; should look like this.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;system.web&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;compilation&lt;/span&gt; &lt;span class="na"&gt;debug=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;targetFramework=&lt;/span&gt;&lt;span class="s"&gt;"4.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;httpHandlers&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;add&lt;/span&gt; &lt;span class="na"&gt;verb=&lt;/span&gt;&lt;span class="s"&gt;"*"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"Nancy.Hosting.Aspnet.NancyHttpRequestHandler"&lt;/span&gt; &lt;span class="na"&gt;path=&lt;/span&gt;&lt;span class="s"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/httpHandlers&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/system.web&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;system.webServer&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;validation&lt;/span&gt; &lt;span class="na"&gt;validateIntegratedModeConfiguration=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;handlers&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;add&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Nancy"&lt;/span&gt; &lt;span class="na"&gt;verb=&lt;/span&gt;&lt;span class="s"&gt;"*"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"Nancy.Hosting.Aspnet.NancyHttpRequestHandler"&lt;/span&gt; &lt;span class="na"&gt;path=&lt;/span&gt;&lt;span class="s"&gt;"*"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/handlers&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/system.webServer&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The final step is to create the hello world module that will handle your route.  Again, like Jessica, it doesn't matter where the module classes are placed but for neatness I just like to stick them in a &lt;code&gt;Modules&lt;/code&gt; folder.  Our module will look very similar to the one we defined for Jessica.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorldModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NancyModule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;HelloWorldModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And we're done.  Fire the app up and behold the "Hello World"-yness of it all.&lt;/p&gt;&lt;h2&gt;Other Micro Framework Related Stuff&lt;/h2&gt;&lt;p&gt;&lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; has some features that aren't technically related to the micro framework aspect  - specifically hosting but I'll touch on that later.  &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; also has a lot of nice features that are related to the micro framework aspects so lets take a bit of a whirlwind tour around the main areas first.&lt;/p&gt;&lt;h3&gt;Routes&lt;/h3&gt;&lt;p&gt;Routes in &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; are pretty powerful.  Obviously you get the declare static routes (as demonstrated above) but dynamic routes (routes with variables for example) are also supported in a few of ways.  First of all you have named segments.  These can be expressed in 2 ways&lt;/p&gt;&lt;ol&gt;	&lt;li&gt;Sinatra style variables &lt;code&gt;/user/:id&lt;/code&gt;, or,&lt;/li&gt;	&lt;li&gt;C#-esque &lt;code&gt;String.format&lt;/code&gt; style &lt;code&gt;/user/{id}&lt;/code&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Declaring a route like this will cause the variable section to be pushed into a named variable within the dynamic object passedin into the route action. &lt;code&gt;/user/jameshu&lt;/code&gt; matches &lt;code&gt;/user/:id&lt;/code&gt; and the dynamic object passed into the action will have &lt;code&gt;x.id == "jameshu"&lt;/code&gt;&lt;/p&gt;&lt;p&gt;The other means of dynamic route matching is the use of regular expressions and named capture groups (or backreferences as they are sometimes known).  Using regular expressions as route parameters allows us to offer more fine grained route matching.  Take the following route as an example,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;@"/users/(?&amp;lt;id&amp;gt;[\d]+)"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;GetUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This route will &lt;span class="caps"&gt;ONLY&lt;/span&gt; match routes whose variable section matches the expression, in this case a numeric value.  So not only have we specified a route variable but we are also limiting access to that action to "valid" values.  This means we could also offer another route that accepts only alphabetical characters.  Powerful enough.&lt;/p&gt;&lt;h3&gt;Route Conditions&lt;/h3&gt;&lt;p&gt;Taking routes another step further &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; also offers optional conditions that can be used to determine if a route should be executed for a given request.  Lets use this heavily contrived example to demonstrate this feature,&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"/user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"secrets"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;  &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So this route will match any route starting with &lt;code&gt;/user&lt;/code&gt; however the condition (the second argument of the definition) will ensure that the route action will only get executed if the query string is populated with an argument called password with a value of secrets i.e &lt;code&gt;http://myserver/user?password=secrets&lt;/code&gt; will match successfully.&lt;/p&gt;&lt;h3&gt;Views&lt;/h3&gt;&lt;p&gt;&lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; supports a decent range of view engines&lt;sup id="fnr2" class="footnote"&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;Razor&lt;/li&gt;	&lt;li&gt;Spark&lt;/li&gt;	&lt;li&gt;NDjango and, obviously,&lt;/li&gt;	&lt;li&gt;Static files&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Serving up dynamic vies is fairly simple&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HelloWorldModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;NancyModule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;HelloWorldModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"index.cshtml"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We basically use the View object to specify the view and optionally pass in a view model as well.  By default Nancy will look in the &lt;code&gt;~/Views&lt;/code&gt; folder for the views but you can also specify a full path if needs be.  The view is then matched against the available view engines and rendered as you would expect.  This is the Razor view specified above.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Hello World&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;@Model&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Responses&lt;/h3&gt;&lt;p&gt;Nancy is also capable of returning static content as different responses (similar to how Jessica does it).  Each route action is expected to return a Nancy Response object. The object itself has a number of convenience methods for serving static files and &lt;acronym title="JavaScript Object Notation"&gt;&lt;span class="caps"&gt;JSON&lt;/span&gt;&lt;/acronym&gt; such as &lt;code&gt;Response.AsJs&lt;/code&gt;, &lt;code&gt;Response.AsJson&lt;/code&gt;, &lt;code&gt;Response.AsXml&lt;/code&gt;, , &lt;code&gt;Response.AsRedirect&lt;/code&gt; etc.&lt;/p&gt;&lt;h2&gt;Hosting&lt;/h2&gt;&lt;p&gt;This is were things move away from "micro framework" world a bit.  Nancy itself abstracts out the hosting environment making the entire framework portable allowing you to host in any number of places including (but not limited to),&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;Self hosted (e.g. embedded within an executable)&lt;/li&gt;	&lt;li&gt;&lt;span class="caps"&gt;ASP&lt;/span&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt;/&lt;span class="caps"&gt;IIS&lt;/span&gt; (as demonstrated above)&lt;/li&gt;	&lt;li&gt;&lt;span class="caps"&gt;WCF&lt;/span&gt; (yep thats right), and,&lt;/li&gt;	&lt;li&gt;Some partial &lt;a href="http://bvanderveen.com/a/dotnet-http-abstractions"&gt;&lt;span class="caps"&gt;OWIN&lt;/span&gt;&lt;/a&gt; support&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This, to me at least, is very interesting.  Being able to build an executable that accepts &lt;span class="caps"&gt;HTTP&lt;/span&gt; requests could be pretty powerful in the right hands and the &lt;span class="caps"&gt;WCF&lt;/span&gt; stuff sounds very interesting (is this similar to what the new &lt;span class="caps"&gt;WCF&lt;/span&gt; Web &lt;span class="caps"&gt;API&lt;/span&gt; is attempting to offer?).  I need to play some more in this area.  I'll report back when I am done.&lt;/p&gt;&lt;h2&gt;Summing Up&lt;/h2&gt;&lt;p&gt;So that was a quick round up of the key features of Nancy and I think it's another one worth looking at.  So hopefully you can also see how it differs from Jessica.  From a code perspective they seem quite similar (though Nancy has possibly a few more features) but the whole hosting abstraction layer is very interesting.  I have had people ask me if this introduced any new (or even old) security concerns and to be honest I don't know but it might certainly be worth looking out for.&lt;/p&gt;&lt;p&gt;As always I may need corrections so please fire away.  Hopefully I can cover off these points with a few more screencasts over the next week.  Stay tuned.&lt;/p&gt;&lt;p id="fn1" class="footnote"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; In my &lt;a href="http://jessicafx.org"&gt;Jessica&lt;/a&gt; I laid out steps to strip the normal &lt;span class="caps"&gt;ASP&lt;/span&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt; Web Project template out of all the unnecessary bits.  Lord knows why I didn't just create an empty web project &amp;lt;facepalm/&amp;gt;&lt;/p&gt;&lt;p id="fn2" class="footnote"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; Apparently it also has a built in view engine called the Super Simple View Engine but as of yet I can't seem to get it to work.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET Screencast: Nancy #1 - Hello World</title><updated>Sun May 22 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/22/micro-web-frameworks-in-net-screencast-nancy-number-1-hello-world/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET Screencast: Nancy #1 - Hello World</id><content type="html">&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/24114023?byline=0&amp;amp;portrait=0" width="601" height="428" frameborder="0"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://vimeo.com/24114023"&gt;Nancy #1 - Hello World&lt;/a&gt; from &lt;a href="http://vimeo.com/user7151248"&gt;James Hughes&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Part of my Micro Web Frameworks in .&lt;span class="caps"&gt;NET&lt;/span&gt; series.  This time looking at Hello World in Nancy (http://nancyfx.org)&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET - Jessica - Configuration</title><updated>Sun May 22 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/micro-web-frameworks-in-net-jessica-configuration/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET - Jessica - Configuration</id><content type="html">&lt;p&gt;The third part of a series covering the various micro web frameworks in .NET. This part shows you how to configure Jessica &lt;a href="http://jessicafx.org"&gt;http://jessicafx.org&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET - Jessica - Static Files</title><updated>Sat May 21 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/micro-web-frameworks-in-net-jessica-static-files/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET - Jessica - Static Files</id><content type="html">&lt;p&gt;The second part of a series covering the various micro web frameworks in .NET. This part shows you how to serve static files through Jessica &lt;a href="http://jessicafx.org"&gt;http://jessicafx.org&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET - Jessica - Hello World</title><updated>Fri May 20 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/screencasts/micro-web-frameworks-in-net-jessica-hello-world/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET - Jessica - Hello World</id><content type="html">&lt;p&gt;The first part of a series covering the various micro web frameworks in .NET. This part shows you how to implement Hello World using Jessica &lt;a href="http://jessicafx.org"&gt;http://jessicafx.org&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Screencasts Galore!  More or less.</title><updated>Thu May 19 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/19/screencasts-galore-more-or-less/"></link><id>urn:yobriefca-se:feed:post:Screencasts Galore!  More or less.</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span class="caps"&gt;SCREEEEEEEEEEEEEEEEEEEEEEENCASTS&lt;/span&gt; &lt;span class="caps"&gt;GALORE&lt;/span&gt;!&lt;/p&gt;&lt;p&gt;So I unveiled the new &lt;a href="/screencasts"&gt;screencasts&lt;/a&gt; section of the site. Can you guess whats in it? Those who said "screencasts" you can have a nice non-patronising pat on the head. The rest of you…. for shame. Tut tut.&lt;/p&gt;&lt;p&gt;Anyways I wanted to start a series of short &lt;a href="/screencasts"&gt;screencasts&lt;/a&gt; that will provide a practical view of the .&lt;span class="caps"&gt;NET&lt;/span&gt; Micro Web Framework series. So far I've managed to cover the basics of &lt;a href="http://jessicafx.org"&gt;Jessica&lt;/a&gt; but there is more to come.&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET Screencast: Jessica #4 - Views</title><updated>Thu May 19 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/19/micro-web-frameworks-in-net-screencast-jessica-number-4-views/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET Screencast: Jessica #4 - Views</id><content type="html">&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/24014537?byline=0&amp;amp;portrait=0" width="601" height="428" frameborder="0"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://vimeo.com/24014537"&gt;Jessica #4 - Views&lt;/a&gt; from &lt;a href="http://vimeo.com/user7151248"&gt;James Hughes&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The four part of a series covering the various micro web frameworks in .&lt;span class="caps"&gt;NET&lt;/span&gt;.  This part shows you how to serve dynamic views with Jessica (http://jessicafx.org).&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET Screencast: Jessica #3 - Configuration</title><updated>Thu May 19 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/19/micro-web-frameworks-in-net-screencast-jessica-number-3-configuration/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET Screencast: Jessica #3 - Configuration</id><content type="html">&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/24014440?byline=0&amp;amp;portrait=0" width="601" height="428" frameborder="0"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://vimeo.com/24014440"&gt;Jessica #3 - Configuration&lt;/a&gt; from &lt;a href="http://vimeo.com/user7151248"&gt;James Hughes&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The third part of a series covering the various micro web frameworks in .&lt;span class="caps"&gt;NET&lt;/span&gt;.  This part shows you how to configure  Jessica (http://jessicafx.org).&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET Screencast: Jessica #2 - Static Content</title><updated>Thu May 19 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/19/micro-web-frameworks-in-net-screencast-jessica-number-2-static-content/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET Screencast: Jessica #2 - Static Content</id><content type="html">&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/24014389?byline=0&amp;amp;portrait=0" width="601" height="428" frameborder="0"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://vimeo.com/24014389"&gt;Jessica #2 - Static Content&lt;/a&gt; from &lt;a href="http://vimeo.com/user7151248"&gt;James Hughes&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The second part of a series covering the various micro web frameworks in .&lt;span class="caps"&gt;NET&lt;/span&gt;.  This part shows you how to serve static files through Jessica (http://jessicafx.org).&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET Screencast - Jessica #1 - Hello World</title><updated>Thu May 19 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/19/micro-web-frameworks-in-net-screencast-jessica-number-1-hello-world/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET Screencast - Jessica #1 - Hello World</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/24014199?byline=0&amp;amp;portrait=0" width="601" height="428" frameborder="0"&gt;&lt;/iframe&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt; &lt;a href="http://vimeo.com/24014199"&gt;Jessica #1 - Hello World&lt;/a&gt; from &lt;a href="http://vimeo.com/user7151248"&gt;James Hughes&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;The first part of a series covering the various micro web frameworks in .&lt;span class="caps"&gt;NET&lt;/span&gt;. This part shows you how to implement Hello World using Jessica (&lt;a href="http://jessicafx.org)."&gt;http://jessicafx.org).&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>Micro Web Frameworks in .NET 101: Jessica</title><updated>Wed May 18 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/18/micro-web-frameworks-in-net-101-jessica/"></link><id>urn:yobriefca-se:feed:post:Micro Web Frameworks in .NET 101: Jessica</id><content type="html">
&lt;p&gt;Now I have got the &lt;a href=":/blog/2011/05/18/why-would-you-use-micro-web-frameworks-in-net"&gt;opinion piece&lt;/a&gt; out of the way I can start the bit I like - dissecting the various technologies that could be classed as &lt;em&gt;micro web frameworks&lt;/em&gt;.  First on the table is &lt;a href=":http://jessicafx.org"&gt;Jessica&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Getting Started - Hello World&lt;/h2&gt;&lt;p&gt;Getting a simple Hello World set up in &lt;a href=":http://jessicafx.org"&gt;Jessica&lt;/a&gt; involves (optionally) taking more out than you put in.  &lt;a href=":http://jessicafx.org"&gt;Jessica&lt;/a&gt; requires very little configuration so creating a new web project, deleting all the extra files and folders, trimming the &lt;code&gt;web.config&lt;/code&gt; and emptying the &lt;code&gt;Global.asax.cs&lt;/code&gt; makes for a nice blank canvas to start with.&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img alt="" src="/images/blog/project-before-and-after.png" /&gt;&lt;/p&gt;&lt;p&gt;Next step is pretty difficult :-).  Within the &lt;a href="http://nuget.org"&gt;NuGet&lt;/a&gt; Package Manager Console just install the Jessica package&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt; &lt;span class="n"&gt;Jessica&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Once installed be need to tell Jess to initialise/bootstrap itself when the application starts by calling the initialise method in the &lt;code&gt;Application_Start&lt;/code&gt; method of the &lt;code&gt;Global.asax.cs&lt;/code&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Global&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpApplication&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Application_Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Jess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Initialise&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;At this point it's also possible to supply a number of configuration options but we will touch on this later along with the optional configuration &lt;span class="caps"&gt;XML&lt;/span&gt;.  Now that Jess is setup to configure itself all we need to do now is define the module that will handle our complex Hello World output.&lt;/p&gt;&lt;p&gt;Modules form the basis of Jessica each one holding a number of routes (based on URL's and &lt;span class="caps"&gt;HTTP&lt;/span&gt; verbs) and associated actions.  Our app will have a single module,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;JessModule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MainModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This simple module defines a single route that will match any standard &lt;code&gt;GET&lt;/code&gt; request for the root of the app. And that's it.  When you run the app Jess will inspect the assembly and find all the modules and wire up the routes.&lt;/p&gt;&lt;h2&gt;The Rest of the Pie&lt;/h2&gt;&lt;p&gt;Obviously serving plain bits of text is a bit useless so there are plenty of things that Jessica offers that makes it, well, actually useful.&lt;/p&gt;&lt;h3&gt;Configuration&lt;/h3&gt;&lt;p&gt;Jessica can be configured programatically or via &lt;span class="caps"&gt;XML&lt;/span&gt; config.  A number of things you can currently set include,&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;Environment - the environment you build in e.g. &lt;code&gt;development&lt;/code&gt;, &lt;code&gt;test&lt;/code&gt;, &lt;code&gt;release&lt;/code&gt;.  Default &lt;code&gt;development&lt;/code&gt;&lt;/li&gt;	&lt;li&gt;Public Directory - The directory to be used to server static content (html, js, css, images) that require no preprocessing. Default &lt;code&gt;public&lt;/code&gt;&lt;/li&gt;	&lt;li&gt;Views Directory - The directory to be used to serve views that can be dynamically rendered (Razor, Textile, Markdown, Liquid etc.).  Default &lt;code&gt;views&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The &lt;span class="caps"&gt;XML&lt;/span&gt; config is outlined below but each of these properties can be set in the &lt;code&gt;Global.asax.cs&lt;/code&gt; programatically as well via the &lt;code&gt;Jess.Configuration&lt;/code&gt; properties.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;configSections&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;section&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"jessica"&lt;/span&gt; 
             &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"Jessica.Configuration.JessicaConfiguration,Jessica"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/configSections&amp;gt;&lt;/span&gt;
    
&lt;span class="nt"&gt;&amp;lt;jessica&lt;/span&gt; &lt;span class="na"&gt;environment=&lt;/span&gt;&lt;span class="s"&gt;"development"&lt;/span&gt; 
         &lt;span class="na"&gt;publicDir=&lt;/span&gt;&lt;span class="s"&gt;"Public"&lt;/span&gt; 
         &lt;span class="na"&gt;viewsDir=&lt;/span&gt;&lt;span class="s"&gt;"Views"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Routes and Actions&lt;/h3&gt;&lt;p&gt;Routes may be defined as a literal path such as &lt;code&gt;/users&lt;/code&gt; or &lt;code&gt;/store/toys&lt;/code&gt; or with variable segments such as &lt;code&gt;/user/:id&lt;/code&gt; or &lt;code&gt;/blog/:year/:month/:day&lt;/code&gt;.  Variable segments are then parsed out of the route url and the values added to the action argument.&lt;/p&gt;&lt;p class="minimal-gist"&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/blog/:year/:month/:day"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"blog"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That seems to be about the extent of the route manipulation for now.  Would be nice to see the other Sinatra stuff included such as &lt;a href="http://www.sinatrarb.com/intro#Routes"&gt;splats&lt;/a&gt;, &lt;a href="http://www.sinatrarb.com/intro#Routes"&gt;regular expressions&lt;/a&gt; and &lt;a href="http://www.sinatrarb.com/intro#Conditions"&gt;conditions&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Actions are fairly simple they take a dynamic object and are expected to return a Jessica &lt;code&gt;Response&lt;/code&gt; object.  The dynamic object passed in contains a number of things such as route parameters, querystring parameters and the current HttpContext.  As an added niceness the Jessica &lt;code&gt;Response&lt;/code&gt; object has a number of &lt;a href="http://msdn.microsoft.com/en-us/library/z5z9kes2(v=vs.71).aspx"&gt;implicit&lt;/a&gt; type conversions that allow us to return simpler objects&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MainModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// string -&amp;gt; text/html&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// int -&amp;gt; http status code&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;404&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                             

    &lt;span class="c1"&gt;// Action&amp;lt;Response&amp;gt; -&amp;gt; Response stream&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Test"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Static Content&lt;/h3&gt;&lt;p&gt;Jessica allows us to return static content simply.  First of all all routes are first checked for the existence of a file at that &lt;span class="caps"&gt;URL&lt;/span&gt;.  If a file is found it is served, if not then Jessica goes to the route table and works it's magic.  Putting resources in the &lt;code&gt;publicDir&lt;/code&gt; allows Jessica to serve static content from the route actions and the &lt;code&gt;Response&lt;/code&gt; object comes with a number of nice helpers to help make your static actions neater.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MainModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsCss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.css"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; 
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"trojanz.exe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/application"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsHtml&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.html"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsJs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test.js"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"James"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;31&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"http://www.google.com"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AsText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Views&lt;/h3&gt;&lt;p&gt;What web technology would be complete without the ability to serve non-static views?  Oddly enough Jessica, out of the box at least, comes with no view engines.  I suspect this is because dynamic view rendering isn't technically something that should form the basis of a web framework - thats the job of the view engines.  Jessica does however come with an extension point of adding X amount of view engines - &lt;code&gt;Jess.ViewEngines&lt;/code&gt;.  There is also a healthy volume of view engines available for Jessica,&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/Jessica-ViewEngine-Razor"&gt;Razor&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/Jessica-ViewEngine-Spark"&gt;Spark&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/Jessica-ViewEngine-DotLiquid"&gt;DotLiquid&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/Jessica-ViewEngine-Markdown"&gt;Markdown&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://nuget.org/List/Packages/Jessica-ViewEngine-Textile"&gt;Textile&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Rendering a view from an action is also very simple&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;JessModule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MainModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/user/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"index"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/user/:username"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;View&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The first option renders a simple view called index while the second takes a model object that is pushed into the scope of the view and can be used to build the view.  Jessica will loop through it's &lt;code&gt;ViewEngines&lt;/code&gt; collection and attempt to resolve the view name to a file in the specified &lt;code&gt;viewsDir&lt;/code&gt; folder.  If it finds a matching view it will render it.&lt;/p&gt;&lt;p&gt;I am going to take the lead from Jessica itself and not dive into the various view choices for now.  They are pretty self explanatory anyway.&lt;/p&gt;&lt;h2&gt;Summing Up&lt;/h2&gt;&lt;p&gt;So thats the 101, the skinny, the lowdown, the starter for 10, the basics and other random statements of &lt;a href=":http://jessicafx.org"&gt;Jessica&lt;/a&gt;. Hopefully this covers the vast majority of what v0.83 of Jessica offers.  It's a nice minimla framework that attempts to stick to Sinatra's philosophy.  It would be nice to see a few more of Sinatras features creeping in especially route &lt;a href="http://www.sinatrarb.com/intro#Conditions"&gt;conditions&lt;/a&gt;.  There is also some nice high level error handling being introduced (for v0.9 I assume) for handling application wide errors and 404's.  I plan on talking about these when they make an appearance.&lt;/p&gt;&lt;p&gt;Corrections, comments and criticisms below please.&lt;/p&gt;</content></entry><entry><title>Why Would You Use Micro Web Frameworks in .NET?</title><updated>Tue May 17 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/17/why-would-you-use-micro-web-frameworks-in-net/"></link><id>urn:yobriefca-se:feed:post:Why Would You Use Micro Web Frameworks in .NET?</id><content type="html">
&lt;blockquote&gt;&lt;p&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;: Oh dear James &amp;lt;shakeshead/&amp;gt;.  So it seems someone was paying attention to my post and noticed my benchmarks were &lt;strong&gt;hideously&lt;/strong&gt; flawed.  I have removed them because I don't like looking like a total fool but I will be replacing the with &lt;strong&gt;honest, real and unstupified&lt;/strong&gt; results as soon as I can.  Apologies for any distress this may have caused.  &lt;acronym title="Fear Uncertainty Doubt"&gt;&lt;span class="caps"&gt;FUD&lt;/span&gt;&lt;/acronym&gt; is a powerful thing in the wrong hands but ignorance is worse.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;I've been doing a bit of research into micro web frameworks in the.&lt;span class="caps"&gt;NET&lt;/span&gt; world recently and I was posed a question by a colleague.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Whats the point of a micro web framework?&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;I gave the usual response - more lightweight, smaller foot print blah blah blah. To be honest I was barely convincing myself.  I suspected I had fallen into the trap of latching on to something because it was novel to me rather than any real reason.  I am not saying they don't have a place in solutions technology stack, quite the opposite in fact, what I am saying is that I didn't have sufficient justification for bothering with them.&lt;/p&gt;&lt;p&gt;So I've spent a bit more time looking at the background of these microframeworks rather than the techs themselves and hopefully the next time someone asks I can give a more convincing reply.&lt;/p&gt;&lt;h2&gt;What Is A Micro Web Framework&lt;/h2&gt;&lt;p&gt;Pretty much a micro web framework is a framework that provides the absolute minimum feature set required to create a web application.  Extra features can be added via extensions etc. but the sole purpose of a microframework is to allow the app to handle requests to certain URLs (routing) with a bit of basic error handling for 404's etc. (possibly).  No feature creep, no bloated deployments just ultra-lean request/response handling.&lt;/p&gt;&lt;p&gt;The most famous microframework, and the one that started it all, comes from the Ruby world - &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; and it's "Hello World" example really highlights the ease of creating an app.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'sinatra'&lt;/span&gt;

&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/hi'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="s2"&gt;"Hello World!"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Run this and navigate to &lt;code&gt;/hi&lt;/code&gt; to see the amazing response of &lt;code&gt;Hello World&lt;/code&gt;.&lt;/p&gt;&lt;h2&gt;Micro Web Frameworks in the .&lt;span class="caps"&gt;NET&lt;/span&gt; World&lt;/h2&gt;&lt;p&gt;So how does this compare to similar technologies in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world.  Most .&lt;span class="caps"&gt;NET&lt;/span&gt; solutions take a lot of inspiration from &lt;a href="http://www.sinatrarb.com/"&gt;Sinatra&lt;/a&gt; (and why not it's pretty much the blueprints for the microframeworks) and this one is written using &lt;a href="http://jessicafx.org"&gt;Jessica&lt;/a&gt;.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyModule&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Jessica&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JessModule&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MyModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hi"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;OK so there is a bit of extra language noise but this is to be expected when moving from Ruby to C# but it's just as readable.  &lt;a href="http://jessicafx.org"&gt;Jessica&lt;/a&gt; isn't the only microframework in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world but it does seem to be one of the better ones (based on a very superficial amount of research - no offence meant to other frameworks - happy to be proven wrong).  The other frameworks considered "micro" include,&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;&lt;a href="http://github.com/thecodejunkie/Nancy"&gt;Nancy&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://github.com/martinrue/Tinyweb"&gt;Tinyweb&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://github.com/jondot/nina"&gt;Nina&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://github.com/carlhoerberg/NSinatra"&gt;NSinatra&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;What Makes Micro Frameworks Worthwhile?&lt;/h2&gt;&lt;p&gt;So lets expand on my original argument of &lt;cite&gt;"more lightweight, smaller foot print blah blah blah"&lt;/cite&gt;.  The most obvious is that a microframework by design is going to be a lot more lightweight than a normal framework.  It's lifecycle for a request is going to be greatly simplified and ultimately this should help increase performance per request (I currently have no figures to back this up yet but I am working on it).  As well as performance and increased throughput this lightweight approach should greatly reduce the memory footprint which will help your app scale a bit better&lt;sup id="fnr1" class="footnote"&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;&lt;p&gt;Another reason to consider a microframework is that you are in control of everything and that is good in my book.  It's one of my arguments for choosing &lt;span class="caps"&gt;MVC&lt;/span&gt; over WebForms.  No unnecessary injection of client side code or script managers or any of that sort of thing.  No overly complex view properties being populated for no reason.  Nothing but the stuff you put in.&lt;/p&gt;&lt;p&gt;Not exactly within the bounds of a microframework by definition but still worth mentioning is hosting.  &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; has a number of hosting options&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;Standalone&lt;/li&gt;	&lt;li&gt;In a web app&lt;/li&gt;	&lt;li&gt;In a &lt;span class="caps"&gt;WCF&lt;/span&gt; Service&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This sort of swappable hosting greatly increases portability of your app.  Now this is &lt;a href="http://nancyfx.org"&gt;Nancy&lt;/a&gt; specific but the effort required to create this sort of functionality with another microframework is trivial in comparison to the what it would be in &lt;span class="caps"&gt;MVC&lt;/span&gt; or WebForms.&lt;/p&gt;&lt;p&gt;Also many of the microframeworks have some sort of support for &lt;a href="http://owin.org/"&gt;&lt;span class="caps"&gt;OWIN&lt;/span&gt;&lt;/a&gt; which is essentially a web abstraction that would allow your app to run on any &lt;span class="caps"&gt;OWIN&lt;/span&gt; supported server&lt;sup id="fnr3" class="footnote"&gt;&lt;a href="#fn3"&gt;3&lt;/a&gt;&lt;/sup&gt;.  This sort of support would take longer to roll out across the larger frameworks which generally stick to &lt;span class="caps"&gt;IIS&lt;/span&gt;.  I'm not saying it won't happen but the bigger your framework the slower change is going to happen.&lt;/p&gt;&lt;p&gt;That brings me to my final point - evolution.  &lt;span class="caps"&gt;ASP&lt;/span&gt;.&lt;span class="caps"&gt;NET&lt;/span&gt; &lt;span class="caps"&gt;MVC&lt;/span&gt; has evolved very fast since v1.0 and there is no sign of that progress abating any time soon.  Still it's speed is hindered by it's size. These microframeworks introduce new features (some experimental for the brave among us) much faster because of the fact they are small.  Extending these frameworks don't require the creation of numerous classes to do simple things - there is generally a common extension point with a very small set of requirements.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;I hope this has helped my colleagues get a better picture of why I think microframeworks are worthy of our time and why I am keen to do a bit of research on them.  I also hope this has broadened a few minds in terms of solution architectures in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world.  Not everything needs to be some configurable, Enterprise ready, heavyweight application sometimes the simplest solution is all you need.&lt;/p&gt;&lt;p id="fn1" class="footnote"&gt;&lt;a href="#fnr1"&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt; Provided you don't do something stupid!&lt;/p&gt;&lt;p id="fn2" class="footnote"&gt;&lt;a href="#fnr2"&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt; As a friend pointed out it's easy to write a fast Hello World server so take these results with a healthy does of skepticism&lt;/p&gt;&lt;p id="fn3" class="footnote"&gt;&lt;a href="#fnr3"&gt;&lt;sup&gt;3&lt;/sup&gt;&lt;/a&gt; It's early days for &lt;span class="caps"&gt;OWIN&lt;/span&gt; but still worth mentioning.&lt;/p&gt;</content></entry><entry><title>Further Adventures in Unit Testing Technologies</title><updated>Mon May 16 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/16/further-adventures-in-unit-testing-technologies/"></link><id>urn:yobriefca-se:feed:post:Further Adventures in Unit Testing Technologies</id><content type="html">
&lt;p&gt;This post is a follow up to &lt;a href="/blog/2011/04/04/a-tale-of-unit-testing-technologies/"&gt;A Tale of Unit Testing Technologies&lt;/a&gt;.  Information about the project repository can be found on my &lt;a href="http://github.com/kouphax/unit-testing"&gt;project page&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;I've finally managed to slog through the long and slightly tedious process of re-writing the same suite of tests in a number of C# and JavaScript unit testing technologies - the results of which can be seen on the &lt;a href="http://github.com/kouphax/unit-testing"&gt;project GitHub page&lt;/a&gt;.  My conclusion, especially in the .&lt;span class="caps"&gt;NET&lt;/span&gt; world, is that it doesn't really matter that much.  I know it's not that much of a brilliant ending but what did you expect?  Fireworks?&lt;/p&gt;&lt;p&gt;Anyways before I start amazing you all with more awe inspiring revelations let me run through the remaining technologies (until such times as more are added at least) and summarise their good and bad points.  The other technologies I brought on board include,&lt;/p&gt;&lt;ul&gt;	&lt;li&gt;&lt;a href="http://www.nunit.org/"&gt;NUnit&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms182486.aspx"&gt;MSTest&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://nspec.org/"&gt;NSpec&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="https://github.com/robconery/Quixote"&gt;Quixote&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://pivotal.github.com/jasmine/"&gt;Jasmine&lt;/a&gt;&lt;/li&gt;	&lt;li&gt;&lt;a href="http://docs.jquery.com/Qunit"&gt;QUnit&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Some obvious ones there just to round out the comparisons and a few JavaScript based ones.&lt;/p&gt;&lt;h2&gt;&lt;a href="http://www.nunit.org/"&gt;NUnit&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Good old &lt;a href="http://www.nunit.org/"&gt;NUnit&lt;/a&gt;.  Whats not to love?  Mark a class as a fixture and mark your methods as a test.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[TestFixture]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TagTest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [Test]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;TagNameNormalisationTest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"My Tag Name"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AreEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"mytagname"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;No point in mentioning too much about NUnit as most people will know the ins and outs of it already.  It has VS integration (via &lt;a href="http://testdriven.net/"&gt;TestDriven&lt;/a&gt;) and a nice NUnit runner capable of watching changes to DLL's and automatically rerunning the test contained within.  One caveat around that is often NUnit hangs on to the &lt;span class="caps"&gt;DLL&lt;/span&gt; reference and requires a restart so Visual Studio can rebuild the &lt;span class="caps"&gt;DLL&lt;/span&gt; which kind of misses the point - but it doesn't happen too often.&lt;/p&gt;&lt;p&gt;The framework itself would lend itself to a bit of betterification through sugar such as &lt;a href="http://should.codeplex.com/"&gt;Should and Should.Fluent&lt;/a&gt; but it does what it says on the tin.&lt;/p&gt;&lt;h2&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms182486.aspx"&gt;MSTest&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Another staple for many people.  I'd never really used it too much prior to doing this post because of the general negative opinion of it.  I must admit I kind of agree with some of these things.  It's pretty much the MS equivalent of NUnit (in terms of features and look and feel)&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[TestClass]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TagTest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [TestMethod]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;TagNameNormalisationTest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"My Tag Name"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AreEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"mytagname"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So why do so many people prefer NUnit over MSTest?  Well I think the main problem is that it is &lt;a href="http://www.richard-banks.org/2010/03/mstest-sucks-for-unit-tests.html"&gt;quite slow&lt;/a&gt; in comparison (from Visual Studio at least) but my main gripe is the amount of &lt;a href="http://en.wiktionary.org/wiki/flob"&gt;flob&lt;/a&gt; it generates for each test run and configuration.  It even produces solution folders for holding test runs and other files. If I want you to create those I'll ask you - don't force it on me.  It also has a very limited set of Assertion methods much like NUnit.&lt;/p&gt;&lt;p&gt;Enough moaning - surely it must have some benefits?  Well yes it does of course.  The main benefit &lt;span class="caps"&gt;IMHO&lt;/span&gt; is that it has very nice integration into Visual Studio which offers some very clean feedback.  This is something that requires &lt;a href="http://testdriven.net/"&gt;TestDriven&lt;/a&gt; for NUNit and the other frameworks which may or may not be free depending on your situation.&lt;/p&gt;&lt;h2&gt;&lt;a href="http://nspec.org/"&gt;NSpec&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Not to be confused with the &lt;a href="http://nspec.tigris.org/"&gt;first hit on Google&lt;/a&gt; which seems to be pretty much a dead project &lt;a href="http://nspec.org"&gt;NSpec&lt;/a&gt; (.org) is an attempt to bring &lt;a href="http://rspec.info"&gt;RSpec&lt;/a&gt; into the .&lt;span class="caps"&gt;NET&lt;/span&gt; world.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;describe_Tag&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;nspec&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;when_setting_the_tag_name_to_My_Tag_Name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"My Tag Name"&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"should normalise the tag name to mytagname"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; 
            &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;should_be&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"mytagname"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is somewhat similar to &lt;a href="https://github.com/machine/machine.specifications"&gt;MSpec&lt;/a&gt; I mentioned in the last post.  My bugbear with MSpec however was that it didn't feel natural to me.  The equivalent MSpec test for the above looks like this,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[Subject("Normalise Tag Name")]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;when_a_tag_is_created&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;Establish&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;Because&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"New Test Tag"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;It&lt;/span&gt; &lt;span class="n"&gt;should_have_a_normalised_name_of_newtesttag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShouldEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"newtesttag"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Maybe it's just me but it doesn't read as well as the NSpec version.  I even found it easier to write the NSpec version - more natural.&lt;/p&gt;&lt;p&gt;NSpec also has a lot of extra goodies.  Currently there is no integration into Visual Studio yet but the runner is simple to enough to spin up from the NuGet Package Manager Console and it has an added bonus of a file watcher.  &lt;a href="http://nspec.org/continuoustesting"&gt;SpecWatchr&lt;/a&gt; is similar to NUnit it can watch for changes and automatically run the specs.  It differs from NUnit in that it watches changes to the code files rather than the &lt;span class="caps"&gt;DLL&lt;/span&gt; - so need to wait for builds to happen etc.  Nice.&lt;/p&gt;&lt;p&gt;One minor gripe with it (bar the lack of VS support - even TestDriven support would be good) is the &lt;code&gt;should&lt;/code&gt; syntax. I prefer the Should.Fluent style syntax &lt;code&gt;Should.Be()&lt;/code&gt; rather than the current &lt;code&gt;should_be&lt;/code&gt; syntax.  It's not Ruby and we should accept that :-P.&lt;/p&gt;&lt;p&gt;It's still quite early for NSpec and hopefully it keeps it's momentum as it would be one of my frameworks of choice.&lt;/p&gt;&lt;h2&gt;&lt;a href="https://github.com/robconery/Quixote"&gt;Quixote&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Quixote by Rob Conery follows on from his current slew of ultra lightweight solutions to common problems (like &lt;a href="https://github.com/robconery/Massive"&gt;Massive&lt;/a&gt; and &lt;a href="https://github.com/robconery/Sugar"&gt;Sugar&lt;/a&gt;).  Quixote blends the &lt;span class="caps"&gt;HTML&lt;/span&gt; reporting directly with the framework and you write tests right in a Razor file.  This is the definition of lightweight.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@using Quixote;
@using CodeSlice.UnitTesting.Model;

&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;'@Url.Content("~/Styles/quixote.css")'&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

@TheFollowing.Describes("Tags")
    @They.Should("Normalise the Tag Name", () =&amp;gt; {
        return new Tag { Name = "Test Name" }.NormalisedName.ShouldEqual("testname");
    })
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The obvious problem with this is that you don't get continuous integration or build support.  There is no need to rebuild anything just refresh your page and the tests re-run.  Still CI/Build integration would be nice for a framework.&lt;/p&gt;&lt;h2&gt;&lt;a href="http://pivotal.github.com/jasmine/"&gt;Jasmine&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;Ah Jasmine.  Jasmine is what I wanted out of a JavaScript testing framework.  Rather than going the JSpec route of creating a preprocessed &lt;acronym title="Domain Specific Language"&gt;&lt;span class="caps"&gt;DSL&lt;/span&gt;&lt;/acronym&gt; Jasmine makes use of JavaScripts dynamic nature and "functions as 1st class citizens" feature to create framework that behaves like RSpec but embraces JavaScript.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Tag'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;beforeEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"should normalise a tag name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A Tag Name'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getNormalisedName&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;toEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'atagname'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Out of the box you get an extensible framework (custom matchers etc), mocking and asynchronous support.  Ajax faking isn't directly available but I recommend using &lt;a href="http://sinonjs.org"&gt;Sinon&lt;/a&gt; for this as it has an amazing FakeServer object that is fully configurable.&lt;/p&gt;&lt;p&gt;Another big advantage of Jasmine is it's build integration.  Be it node.js, Java or Ruby you can integrate Jasmine specs into your build process.  .&lt;span class="caps"&gt;NET&lt;/span&gt; integration isn't available yet but &lt;a href="http://jurassic.codeplex.com"&gt;Jurassic&lt;/a&gt; would be a suitable host if anyone wants to make it happen (hint, hint :-P).&lt;/p&gt;&lt;h2&gt;&lt;a href="http://docs.jquery.com/Qunit"&gt;QUnit&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;And finally QUnit.  QUnit is the NUnit/JUnit equivalent for the JavaScript world.  Well known and mature project with some very nice &lt;span class="caps"&gt;HTML&lt;/span&gt; output.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Tag Model Validation'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Tag name normalisation'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'A Tag Name'&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
        &lt;span class="nx"&gt;normalisedName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getNormalisedName&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'atagname'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;normalisedName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Not much else to say on this one right now.... must be running out of steam.&lt;/p&gt;&lt;h2&gt;Summing Up&lt;/h2&gt;&lt;p&gt;And there you have it.  The first real post since the move and it's a bit of a waffly long one.  Anyways what would be my recommendations?  Well for .&lt;span class="caps"&gt;NET&lt;/span&gt; I'm torn.  Currently I'd say NUnit with Should.Fluent extensions.  This is a nice combo for creating simple tests that people can execute from within Visual Studio.  However NSpec has potential - I'd like to see it grow a bit more and I'll keep watching it.  Visual Studio integration would be aces though.  JavaScript on the other hand I have a clear cut winner.  Jasmine is awesome.  It's boosted my code quality and productivity no end on recent projects and with the help of &lt;a href="http://sinonjs.org"&gt;Sinon&lt;/a&gt; for mocking and controlling Ajax it'll be a long time before I am this happy with a .&lt;span class="caps"&gt;NET&lt;/span&gt; equivalent tech.  Fingers crossed for NSpec on that front.&lt;/p&gt;</content></entry><entry><title>The First Day of the Rest of My Life</title><updated>Sun May 15 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/15/the-first-day-of-the-rest-of-my-life/"></link><id>urn:yobriefca-se:feed:post:The First Day of the Rest of My Life</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;Move complete, more or less. Still a few broken images and stuff but like the unpacked boxes of a new house these will probably remain that way until about next year or so. Decent enough weekend project all in. For anyone who's interested in what's went on under the hood - read on.&lt;/p&gt;&lt;h2&gt;Abandoning Posterous&lt;/h2&gt;&lt;p&gt;&lt;a href="http://posterous.com"&gt;Posterous&lt;/a&gt; is great as a simple blogging platform but in terms of giving me exactly what I wanted at any point in time &lt;a href="http://posterous.com"&gt;Posterous&lt;/a&gt; and most other platforms didn't give me exactly what I wanted (I am quite a demanding character I guess).&lt;/p&gt;&lt;p&gt;I was doing some research on .&lt;span class="caps"&gt;NET&lt;/span&gt; Micro Web Frameworks and came across &lt;a href="http://jessicafx.org"&gt;Jessica&lt;/a&gt; which was created and maintained by &lt;a href="http://tombell.org.uk"&gt;Tom Bell&lt;/a&gt;.&lt;/p&gt;&lt;h2&gt;Jekyll&lt;/h2&gt;&lt;p&gt;Browsing Toms &lt;a href="http://github.com/tombell"&gt;github repo&lt;/a&gt; I discovered he prodced his site using &lt;a href="https://github.com/mojombo/jekyll"&gt;Jekyll&lt;/a&gt; a Ruby based static site generator and I knew I had to have it! So I ripped off Toms nice design, uglified it with my own tweaks and what you see now is the finished site. This site is also mirroed on &lt;a href="http://kouphax.github.com"&gt;Github&lt;/a&gt; but some of the functions (category pages) are missing due to some custom generators that the Github Jekyll instance doesn't run.&lt;/p&gt;&lt;p&gt;There is more to be deployed but I'd rather polish what I have before pushing more stuff out. I've tinkered with Project page generation, &lt;a href="https://github.com/schacon/showoff"&gt;Showoff&lt;/a&gt; for presentations and a few other things that will appear over time.&lt;/p&gt;&lt;h2&gt;Domain Name&lt;/h2&gt;&lt;p&gt;Seriously - how couldn't I go for a shiny web 2.0 ultra elite domain name given the chance? &lt;a href="http://gandi.net"&gt;Gandi.net&lt;/a&gt; was recommended to me and I must admit the set up was fairly painless and their admin panel stuff is very nice.&lt;/p&gt;&lt;h2&gt;Appharbor&lt;/h2&gt;&lt;p&gt;Currently my static site is hosted on &lt;a href="http://appharbor.com"&gt;appharbor&lt;/a&gt;. Appharbor is awesome. Typically I've used it to spin up quick demos and stuff and the simplicity of doing a &lt;code&gt;git push&lt;/code&gt; to deploy your new version is like a dream (especially given the heavy handed approach of Azure).&lt;/p&gt;&lt;h2&gt;Thats It&lt;/h2&gt;&lt;p&gt;Not sure that anyone cares about this sort of stuff but I just want to take my new process for a spin anyway. Any questions - let me know.&lt;/p&gt;</content></entry><entry><title>On The Move</title><updated>Fri May 13 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/13/on-the-move/"></link><id>urn:yobriefca-se:feed:post:On The Move</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;So I've decided to go ahead and move away from &lt;a href="http://yobriefcase.posterous.com"&gt;Posterous&lt;/a&gt; - the call of &lt;a href="https://github.com/mojombo/jekyll"&gt;Jekyll&lt;/a&gt; was just too much. I've migrated a lot of my content but there is some dodgy formatting and plenty of missing images so I am in the process of sorting everything out.&lt;/p&gt;&lt;p&gt;Also thanks to &lt;a href="http://tombell.org.uk"&gt;Tom Bell&lt;/a&gt; for letting me use his template.&lt;/p&gt;</content></entry><entry><title>Providing a Foundation for .NET Projects using NuGet</title><updated>Thu May 05 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/05/05/providing-a-foundation-for-net-projects-using-nuget/"></link><id>urn:yobriefca-se:feed:post:Providing a Foundation for .NET Projects using NuGet</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;It's always good to have a foundation, right?  I mean if you have a heap of projects going on or you work for a company that has a load of active or future projects then why not have a foundation?  Foundations are good.  I mean they promote DRY cross projects, reduce development time for many common tasks (logging, data access, etc.).  Foundations can also provide a number of tools and technologies that are “approved” — that is they have been throughly researched and you aren't going to trip up over license issues, inexperience of the tech or shortcomings of the tech/tools.&lt;/p&gt;&lt;p&gt;The problem with foundations is that people start assuming they are some sort of magical framework that is supposed to do all the work.  You wrong son!  A foundation works best when it's a pile of techs/lightweight solutions to common problems.  They don't work too well when you just get a big monolithic DLL that acts as an abstraction for other technologies.  Abstractions are good for certain things but not everything.  A big dll is also harder to document.  How do you know whats in it bar using Intellisense or reflector?  OK OK OK there should also be some sort of associated documentation that comes with it but keeping these two things inline is extra work.  Extra work that doesn't get done — it's not funded work so why bother? Eh?&lt;/p&gt;&lt;p&gt;In our company we use this approach and it works from a technology perspective but from a people perspective it could be better.  There is a general lack awareness of what is contained within the magical 3 dlls that get generated and as such less stuff get pushed into the foundation as it probably could.  The current process works like so,&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm3.static.flickr.com/2076/5719795098_ee9676646b_b.jpg" /&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Create a new Subversion repository for the project&lt;/li&gt;&lt;li&gt;Branch the current Foundation (a Visual Studio Solution) repository for the current project&lt;/li&gt;&lt;li&gt;Tweak the build step of the Foundation solution to dump it's dlls into the lib folder of the project&lt;/li&gt;&lt;li&gt;Commit foundation changes to the project foundation branch&lt;/li&gt;&lt;li&gt;Review and selectively merge changes into the trunk of the Foundation repository&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Even writing the steps out and thinking about all the undocumented steps required to get this to work smoothly tells me that it is possibly more complicated than it has to be.  In fact the merging back into the Foundation trunk doesn't happen as much as it should.  In fact projects adding thing to their own branch of the foundation rarely happens cause it's a bit messy.&lt;/p&gt;&lt;p&gt;Another issue with this approach is that it only makes sense for bespoke code.  I mean if you are building out a dll how do you tell people that “actually JSON.NET is our preferred JSON Serialiser”.  It's not easy as the documenting and announcing of such things is in another document out in the ether.&lt;/p&gt;&lt;h2&gt;An Alternative Approach&lt;/h2&gt;&lt;p&gt;Modular.  It's the future I tell thee!  Discrete pieces of functionality that can run independently.  This enables project to take the parts of the foundation they want and build a framework from it.  Much like ender (&lt;a href="http://ender.no.de/"&gt;&lt;/a&gt;&lt;a href="http://ender.no.de/"&gt;http://ender.no.de/&lt;/a&gt;) is doing in the JavaScript world currently being able to take micro-libraries and build a macro-framework that suits your needs and just that.  No risk of featureitis, evolve your framework as you need and be in complete control doing it.&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm4.static.flickr.com/3045/5719234363_1b5f04b781_z.jpg" /&gt;&lt;/p&gt;&lt;p&gt;Thats the plan but how do we achieve it?  HINT: The answer, a partial one anyway, is in the title of this post.  Well, first things first we would need to take our current foundation and modularise it.  Along with that we'd probably need to ensure tests etc. are up to scratch (start on the right foot).  Secondly we would need to change the delivery mechanism - no more branching and merging of codebases - too heavyweight.  NuGet (&lt;a href="http://nuget.codeplex.com"&gt;nuget.codeplex.com&lt;/a&gt;) will help.  With NuGet we could create packages from our modules, manage dependencies, document usage right in the package manifest, and expose these packages as a server and searchable package repository (a la &lt;a href="http://www.nuget.org"&gt;nuget.org&lt;/a&gt;).  Into this repository we could push all our approved technologies (researched and vetted previously) as well as new versions of our bespoke code.  As time goes on this repository can grow and more people can add to it.  Heck we can even make the submission process as easy as we see on nuget.org.  Wunderbar!&lt;/p&gt;&lt;p&gt;So how would this affect how projects operate on a daily basis?&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm4.static.flickr.com/3532/5719234439_0171705928_z.jpg" /&gt;&lt;/p&gt;&lt;p&gt;Well the mechanism for the typical usage is greatly simplified.  The project pushes and pulls packages from a central package repo.  People use the repo to search for things they need (JSON Serialiser, Micro-ORM etc) and if they don't find something they can then push a new package into the repo (or just suggest one).  Self documenting use of 3rd party techs, all available all searchable.&lt;/p&gt;&lt;p&gt;Another advantage of this approach, and using NuGet in particular, allows us to bundle not just dlls but also transform files for configuration (web.xml etc.), javascript/css/other client side assets, project and solution wide tools (executable that can be run from the VS command prompt etc.) and even code samples and templates.  This gives us a richer, more cohesive delivery mechanism for a foundation to a project.&lt;/p&gt;&lt;p&gt;Of course it's not all perfect.  Urgent changes to bespoke code will need to be done the old fashioned way and pushed to the main server which is less than ideal but it's a bit of an edge case.&lt;/p&gt;&lt;p&gt;So whats not to like about this idea?  Foundations not frameworks is the key here.  Let the project create its own framework as that will work much better than imposing one upon them.&lt;/p&gt;&lt;p&gt;So this is a bit of a brain dump (some would say brain fart no doubt) as usual and I am open to suggestions and corrections.  I've likely missed a few glaring problems with the new approach so let the critics loose and I'll try my best to address their concerns.&lt;/p&gt;&lt;p&gt;Next step - setting up a simple POC to test the general workflow.&lt;/p&gt;</content></entry><entry><title>Flashbag: Glimpse</title><updated>Wed Apr 20 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/04/20/flashbag-glimpse/"></link><id>urn:yobriefca-se:feed:post:Flashbag: Glimpse</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/22708858?portrait=0" frameborder="0" height="400" width="100%"&gt;&lt;/iframe&gt;&lt;/p&gt;&lt;p&gt;Glimpse is an extensible client side debugging dev tool for server side stuff in any ASP.NET (WebForms) and ASP.NET MVC (RoR and PHP forthcoming). Think Firebug for your server and you have the gist of it.  This presentation is a very brief introduction/overview/review.&lt;/p&gt;&lt;p&gt;Also the quality totally blows I promise it'll be better next time.&lt;/p&gt;&lt;p&gt;&lt;span style="color: #888888;"&gt;&lt;em&gt;&lt;strong&gt;Flashbag (fuh-lash-bah-ag)&lt;/strong&gt;: A flashbag is a group of people who assemble suddenly in a meeting room type place, partake in a brief technical discussion (or demonstration), then disperse, often for the purposes of education and betterization&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;</content></entry><entry><title>Guerrilla Learning Experiment #2: FlashBags</title><updated>Wed Apr 20 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/04/20/guerrilla-learning-experiment-number-2-flashbags/"></link><id>urn:yobriefca-se:feed:post:Guerrilla Learning Experiment #2: FlashBags</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;So I kicked off another experiment in my war against ignorance in the IT industry.  To make myself look cool I am using the term Guerrilla Learning.  The experiment?  Well I am calling it FlashBag*&lt;/p&gt;&lt;blockquote&gt;Flashbag (fuh-lash-bah-ag): A flashbag is a group of people who assemble suddenly in a meeting room type place, partake in a brief technical discussion (or demonstration), then disperse, often for the purposes of education and betterization.&lt;/blockquote&gt;&lt;p&gt;The name itself is a portmanteau of Flash Mob (I assume you all know what this is by now) and Whitebag - a technology awareness session held during lunch - hence Whitebag (the one you bring your lunch in).&lt;/p&gt;&lt;p&gt;Anyway whats it all about then?  Well a typical scenario would be this..... &lt;/p&gt;&lt;ul&gt;&lt;li&gt;I am reading my tech blogs on a day and come across a new tech that seems really nice. &lt;/li&gt;&lt;li&gt;As usual I link it on yammer or twitter or whatever and continue to dive into it.&lt;/li&gt;&lt;li&gt;I'm sold.  This is something other people need to know about.&lt;/li&gt;&lt;li&gt;So I book a room for 1/2 hour at the last minute, post internally the room and the topic (say 12:00, Boardroom, Glimpse for .NET)&lt;/li&gt;&lt;li&gt;Once the party is assembled I do a very short talk, possibly demo (15 minutes tops) and thats it.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;So what would this achieve?  Well I believe a lot of people throw links and stuff into a “Read Later” pile or have a quick look at the page and go “Cool”.  Then they forget all about it.  This quick interactive, community focused practical sessions forces them to actually think about whats going on (James Hughes PHd in Theoretical Theories and Things like that) and how they would apply it to their work.  It's more in your face and creates a better connection in those peoples minds.  The news then precipitates  further - the people attending the FlashBag post on yammer/twitter and talk about it more rather than just little old me.&lt;/p&gt;&lt;p&gt;As always I record these things as a memento of my awesome presentation skills (#lies) and allowing other people to see what went on.&lt;/p&gt;&lt;p&gt;It's early days and I've done one so far (posting shortly) - turn out was mediocre but perhaps that will change, perhaps it won't.  Mediocre turn out is better than none I guess - perhaps people are busy, perhaps enthusiasm is waining among my colleagues, or perhaps they are sick of me spamming every sense they have on a daily basis!&lt;/p&gt;&lt;p&gt;Thoughts?&lt;/p&gt;&lt;p&gt;* After some internal discussion the name is up for debate.  I like it but mostly because of my huge ego.  Thoughts?&lt;/p&gt;</content></entry><entry><title>Crucible Code Review Guidelines</title><updated>Mon Apr 18 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/04/18/crucible-code-review-guidelines/"></link><id>urn:yobriefca-se:feed:post:Crucible Code Review Guidelines</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;I've been using Crucible on a number of projects for the last few months and I love it.  So now comes the time we want to try and make a case for it to be rolled out to other projects in the company I work for.  Along with the usual stuff such as cost of licenses, logistics, hardware need and so on I also need to consider education and best practise guidelines (oh I said “best practise” that term makes me feel slightly queasy) because after all — if people aren't using it right they probably wont see the benefit, stop using it and then we are back to square one with wasted time, money and effort.&lt;/p&gt;&lt;p&gt;Some of you may be thinking,&lt;/p&gt;&lt;blockquote&gt;“But it's just a code review tool - what guidelines do you really need?”&lt;/blockquote&gt;&lt;p&gt;But truth be told you do need some, and I learnt this the hard(ish) way.  So here are some top tips to getting the most out of Crucible and at the same time possibly embettering (not a real word but should be) your development approach.&lt;/p&gt;&lt;h2&gt;Rule 1: Review Early, Review Often&lt;/h2&gt;&lt;p&gt;Don't wait for a whole week before committing 600 files as a single review — no ones going to actually do a proper review as they don't have time or patience.  Generally speaking your work should be sufficiently broken down so that you can do a few hours of work, commit that code and submit that changeset as a review.  Typically speaking a small review may only take about 5 minutes or so to review per person which is a lot easier than having to actually book time to read an entire books worth of code.  Obviously don't be going insane on this.  A code review should encompass a complete feature and not some arbitrary files that are only a small piece of the puzzle.  Feel free to merge changesets where necessary too.&lt;/p&gt;&lt;h2&gt;Rule 2: Refrain from adding new content to old reviews&lt;/h2&gt;&lt;p&gt;I've seen a few cases were a bunch of files have been added to a review and this review has been used over and over with people adding files and new versions until the whole thing had pretty much the entire codebase included.  Needless to say that's wrong.  Create a review and don't be adding new files or versions too it unless it's in the early stages of review and it is deemed necessary.  Don't be afraid of kicking off more than one review - it's not a crime, in fact it's advised&lt;/p&gt;&lt;h2&gt;Rule 3: Don't be TOO iterative&lt;/h2&gt;&lt;p&gt;Another problem I've experienced is that a review can run for a very long time.  This is linked to Rule 2 in that, based on my review comments, people will make the changes and bring that file in that review up to date.  This leads to further comments and the whole thing just consumes too much time.  Crucible gives you the power to do this but you need a bit of discipline to simply know when to stop and when to start a new review.&lt;/p&gt;&lt;p&gt;In recent projects I have been performing a review, making comments etc. and completing the review (unless there is anything serious I want to make sure gets addressed ASAP).  When changes are made they generally get picked up in the next set of reviews anyway and as I talk to my team all the time I can give a brief sanity review when the changes are made.  &lt;/p&gt;&lt;p&gt;Making your submitted reviews lightweight helps with this.&lt;/p&gt;&lt;h2&gt;Rule 4: Keep reviews small&lt;/h2&gt;&lt;p&gt;Again similar to Rule 1 and 2.  Don't be submitting hundreds of files for a review.  If you need to make a big review considering breaking it down into smaller feature sets and describing this in the review.  It makes it easier for reviewers to perform the review and for yourself to make the changes quickly (if any)&lt;/p&gt;&lt;h2&gt;Rule 5: Let everyone get involved&lt;/h2&gt;&lt;p&gt;Gone are the days when a single (supposedly) smart person performs all reviews.  Social code reviews are and should be the future.  Embrace this.  Every single dev on my projects are added to reviews by default, even people with little to no experience of the technology.  Not only does it help people spot things that others may miss but it also &lt;/p&gt;&lt;ul&gt;&lt;li&gt;generates debate and discussion around coding style&lt;/li&gt;&lt;li&gt;helps people get an understanding of the coding practise in the project&lt;/li&gt;&lt;li&gt;acts as a learning tool for people&lt;/li&gt;&lt;li&gt;helps people feel more involved in the project and team&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Conclusion (cause I can't think of a better word)&lt;/h2&gt;&lt;p&gt;Hopefully these guidelines give you a better understanding of how I work and how I get maximum benefit from Crucible.  After all it is just a tool and therefore open to abuse.  Honestly it's better than our previous approach (literally using Word documents) and I can't imagine anyone would prefer our old approach to this.  Does anyone else have any recommendations around the use of Crucible?&lt;/p&gt;</content></entry><entry><title>PowerMockup â Wireframe Toolkit for PowerPoint</title><updated>Fri Apr 15 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/04/15/powermockup-a-wireframe-toolkit-for-powerpoint/"></link><id>urn:yobriefca-se:feed:post:PowerMockup â Wireframe Toolkit for PowerPoint</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;Link: &lt;a href="http://www.powermockup.com/"&gt;&lt;/a&gt;&lt;a href="http://www.powermockup.com/"&gt;http://www.powermockup.com/&lt;/a&gt;&lt;/p&gt;&lt;blockquote&gt;Disclosure Time:  I approached the developer of this app offering to do a review in exchange for a review license.  This isn't the sort of thing I usually blog about (except internally, a few times, in the company I work for) but I'm all for supporting the people who deserve support and truth be told I would probably have written this blog post anyway (yep it'll be a positive one)&lt;/blockquote&gt;&lt;p&gt;Happy with that?  Good lets crack on.&lt;/p&gt;&lt;h2&gt;The Past&lt;/h2&gt;&lt;p&gt;I've written a fair number UI design documents over the last few years and for all of these I have used &lt;a href="http://balsamiq.com"&gt;Balsamiq&lt;/a&gt;.  The problem with that is our companies standard/approved wire-framing solution is Visio (with the &lt;a href="http://www.guuui.com/issues/02_07.php"&gt;GUUUI templates&lt;/a&gt;).  Why did I go against the grain? Well I am not a mad fan of Visio.  It's not exactly lightweight, in fact it's extremely bloated (at least in the wire-framing sense), and that is easily the worst thing you can ask for when dealing with highly iterative, dynamic, interactive wire-framing.  You need to work with the Visio rather than let Visio work for you and that is slightly too much fail for me most of the time.  Admittedly there has been times I prefer the slicker, more professional, output of Visio (our customers come from the stiff collar world of finance and the like so it tends to sit better with them).  But were I can I stick with Balsamiq.  Until now....&lt;/p&gt;&lt;h2&gt;The Present&lt;/h2&gt;&lt;p&gt;The problem I've always faced with Balsamiq is that when I produce a first draft of a document with, say, 20 odd wireframes in it and something fundamental is changed by the customer then I have a hard slog ahead to fix all those images.  Thankfully a colleague of mine, one much better at this wire-framing/UX stuff than me, pointed me towards PowerMockup.  Rather than make a new standalone application that has it's own random UI features and offers little to on integration into the app we use day today PowerMockup is simply an extension/add-on to Powerpoint.  So what does this mean?  Well it means,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The devs spent more time into the templates and stuff that matter&lt;/li&gt;&lt;li&gt;Fully integrated into the Office application stack (think ability to tweak results when embedded into Word)&lt;/li&gt;&lt;li&gt;Already makes use of the built in “smart” styling and other features that make Powerpoint actually a fairly decent app for quick templating and stuff.  In fact take it a step further and you could make use of Powerpoint's animation stuff to demonstrate UI interactions.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;So given 5 minutes what could I do?  And I do mean 5 minutes (possibly slightly less).  Well I mocked up the website you are reading right now. &lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm4.static.flickr.com/3151/5720048084_8b4994da36_z.jpg" /&gt;&lt;/p&gt;&lt;p&gt;Trippy eh?  It's not my best work (far from it) but it did help me test drive PowerMockup and get a better feel of it.  So I like it.  I like it because of all the things I've mentioned above (who wouldn't).  I'd use it, and will use it, as my wire-framing tool of choice (and not because I got a free license - my work would pay for it anyway cause they are total aces) from now on but it's not perfect.  If I had to pick holes there are a few things that I'd mention,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;My biggest peeve but it's not a show stopper is the location of the various shapes/widgets.  Dropdowns are in “Navigation” and Text fields are in “Text” but 99% of the time I'd want both of those when constructing a form.  I'd really like a “Form” collection with all of these things grouped.&lt;/li&gt;&lt;li&gt;If I drop a browser container on the page then drop a scrollbar on the page I need to resize said scrollbar to fit the browser container.  Problem is, as the scrollbar is a grouped object, it will stretch the entire thing rather than extend the base rectangle.  Again no show stopper but it is a bit of a pain.  I guess offering the components as separate shapes might make it easier (just a suggestion).&lt;/li&gt;&lt;li&gt;There a plenty of icons but I'd like to see a few more (“comments” icon, “tags” icon) - the equivalent famfamfam icon set would blow me away (and also a lot to ask).  Possibly some more mobile shapes etc. would also be nice.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;The Future&lt;/h2&gt;&lt;p&gt;So thats it for now I am very happy, not because I managed to blag a free license, but rather because the app/plugin/whatever fits into how I do things so seamlessly.  Good work and I look forward to future updates - hopefully more people think the same.&lt;/p&gt;</content></entry><entry><title>Technically Non-Technical: The Pomodoro Technique</title><updated>Tue Apr 05 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/04/05/technically-non-technical-the-pomodoro-technique/"></link><id>urn:yobriefca-se:feed:post:Technically Non-Technical: The Pomodoro Technique</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm4.static.flickr.com/3498/5720110114_ff737584c9_o.png" /&gt;&lt;/p&gt;&lt;p&gt;I've been going through a period recently of attempted self improvement.  Trying new things to make myself a better more effective code monkey — iterative code reviews, todo lists, time management, pair programming, TDD, BDD, la dee dee.... &lt;/p&gt;&lt;p&gt;My old strategy was simple — get given or discover a piece of work, usually pretty large, and start coding.  Once done do a bit of exploratory testing, read the spec to check I've covered everything, commit the code and submit a bit of a code review.  These sessions tend to last a number of days which leads to an eventual short term burnout.  Now because I always write 100% flawless code first attempt that's generally fine but it doesn't really allow other people to learn from my excellence.  Also being the smartest person on any project I am generally being pulled left and right answering queries and explaining my very clever designs — this creates regular context shifts and makes it harder for me to get my focus back on creating all the awesome!&lt;/p&gt;&lt;p&gt;One of the techniques I have found quite successful recently was the Pomodoro Technique.  The name comes from the original kitchen timer used by the creator and the focus is squarely on a low tech approach.  The premise behind it is simple but effective.  You focus on your task for a 25 minute period (a pomodoro) ignoring all external interruptions (or you forfeit the pom), take 5 minute break, repeat. After 4 pomodoros you take a longer break (15-30 minutes).  The rules are as follows,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;A Pomodoro Consists of 25 minutes Plus a Five-Minute Break.&lt;/li&gt;&lt;li&gt;After Every Four Pomodoros Comes a 15-30 Minute Break. &lt;/li&gt;&lt;li&gt;The Pomodoro Is Indivisible. There are no half or quarter Pomodoros. &lt;/li&gt;&lt;li&gt;If a Pomodoro Begins, It Has to Ring:   &lt;ul&gt;&lt;li&gt;If a Pomodoro is interrupted definitively – i.e. the interruption isn't handled it's considered void, never begun, and it can't be recorded with an X. &lt;/li&gt;&lt;li&gt;If an activity is completed once a Pomodoro has already begun, continue reviewing the same activity until the Pomodoro rings. &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Protect the Pomodoro. Inform effectively, negotiate quickly to reschedule the interruption, call back the person who interrupted you as agreed. &lt;/li&gt;&lt;li&gt;If It Lasts More Than 5-7 Pomodoros, Break It Down. Complex &lt;/li&gt;&lt;li&gt;activities should be divided into several activities. &lt;/li&gt;&lt;li&gt;If It Lasts Less Than One Pomodoro, Add It Up. Simple tasks can be combined. &lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;My Honest Opinion&lt;/h2&gt;&lt;p&gt;My previous development strategy was all kinds of broken.  It worked provided I wasn't disturbed and I didn't really have a point at which I could turn round to someone and say &lt;/p&gt;&lt;p&gt;&lt;span style=""&gt; &lt;span style=""&gt; &lt;/span&gt;&lt;/span&gt;“give me 5 minutes and I'll get back to you”&lt;/p&gt;&lt;p&gt;The pom gives me that excuse and with the use of a simple widget I am able to know when I'll next be free (and so are other people).  The hardest thing for me to accept was actually taking the 5 minute break post-pom. Initially (and sometimes still) I'd just work over the 5 minutes — just one more line of code, just test it once more — excuses, excuses, excuses.  Breaking old habits is hard!  Some of the other benefits from the technique is that, &lt;/p&gt;&lt;ul&gt;&lt;li&gt;it really helps you visualise your task up front and break it down much better&lt;/li&gt;&lt;li&gt;it helps you track work that you have done and how long tasks take.  &lt;/li&gt;&lt;li&gt;some pomodoro software even integrates into popular task lists and calendars&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Resources&lt;/h2&gt;&lt;p&gt;The Pomodoro Technique website is by far the best resource for this stuff and even has a free ebook on the technique and its background&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The site: &lt;a href="http://www.pomodorotechnique.com/"&gt;&lt;/a&gt;&lt;a href="http://www.pomodorotechnique.com/"&gt;http://www.pomodorotechnique.com/&lt;/a&gt;&lt;/li&gt;&lt;li&gt;The book: &lt;a href="http://www.pomodorotechnique.com/products.html#pomodoropdf"&gt;&lt;/a&gt;&lt;a href="http://www.pomodorotechnique.com/products.html#pomodoropdf"&gt;http://www.pomodorotechnique.com/products.html#pomodoropdf&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Personally it's going to take me while longer to get into the right mentality 100% but I think the benefits will be worth it.  One day I'll be so efficient the code will just stream out of my brain and into my IDE - but for now I'll stick the mortals way of doing things.&lt;/p&gt;</content></entry><entry><title>A Tale of Unit Testing Technologies</title><updated>Sun Apr 03 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/04/03/a-tale-of-unit-testing-technologies/"></link><id>urn:yobriefca-se:feed:post:A Tale of Unit Testing Technologies</id><content type="html">
&lt;p&gt;I've had a big bad dirty secret for a while now.  One I've kept hidden from my friend and colleagues for years.  But now its time to come clean...... here goes..... hold onto your pants.&lt;/p&gt;&lt;blockquote&gt;&amp;lt;pause for dramatic effect&amp;gt;&lt;br /&gt;I hardly ever write unit tests!&lt;br /&gt;&amp;lt;gasps&amp;gt;&lt;/blockquote&gt;&lt;p&gt;I've come clean and it feels good.  Another reason why it feels good is that it is slowly but surely becoming a falsehood.  I've had my eyes opened recently to the art of BDD and discovered that I've simply been doing it wrong - I was so busy writing code first and then tests to fit my, possibly incorrect, assumptions that tests failed to hold any worthwhile value — time wasted in my mind.  Also when writing said tests I was too worried on the internal mechanics of the "unit" being tested and not about its intended behaviour — essentially missing the point and time wasted once more.  Being a man that has no time for, well, time wasting I feel I have come full circle on why I didn't test.  In the past at least....&lt;/p&gt;&lt;p&gt;But I digress.... Where was I?  Ah yes I've had my eyes opened - I've been doing it right.  Yep I've gone all TDD/BDD - write a failing test, write some code to make it work, make the test fail again and so on and so forth.  Its been a good experience.  I finally see the benefit in it.  Yes, it takes slightly longer and it's hard to break old habits but the amount of times I've been tripped up by assumptions and edge cases that would only normally be found during system testing after a frustrating debugging session with plenty of hair pulling and swearing.&lt;/p&gt;&lt;p&gt;Since the whole behaviour driven enlightenment I've been trying to find a suitable set of technologies that allow me to perfect my new found approach and turn me into a fast and effective code cutting machine.  NUnit is fine — it does the job but it's a task in itself to express what you want out of a test which slows things down.  No if you're going to do it right you want the best tools.  So I have decided to jump in head first and discover what testing tools are the most effective.  I've been through a few already , including,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;SpecFlow (C#)&lt;/li&gt;&lt;li&gt;MSpec (Machine.Specifications) (C#) &lt;/li&gt;&lt;li&gt;JSpec  (JavaScript)&lt;/li&gt;&lt;li&gt;Should/Should.Fluent (C#)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Here are a few of my thoughts so far. &lt;/p&gt;&lt;h2&gt;SpecFlow (&lt;a href="http://www.specflow.org/"&gt;http://www.specflow.org/&lt;/a&gt;)&lt;/h2&gt;&lt;p&gt;SpecFlow is essentially the .NET equivalent of Cucumber (from the Ruby world).  Offering natural language syntax for defining scenarios that make up the behaviour of a feature.  Its better as an example,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;Feature:&lt;/span&gt;&lt;span class="nf"&gt; Tags&lt;/span&gt;

&lt;span class="k"&gt;Scenario:&lt;/span&gt;&lt;span class="nf"&gt; Normalise Tag Name&lt;/span&gt;
&lt;span class="k"&gt;	Given &lt;/span&gt;&lt;span class="nf"&gt;I have created a new tag&lt;/span&gt;
&lt;span class="nf"&gt;	&lt;/span&gt;&lt;span class="k"&gt;When &lt;/span&gt;&lt;span class="nf"&gt;I set its name to "&lt;/span&gt;&lt;span class="s"&gt;New Test Tag&lt;/span&gt;&lt;span class="nf"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;	&lt;/span&gt;&lt;span class="k"&gt;Then &lt;/span&gt;&lt;span class="nf"&gt;it should have a normalised name of "&lt;/span&gt;&lt;span class="s"&gt;newtesttag&lt;/span&gt;&lt;span class="nf"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;So you see the test here is written in pretty much straight english.  It's easy to understand and this means even domain experts (aka "the business folk") can help write them.  So how does this end up being turned into executable tests.  Well, initially you need to do a bit of wiring up using Step Definitions.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[Binding]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TagSteps&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="na"&gt;    [Given(@"I have created a new tag")]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;GivenIHaveCreatedANewTag&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [When(@"I set its name to ""(.*)""")]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;WhenISetItsNameToNewTestTag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [Then(@"it should have a normalised name of ""(.*)""")]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ThenItShouldHaveANormalisedNameOfNewtestag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;normalisedname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;normalisedname&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see we can match up each line of the feature file with an appropriate executable action.  We even have the power of regular expressions at our disposal - allowing us to reuse steps and build up a decent library that comes with us across projects.  Some very very powerful stuff here — we can get everyone involved in writing these things,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Developers and business users during design&lt;/li&gt;&lt;li&gt;Developers during development&lt;/li&gt;&lt;li&gt;Testers during system test — in fact why not write a feature as the "Steps to reproduce"?  Win, win!&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The thing is...... well.... Technically speaking SpecFlow isn't really aimed at the fine grained, unit test level of testing.  It's more about the higher level integration testing.  I mean it's useable but it does become a bit awkward to express things in a reusable manner.  The problem with that is things start feeling quite heavy weight and you need to do a lot of extra work to get them to fit.  No I think, while SpecFlow is great for many things (automated testing, integration testing, system testing etc.) it's not the best fit for what I am looking for in this article - unit testing tools.&lt;/p&gt;&lt;h2&gt;MSpec (&lt;a href="https://github.com/machine/machine.specifications"&gt;https://github.com/machine/machine.specifications&lt;/a&gt;)&lt;/h2&gt;&lt;p&gt;Machine.Specifications (MSpec for short) is a Context/Specification framework geared towards 	removing language noise and simplifying tests. &lt;/p&gt;&lt;p&gt;Thats the official intention and I must admit I was initially taken by it.  Rather than having a single huge class filled with methods representing tests MSpec takes the approach that a single class represents a single scenario and uses lambda expressions to offer the BDD style syntax (Because/It/Subject etc.).  So lets take the Tag scenario described above and convert it to MSpec format,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[Subject("Normalise Tag Name")]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;when_a_tag_is_created&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;Establish&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;Because&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"New Test Tag"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;It&lt;/span&gt; &lt;span class="n"&gt;should_have_a_normalised_name_of_newtesttag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="n"&gt;_tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ShouldEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"newtesttag"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;      
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It's quite simple and it really forces you to ensure your tests are as simple as possible.  To be honest having used it on a project I'm not 100% sold.  I think its down to my personal coding style.  I honestly think while it reduces line count it doesn't necessarily reduce language noise.  Also having used on a project I found it quite awkward to write — the style is different to what I am used to and I guess with training that would change.  As I say this is a personal opinion and I am happy to be shown a better approach.&lt;/p&gt;&lt;h2&gt;JSpec (&lt;a href="http://visionmedia.github.com/jspec/"&gt;http://visionmedia.github.com/jspec/&lt;/a&gt;)&lt;/h2&gt;&lt;p&gt;JSpec is a clever little JavaScript testing framework.  I's no longer supported by it's creators (booooo!) but honestly its stable enough to use anyway.  JSpec takes the RSpec DSL (a decent subset at least) and is capable of converting it to JavaScript for execution in the browser.  It's got a heap of stuff in it,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Mock Ajax&lt;/li&gt;&lt;li&gt;Stubbing&lt;/li&gt;&lt;li&gt;Fake timers&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Lets take a look at a simple example,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;describe&lt;/span&gt; &lt;span class="s2"&gt;"Utils.Arrays.compare method"&lt;/span&gt;    
    &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"should compare and sort 2 numeric arrays successfully"&lt;/span&gt;
        &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                
        &lt;span class="nx"&gt;Utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Arrays&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;should&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;        
    &lt;span class="nx"&gt;end&lt;/span&gt;            
    
    &lt;span class="nx"&gt;it&lt;/span&gt; &lt;span class="s2"&gt;"should compare 2 numeric arrays unsuccesfully when not sorted"&lt;/span&gt;
        &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
                
        &lt;span class="nx"&gt;Utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Arrays&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;should&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;        
        &lt;span class="nx"&gt;Utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Arrays&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;should&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;        
    &lt;span class="nx"&gt;end&lt;/span&gt;        
&lt;span class="nx"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;People who have used RSpec before will feel right at home.  People who haven't should be able to understand exactly what is going on.  Pretty - right?  I thought so.  For the people who think — "ugh we don't need another dialect/language" - wise up!  Out of all the testing techs I've used recently this one has been the most successful and the output it generates is nice and clean.&lt;/p&gt;&lt;h2&gt;Should/Should.Fluent (&lt;a href="http://should.codeplex.com/"&gt;http://should.codeplex.com/&lt;/a&gt;)&lt;/h2&gt;&lt;p&gt;This little gem isn't a framework in itself and can be used with any framework you care to use.  Should provides a more expressive way of stating assertions in your code by making the code closer to natural language using extensions methods and nicer method names.  Example I hear you say?  Why certainly sirs and madams,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Should_fluent_assertions&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Null&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OfType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Null&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SameAs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OfType&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"foo"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"x"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"y"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"z"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"z"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SameAs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"x"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="s"&gt;"This String"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Contain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="s"&gt;"This String"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="s"&gt;"This String"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"foobar"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;False&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;True&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
    &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Be&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Should&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Contain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I stole this one from the Should Codeplex site (linked above) and it makes use of the fluent syntax (optional).  OK technically it doesn't do much but it really helps when trying to express assertions in your tests.  Highly recommend this one.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Thats the first lot of technologies covered but there are still plenty out there.  I think my next port of call is to actually spin up IronRuby and get RSpec involved.  After all it is the marker by which I am comparing these things so why did I not jump on it first of all?  Perhaps I like to build up suspense :-P&lt;/p&gt;&lt;p&gt;Also worth pointing out is that I haven't touched upon technologies for mocking and stubbing - that is for another time.&lt;/p&gt;&lt;p&gt;As always heap criticism my way and I'll happily fight my corner and stubbornly refuse to back down :-P (second smilie within a few paragraphs time to end this post).&lt;/p&gt;&lt;p&gt;UPDATE:  I've pushed some of my code to GitHub and intend to expand on this using the various other technologies.  The repository can be found on my &lt;a href="https://github.com/kouphax/unit-testing"&gt;repo on GitHub&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>Serialising .NET Resources to JSON for Web Apps</title><updated>Mon Mar 28 23:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/03/28/serialising-net-resources-to-json-for-web-apps/"></link><id>urn:yobriefca-se:feed:post:Serialising .NET Resources to JSON for Web Apps</id><content type="html">
&lt;p&gt;Pop quiz hotshot - you are building one of them massive single page web apps  using something like Sencha where the vast majority of work is pushed into the  JavaScript realm and some crazy nut wants everything localised or at least  all text strings push to RESX files.  What do you do?  WHAT DO YOU DO?&lt;/p&gt;&lt;h2&gt;Option 1&lt;/h2&gt;&lt;p&gt;Well the most obvious option, and IMHO the most horrible, would be to convert  all those JavaScript files into aspx's or cshtml's or whatever and embed the  resource references directly into the files.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Import Namespace="Some.Resources" %&amp;gt;

var myWidget = new Widget({
    title:       '&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Strings.DefaultWidgetTitle%&amp;gt;',
    description: '&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Strings.DefaultWidgetDescription%&amp;gt;'
});

myWidget.show(document.body);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is far from nice.  For one it kind of makes any static compression of  the files either impossible or at best annoyingly fiddly.  It also prevents any  sort of quick client side caching unless you use some sort of VaryByCulture  Output Caching strategy on the server side.  Blegh.&lt;/p&gt;&lt;h2&gt;Option 2&lt;/h2&gt;&lt;p&gt;The next option, and certainly much better would be to pull out the direct  &amp;lt;%= %&amp;gt; references and store them in another smaller file and reference  them through a global JS object&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%-- DECALRED WITHIN THE MAIN HTML PAGE --%&amp;gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;Strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;DefaultWidgetTitle&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;       &lt;span class="s1"&gt;'&amp;lt;%=Strings.DefaultWidgetTitle%&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;DefaultWidgetDescription&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;%=Strings.DefaultWidgetDescription%&amp;gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myWidget&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Widget&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;       &lt;span class="nx"&gt;Strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DefaultWidgetTitle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Strings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DefaultWidgetDescription&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;myWidget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is nicer because it means you can compress and cache the JavaScript file  globally without having to worry about different cultures etc.  It does leave  one annoyance though.  The Strings object above it essentially boiler plate.   The names are a 1:1 mapping of the resource file so we have introduced a layer  of abstraction we have to write manually leaving us open to make some mistakes.   Slightly less blegh, but still blegh.&lt;/p&gt;&lt;h2&gt;Option 3&lt;/h2&gt;&lt;p&gt;The solution I like the best is to provide a mechanism for serialising the  Resource file into the equivalent JSON object.  You get all the benefits of  option 2 while not having to worry about having to write the mapping file.  So  lets keep the JS file from the second option and change the ASPX file&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%-- DECALRED WITHIN THE MAIN HTML PAGE --%&amp;gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;Strings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="nx"&gt;ResourceSerialiser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ToJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Resource&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Strings&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now lets look at the magic behind this option - the JSON Serialiser&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Utility class that allows serialisation of .NET resource files (.resx) &lt;/span&gt;
&lt;span class="c1"&gt;/// into different formats&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ResourceSerialiser&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cp"&gt;#region JSON Serialisation&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Converts a resrouce type into an equivalent JSON object using the &lt;/span&gt;
    &lt;span class="c1"&gt;/// current Culture&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="resource"&amp;gt;The resoruce type to serialise&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;A JSON string representation of the resource&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ToJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;CultureInfo&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CultureInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CurrentCulture&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;ToJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Converts a resrouce type into an equivalent JSON object using the &lt;/span&gt;
    &lt;span class="c1"&gt;/// culture derived from the language code passed in&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="resource"&amp;gt;The resoruce type to serialise&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="languageCode"&amp;gt;The language code to derive the culture&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;A JSON string representation of the resource&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ToJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;languageCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;CultureInfo&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;CultureInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetCultureInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;languageCode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;ToJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Converts a resrouce type into an equivalent JSON object&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="resource"&amp;gt;The resoruce type to serialise&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="culture"&amp;gt;The culture to retrieve&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;A JSON string representation of the resource&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ToJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CultureInfo&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dictionary&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ResourceToDictionary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SerializeObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dictionary&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#endregion&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Converts a resrouce type into a dictionary type while localising &lt;/span&gt;
    &lt;span class="c1"&gt;/// the strings using the passed in culture&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="resource"&amp;gt;The resoruce type to serialise&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="culture"&amp;gt;The culture to retrieve&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;A dictionary representation of the resource&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ResourceToDictionary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Type&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CultureInfo&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ResourceManager&lt;/span&gt; &lt;span class="n"&gt;rm&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ResourceManager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;PropertyInfo&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;pis&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;resource&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BindingFlags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Public&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;BindingFlags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Static&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;KeyValuePair&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
            &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;pis&lt;/span&gt;
            &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PropertyType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;KeyValuePair&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
                &lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;rm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;culture&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dictionary&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToDictionary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dictionary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Simple enough little class that is configurable by Culture etc. so you can  pull different translation out on demand if needs be.  Obviously it doesn't do  anything around caching - SRP and all that stuff you know :-P&lt;/p&gt;&lt;p&gt;Yeah so I've used this on 2 projects already with great success so hopefully  someone else finds it useful.&lt;/p&gt;</content></entry><entry><title>CoffeeScript: JavaScript sans Bad Parts</title><updated>Sun Mar 13 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/03/13/coffeescript-javascript-sans-bad-parts/"></link><id>urn:yobriefca-se:feed:post:CoffeeScript: JavaScript sans Bad Parts</id><content type="html">
&lt;blockquote&gt;This was written a long time ago when coffeescript was in early beta and I've  decided to push it into the public domain.  I've updated it a bit but I  apologise for any oversights.&lt;p&gt;Also worth mentioning that I am a lot more taken with it than I was at the time of the original blog.  I will be revisiting this again soon.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;If I've said it once, I've said it a million times - JavaScript is  misunderstood.  Sure it's got it's bad parts (eval, ==, typed wrappers etc.) and  it's got it's VERY bad parts (global variables, scope, typeof etc) but it's also  got a lot of beautiful parts (functions as first class objects, object and array  literals, dynamic objects and prototypal inheritance etc.).  JSLint validates  your code against the good subset to ensure you aren't using all that nasty  stuff that is going to break your code eventually but can we take it a step  further?  What if we could take out the good subset and create a subset of the  language so we couldn't ever use the bad stuff?  Well turns out you can.&lt;/p&gt;&lt;h2&gt;Enter CoffeeScript&lt;/h2&gt;&lt;p&gt;&lt;a href="http://jashkenas.github.com/coffee-script/"&gt;CoffeeScript&lt;/a&gt; is a  full featured subset of JavaScript.  Essentially it is a meta-language that  compiles into JavaScript.  Its syntax is a bit different, more like Perl, Python  or Ruby and the JavaScript it generates only uses the "good parts" of JavaScript  and passes all strict JSLint tests.&lt;/p&gt;&lt;h3&gt;Syntax&lt;/h3&gt;&lt;p&gt;CoffeeScripts syntax is close to JavaScript and besides a few extra goodies  it is nearly a 1:1 mapping.  However it strips away a lot of the boilerplate  stuff to make the code you write slightly more succinct.  Another feature of the  syntax is that indentation is important (like Perl or Python, I can never  remember which) as there are no line ending tokens.  So lets look at a few  statements&lt;/p&gt;&lt;p&gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Assignment&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;number&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="nx"&gt;opposite&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Assignment:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;opposite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;opposite&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This simple example of variable creation and assignment demonstrates how  CoffeeScripts avoids one of the most common and potentially fatal mistakes made  by many JavaScript developers - Inadvertent global variable declaration.  In  plain old JavaScript leaving out a var statement on a variable declaration  results in the variable being declared (or overwritten -eek!) in the global  scope.  In CoffeeScript var is optional and all variable declarations are made  in the current scope (and pushed to the top of the scope as is the JavaScript  way).  Want global scope declaration for some random reason?  Use  window.variable name and then ask yourself why you are doing it wrong.&lt;/p&gt;&lt;p&gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Conditions&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;opposite&lt;/span&gt;

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Conditions:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;opposite&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style=""&gt;&lt;a title="http://gist.github.com/868210" href="http://gist.github.com/868210"&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Functions&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Functions:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Function declarations are a good example of how succinct CoffeeScript can be  in comparison to JavaScript.  In fact anyone keen on C#'s lambdas or Groovys  syntax will find this quite familiar.  Gone are the bloating function and return  keywords ( -&amp;gt; is the empty function - function() {})&lt;/p&gt;&lt;p&gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;math&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;
  &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;
  &lt;span class="nx"&gt;cube&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; 

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Objects:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;math&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;math&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;cube&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style=""&gt;&lt;a title="http://gist.github.com/868212" href="http://gist.github.com/868212"&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Existence&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;alert&lt;/span&gt; &lt;span class="s2"&gt;"I knew it!"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;elvis&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Existence:&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;elvis&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"undefined"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;elvis&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"I knew it!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Existence is a nice feature as it keeps non-boolean falsy values nice and  strict and saves a lot of guessing.  That's the basics but as I mentioned  earlier there are some nice bonus features that can make some common tasks very  clean.&lt;/p&gt;&lt;h3&gt;&lt;strong&gt;The Goodie Bag&lt;/strong&gt;&lt;/h3&gt;&lt;h4&gt;Array Comprehension&lt;/h4&gt;&lt;h4&gt;&lt;span style="font-weight: normal;"&gt;&lt;/span&gt;&lt;/h4&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt; &lt;span class="nx"&gt;Comprehension&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Array Comprehension:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_len&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_ref&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;_ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;_i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;_len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;_i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_ref&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nx"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Array comprehension is quite a common task in many JavaScript solutions.   Simply put this is the Map part of any typical MapReduce situations (common in  many NoSQL style databases and Ajax situations).  MapReduce is essentially -  given an array of items,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Map: Transform that list into what you want (SQLs SELECT) &lt;/li&gt;&lt;li&gt;Reduce: Based on that mapping remove unwanted items&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In the Ajax world you'd probably need to do this when dealing with JSON or  XML responses which are quite often generally result sets.  The example above  shows just how easy it is to apply an arbitrary function over an array of  items.  Now I guess the equivalent JS could be written slightly better by hand  but would it not take longer and be harder to maintain?  Probably.  This being  quite a common task the less you have to write the better!&lt;/p&gt;&lt;h4&gt;Splats&lt;/h4&gt;&lt;p&gt;Splats are a convenient way to work with the arguments object in JavaScript  while clearly outlining required and optional arguments.  Lets take this  example.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Splats&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;alert&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt;
&lt;span class="nx"&gt;printResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;runners&lt;/span&gt;&lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="s2"&gt;"Winner:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;runners&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
    &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="s2"&gt;"Runner up"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;runners&lt;/span&gt;

&lt;span class="nx"&gt;printResults&lt;/span&gt; &lt;span class="s2"&gt;"James"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Paul"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Martin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"John"&lt;/span&gt;

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Splats:&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;printResults&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;__slice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"undefined"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;printResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;runners&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_len&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;winner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;runners&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;__slice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Winner:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;runners&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;_results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;runners&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;_i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;_len&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;_i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;runners&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;_i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;_results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Runner up"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;_results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;printResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"James"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Paul"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Martin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Look familiar to you .NET people?  It should because for all intents and  purposes this is the same as the param keyword.  Implementing this sort of thing  in JavaScript is no easy task.  Again the generated JavaScript could be  simplified if written by a human bean but could you get it right first time  without seeing this generated code?&lt;/p&gt;&lt;h4&gt;Destructing Assignment&lt;/h4&gt;&lt;p&gt;Already part of the JavaScript spec but not present in all implementations  (see MDC article) destructing assignments allow the assigning of multiple values  in one fell swoop e.g.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Destructing&lt;/span&gt; &lt;span class="nx"&gt;Assignments&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="nx"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; 

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Multiple&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;
&lt;span class="nx"&gt;weatherReport&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Sunny"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;forecast&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;weatherReport&lt;/span&gt; &lt;span class="s2"&gt;"Belfast"&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Belfast"&lt;/span&gt;
&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;forecast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Sunny"&lt;/span&gt;

&lt;span class="c1"&gt;//-- GENERATED JS &lt;/span&gt;

&lt;span class="c1"&gt;// Destructing Assignments&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;forecast&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;weatherReport&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;_ref2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;_ref&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_ref&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_ref&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// a now = 0, b now = 1000 &lt;/span&gt;

&lt;span class="nx"&gt;weatherReport&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Sunny"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;_ref2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;weatherReport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Belfast"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_ref2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_ref2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;forecast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_ref2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// city = "Belfast"&lt;/span&gt;
&lt;span class="c1"&gt;// temp = 20&lt;/span&gt;
&lt;span class="c1"&gt;// forecast = "Sunny"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;I've never really come across a massive need for something like this but I  guess it's another handy tool to have in the scripting world.&lt;/p&gt;&lt;h3&gt;Should We All Be Using It?&lt;/h3&gt;&lt;p&gt;If you think I am going to say "Yes" immediatley you are wrong.  And if you are now  asking yourself why I even bothered typing this.... lets just say train rides are  boring but not as boring as most of the TV shows my darling wife makes me sit  through EVERY FLIPPING NIGHT OF MY LIFE!!!!&lt;/p&gt;&lt;p&gt;The big reason I am not recommending it yet is that I am not 100% convinced  of the benefits of using it above plain old JavaScript.  It's a bit like  marmite.  Many node.js developers write in CoffeeScript, and ONLY CoffeeScript  but many still simply reject it.  I can understand both standpoints so until I  can say without faltering that one side has got it right I am not taking it  seriously.  That said I prefer to form my own opinions anyway so I need to spend a lot more time with it before I can jump to any conclusions.&lt;/p&gt;&lt;p&gt;Oh and, honestly, who actually really wants to spend what limited time they  have trying new things, hacking around, experimenting, taking risks, getting  frustrated and making plenty of mistakes?  Anyone? Anyone at all?  Nah I didn't think so.  Computers are just a 9-5 job for all of us ;-P&lt;/p&gt;</content></entry><entry><title>Node.js Lightning Talk</title><updated>Thu Mar 10 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/03/10/node-dot-js-lightning-talk/"></link><id>urn:yobriefca-se:feed:post:Node.js Lightning Talk</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;So I held a lightning talk on &lt;a href="http://nodejs.org/"&gt;node.js&lt;/a&gt; the  other day.  A lightning talk is essentially a short (15 minute) talk about a  subject with a bit of discussion at the end of it.  There was a slight technical  hitch in that I forgot to record the session but I have made up for it by  sitting alone in a quiet room early this morning a re-recorded the session –  minus the actual discussion bit.&lt;/p&gt;&lt;p&gt;Obviously it's not as good as the original talk but thought it might be worth  putting out there anyway as it's better than the slides alone.  So here it is –  a very high level view of node.js.&lt;/p&gt;&lt;h2&gt;Video&lt;/h2&gt;&lt;iframe src="https://player.vimeo.com/video/20868544?portrait=0" frameborder="0" height="283" width="500"&gt;&lt;/iframe&gt;&lt;h2&gt;Slides&lt;/h2&gt;&lt;p&gt;&lt;object height="417" width="500"&gt;&lt;param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=node-lightning-talk-110310015300-phpapp02&amp;amp;stripped_title=nodejs-lightning-talk"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;param name="allowScriptAccess" value="always"&gt;&lt;/param&gt;&lt;embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=node-lightning-talk-110310015300-phpapp02&amp;amp;stripped_title=nodejs-lightning-talk" allowfullscreen="true" type="application/x-shockwave-flash" allowscriptaccess="always" height="417" width="500"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;</content></entry><entry><title>Mobile Redirection for Web Applications in ASP.NET MVC</title><updated>Thu Feb 24 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/02/24/mobile-redirection-for-web-applications-in-asp-dot-net-mvc/"></link><id>urn:yobriefca-se:feed:post:Mobile Redirection for Web Applications in ASP.NET MVC</id><content type="html">
&lt;p&gt;&lt;span style=""&gt; &lt;/span&gt;Mobile redirection is simple stuff but what happens when you need to deep link into the mobile application?&lt;/p&gt;&lt;p&gt;On a recent project we needed to produce mobile (iPad specifically)  equivalent.  The desktop app itself was developed using ExtJS (3.3.1) and had  three primary entry points,&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Logon Screen&lt;/li&gt;&lt;li&gt;Home Screen&lt;/li&gt;&lt;li&gt;Product Screen (New/View/Edit)&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Due to the way the project was implemented these became as 3 distinct  pages/controllers/actions.  Users could receive emails with links to a  particular product and they would go directly to that view (with a login  redirect if not previously authenticated).  The mobile solution, however,  written using Sencha Touch (consistent development experience, native-esque UI  with little effort) is a single page application.  This presents a problem when  the user is on a compatible mobile device and they receive a link to a  particular placement - how do we push that sort of deep linking into a single  page app.  Well on the client side frameworks such as Backbone.js, jQuery Mobile  and Sencha Touch [anyone got more please?] all offer history support using hash  navigation.  That's the client side sorted but how do we translate, say,  /Product/Show/12345 into /Mobile#placement/12345?&lt;/p&gt;&lt;h2&gt;MobileRedirectAttribute&lt;/h2&gt;&lt;p&gt;Firstly I created an extension of the AuthorizationAttribute that will act as  an interim redirection and request parser between the mobile and desktop  solutions.  Here's the code (usage follows),&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Redirects to the mobile view if on a supported device&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="na"&gt;[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MobileRedirectAttribute&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AuthorizeAttribute&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;_clientFragment&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Default Constructor&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MobileRedirectAttribute&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_clientFragment&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Constructor that takes an argument&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="clientUrl"&amp;gt;The url fragment we should append to the url&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MobileRedirectAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;clientFragment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_clientFragment&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clientFragment&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Tests if this request originates from a supported mobile device &lt;/span&gt;
    &lt;span class="c1"&gt;/// and redirects as appropriate&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="ctx"&amp;gt;The action execution context&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnAuthorization&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AuthorizationContext&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Browser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsMobileDevice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// parse the fragment with request parameters&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;fragment&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ParseClientFragment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// construct the redirect url&lt;/span&gt;
            &lt;span class="n"&gt;UrlHelper&lt;/span&gt; &lt;span class="n"&gt;urlHelper&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;UrlHelper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RequestContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{0}#{1}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;urlHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Index"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Mobile"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;fragment&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// return redirect result to prevent action execution&lt;/span&gt;
            &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;RedirectResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;            
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Parses the client fragment and replaces :[token] with the request parameter&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="ctx"&amp;gt;The controller context&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;The parsed fragment&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ParseClientFragment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ControllerContext&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;parsedFragment&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_clientFragment&lt;/span&gt; &lt;span class="p"&gt;??&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsedFragment&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;NameValueCollection&lt;/span&gt; &lt;span class="n"&gt;@params&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;MatchCollection&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_clientFragment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;":[a-zA-Z]+"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;RouteData&lt;/span&gt; &lt;span class="n"&gt;routeData&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;RouteTable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Routes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetRouteData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// check each token and replace with param or route values&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Match&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;matches&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TrimStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;':'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;@params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

                &lt;span class="c1"&gt;// if we haven;t got a parameter here we must check the route values&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;routeData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContainsKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;routeData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;

                &lt;span class="c1"&gt;// perform the replace&lt;/span&gt;
                &lt;span class="n"&gt;parsedFragment&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parsedFragment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;parsedFragment&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;h2&gt;Usage&lt;/h2&gt;&lt;p&gt;So for our 3 entry points into our application we attribute the controller  actions with the MobileRedirectAttribute and give it a client fragment.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProductController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [MobileRedirect("[product/:id")]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Nullable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// perform action&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [MobileRedirect("home")]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthenticationController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [MobileRedirect("home")]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The client fragment is capable of translating tokens embedded within it ( as  :&amp;lt;token_name&amp;gt;) and replacing the token with a matching route value or  request parameter.  The ProductController Index action is a good example of  this.  A request to /Product/Index/12345 on a mobile device would translate  to /Mobile/#product/12345&lt;/p&gt;&lt;h2&gt;How It Works&lt;/h2&gt;&lt;p&gt;Pretty simple really.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The attribute checks if the device is a compatible/mobile device.&lt;/li&gt;&lt;li&gt;If it is the retrieves the client fragment and extracts the tokens -  :&amp;lt;token_name&amp;gt;&lt;/li&gt;&lt;li&gt;It attempts to match the token names against request parameters first&lt;/li&gt;&lt;li&gt;If there is no parameter it then looks into the route values (eg. :id in the  above url isn;t a parameter but rather a route value)&lt;/li&gt;&lt;li&gt;It replaces the token with the real value&lt;/li&gt;&lt;li&gt;It performs a redirect to /Mobile#&amp;lt;client_fragment&amp;gt; which cancels the  execution of the action.&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Other Points&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;It's probably not the most robust solution in that more complex scenarios  may not work as expected but it's a decent base that can be extended. &lt;/li&gt;&lt;li&gt;I have hardcoded the mobile route as it fitted my needs so I think that  should be externalised as well.&lt;/li&gt;&lt;li&gt;The determination of whether a device is a compatible device is facilitated  through Browser.IsMobileDevice.  This is for demonstration purposes only.  In  the project we use a different solution but it is a bit more long winded to  explain here.&lt;/li&gt;&lt;li&gt;The "redirect to logon" handling is performed within the app itself so that  is why the Logon view's fragment is simply "home".&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;h2 style="font-size: 1.5em;"&gt;Demo&lt;/h2&gt;&lt;p&gt;I've pushed a very quick and dirty demo of this onto GitHub for anyone interested - &lt;a href="https://github.com/kouphax/mobileredirect-mvc/"&gt;https://github.com/kouphax/mobileredirect-mvc/&lt;/a&gt; .  It uses a really quick UserAgent.Contains("iPad") check for "mobile" detection so use and iPad or set your User Agent to try it out.&lt;/p&gt;&lt;ul&gt;&lt;/ul&gt;&lt;p&gt;Any use to anyone out there?  Any problems with it?  Let me know.&lt;/p&gt;</content></entry><entry><title>Entity Framework: Code First - Head First</title><updated>Fri Feb 11 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/02/11/entity-framework-code-first-head-first/"></link><id>urn:yobriefca-se:feed:post:Entity Framework: Code First - Head First</id><content type="html">
&lt;p&gt;I find the data access layer on most projects to be either overly complex or  fiddly with lots of XML mapping files that are difficult to debug so anything  that could make this layer more developer friendly I'm all for it.  Though I  also want to point out that I understand why the DAL is often complex - there is  a lot to consider and so I want to also understand if these "friendlier"  technologies can handle that sort of complexity or if they simply make the happy  path easier but making the more complex scenarios more difficult or even  impossible (which is obviously a blocker).&lt;/p&gt;&lt;p&gt;So I've been tinkering with this new Entity Framework CTP5 release and the  "Code First" features recently.  This comes after some time-out from EF due to  some really bad experiences with EF1. I was promised that there has been  significant changes/improvements since I last dabbled and it really seems there  have been.  So I wanted to put it to the test and as one of my co-workers wanted  an "Ideas" app I thought it would be a fun [may not be anyone's definition of  fun but my own] to throw an MVC app together using EF "Code First" to model my  domain entities.  To make it all even more simple I went ahead and used SQL  Server CE 4 for persistence.  So what did the solution need to do?  The basic  requirements were,&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Use Windows Authentication for users &lt;/li&gt;&lt;li&gt;Allow users to submit an idea (Title, Description) &lt;/li&gt;&lt;li&gt;Allow users to tag an idea with a variable number of tags &lt;/li&gt;&lt;li&gt;Allow users to vote up or vote down ideas (but not their own) &lt;/li&gt;&lt;li&gt;Allow users to comment on ideas &lt;/li&gt;&lt;li&gt;Allow users to filter ideas by tags &lt;/li&gt;&lt;li&gt;Allow users to sort ideas by newest ideas or by most popular. &lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Nothing too extreme involved - not unless you turn the whole thing into a  computer game style EXTREME speed run competition - man vs. machine - the  ULTIMATE [typing] battle.... with bathroom and snack breaks!  Just to make it even  more INSANE I documented my steps and created a graphical timeline of the  session in a PRETTY timeline. &lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm3.static.flickr.com/2244/5720966217_edd48fc966_o.png" width="500" /&gt;&lt;/p&gt;&lt;p&gt;Ammmm don't mean to be rude but your jaw.... we'll it's on the floor.  Can you  pick it up please?  17:21 to 20:38 minus about an hour and a bit for bathroom,  snack and chat breaks - zero to datafied in less than 3 hours!  Few points to  note,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;This experiment focused on the data model, EF CTP5 and the database. &lt;/li&gt;&lt;li&gt;There is a working UI (MVC3) it's just not exactly pretty &lt;/li&gt;&lt;li&gt;I had no EF "Code First" experience before hand &lt;/li&gt;&lt;li&gt;I could be doing a few things incorrectly &lt;/li&gt;&lt;li&gt;It'll probably take me longer to write this post than it did the app. &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;So lets look at what I produced.  The source is &lt;a href="https://github.com/kouphax/ideas/"&gt;available on Github&lt;/a&gt;* for  your fiddling pleasure.&lt;/p&gt;&lt;p&gt;I am not going to dive into the whole MVC part of it as the source is  available but I may touch on some of the interface points such as controllers  and binders.&lt;/p&gt;&lt;h2&gt;The Domain Models&lt;/h2&gt;&lt;p&gt;Lets take a high level look at our domain models.&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm4.static.flickr.com/3554/5721525430_5e527060d2_o.png" width="650" /&gt;&lt;/p&gt;&lt;h3&gt;DomainEntity&lt;/h3&gt;&lt;p&gt;The abstract domain entity is used to prevent me having to repeat common  auditing and database related stuff across all my entities.  It is not mandatory  or derived from anything related to Entity Framework - all these classes are  simple POCO's.  DomainEntity sets up the entities primary key using the Key  attribute and also gold 2 audit related properties CreatedBy and  CreatedDate.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Base class for domain entities responsible for holding auditing and &lt;/span&gt;
&lt;span class="c1"&gt;/// persistence related properties&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DomainEntity&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [Key]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;CreatedBy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt; &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Idea&lt;/h3&gt;&lt;p&gt;Idea is our principle class in our domain.  As you can see there are various  associations set up between the other classes.  2 1-* mappings between Comment  and Vote and a *-* mapping between itself and tag (a tag can exist for any  number of ideas and an idea can have many tags).  It also holds a number of  methods related to business logic - specifically calculating Votes, number of  Comments etc.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Main domain object in the idea solution.  Represents an idea&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Idea&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainEntity&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [Required]&lt;/span&gt;
&lt;span class="na"&gt;    [MaxLength(255)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Title&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Description&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [DefaultValue(false)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;IsRejected&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cp"&gt;#region Associations&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;ICollection&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Comments&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;ICollection&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Tags&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;ICollection&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Vote&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Votes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#endregion&lt;/span&gt;

    &lt;span class="cp"&gt;#region Business Logic&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Score&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Votes&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Votes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;VoteCount&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Votes&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Votes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;CommentCount&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Comments&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Comments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#endregion&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Tag&lt;/h3&gt;&lt;p&gt;Tag is pretty simple.  The only interesting thing about it is the use of  NormalisedName - essentially the name field lowercased and whitespace removed.   This is used when attempting to fetch potentially existing tags from the  database.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Represents a tag in the idea solutuion&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tag&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainEntity&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="na"&gt;        &lt;/span&gt;
&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;set&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;NormalisedName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToLower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;NormalisedName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cp"&gt;#region Associations&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;ICollection&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Idea&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Ideas&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#endregion&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;h3&gt;Vote&lt;/h3&gt;&lt;p&gt;Rather than just store a calculated value against an idea the Vote object  represents a rich representation of a Vote (either up or down, whom by and  when).  This allows us to provide extra validation when we need it.  For example  people not allowed to vote on their own idea or vote on an idea in any  particular direction more than once.  Having this rich association makes these  things much easier and we aren't forced to create custom objects to track this  sort of thing.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Represents a single vote for an idea&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vote&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainEntity&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [Required]&lt;/span&gt;
&lt;span class="na"&gt;    [Range(-1,1)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cp"&gt;#region Associations&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Idea&lt;/span&gt; &lt;span class="n"&gt;Idea&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#endregion&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Comment&lt;/h3&gt;&lt;p&gt;Nothing special here. &lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Represents an ideas comment&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DomainEntity&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Gets or sets the comments content&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cp"&gt;#region Associations&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Gets or sets the link to the parent idea&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;virtual&lt;/span&gt; &lt;span class="n"&gt;Idea&lt;/span&gt; &lt;span class="n"&gt;Idea&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#endregion&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;The Database Context&lt;/h2&gt;&lt;p&gt;This is where all the EF magic happens.  We use this class to provide an  entry point into our database.  It's possible to configure entities here in  terms of mapping and associations as well as providing a means to seed the  database with initial data but I didn't need any of that.  No I just defined my  sets and added a method for filtering/sorting ideas based on criteria.  Simple  stuff yet again.  It just extends the DbContext class from Entity Framework.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Data Repository for the ideas solution&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IdeaRepository&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DbContext&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cp"&gt;#region Db Sets&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DbSet&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Comment&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Comments&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DbSet&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Idea&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Ideas&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DbSet&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Tags&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;DbSet&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Vote&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Votes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="cp"&gt;#endregion&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Main entry point for querying the ideas dat&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="filters"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IList&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Idea&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;QueryIdeas&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IdeaFilter&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;IQueryable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Idea&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ideas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ideas&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;IdeaFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrderBy&lt;/span&gt; &lt;span class="n"&gt;orderBy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;IdeaFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrderBy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MostVotes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNullOrWhiteSpace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;ideas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ideas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="n"&gt;orderBy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetValueOrDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IdeaFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrderBy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MostVotes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;IdeaFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrderBy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MostVotes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;ideas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ideas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrderByDescending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Votes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;IdeaFilter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrderBy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Newest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;ideas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ideas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;OrderByDescending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreatedDate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ideas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;            
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Tag Model Binder&lt;/h2&gt;&lt;p&gt;This was an interesting thing I discovered.  If you are using a *-*  relationship and are associating one side with an object that already exists you  are required to fetch this object before using it.  For example when adding a  tag to an idea I need to attempt fetch that tag first of it exists.  What I  can't do is create a new tag object and assign an existing Id to it - this will  be thrown away and saved as a new instance.  To fix this problem I feel back  onto a Tag Model binder that attempts to fetch or create tags depending on their  normalised name.  It won't save new tag - simply create them (this is why I use  a shared DbContext between the controller and the binder).  The binder takes a  CSV styled string, breaks it apart, "normalises" the string and tries to fetch  tags based on their normalised name.  If it finds one it pushes it into the  collection otherwise it creates a new tag object and pushes that in instead.   Probably a better way to do that and I am open to suggestions.  But what I don't  want is saving tags that are then going to become orphaned if the other save  didn't go through for some reason.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Converts a string of tags (comma seperated) into a list of tags - &lt;/span&gt;
&lt;span class="c1"&gt;/// creating new ones where necessary and fecthing exisitng ones&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TagCollectionModelBinder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;DefaultModelBinder&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="nf"&gt;BindModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ControllerContext&lt;/span&gt; &lt;span class="n"&gt;controllerContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ModelBindingContext&lt;/span&gt; &lt;span class="n"&gt;bindingContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;            
        &lt;span class="n"&gt;HttpContextBase&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;controllerContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Identity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;bindingContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelName&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IdeaRepository&lt;/span&gt; &lt;span class="n"&gt;_db&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Utils&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BaseIdeaController&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DataContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNullOrWhiteSpace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;clientTags&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;','&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;clientTag&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;clientTags&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;normalised&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clientTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToLower&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Empty&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FirstOrDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NormalisedName&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;normalised&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Tag&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;CreatedBy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;CreatedDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;clientTag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Trim&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                    &lt;span class="p"&gt;};&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;

                &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Other Things&lt;/h2&gt;&lt;p&gt;Not very much else worth mentioning right now as I more or less used the MVC  scaffolding to build the views (with some minor tweaking).  Validation on the UI  is pushed from the domain object making things a lot more streamlined.  The  controllers are still very light and could be made lighter by pushing stuff into  the IdeaRepository as well but that's for another day.&lt;/p&gt;&lt;p&gt;So there you go.  A very quick and dirty intro into the world of Entity  Framework.  There isn't anything complex going on here and I was worried that EF  would mask a lot of stuff that we would need access to but it seems there is  plenty of configuration points to hook into.  It has come on leaps and bounds  since I last dipped my toes into EF and hopefully they keep up the same  momentum.  There is still a lot of due diligence required before I'd recommend  EF over any other data access layer that we are currently using but I am  certainly keen to dig deeper and push it to it's limits.&lt;/p&gt;&lt;p&gt;Once again the source for the solution is &lt;a href="https://github.com/kouphax/ideas/"&gt;available on Github&lt;/a&gt;*.  Phew..... &lt;/p&gt;&lt;p&gt;&lt;em&gt;* Expect bugs.&lt;/em&gt;&lt;/p&gt;</content></entry><entry><title>MvcConf2: Post Conference Rubdown</title><updated>Wed Feb 09 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/02/09/mvcconf2-post-conference-rubdown/"></link><id>urn:yobriefca-se:feed:post:MvcConf2: Post Conference Rubdown</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;So MvcConf has been and gone.  Lots of interesting talks though sadly I hit a ton of technical issues with laggy video and eventually total audio/video loss.  To my ISP – shame on you!  Luckily I did get to see most of the things I wanted (or at least hear).  Here's the final outlay of talk I attended (green for attended, yellow for not attended),&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm4.static.flickr.com/3660/5720976997_50da11c786_o.png" style="width:100%;" /&gt;&lt;/p&gt;&lt;p&gt;I kind of wanted to attend,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Talks that I already had some experience in to see if my thoughts where aligned with others, and, &lt;/li&gt;&lt;li&gt;Talks that I had not experience in but had an interest in. &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Problem was that in some of these talks I felt a bit out of my depth which wasn't helped by the fact my video was busy drawing part of slides 4 and 5 when the presenter was already on slide 7 :(.  Other times I felt I had already dived deeper than the talk was covering.  I guess it happens!  Anyway enough complaining lets see what we covered,&lt;/p&gt;&lt;h2&gt;Real World Application Development with Mvc3 NHibernate, FluentNHibernate and Castle Windsor&lt;/h2&gt;&lt;p&gt;Presenter: Chris Canal&lt;/p&gt;&lt;p&gt;I went into this talk wanting to get a quick glance at NHibernate specifically.  I've had no exposure to any of these technologies (bar MVC3 of course) and I think I suffered because of it.  It was very Castle Windsor focused and I think I would have been better jumping into a pure NHibernate talk if there was one.  That said I really should have a look at Castle Windsor.&lt;/p&gt;&lt;h2&gt;BDD in ASP.NET MVC using SpecFlow, WatiN and WatiN Test Helpers&lt;/h2&gt;&lt;p&gt;Presenter: Brandon Satrom&lt;/p&gt;&lt;p&gt;I recently attended a talk on using Cucumber to provide Automated UI testing on our projects (held internally in our company) so I wanted to see what SpecFlow brought to the table over running Ruby and Cucumber.  I was very impressed with what I seen.  Nice explanation of the theory behind BDD.  I like the whole “Working Software” vs “Working Software &lt;strong&gt;That Matters&lt;/strong&gt;” comparison.&lt;/p&gt;&lt;p&gt;In terms of SpecFlow itself I like the tighter integration into .NET, the ability to debug your steps is very nice and might help increase acceptance in a team when compared to introducing Ruby.  I can see the arguments for using Ruby – Cucumber is a more mature, Ruby is faster to setup and tear down, using the right tool for the right job etc. but I can also see the arguments against.  At least both farmeworks use the Gherkin language so writing the scenarios is done in the same way.  This is something I have to mull over a bit more.&lt;/p&gt;&lt;h2&gt;Quality Driven Web Acceptance Testing&lt;/h2&gt;&lt;p&gt;Presenter: Amir Barylko&lt;/p&gt;&lt;p&gt;I had a few audio and video problems during this presentation.  It touched on some theory behind Acceptance testing, Green Field vs Brown Field and things like that then dropped into using Cucumber for some acceptance testing.  Having just attended the same type of talk a few days previous there wasn't anything new here, and given the bad connection I skipped the rest of the talk.&lt;/p&gt;&lt;h2&gt;Keynote&lt;/h2&gt;&lt;p&gt;Presenter: Scott Guthrie&lt;/p&gt;&lt;p&gt;The Gu talking about stuff – nuff said.  Nice to see him describing how cross pollination of the three platforms (WebMatrix, WebForms, MVC) works (Razor from WM, Routing from MVC etc.).  Also nice to hear that we can expect an MVC release on a near annual basis with plenty of preview releases inbetween!&lt;/p&gt;&lt;h2&gt;CQRS and Event Sourcing with MVC 3&lt;/h2&gt;&lt;p&gt;Presenter: Ashic Mahtab&lt;/p&gt;&lt;p&gt;Wow – CQRS is something I'd heard of previously and it's a pretty big buzz word in the Silverlight world at the minute so I wanted to check it out.  My head was thoroughly melted after this one.  Quite fast paced and I could have benefited from getting some prior reading in and around the CQRS concept.  Will revisit this once the video and slides are available.&lt;/p&gt;&lt;p&gt;One unexpected thing I got out of this talk was &lt;a href="https://github.com/machine/machine.specifications"&gt;MSpec&lt;/a&gt;.  I always wondered how things like cucumber and spec flow handled lower level unit testing and it seems they aren't really suited to that sort of thing.  &lt;a href="https://github.com/machine/machine.specifications"&gt;MSpec&lt;/a&gt;, as I have discovered, is essentially a unit level focused BDD framework.  Going to check this out very soon.&lt;/p&gt;&lt;h2&gt;Deploy ASP.NET MVC with No Effort&lt;/h2&gt;&lt;p&gt;Presenter: Troels Thomsen&lt;/p&gt;&lt;p&gt;Honorable mention here as my connection was flat out dying on this room.  I couldn;t make out video or audio very well.  I did spend a few hours a few days ago looking at &lt;a href="https://appharbor.com/"&gt;appharbor&lt;/a&gt; and it looks like, as the name says, “Azure done right”.  Love the use of git as a deployment framework, love the minimal interface, love the pricing model, just love the whole thing – so I'm sorry I had to miss the presentation.  Another one to check out later.&lt;/p&gt;&lt;h2&gt;Entity Framework "Code First": Domain Driven CRUD&lt;/h2&gt;&lt;p&gt;Presenter: Chris Zavaleta&lt;/p&gt;&lt;p&gt;Having been playing with EF CTP5 recently I wanted this one to see if there was anything worth expanding on.  Turns out there is some nice stuff I didn't see during my tinkering.  Especially around the use of Configuration Categories to break apart the OnModelCreating configuration and the Seeding stuff to populate the DB with data.  Again my audio was dropping off constantly and my video was struggling so I had to drop out early on this one and throw in the towel.&lt;/p&gt;&lt;h2&gt;Summing Up&lt;/h2&gt;&lt;p&gt;All in all a decent conference I could have done with reading up on some of the subjects before hand.  I will be visiting many of talks again when they come online to get a better picture (pun intended) of the topics.  Big thanks to all involved.&lt;/p&gt;</content></entry><entry><title>jQuery 1.5 Released</title><updated>Tue Feb 01 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/02/01/jquery-1-dot-5-released/"></link><id>urn:yobriefca-se:feed:post:jQuery 1.5 Released</id><content type="html">
&lt;p&gt;Official Post: &lt;a href="http://blog.jquery.com/2011/01/31/jquery-15-released/"&gt;http://blog.jquery.com/2011/01/31/jquery-15-released/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Quick post just spread the word that jQuery 1.5 dropped today which is right  on time according the the schedule :) .  I guess it's a bit pointless because  the people that care already know this, but humour me!&lt;/p&gt;&lt;h2&gt;New Stuff&lt;/h2&gt;&lt;p&gt;There isn't a heap of "new to jQuery" stuff in this release which I guess is  a good thing.  The MS contributed plugins have stayed just that - plugins.   Previous talk of templating becoming absorbed into the jQuery core in 1.5 has  failed to bear fruit.  Good stuff - not sure why it should have been a core  function - there is no one right templating strategy/engine and putting one in  the core is sure to annoy as many people as it will please others.  So what's  new?&lt;/p&gt;&lt;h3&gt;Deferred Objects&lt;/h3&gt;&lt;p&gt;Deferred Objects are objects that return a contract that will be adhered to  regardless of when actions are bound to it.  This forms the heart of the Ajax  rewrite which I'll touch on in a minute.  This is essentially the &lt;a href="http://en.wikipedia.org/wiki/Futures_and_promises"&gt;Promise&lt;/a&gt; implementation  pattern.  Basically when you execute a function that returns a Deferred object  you can keep attaching callbacks to that object and they will get executed even  if they are bound after the function completes whatever operation it has to do.   The jQuery example demonstrates this very well,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;// Assign handlers immediately after making the request,&lt;/span&gt;
&lt;span class="c1"&gt;// and remember the jxhr object for this request&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;jxhr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"example.php"&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"success"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"error"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"complete"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// perform other work here ...&lt;/span&gt;

&lt;span class="c1"&gt;// Set another completion function for the request above&lt;/span&gt;
&lt;span class="nx"&gt;jxhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt; &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"second complete"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So basically at any point in time we can attach callbacks to the ajax  promise, pass it around our system, let other functions attach handlers etc and  it will all get resolved/executed when the call has completed.  It's nice stuff  though it may not have a TON of uses.&lt;/p&gt;&lt;h3&gt;jQuery.sub()&lt;/h3&gt;&lt;p&gt;Similar in a way to the YUI3 sandboxing technique jQuery.sub() lets us create  a clone or subclass of the global jQuery object and hack it to bits for our own  needs without affecting the global jQuery object.  Again limited uses but when  you need it it can be a godsend.  Here is a very contrived example,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sub$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;sub$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;// some sort of stub for ajax testing&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// run tests involving ajax here&lt;/span&gt;
    &lt;span class="c1"&gt;// ... &lt;/span&gt;
&lt;span class="p"&gt;})(&lt;/span&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;You see what I am doing?  Say I wanted to perform some sort of unit test  involving calls to the Ajax method.  I could, in isolation, make the Ajax method  return whatever I want without worrying about breaking other tests. &lt;/p&gt;&lt;h2&gt;Improved/Refactored Stuff&lt;/h2&gt;&lt;h3&gt;Ajax Rewrite&lt;/h3&gt;&lt;p&gt;The ajax module has been rewritten entirely.  It will now return a special  jQuery based XHR object (rather than a plain XHR object).  This object is a  deferred object that I spoke about above.  This means we can pass the ajax  request around and let components or modules attach their own handlers when  they need to.  We can also bind multiple callbacks via the jQuery chaining  pattern (see example above)&lt;/p&gt;&lt;h3&gt;Moar Speed&lt;/h3&gt;&lt;p&gt;As usual many of the DOM traversal methods have been given performance tweaks  - .children(), .prev(), and .next().  The official post has shiny graphs and  such so hop on over there for a full run down.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;I hate that word but I don't have another word I like better.... Yes so I  haven't played with this a great deal recently, just some superficial tinkering  but it's a solid release that adds some nice features (with limited uses for  most I suspect).  I mean they could have kept deferred objects internal - they  were implemented for the Ajax stuff - but why not make it public - could be  useful to many people, and that is a good attitude. &lt;/p&gt;&lt;p&gt;Also good decisions were made to keep templating out of the core I think  (others may disagree but that's life!).  Finally this is going to be the jQuery version used in jQuery Mobile when it hits shortly!&lt;/p&gt;</content></entry><entry><title>.NET Web Technology Stack in 2011 and Beyond....</title><updated>Mon Jan 24 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/01/24/dot-net-web-technology-stack-in-2011-and-beyond-dot-dot-dot/"></link><id>urn:yobriefca-se:feed:post:.NET Web Technology Stack in 2011 and Beyond....</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;Change, or lack thereof is often an indication of how successful your technology stack is.  If you are constantly swapping out core technologies then chances are you'll need to rethink your approach.  If, on the other your stack remains fairly stable save a few new additions or tweaks then odds are you made the right choices at the start. &lt;/p&gt;&lt;p&gt;Last year, after careful consideration, I published an internal post that detailed my recommendations for the standard Microsoft Web Technology stack.  Looking back on this I feel happy enough that my choices have proven themselves.  But time goes on and technologies change so perhaps it's time for another look at the stack.  Perhaps give it a bit of a refresh after all there has been a number of great developments in the .NET world.  Do I need to refresh it?  No not really – I could quite easily stick with my previous stack and truth be told the changes are fairly minimal – mostly version bumps.  But I also want to look to the future, is there anything worth keeping an eye on (there is BTW), is there anything we should be avoiding (probably) etc.&lt;/p&gt;&lt;h2&gt;Recap&lt;/h2&gt;&lt;p&gt;Lets take a very quick run through of the recommended stack from last year.&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm3.static.flickr.com/2145/5720987951_2b7e6ccbd2_o.png" /&gt;&lt;/p&gt;&lt;p&gt;The choice of client side technology really depended on the project requirements.  Essentially I suggested most projects use jQuery and some jQueryUI stuff.  If the UI required a higher degree of complexity/richness I recommended moving towards Sencha and Silverlight with Silverlight being the preferred option unless there was some project requirement that blocked the use of browser plugins.  That's the general gist anyway – I can elaborate if anyone wants to hear more.&lt;/p&gt;&lt;h2&gt;Refresh&lt;/h2&gt;&lt;p&gt;So I'm recommending a few minor tweaks in an attempt to keep the stack up to date.  After all .NET4 and Visual Studio 2010 have dropped since I last made my recommendations.  Here goes&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm3.static.flickr.com/2312/5721546972_c2cfd6bf2a_o.png" /&gt;&lt;/p&gt;&lt;p&gt;Not exactly a major overhaul and it shouldn't be.  The stack is simple enough to handle many types of project and has been fairly successful since it was put forward.  So I wanted to keep moving in that direction while bumping the versions up to the latest and greatest.  So lets attempt to justify some of these changes.&lt;/p&gt;&lt;h3&gt;MVC3&lt;/h3&gt;&lt;p&gt;Look at the past posts on this blog and you will see I've spent a lot of time with MVC and, more recently MVC3 (Beta – RC – RTM).  This blog doesn't cover half the MVC hacking I do on a daily basis.  So MVC3 is just released and that might make some people nervous about using it as the primary basis for a web stack. I'm not - it's a quality release both in terms of features and stability.  MVC3, for the most part, should be considered more as a refinement of MVC2 with a nice new shiny ViewEngine slapped on top.  There really isn't much there that couldn't be done in MVC2 with a bit of custom code.  But that's good it kind of hints that they were on the right path to begin with.  But the enhancements it does offer make development a much cleaner experience.  I already have a heap of blogs on the subject so if you want to know more about my views on MVC3 have a look at these,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://yobriefcase.posterous.com/aspnet-mvc-3-preview-1-first-look"&gt;ASP.NET MVC3 Preview 1 First Look&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://yobriefcase.posterous.com/aspnet-mvc3s-jsonvalueproviderfactory" title="http://yobriefcase.posterous.com/aspnet-mvc3s-jsonvalueproviderfactory"&gt;ASP.NET MVC3 JsonValueProvider&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://yobriefcase.posterous.com/remote-validation-in-aspnet-mvc-3-rc1"&gt;Remote Validation in ASP.NET MVC3 RC1&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://yobriefcase.posterous.com/unobtrusive-javascript-in-mvc3" title="http://yobriefcase.posterous.com/unobtrusive-javascript-in-mvc3"&gt;Unobtrusive JavaScript in MVC3&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Razor View Engine&lt;/h3&gt;&lt;p&gt;Razor is Microsoft's internally developed View Engine.  It could be seen as a successor to the old clunky WebForms View Engine.  I am not going to get into the syntax of Razor and how it is much cleaner than the WebForms syntax, no, if you want to digest all that have a look at one of my earlier posts – &lt;a href="http://yobriefcase.posterous.com/the-razor-view-engine"&gt;The Razor View Engine&lt;/a&gt;.  In that post I also discuss the differences between Razor and Spark – my previous View Engine recommendation.  So what has changed?  Why am I recommending Razor over Spark now?  Well as the previous post says it best with these lines,&lt;/p&gt;&lt;blockquote class="posterous_medium_quote"&gt;&lt;p&gt;[Spark is] a great engine but has suffered from a lack of tooling.&lt;/p&gt;&lt;p&gt;Spark, especially in comparison to Razor, has very limited tooling support in VS2010 – sure you have SparkSense but it's currently not a patch on Razor support (which is still in itself RC status) and I can't help but feel they'll always be playing catch-up with in house developed/supported stuff.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;That view hasn't changed.  Razor has a brighter future simply because of the in-house support being offered by Microsoft.  Also there isn't an official release of Spark that works against MVC3 (still built against MVC2 dlls) – it is possible to grab a branch off Github though.  It's a shame though and I wish all the best for Spark in the future – perhaps our paths will cross again?&lt;/p&gt;&lt;p&gt;The last thing I want to mention about view engine choice – or at least ask – is does it matter?  The answer, of course, is not really.  Your views should be very light and not have a lot of serverside logic built into them.  So sticking with one view engine isn't going to ruin anyone's life.&lt;/p&gt;&lt;h3&gt;jQuery/jQueryUI&lt;/h3&gt;&lt;p&gt;OOTB integration with MVC is the most obvious reason for sticking with jQuery here.  Also the fact that it has proven popular in many of our previous projects means it'll be sticking around for some time.  The majority of our companies web projects can be catered for by jQuery (and sometimes including jQueryUI for some richer elements) as a decent base to write custom code.  It may not be necessary in all projects of course and shouldn't be abused but that will depend on the project but I am confident enough to say 95% of our web based projects (not including mobile which I will touch on shortly) will feature jQuery and a few plugins to get the job done.  All we need to do is bump the version up to the latest and we are flying.&lt;/p&gt;&lt;h3&gt;Sencha (version 3)&lt;/h3&gt;&lt;p&gt;OK I am still recommending Sencha/ExtJS for the small niche of projects that need a lot of richness but can't make the move to Silverlight.  As before I am caveating this (or covering my ass).  Sencha is a complex beast it has a steep learning curve and simply hacking away at it in a project is never going to be the best approach.  It's best to avoid it but if jQueryUI doesn't cut it and Silverlight can't be deployed make sure the developers have plenty of time to get up to speed, have a capable mentor and code is checked for any bad smells early and often.  Done right Sencha can produce brilliant results – but getting it right can be tricky.&lt;/p&gt;&lt;h2&gt;Revolution&lt;/h2&gt;&lt;p&gt;As always on the Web front things are changing fast.  There new techs and new versions always round the corner.  Some will be relevant, some just novelty.  Obviously to stay ahead of the game we want to be keeping an eye on the technologies and applying some scientific rigour to determine if these should be brought into the recommended web stack.  Here is a very quick rundown of my “ones to watch in 2011”.&lt;/p&gt;&lt;h3&gt;Knockout.js &amp;amp; Backbone.js&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://knockoutjs.com/"&gt;&lt;/a&gt;&lt;a href="http://knockoutjs.com"&gt;http://knockoutjs.com&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://backbonejs.org/"&gt;&lt;/a&gt;&lt;a href="http://backbonejs.org/"&gt;http://backbonejs.org/&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Complex UI require a bit of structure or you'll end up with a lovely pile of spaghetti, the bad kind not the tasty good kind.  jQuery and jQueryUI don't really offer anything in this area – sure you can get a collection of plugins to solve this but the best approach is a single unified lightweight framework that does one thing in a simple and consistent manner.  Both Knockout and Backbone provide this but in via different routes.  Knockout.js offers an MVVM framework similar to Silverlight/WPF data binding while Backbone offers a minimal MVC framework.  It's hard to say at this point which is better – both have their pros and cons but that is for another post.&lt;/p&gt;&lt;h3&gt;jQuery 1.5&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://blog.jquery.com/2011/01/14/jquery-1-5-beta-1-released/"&gt;&lt;/a&gt;&lt;a href="http://blog.jquery.com/2011/01/14/jquery-1-5-beta-1-released/"&gt;http://blog.jquery.com/2011/01/14/jquery-1-5-beta-1-released/&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Looks to be more betterness from the jQuery team (yes i know it's not a real word).  So far there has been a re-architecting of the Ajax component so that you can bind callbacks at anytime (thanks the &lt;a href="http://en.wikipedia.org/wiki/Promise_(programming)"&gt;Promise&lt;/a&gt; pattern).  This pattern is likely to make it into the public API as well so your own can make use of the promise construct.  Another new feature appear to be subclassing of the jQuery object.  Similar to the YUI3 sandboxing approach subclassing allows us to extend our own instance of the jQuery object without polluting the global instance.  These changes combined with a number of bug fixes and performance improvements means jQuery vNext is looking good. &lt;/p&gt;&lt;h3&gt;Ext JS 4&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.sencha.com/"&gt;&lt;/a&gt;&lt;a href="http://www.sencha.com"&gt;http://www.sencha.com&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Ext JS 4 promises to be a big improvement over the current versions.  Many things have been simplified and refactored which is good because often in version 3+ you get the feeling that things are perhaps OVER-engineered and needlessly complicated.  Ext 2/3 also feels like it has massive functionality holes – there are nice widgets but the data package and application architecture always felt half baked.  Not with 4 it seems.  The data package is shaping up very nicely and they have their own MVC based classes built right into the framework.  This could make large applications much easier to manage.&lt;/p&gt;&lt;h3&gt;jQuery Mobile &amp;amp; Sencha Touch&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://jquerymobile.com/"&gt;&lt;/a&gt;&lt;a href="http://jquerymobile.com/"&gt;http://jquerymobile.com/&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://www.sencha.com/products/touch/"&gt;&lt;/a&gt;&lt;a href="http://www.sencha.com/products/touch/"&gt;http://www.sencha.com/products/touch/&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Mobile is still an immature area among big companies.  Many of them have native apps in various app stores etc. but they still don't really know how best to manage them.  Good web based mobile frameworks appeal to them more as it gives them much greater control over the content – after all they own the hosting servers etc.  jQuery Mobile and Sencha Touch have address different concerns – jQuery Mobile focuses on offering a consistent cross device experience while Sencha Touch aims to provide a native look and feel on WebKit based browsers (iPhone/iPad/Playbook/Android devices).  I expect these frameworks to come into their own this year as they move out of immaturity and more people start experimenting.&lt;/p&gt;&lt;h3&gt;Nuget&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://nuget.codeplex.com/"&gt;&lt;/a&gt;&lt;a href="http://nuget.codeplex.com"&gt;http://nuget.codeplex.com&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;A nice little package manager for the .NET world offering public and locally hosted repositories.  What makes it different to other is that a package could be anything – not just DLL.  I like this and one of the reasons why I like it is that I can use it to distribute my JavaScript frameworks without have to maintain a big list of files needed for each one.  I just click jQuery Mobile and it will pull in jQuery and the necessary CSS.  I want Backbone and it will pull down Underscore for me as well.  Same goes for any .NET libraries.  Combine this with the ability to version packages etc. and it will make projects less of a “DLL Hell”.&lt;/p&gt;&lt;h3&gt;WCF HTTP&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP"&gt;&lt;/a&gt;&lt;a href="http://wcf.codeplex.com/wikipage?title=WCF%20HTTP"&gt;http://wcf.codeplex.com/wikipage?title=WCF%20HTTP&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The new WCF HTTP stuff is looking really really nice.  One of the big issues with WCF is all the XML configuration and other blah that goes along with it.  Newer WCF has given us .svc-less services generated from pure code, better configuration control and now some really nice improvements to the WCF communication channel.  You can have custom Media Processors that let you control what is served to the client based on requested content types or actions (JSON, XML, Images – anything you can think of).  The cleaning up of UriTemplates also mean that you can produce a very lightweight RESTful service or MVC like architecture very very easily.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Another long ramble that's taken me longer than expected to write up!  Obviously there is plenty there to think about and hopefully it will generate some discussion.  Hopefully I get a chance to flesh a lot of this out and offer more low level justification on my tech choices.  2011 is shaping up to be a great year in the tech world.&lt;/p&gt;</content></entry><entry><title>Using CoffeeScript in .NET</title><updated>Mon Jan 17 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/01/17/using-coffeescript-in-net/"></link><id>urn:yobriefca-se:feed:post:Using CoffeeScript in .NET</id><content type="html">
&lt;p style="text-align: center;"&gt;&lt;img src="http://posterous.com/getfile/files.posterous.com/temp-2011-01-17/GzcAkdBuuBhqnFqExtbBrpthjHwbqpFyslpzEnJEiIiwqmulsAjFgFAuciHJ/coffeescriptdotnet.png.scaled500.png" alt="" /&gt;&lt;/p&gt;&lt;p&gt;I've been playing with &lt;a href="http://nodejs.org/"&gt;node.js&lt;/a&gt; for a while  now and some of the things coming out of that community are simply amazing.  One  of the interesting techs is &lt;a href="http://coffeescript.org/"&gt;CoffeeScript&lt;/a&gt; which is essentially a  JavaScript dialect that compiles into JavaScript.  It's goal is to simplify the  JavaScript language by removing all those bad parts that can cause a lot of  grief and taking all those common tasks (array manipulation, scoping etc and  making them very very easy to achieve.  I have an old post I have yet to publish  externally on the subject but I will get it out there ASAP.&lt;/p&gt;&lt;p&gt;CoffeeScript comes in 2 variations - a node.js specific version and a  "standalone" version that can be run in browser (pure JavaScript without any of  the node.js assumptions basically).  Considering it's parsing one language into  another it's not recommended to be running the script client side every time so  if you want to use it you want to be doing the parsing server side and possibly  using some sort of caching to prevent redoing the parsing on every request.   This is the bad(ish) news for .NET folks because there really isn't any stable  JavaScript implementations that can run a script as complex a CoffeeScript (this  is essentially a compiler of sorts).  Well at least not until now....  When i  first investigated this I tried using a number of JavaScript engines,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="https://github.com/fholm/IronJS"&gt;IronJS&lt;/a&gt;: Currently only a  partial implementation and doesn't appear to run the CoffeeScript  compiler fully.&lt;/li&gt;&lt;li&gt;&lt;a href="http://javascriptdotnet.codeplex.com/"&gt;JavaScript.NET&lt;/a&gt;: Worked  for version 0.9 or so of CoffeeScript but there is a bug that causes version 1.0  to fall over and development appears to have ceased.&lt;/li&gt;&lt;li&gt;Command line JScript: Epic fail.  Just wouldn't do what I needed it  to.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Then along came yet another JavaScript engine for .NET so I gave it a shot.   On the surface &lt;a href="http://jurassic.codeplex.com/"&gt;Jurassic&lt;/a&gt; appeared to  offered everything I needed and upon diving a bit deeper I discovered that, yes,  it does indeed offer everything I needed - it runs the CoffeeScript compiler.   Sweet.&lt;/p&gt;&lt;p&gt;So lets write the compiler wrapper.  Pretty basic really,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Processes CoffeeScript files into javascript&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CoffeeScriptProcessor&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;COMPILE_TASK&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"CoffeeScript.compile(Source, {bare: true})"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="na"&gt;    [ThreadStatic]&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;ScriptEngine&lt;/span&gt; &lt;span class="n"&gt;_engine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;_o&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;ScriptEngine&lt;/span&gt; &lt;span class="n"&gt;Engine&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;get&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_engine&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;_engine&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;ScriptEngine&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
                &lt;span class="n"&gt;_engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Resources&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CoffeeScriptSource&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_engine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Precesses contents as a coffeescript file&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="contents"&amp;gt;The javascript contents&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;lock&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;Engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SetGlobalValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Source"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;contents&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Engine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Evaluate&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;COMPILE_TASK&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Few points worth noting here,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;I create a ThreadStatic instance of the Jurassic Engine and feed the  CoffeeScript compiler source into it once.  The executions/evaluation of that  script is quite slow so I only wanted to take the hit once.  Jurassic isn't  thread safe so I made the reference ThreadStatic.&lt;/li&gt;&lt;li&gt;Just to be extra safe I am locking during the execution of the compilation  task.&lt;/li&gt;&lt;li&gt;Any errors just return null.  This is a bare bones implementation I have  stripped out custom logging and profiling code and an interface that means  little in this context.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I have created a VS2010 MVC3 solution that makes use of this processor to generate JavaScript from a simple CoffeeScript example.  Have fun.&lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/kouphax/coffeescript-dotnet"&gt;https://github.com/kouphax/coffeescript-dotnet&lt;/a&gt;&lt;/p&gt;&lt;h2&gt;What's Next?&lt;/h2&gt;&lt;p&gt;This is obviously only the start - if I wanted to use CoffeeScript in a .NET  environment I am going to need to consider how it is used.  When should I be  compiling it?  How should I be caching it etc?  The usual things.  Hopefully  I'll touch on these in a later post.&lt;/p&gt;&lt;p&gt;So right now this is kind of a request for comments, this was knocked up quite quickly and likely I have forgotten something or made a massive mistake.  Anything you want to offer fire away.&lt;/p&gt;</content></entry><entry><title>VS2010/MVC JavaScript Intellisense HTML âHackâ</title><updated>Fri Jan 07 00:00:00 UTC 2011</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2011/01/07/vs2010-slash-mvc-javascript-intellisense-html-ahacka/"></link><id>urn:yobriefca-se:feed:post:VS2010/MVC JavaScript Intellisense HTML âHackâ</id><content type="html">
&lt;p&gt;There is a problem with Visual Studio 2010 JavaScript Intellisense (using MVC in this case) and it is  this - If you are referencing JavaScript resources correctly (or Stylesheets for  that matter) as you all should be you wont actually get intellisense within an  SCRIPT blocks in HTML.  So using this sort of script reference,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"@Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;json2&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Will not give you intellisense.  Sure you can use JavaScript files and make  use of the reference VSDOC tag (in fact maybe you should)&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;reference path=?~/Scripts/json2.js?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But this doesn't work (yet) within SCRIPT blocks.  So what about those rare  times you actually want intellisense within your page?  It's actually quite easy  though a bit hacky - stick it in a condition branch that never gets reached.   So,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@if (false) {
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"~/Scripts/json2.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
}
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"@Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;json2&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Intellisense parser doesn't really care about the fact that the code wont  ever get hit and you still get your typeahead and documentation etc. but it  won't ever get accidentally written to the generated output (as often happened  to me).  I guess this seems kind of obvious or whatever but perhaps so obvious  some people may not have even though about it and hopefully it'll help someone  out somewhere at some point sometime, eventually - maybe.&lt;/p&gt;</content></entry><entry><title>Better JSON Serialisation for ASP.NET MVC</title><updated>Sat Nov 20 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/11/20/better-json-serialisation-for-asp-dot-net-mvc/"></link><id>urn:yobriefca-se:feed:post:Better JSON Serialisation for ASP.NET MVC</id><content type="html">
&lt;p&gt;&lt;/p&gt;&lt;p&gt;The Json() method of controllers is a nice little convenience method for  serialising server side data into JSON for handy consumption on the client side  but it's not without it's issues.  Firstly it just cant serialise dates in any  nice way due to no hard and fast standard in JSON for dates.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SomeDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Simply gets spat back to the front end as a string like so,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="s2"&gt;"/Date(1290181373164)/"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Not exactly the most useful thing in the world.  You are going to need to make use of JSON's reviver callback to parse this accordingly.&lt;/p&gt;&lt;p&gt;Another issue is lack of configurability.  C# coding standards recommend the  use of PascalCased property names whereas in JavaScript land the standard is  camelCase (and many frameworks assume this casing which can cause issues behind  the scenes *cough*ExtJS*cough*).  You can fix this by adding DataContract and  DataMember attributes to your model object but it gets a bit messy and open to  error if an attribute is left out accidentally.  Or what if you don't have  access to the model code?&lt;/p&gt;&lt;h2&gt;JSON.NET&lt;/h2&gt;&lt;p&gt;&lt;a href="http://json.codeplex.com/"&gt;Json.NET&lt;/a&gt; is my JSON de/serialiser of  choice.  It's fast and widely configurable plus it fixes the problems I've  mentioned above (among many many other things).  Not to mention some BSON  (Binary JSON) support which could come in handy in the future.&lt;/p&gt;&lt;h2&gt;JsonNetResult&lt;/h2&gt;&lt;p&gt;So lets fix things.  Lets wrap Json.NET up in an ActionResult type and offer  a configurable entry point.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;/// Simple Json Result that implements the Json.NET serialiser offering more versatile serialisation &lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;JsonNetResult&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;JsonNetResult&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;JsonNetResult&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;responseBody&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;ResponseBody&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;responseBody&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;JsonNetResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;responseBody&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;JsonSerializerSettings&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;Settings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;Gets or sets the serialiser settings&amp;lt;/summary&amp;gt; &lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;JsonSerializerSettings&lt;/span&gt; &lt;span class="n"&gt;Settings&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;Gets or sets the encoding of the response&amp;lt;/summary&amp;gt; &lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;Encoding&lt;/span&gt; &lt;span class="n"&gt;ContentEncoding&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;Gets or sets the content type for the response&amp;lt;/summary&amp;gt; &lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;Gets or sets the body of the response&amp;lt;/summary&amp;gt; &lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;ResponseBody&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;Gets the formatting types depending on whether we are in debug mode&amp;lt;/summary&amp;gt; &lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Formatting&lt;/span&gt; &lt;span class="n"&gt;Formatting&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;get&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Debugger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsAttached&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="n"&gt;Formatting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Indented&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Formatting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;None&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt; &lt;/span&gt;
    &lt;span class="c1"&gt;/// Serialises the response and writes it out to the response object &lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt; &lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="context"&amp;gt;The execution context&amp;lt;/param&amp;gt; &lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ExecuteResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ControllerContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"context"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;HttpResponseBase&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// set content type &lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt; 
        &lt;span class="k"&gt;else&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// set content encoding &lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ContentEncoding&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentEncoding&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ContentEncoding&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ResponseBody&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SerializeObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ResponseBody&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Formatting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Settings&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;             
        &lt;span class="p"&gt;}&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see I have exposed the JsonSerializerSettings object allow  developers to tune the serialisation all they want.  We can also make things  even simpler by providing a Controller extension method that we can call without  having to create the object directly,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ControllerExtensions&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;JsonNetResult&lt;/span&gt; &lt;span class="nf"&gt;JsonEx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;responseBody&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;JsonNetResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;responseBody&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;JsonNetResult&lt;/span&gt; &lt;span class="nf"&gt;JsonEx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;responseBody&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;JsonSerializerSettings&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;JsonNetResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;responseBody&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Simple stuff but still pretty powerful, lets see it in action.&lt;/p&gt;&lt;h2&gt;The Date Problem&lt;/h2&gt;&lt;p&gt;We can fix the date problem in a number of ways via converters.  Converters  give us a custom way to convert values of objects and Json.NET provides a number  of converters out of the box - IsoDateTimeConverter, JavaScriptDateTimeConverter  and an abstract base DateTimeConverter to roll your own.  Lets use the  JavaScriptDateTimeConverter for this example,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SomeDate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JsonEx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JsonSerializerSettings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Converters&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;JsonConverter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;JavaScriptDateTimeConverter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The resultant JSON object will generate a JSON object with a JavaScript Date  constructor&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While not strictly valid JSON native JSON parsers and JSON2 handle it  fine.&lt;/p&gt;&lt;h2&gt;The Casing Issue&lt;/h2&gt;&lt;p&gt;We can use Json.NET's contract resolver to automagically convert PascalCased  property names of C# to camelCased property names of JavaScript.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="n"&gt;SomeProperty&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt;&lt;span class="n"&gt;my&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt; 

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;JsonEx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;JsonSerializerSettings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="n"&gt;ContractResolver&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;CamelCasePropertyNamesContractResolver&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Which gives us a response like so,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nx"&gt;someProperty&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="nx"&gt;my&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Fixed.&lt;/p&gt;&lt;h2&gt;Wrap Up&lt;/h2&gt;&lt;p&gt;So we have managed to create a much more powerful JSON serialisation  technique without having to sacrifice too much of the convenience of the Json()  method in the controller.  Anyone got any recommendations/enhancements they are  willing to share?  &lt;/p&gt;</content></entry><entry><title>The Razor View Engine</title><updated>Thu Nov 18 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/11/18/the-razor-view-engine/"></link><id>urn:yobriefca-se:feed:post:The Razor View Engine</id><content type="html">
&lt;p&gt;&lt;em&gt;Sorry for the poor syntax highlighting on the source code... not much support for Razor ATM&lt;/em&gt;&lt;/p&gt;&lt;p&gt;I've been holding off trying to form an opinion on this until we got the full  package so to speak.  With MVC 3 RC1 being released last week I guess it's time  to share my thoughts on the &lt;a href="http://blog.robertgreyling.com/2010/07/is-razor-just-wolf-in-sparks-clothing.html"&gt;mildly  contentious&lt;/a&gt; view engine.  First things first though - lets be extremely  superficial and compare a a bit of code.  Specifically lets look a using a loop  to output a list of "really useful" list items.  In the older WebForms view  engine you'd do something like this.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% foreach (int index in Enumerable.Range(1, 10))
        { %&amp;gt;        
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Entry &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=index%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;        
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%}%&amp;gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Even for a very trivial example it does look a bit messy - lots of redundant  start and end markers etc.  Lets look at the equivalent in Razor,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;    
    @foreach (int index in Enumerable.Range(1, 10))    
    {        
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Entry @index&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;        
    }
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now excuse me while I go off on a tiny bit of a tangent.  There are two  things you probably notice about the equivalent Razor syntax,&lt;/p&gt;&lt;ol&gt;&lt;li&gt;It's much cleaner because of some very clever parsing &lt;/li&gt;&lt;li&gt;It's still embedding code in a view.  THE HORROR!!!! &lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Yeah I know in this day and age when everyone is trying to eradicate every  suggestion of server code in a view Microsoft have went right ahead and actually  enabled you to do it in neat/cleaner manner.  It works though - doesn't it?  I  mean why abstract C# when your view is going to get compiled into C#?  Why try  and mimic HTML/XML when technically speaking your custom markup is pretty much  meaningless?  What's wrong with being able to see when server side syntax  finishes and front end syntax takes over?  It makes a lot of sense to me  actually.&lt;/p&gt;&lt;h2&gt;The Truth Will Out&lt;/h2&gt;&lt;p&gt;I've been holding off judgement on Razor for a while until it was more  "complete".  Truth be told for a long time I could have easily summed it up  as&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;&lt;span style="color: #000000;"&gt;It's the WebForms View Engine with the &amp;lt;% %&amp;gt;  replaced with @&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;In some ways thats true but at an extremely basic level.  I misjudged it.  I  was wrong.  I actually kind of like it now.&lt;/p&gt;&lt;h2&gt;But what about Spark?&lt;/h2&gt;&lt;p&gt;I've been an advocate of the &lt;a&gt;Spark View  Engine&lt;/a&gt; for sometime in my company and I've used it on a few projects very  successfully.  It's a great engine but has suffered from a lack of tooling -  there are plenty of developers that simply reject or get a negative impression  of something because of the lack of tooling for the tech.  Spark, especially in  comparison to Razor, has very limited tooling support in VS2010 - sure you have  SparkSense but it's currently not a patch on Razor support (which is still in  itself RC status) and I can't help but feel they'll always be playing catch-up  with in house developed/supported stuff.  Would I have like to see Spark being  adopted by MS rather than them rolling their own view engine?  Perhaps, but  variety is the spice of life and I'm happy to have Razor around.&lt;/p&gt;&lt;p&gt;So what will I use on future projects?  I'm tending towards Razor for now but  perhaps that's because it's new and I'm a technology magpie.  Also it easier to  justify the use of a technology to a customer when they are provided pre-bundled  and supported by such a large entity.&lt;/p&gt;&lt;p&gt;&amp;lt;/opinion piece&amp;gt;&lt;/p&gt;&lt;h2&gt;Back On Track&lt;/h2&gt;&lt;p&gt;Now I've had my little digression lets look at some of the features of  Razor.&lt;/p&gt;&lt;h3&gt;Syntax&lt;/h3&gt;&lt;p&gt;As I showed above the syntax is much more terse.  The parser is clever enough  to detect when you mean server code and when you mean client code.  This reduces  the need to use a lot of bulky delimiters like &amp;lt;% and %&amp;gt;.  Also the parser  is able to determine if the @ you just wrote is a server side delimiter or just  plain text - most of the time.  Alternatively you can fall back on the @@ syntax  to use a literal @. &lt;/p&gt;&lt;p&gt;The parser does choke sometimes when you don't wrap text in an appropriate  element or tag&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@if (true) 
{
    this is a test
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;fails whereas&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@if (true)  
{ 
    &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;this is a test&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt; 
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;is fine.  If you really insist on not using an HTML element you can fall back  on one of two things.  Using a &amp;lt;text&amp;gt; element that is a Razor tag for  marking an area for processing as plain text or a slightly shorter single line  equivalent of @:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@if (true) 
{
    &lt;span class="nt"&gt;&amp;lt;text&amp;gt;&lt;/span&gt;this is a test&lt;span class="nt"&gt;&amp;lt;/text&amp;gt;&lt;/span&gt;
}

@if (true) 
{
    @:this is a test
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Layouts and Sections&lt;/h3&gt;&lt;p&gt;Razor (obviously) has support for master pages and rending content sections  including optional sections.  Syntax for it is pretty simple, lets start with  our master page _Layout.cshtml&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;@View.Title&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"menu"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            @RenderSection("menu", required: true);
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            @RenderBody()
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            @RenderSection("footer", required: false);
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Couple of features worth mentioning,&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Use of RenderSection() to render named sections from the view, also able to  specify if the section is a required or optional section throught the use of the required argument (i.e. if it needs to or  doesn't need to be included in the view during render - if required is true and the section is missing you'll get a runtime error when you attempt to access the page).  Another option for  optional sections is to use the IsSectionDefined method e.g. &lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    @if (IsSectionDefined("footer"))
    {
        RenderSection("footer");
    }
    else
    {
        @:Default Footer Text
     }
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;This way we can, as demonstrated, provide some sort of default as well.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;RenderBody convenience method.  This is used to render the body of the  view.  Unlike WebForms view engine Razor doesn't require you to wrap the main  body in a content area - similar to Spark. &lt;/li&gt;&lt;/ol&gt;&lt;p&gt;So an appropriate view that this master layout can "consume" could look like  this&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@{    
    Layout = "~/Views/Shared/_Layout.cshtml";
}

&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Super Useful Page&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;It really is super useful&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt; 

@section menu {
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Login&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Register&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice I have left out the footer but that's not an issue as it has been  marked as optional.  Also notice that the body is at the root level of the view  and sections are wrapped in a section marker.  This all adds a more minimal look  and feel to the whole view.&lt;/p&gt;&lt;h3&gt;_ViewStart.cshtml&lt;/h3&gt;&lt;p&gt;Another leaf from the Spark world (and probably many other - sorry to  everyone else that supports/implements this sort of feature) is the ability to  provide a global place to put code that applies to every view.  Using the  _ViewStart.cshtml file we could easily just put the Layout declaration from the  view code above into a single global location.  We can also add helpers and  functions there too which I'll talk about now.&lt;/p&gt;&lt;h2&gt;Helpers&lt;/h2&gt;&lt;p&gt;Helpers enable use to provide context aware reusable HTML "templates" in our  views.  Imagine a view that listed Top Selling, New and Top Rated products in 3  lists.  The most basic way to implement this would be 3 loops like so&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Top Selling Product List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;    
    @foreach (string product in View.TopSellers)    
    {        
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;@product&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;    
    }
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;New Product List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;    
    @foreach (string product in View.NewProducts)    
    {        
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;@product&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;    
    }
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Top Rated Product List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;    
    @foreach (string product in View.TopRated)    
    {        
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;@product&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;    
    }
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But that's not exactly DRY is it?  Helpers can help us out here (hence the  name I guess :-P).  Lets create a helper that does the repeatable stuff for  us,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@helper ProductLister(List&lt;span class="nt"&gt;&amp;lt;string&amp;gt;&lt;/span&gt; products){    
    &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;        
        @foreach (string product in products)        
        {            
            &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;@product&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;        
        }    
    &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And the rest of the view...&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Top Selling Product List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
@ProductLister(View.TopSellers) 
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;New Product List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
@ProductLister(View.NewProducts) 
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Top Rated Product List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
@ProductLister(View.TopRated)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Much simpler, cleaner, whateverer.  Again with the Spark similarities - these  are similar to macros.&lt;/p&gt;&lt;h2&gt;Functions&lt;/h2&gt;&lt;p&gt;Razor also makes it easier to embed arbitrary methods on your page which I  guess can be useful for formatting and things like that.  This give us the power  to extend the Razor view engine and adapt it to suit our needs - powerful yes  but also open to abuse as you might guess.  Here's a simple function in  action,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@functions {
    string Encrypt(string value){        
        char[] asArray = value.ToCharArray();        
        Array.Reverse(asArray);        
        return new string(asArray);    
    }
} 
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;    
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Username: @View.Username&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;    
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;Password: @Encrypt(View.Password)&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The function applies a highly technical string encryption technique to a  string value returning the result.&lt;/p&gt;&lt;h2&gt;Inline Templates&lt;/h2&gt;&lt;p&gt;One more feature before I head off to watch telly or sleep or something.   Razor allows you to pass little html nuggets or templates as arguments to  helpers and functions.  There is a details blog post about this over at &lt;a href="http://blog.andrewnurse.net/2010/08/02/InsideRazorPart3Templates.aspx"&gt;Vibrant  Code&lt;/a&gt; and my example is heavily inspired/plagiarised from that.  Lets see  what we can do,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@functions {     
    IHtmlString Times(int times, Func&lt;span class="nt"&gt;&amp;lt;int&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="na"&gt;object&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; template) {        
        StringBuilder b = new StringBuilder();        
        for(int i = 0; i &lt;span class="nt"&gt;&amp;lt; times&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt; &lt;span class="na"&gt;i&lt;/span&gt;&lt;span class="err"&gt;++)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;              
            &lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Append&lt;/span&gt;&lt;span class="err"&gt;(&lt;/span&gt;&lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="err"&gt;(&lt;/span&gt;&lt;span class="na"&gt;i&lt;/span&gt;&lt;span class="err"&gt;));&lt;/span&gt;          
        &lt;span class="err"&gt;}&lt;/span&gt;         
        &lt;span class="na"&gt;return&lt;/span&gt; &lt;span class="na"&gt;new&lt;/span&gt; &lt;span class="na"&gt;HtmlString&lt;/span&gt;&lt;span class="err"&gt;(&lt;/span&gt;&lt;span class="na"&gt;b&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ToString&lt;/span&gt;&lt;span class="err"&gt;());&lt;/span&gt;    
    &lt;span class="err"&gt;}&lt;/span&gt; 
&lt;span class="err"&gt;}&lt;/span&gt;   
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;ul&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;    
    @Times(10, @&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;List Item @item&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;)
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Look at the second to last line see that little block of html preceded by the  Razor marker (@)?  See how in our Times function the HTML block it is getting translated transparently as a  Func&amp;lt;int, object&amp;gt; the we can call and manipulate in the helper function anyway we want?  Can  you see how powerful that could be in the right place?  Me too.  Me too.&lt;/p&gt;&lt;h2&gt;Done For Now&lt;/h2&gt;&lt;p&gt;There is more to Razor than all this stuff including the ability to use it  outside of the view context so it could simply be a templating language if needs  be.  There is even more than that too but thats for another time.  Imagine  though.... I started out this blog post more or less telling you I used to think  Razor was simply the WebForms view engine but with @'s instead of &amp;lt;% %&amp;gt;'s  and hopefully you can see why I was wrong and why I was happy to admit it.  My  only gripe is that the Razor documentation could be a bit more available.  There  isn't really a great deal out there ATM.  However this has been promised for the  official release so I am looking forward to that.&lt;/p&gt;&lt;p&gt;Oh Misfits is about to start......&lt;/p&gt;</content></entry><entry><title>Underscore.js: The JavaScript Utility Library</title><updated>Fri Nov 12 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/11/12/underscore-dot-js-the-javascript-utility-library/"></link><id>urn:yobriefca-se:feed:post:Underscore.js: The JavaScript Utility Library</id><content type="html">
&lt;p class="img-holder"&gt;&lt;img alt="" src="http://farm5.static.flickr.com/4017/5168979559_07b681c56d_o.gif" /&gt;&lt;/p&gt;&lt;p&gt;I've been meaning to give &lt;a href="http://documentcloud.github.com/underscore/"&gt;Underscore.js&lt;/a&gt; some love  for some time but never got around to it.  The other day I though about writing  about &lt;a href="http://backbonejs.org/"&gt;Backbone.js&lt;/a&gt; (another excellent library from the &lt;a href="http://www.documentcloud.org/home"&gt;same people&lt;/a&gt;) but didn't feel it  was possible to do so without first talking about Underscore as it underpins  most of the Backbone.js functionality.  So without further ado...&lt;/p&gt;&lt;h2&gt;Introduction&lt;/h2&gt;&lt;p style="padding-left: 30px;"&gt;&lt;em&gt;&lt;a href="http://github.com/documentcloud/underscore/"&gt;Underscore&lt;/a&gt; is a  utility-belt library for JavaScript that provides a lot of the functional  programming support that you would expect in &lt;a href="http://prototypejs.org/api"&gt;Prototype.js&lt;/a&gt; (or &lt;a href="http://www.ruby-doc.org/core/classes/Enumerable.html"&gt;Ruby&lt;/a&gt;), but  without extending any of the built-in JavaScript objects. It's the tie to go  along with &lt;a href="http://docs.jquery.com/"&gt;jQuery&lt;/a&gt;'s tux.&lt;/em&gt;&lt;/p&gt;&lt;p style="text-align: right;"&gt;- &lt;a href="http://documentcloud.github.com/underscore/"&gt;http://documentcloud.github.com/underscore/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;That's how the creators describe underscore and it's pretty spot  on.  Underscore offers numerous low level functions that can be used to  perform on arrays and collections (as well as few for object and functions).   Some of these functions are available in some browsers but not all and because  of this Underscore provides a nice layer of abstraction to avoid having to  constantly feature detect.  Incidentally where a browser does implement a  specific function Underscore will always fallback on the native implementation  ensuring that performance is as optimal as possible.  This is the sort of stuff  that you would probably miss if you moved from &lt;a href="http://www.prototypejs.org/"&gt;Prototype&lt;/a&gt; to &lt;a href="http://www.jquery.com/"&gt;jQuery&lt;/a&gt;.  Another point to note is that  Underscore doesn't extend native objects with it's own implementations which  most people will consider a good thing.  Instead, like jQuery, Underscore  exposes a single global object aptly named _.  Now I am not 100% sold on this, I  am a bit strange when it comes to neat code and I think this variable name makes  code look a bit "noisy".  Not a big deal but I like to pick holes in things  :-).&lt;/p&gt;&lt;h2&gt;Coding Styles&lt;/h2&gt;&lt;p&gt;There are two ways to write code using Underscore.  Imagine the scenario that  given an array of numbers You can go the functional route and call methods  directly on the underscore object,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;filtered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filtered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; 101&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Or go the OO route for those familiar with the jQuery style syntax, &lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;memo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; 101    &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;These 2 examples acheive the same thing - filter a list to those  values greater than 10 and derive a sum of these values; but the look and feel  are actually quite different.  Which one you chose will be down to personal  preference (I tend towards the former approach but the jury is still out).&lt;/p&gt;&lt;h2&gt;Collections/Arrays&lt;/h2&gt;&lt;p&gt;Underscore has a ton of collection and array methods -  a  collection being either an array or an object (as in an object is essentially a  property bag/associative array). &lt;/p&gt;&lt;p&gt;The usual suspects for array manipulation/traversal are all here.  Coming  from a C# background some of the names will be familiar and some are different  but they achieve the same thing - here is just a tiny subset,&lt;/p&gt;&lt;h3&gt;each()&lt;/h3&gt;&lt;p&gt;Loops over a collection executing a callback on each element.  break; support  provided by _.breakLoop()&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"First prime: "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;breakLoop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;First&lt;/span&gt; &lt;span class="nx"&gt;Prime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;map()&lt;/h3&gt;&lt;p&gt;Creates a new array from a collection where each element can be transformed  by a callback&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isPrime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; [false,false,false,false,true,true,true]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;first()/last()&lt;/h3&gt;&lt;p&gt;Select the first and last elements from an array&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; 2&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Functions&lt;/h2&gt;&lt;p&gt;Underscore provides a number of methods that can be applied to functions.   These include the ability to bind methods to a specific object/context (bind),  memoize functions (caching or results to increase calculation speeds), delay and  defer execution of methods and perform some AOP style actions on functions (wrap  and compose).&lt;/p&gt;&lt;h2&gt;Objects&lt;/h2&gt;&lt;p&gt;Underscore provides a number of common object related methods such as the  ubiquitous extend and clone functions.  Other useful functions include the use  of a tap function.  This function lets you "tap" into an execution chain and  inspect/manipulate the object in its current state.  The underscore example  demonstrates this perfectly.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nx"&gt;chain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//=&amp;gt; [2, 200]&lt;/span&gt;
&lt;span class="c1"&gt;//=&amp;gt; [4, 40000]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see the tap function is used to push the object (in this case an  array) through.  There is also a fairly robust set of type detection  functions.&lt;/p&gt;&lt;p style="padding-left: 30px;"&gt;&lt;em&gt;isEqual, isEmpty, isElement,isArray, isArguments, isFunction, isString,  isNumber, isBoolean, isDate,isRegExp isNaN, isNull, isUndefined&lt;/em&gt;&lt;/p&gt;&lt;h2&gt;Other Things&lt;/h2&gt;&lt;p&gt;There are a few other specific methods in Underscore that provide utility  functions including,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;template - super simple templating function&lt;/li&gt;&lt;li&gt;mixin - extend underscores functionlaity with your own custom methods&lt;/li&gt;&lt;li&gt;times - execute a callback a specified number of times&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;This is a very high level overview of Underscore (so I can now go and get all  excited abuot Backbone) but hopefully you can see it's worth.  Have a look at  the &lt;a href="http://documentcloud.github.com/underscore/"&gt;comprehensive docs&lt;/a&gt; that many of my example where derived from and see if there is anything useful  for you there.&lt;/p&gt;
</content></entry><entry><title>Remote Validation in ASP.NET MVC 3 RC1</title><updated>Wed Nov 10 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/11/10/remote-validation-in-asp-dot-net-mvc-3-rc1/"></link><id>urn:yobriefca-se:feed:post:Remote Validation in ASP.NET MVC 3 RC1</id><content type="html">
&lt;p&gt;&lt;em&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;:  Added some words around the extra configuration options available&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Remote validation has finally landed in RC1 of &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/11/09/announcing-the-asp-net-mvc-3-release-candidate.aspx"&gt;ASP.NET  MVC 3&lt;/a&gt;.  It's a weird area as more often than not people tend to over  complicate something that is really pretty simple.  Thankfully the MVC  implementation is fairly straightforward by simply providing wiring allowing the &lt;a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/"&gt;jQuery  Validation&lt;/a&gt; plugin to work it's magic.  Basically there is a new Remote attribute that can be used  like so.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Credentials&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;    
&lt;span class="na"&gt;    [Remote("Username", "Validation")]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Password&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see we have attributed the Username field with a Remote  attribute.  The 2 parameters tell us what Action and Controller we should call  to perform the validation.  This does make me feel slightly uneasy as it kind of feels like you are coupling the controller to the model which doesn't sit right by me.  currently sitting on the fence I'll see how it works in real life.  Anyway I implemented it like so,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ValidationController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;UserName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;JsonRequestBehavior&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AllowGet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And thats you - provided you have the necessary client side libraries included  of course (jQuery, jQuery Validate etc). and have Client Side Validation turned on (now by default in MVC3).&lt;/p&gt;&lt;h2&gt;Configuration&lt;/h2&gt;&lt;p&gt;The Remote attribute offers a few nice little configuration options to make  things easier.  The typical ones are there such as ErrorMessage, ErrorResource  etc. but there are a few specific ones as well.&lt;/p&gt;&lt;h3&gt;Fields&lt;/h3&gt;&lt;p&gt;There may be a case where ding the name and the value of a single form field  isn't enough to perform validation.  Perhaps validation is affected by some  other field/value in the form.  The Remote attribute accepts a comma separated  list of other fields that need to be sent up with the request using the Fields  parameter&lt;/p&gt;&lt;p&gt;This basic example will send up the value of the EmailService input field  along with the value of Username.  Clean and simple.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[Remote("Username", "Validation", Fields = "EmailService")]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;HttpMethod&lt;/h3&gt;&lt;p&gt;HttpMethod simply allows us to change how the ajax request is sent e.g. via  POST or GET or anything else that makes sense.  So to send a remote request via  POST&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[Remote("Username", "Validation", HttpMethod = "POST")]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;A Minor Difference&lt;/h2&gt;&lt;p&gt;You might notice if you read the release notes for RC1 that my implementation  of the controller is slightly different.  The reason being that the example in  the release notes is broken :-).  The example looks like this&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UsersController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;UserNameAvailable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="n"&gt;MyRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserNameExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;However the Validate plugin expects a JSON response which is fine on the  surface but returning a boolean response to the client side results in a  response body of False (notice the captial F) which in turn causes a parse error  when the plugin performs JSON.parse.  My suggested solution is actually more  inline with how most people would typically write an Ajax capable controller  action anyway (though I am not happy with the JsonRequestBehaviour usage) but  there are other ways but they aren't pretty....&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ValidationController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;        
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;Repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UserExists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="n"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;ToLower&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;See?  Ugly and plain &lt;strong&gt;WRONG&lt;/strong&gt; (but it will work).&lt;/p&gt;&lt;p&gt;Nice to see this feature finally landing as it can be useful in certain  situations.&lt;/p&gt;</content></entry><entry><title>All Change! ValidateInputAttribute and SkipRequestValidation</title><updated>Tue Nov 09 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/11/09/all-change-validateinputattribute-and-skiprequestvalidation/"></link><id>urn:yobriefca-se:feed:post:All Change! ValidateInputAttribute and SkipRequestValidation</id><content type="html">
&lt;p&gt;I recently &lt;a href="http://yobriefcase.posterous.com/validateinputattribute-changes-in-mvc3"&gt;blogged&lt;/a&gt; about the new Exclude feature of ValidateInputAttribute in ASP.NET MVC 3 (Beta  2).  Well as with most early adopter types I've been shafted!  Well not really  but things have changed slightly and it makes my last &lt;a href="http://yobriefcase.posterous.com/validateinputattribute-changes-in-mvc3"&gt;post&lt;/a&gt; slightly redundant.&lt;/p&gt;&lt;p&gt;Let me clarify a bit.  The Exclude property no longer exists.  It has instead  been replaced with a new attribute SkipRequestValidation.  This is a  per-property attribute that lets you specify what properties should be  excluded.  This has the effect of pushing the validation flag down to the model  rather than on the controller.  This make a lot more sense especially in a  controller/solution that has many actions accepting the model.  So what  changes?  Lets take a look at our old code.&lt;/p&gt;&lt;p&gt;First things first the controller no longer need the ValidateInput attribute  and goes back to the old "thinner" version of itself,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [HttpPost]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ForumPost&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We then need to mark the Bodyt property of our ForumPost model to skip  validation,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ForumPost&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Subject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;    &lt;/span&gt;
&lt;span class="na"&gt;    [SkipRequestValidation]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Thats it.  To be honest this feels like a much neater solution.  It feels  more a solution rather than a tacked on property that was added to cater for a  specific case.&lt;/p&gt;&lt;p&gt;ASP.NET MVC 3 RC1 is out now - read more &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/11/09/announcing-the-asp-net-mvc-3-release-candidate.aspx"&gt;here&lt;/a&gt;,  and get it &lt;a href="http://go.microsoft.com/fwlink/?LinkID=191797"&gt;here&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>Mobilize! Adapting the ASP.NET MVC Project Template for jQuery Mobile</title><updated>Mon Oct 25 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/25/mobilize-adapting-the-asp-dot-net-mvc-project-template-for-jquery-mobile/"></link><id>urn:yobriefca-se:feed:post:Mobilize! Adapting the ASP.NET MVC Project Template for jQuery Mobile</id><content type="html">
&lt;p class="img-holder"&gt;&lt;img alt="" src="http://farm3.static.flickr.com/2413/5721975436_fb47349a04_o.png" /&gt;&lt;/p&gt;&lt;p style="text-align: right;"&gt;&lt;em&gt;&lt;span style="font-size: xx-small;"&gt;&lt;span style="font-size: xx-small;"&gt;Image&lt;/span&gt; courtesy of the  awesome &lt;/span&gt;&lt;/em&gt;&lt;em&gt;&lt;span style="font-size: xx-small;"&gt;&lt;a href="http://www.threadless.com/"&gt;Threadless&lt;/a&gt;&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Having tinkered with &lt;a href="http://yobriefcase.posterous.com/practical-jquery-mobile-with-aspnet-mvc"&gt;jQuery  Mobile and MVC&lt;/a&gt; a bit recently but I wanted to do a bit more.  The first app  I wrote was read only which is actually pretty easy to accomplish regardless of  technology/framework.  So I wanted to try a little something different,  something a kin to a real application.  However time and coders block left me  with little inspiration for creating the worlds next amazing mobile app.   Sitting there with a new ASP.NET MVC project open in Visual Studio I couldn't  think of what to write.  Then it struck me... don't write anything.  The MVC  starter skeleton project was sitting right there why not just mobilise it?  So I  did.  Without modifying any server side code and only tweaking the views I found  I was able to create a fully jquery-mobilified version of the template.  Here's  what I did.&lt;/p&gt;&lt;h2&gt;Clean House&lt;/h2&gt;&lt;p&gt;We need to strip some of the dead weight out of our project including some of  the mark up and resources,&lt;/p&gt;&lt;h3&gt;Resources&lt;/h3&gt;&lt;p&gt;First things first I dropped the current bundle of scripts (MS Ajax, jQuery  1.4.1, Validate etc.) and the Site.css.  Next I added the jQuery Mobile  assets,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;jQuery 1.4.3 (jquery-1.4.3.js)&lt;/li&gt;&lt;li&gt;jQuery Mobile 1.0a1 (jquery.mobile-1.0a1.js)&lt;/li&gt;&lt;li&gt;jQuery Mobile CSS (jquery.mobile-1.0a1.css)&lt;/li&gt;&lt;li&gt;jQuery Mobile images&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;In the following project structure&lt;/p&gt;&lt;p class="img-holder"&gt;&lt;img src="http://farm3.static.flickr.com/2696/5721416507_18861d0ae1_o.png" /&gt;&lt;/p&gt;&lt;h3&gt;Site.Master&lt;/h3&gt;&lt;p&gt;Next I cut down the Site.Master to it's bare minimum and add the HTML5  DocType and the references to jQuery Mobile ,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %&amp;gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"Head1"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;asp:ContentPlaceHolder&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"../../Content/jquery.mobile-1.0a1.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"../../Scripts/jquery-1.4.3.min.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"../../Scripts/jquery.mobile-1.0a1.min.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;asp:ContentPlaceHolder&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;I could have included the basic markup for a full page but I wanted to keep  it as flexible for now.  I also removed the LogOnUserControl for now as I want  to just push it into the main page.&lt;/p&gt;&lt;h2&gt;Marking Up&lt;/h2&gt;&lt;p&gt;The next step was to tweak the markup of the pages so they comply with jQuery  Mobile and can be mobilised correctly.  Most pages are marked up in the same way  so rather than list them all I'll just list the highlights.&lt;/p&gt;&lt;h3&gt;Home.aspx&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %&amp;gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content1"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Home Page
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content2"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
		    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Home Page&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% if (!Request.IsAuthenticated) { %&amp;gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Action("&lt;/span&gt;&lt;span class="na"&gt;LogOn&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="na"&gt;Account&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" data-icon="forward" class="ui-btn-right"&amp;gt;Logon&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% } else { %&amp;gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Action("&lt;/span&gt;&lt;span class="na"&gt;LogOff&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="na"&gt;Account&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" data-icon="back" class="ui-btn-right"&amp;gt;Log Off&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% } %&amp;gt;
	    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;	
		    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: View.Message %&amp;gt;&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;	
	    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I have a top toolbar button for Login/Log Off (depending on context) and the  View.Message from the old page is included as the content of the page.&lt;/p&gt;&lt;h3&gt;Logon.aspx&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;Mvc&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Mobile&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Template&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;LogOnModel&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" %&amp;gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"loginTitle"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Log On
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"loginContent"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
		    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Log On&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Action("&lt;/span&gt;&lt;span class="na"&gt;Register&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="na"&gt;Account&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" data-icon="gear" class="ui-btn-right"  data-rel="dialog" data-transition="pop"&amp;gt;Register&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;	            
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Please enter your username and password&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% using (Html.BeginForm()) { %&amp;gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"fieldcontain"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.LabelFor(m =&amp;gt; m.UserName) %&amp;gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.TextBoxFor(m =&amp;gt; m.UserName) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.ValidationMessageFor(m =&amp;gt; m.UserName) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"fieldcontain"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.LabelFor(m =&amp;gt; m.Password) %&amp;gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.PasswordFor(m =&amp;gt; m.Password) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.ValidationMessageFor(m =&amp;gt; m.Password) %&amp;gt;   
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"fieldcontain"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.CheckBoxFor(m =&amp;gt; m.RememberMe) %&amp;gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.LabelFor(m =&amp;gt; m.RememberMe) %&amp;gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Log On"&lt;/span&gt; &lt;span class="na"&gt;data-theme=&lt;/span&gt;&lt;span class="s"&gt;"b"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% } %&amp;gt;
	    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Gives us the login form and a link to the Register screen.  I marked the  Register screen as a Dialog just to be a wee bit different.  The Register screen  itself is marked up as a normal page (the framework handles the displaying of a  dialog automagically).  I also added a different theme to the login button and a  "pop" transition to the Register dialog through the data- attributes.&lt;/p&gt;&lt;p&gt;One other thing I had to do was add a "fieldcontain" wrapper to the field  blocks as there was some layout issues.  This is documented in the jQuery Mobile  docs.&lt;/p&gt;&lt;h3&gt;Register.aspx&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;Mvc&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Mobile&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Template&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;RegisterModel&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" %&amp;gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"registerTitle"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Register
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"registerContent"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
		    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Register&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Action("&lt;/span&gt;&lt;span class="na"&gt;Index&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="na"&gt;Home&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" data-icon="grid" class="ui-btn-right"&amp;gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
	    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;	
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Passwords are required to be a minimum of &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: View.PasswordLength %&amp;gt; characters in length.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;		    
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% using (Html.BeginForm()) { %&amp;gt;
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"fieldcontain"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.LabelFor(m =&amp;gt; m.UserName) %&amp;gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.TextBoxFor(m =&amp;gt; m.UserName) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.ValidationMessageFor(m =&amp;gt; m.UserName) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"fieldcontain"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.LabelFor(m =&amp;gt; m.Email) %&amp;gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.TextBoxFor(m =&amp;gt; m.Email) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.ValidationMessageFor(m =&amp;gt; m.Email) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"fieldcontain"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.LabelFor(m =&amp;gt; m.Password) %&amp;gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.PasswordFor(m =&amp;gt; m.Password) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.ValidationMessageFor(m =&amp;gt; m.Password) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                
                &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"fieldcontain"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.LabelFor(m =&amp;gt; m.ConfirmPassword) %&amp;gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.PasswordFor(m =&amp;gt; m.ConfirmPassword) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
                        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%: Html.ValidationMessageFor(m =&amp;gt; m.ConfirmPassword) %&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

                &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Register"&lt;/span&gt; &lt;span class="na"&gt;data-theme=&lt;/span&gt;&lt;span class="s"&gt;"b"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;
	    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The most complex screen in the app.  Nothing special added only some  different markup.&lt;/p&gt;&lt;h2&gt;Running It&lt;/h2&gt;&lt;p&gt;That was it.  After adapting the rest of the pages (via manipulating markup)   I was left with a mobilized version of the MVC project template.  Again I didn't  have to make any code changes - validation still worked, forms still worked,  login, register, change password - all worked as expected (screen shots from Chrome which has a few issues with rounded corners on fields in jQuery Mobile).&lt;/p&gt;&lt;p class="img-holder"&gt;  &lt;img style="border:1px solid #000;" width="200" src="http://farm4.static.flickr.com/3502/5721975554_272b823481_o.png" /&gt;  &lt;img style="border:1px solid #000;" width="200" src="http://farm3.static.flickr.com/2756/5721416629_4b4e7ebaed_o.png" /&gt;  &lt;img style="border:1px solid #000;" width="200" src="http://farm3.static.flickr.com/2510/5721416689_20e25e2983_o.png" /&gt;&lt;/p&gt;</content></entry><entry><title>ValidateInputAttribute Changes in MVC3 (Beta 2)</title><updated>Sat Oct 23 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/23/validateinputattribute-changes-in-mvc3-beta-2/"></link><id>urn:yobriefca-se:feed:post:ValidateInputAttribute Changes in MVC3 (Beta 2)</id><content type="html">
&lt;p&gt;&lt;strong&gt;This post is now redundant with the release of ASP.NET MVC 3 RC 1.  SkipRequestValidation is the new ValidateInputAttribute(Exclude="").&lt;/strong&gt;&lt;span style="color: #ff6600;"&gt;&lt;strong&gt;  &lt;a href="http://yobriefcase.posterous.com/all-change-validateinputattribute-and-skipreq"&gt;Read More.&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;The ValidateInputAttribute has received a nice little tweak in MVC 3 offering  more fine grained control over parameters of a request. &lt;/p&gt;&lt;p&gt;In MVC 2 using ValidateInputAttribute was limited to the request level, that  is all parameters in the request where either validated or not.  Lets  demonstrate this with a simple example - a simple forum posting page,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;Common&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Model&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ForumPost&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" %&amp;gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
	Login
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Post Question&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% using (Html.BeginForm()) { %&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.LabelFor(c =&amp;gt; c.Subject)%&amp;gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.TextBoxFor(c =&amp;gt; c.Subject)%&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.LabelFor(c =&amp;gt; c.Body)%&amp;gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.TextAreaFor(c =&amp;gt; c.Body)%&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Post"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We don;t care what the controller actions actually do but lets describe them  here anyway&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [HttpPost]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ForumPost&lt;/span&gt; &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now attempting to post any sort of markup back to the action will result in  an exception being thrown,&lt;/p&gt;&lt;p&gt;[[posterous-content:DasrjrabrrcEcDeiHdeG]]&lt;/p&gt;&lt;p&gt;If we wanted to allow markup to go through we can add the  [ValidateInput(false)] attribute to the action.  The only problem with that is  if we only wanted to allow markup in the Body and not the Subject we would have  to write our own tests in the controller to prevent this.  Not the most ideal or  clean solution.&lt;/p&gt;&lt;p&gt;MVC 3 solves this quite simply by extending the ValidateInputAttribute and  allowing use to specify exclusions.  This means we can have validation turned on  but specifically state the we don't want to validate a specific request  parameter(s) (e.g. Body).&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[ValidateInput(true, Exclude = "Body")]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;A very minor tweak that makes a big leap to being able to produce cleaner more readable code.&lt;/p&gt;</content></entry><entry><title>Unobtrusive JavaScript in MVC3</title><updated>Fri Oct 22 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/22/unobtrusive-javascript-in-mvc3/"></link><id>urn:yobriefca-se:feed:post:Unobtrusive JavaScript in MVC3</id><content type="html">
&lt;p&gt;&lt;/p&gt;&lt;p&gt;One of the "it's not new but it's cool" features that appeared in the ASP.NET  MVC3 Beta was Unobtrusive JavaScript (well actually Unobtrusive Ajax and an  unobtrusive validation adapter for the jQuery Validation plugin).  Before we  dive into how this differs from MVC2 lets talk about JavaScript in MVC.&lt;/p&gt;&lt;p&gt;First things first - jQuery is now the defacto standard for any ASP.NET MVC  solution.  In the MVC2 Project Template you got both the Microsoft Ajax Library  and jQuery.  This hasn't changed in MVC3 but the Microsoft stuff is only there  for any potential backward compatibility issues a solution may have - feel free  to just delete these files and embrace jQuery.  All the new client side stuff is  all facilitated through jQuery which means you wont have to have some other  framework on your page just because generated code mandates it.&lt;/p&gt;&lt;h2&gt;What Is It?&lt;/h2&gt;&lt;p&gt;Unobtrusive JavaScript (in the MVC3 sense) is a strategy that ensures that no  JavaScript is embedded within the markup (unless you do it yourself).  100% no  generated code muddying your markup.  No code islands, no inline event handlers,  better handling of failure cases and no dependence on any specific framework.   To me, being a web focused developer, this is HUGE and it should be to you too -  it is after all considered best practise.&lt;/p&gt;&lt;h2&gt;Turn It On&lt;/h2&gt;&lt;p&gt;There are two ways to turn Unobtrusive Ajax/Validation on,&lt;/p&gt;&lt;h3&gt;1. Web.Config&lt;/h3&gt;&lt;p&gt;Within the &amp;lt;appSettings&amp;gt; config node in Web.Config you can specify  whether unobtrusive JavaScript is on or off&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;appSettings&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;add&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"enableSimpleMembership"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;add&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"ClientValidationEnabled"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;add&lt;/span&gt; &lt;span class="na"&gt;key=&lt;/span&gt;&lt;span class="s"&gt;"UnobtrusiveJavaScriptEnabled"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt; 
  &lt;span class="nt"&gt;&amp;lt;/appSettings&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;2 On a Per-Page Basis&lt;/h3&gt;&lt;p&gt;Just like EnableClientValidation it is possible to activate unobtrusive  JavaScript  at a page level.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% Html.EnableUnobtrusiveJavaScript(); %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;What Does It Do&lt;/h2&gt;&lt;p&gt;Best way to show what it does is by comparison to, um, Obtrusive JavaScript.   So lets create a scenario in both MVC2 and MVC3 and seeing how they compare.   The scenario will be a simple ajaxified login form (Username and Password) with  some client validation and no server side magic.  Most of this, bar the client  side scripts, is the same across both solutions. &lt;/p&gt;&lt;h2&gt;The Model&lt;/h2&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Credentials&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Username&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="na"&gt;    [Required]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Password&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Two properties with basic [Required] validation. &lt;/p&gt;&lt;h2&gt;The Controller&lt;/h2&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="na"&gt;    [HttpPost]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Credentials&lt;/span&gt; &lt;span class="n"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;EmptyResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Again there isn't much going on here, this all about the client side!&lt;/p&gt;&lt;h2&gt;The View&lt;/h2&gt;&lt;h3&gt;Common Parts&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;Common&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Model&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Credentials&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" %&amp;gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
	Login
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Login&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% using (Ajax.BeginForm(new AjaxOptions { OnSuccess = "onSuccess" })) { %&amp;gt;
        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.ValidationSummary(true)%&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.LabelFor(c =&amp;gt; c.Username)%&amp;gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.TextBoxFor(c =&amp;gt; c.Username)%&amp;gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.ValidationMessageFor(c =&amp;gt; c.Username)%&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.LabelFor(c =&amp;gt; c.Password)%&amp;gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.PasswordFor(c =&amp;gt; c.Password)%&amp;gt;
            &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.ValidationMessageFor(c =&amp;gt; c.Password)%&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Login"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;

    &lt;span class="c"&gt;&amp;lt;!-- JAVASCRIPT LIBRARIES GO HERE! --&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// logged in, carry on&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A simple view that is the same across both projects the only thing that is  going to differ is the actual libraries/scripts that do all the wiring up. &lt;/p&gt;&lt;h3&gt;MVC2&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;MicrosoftAjax&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;MicrosoftMvcAjax&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;MicrosoftMvcValidation&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using the out of the box Microsoft Ajax Library (which is now deprecated)&lt;/p&gt;&lt;h3&gt;MVC3&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery-1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;4&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;validate&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unobtrusive-ajax&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Scripts&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;validate&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;unobtrusive&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using the out of the box jQuery plugins.&lt;/p&gt;&lt;p&gt;Done.  That's all we need for our solutions to function.  The form will be  validated on the client side and submission will be performed through ajax.   Both will behave exactly the same but the markup and code they generate will be  quite different.  I've tidied both up in terms of formatting just so it's  slightly easier to read but I've tried to keep the layout style consistent  across both.&lt;/p&gt;&lt;h2&gt;MVC2 Output&lt;/h2&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/1999/xhtml"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Login &lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc2/Content/Site.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc2/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;
        Login&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc2/Home/Login"&lt;/span&gt; 
          &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form0"&lt;/span&gt; 
          &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt; 
          &lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;"Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));"&lt;/span&gt;
          &lt;span class="na"&gt;onsubmit=&lt;/span&gt;&lt;span class="s"&gt;"Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, onSuccess: Function.createDelegate(this, onSuccess) });"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"validation-summary-valid"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"validationSummary"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"display: none"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Username&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field-validation-valid"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"Username_validationMessage"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"Password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"Password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Password"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field-validation-valid"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"Password_validationMessage"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Login"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; 
    &lt;span class="c1"&gt;//&amp;lt;![CDATA[&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mvcClientValidationMetadata&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mvcClientValidationMetadata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mvcClientValidationMetadata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="s2"&gt;"Fields"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
                &lt;span class="s2"&gt;"FieldName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Username"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"ReplaceValidationMessageContents"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"ValidationMessageId"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Username_validationMessage"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"ValidationRules"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
                    &lt;span class="s2"&gt;"ErrorMessage"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"The Username field is required."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="s2"&gt;"ValidationParameters"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
                    &lt;span class="s2"&gt;"ValidationType"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"required"&lt;/span&gt;
                &lt;span class="p"&gt;}]&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="s2"&gt;"FieldName"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Password"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"ReplaceValidationMessageContents"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"ValidationMessageId"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Password_validationMessage"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;"ValidationRules"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;
                    &lt;span class="s2"&gt;"ErrorMessage"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"The Password field is required."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="s2"&gt;"ValidationParameters"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
                    &lt;span class="s2"&gt;"ValidationType"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"required"&lt;/span&gt;
                &lt;span class="p"&gt;}]&lt;/span&gt;
            &lt;span class="p"&gt;}],&lt;/span&gt;
            &lt;span class="s2"&gt;"FormId"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"form0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"ReplaceValidationSummary"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;"ValidationSummaryId"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"validationSummary"&lt;/span&gt; 
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="c1"&gt;//]]&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc2/Scripts/MicrosoftAjax.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc2/Scripts/MicrosoftMvcAjax.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc2/Scripts/MicrosoftMvcValidation.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// logged in, carry on&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;MVC 3 Output&lt;/h2&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/1999/xhtml"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Login &lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc3/Content/Site.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc3/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Home&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;
        Login&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc3/Home/Login"&lt;/span&gt; 
          &lt;span class="na"&gt;data-ajax=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; 
          &lt;span class="na"&gt;data-ajax-success=&lt;/span&gt;&lt;span class="s"&gt;"onSuccess"&lt;/span&gt;
          &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form0"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Username&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-val=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;data-val-required=&lt;/span&gt;&lt;span class="s"&gt;"The Username field is required."&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field-validation-valid"&lt;/span&gt; &lt;span class="na"&gt;data-valmsg-for=&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt; &lt;span class="na"&gt;data-valmsg-replace=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"Password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;data-val=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;data-val-required=&lt;/span&gt;&lt;span class="s"&gt;"The Password field is required."&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"Password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Password"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"field-validation-valid"&lt;/span&gt; &lt;span class="na"&gt;data-valmsg-for=&lt;/span&gt;&lt;span class="s"&gt;"Password"&lt;/span&gt; &lt;span class="na"&gt;data-valmsg-replace=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Login"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc3/Scripts/jquery-1.4.1.min.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc3/Scripts/jquery.validate.min.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc3/Scripts/jquery.unobtrusive-ajax.min.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Mvc3/Scripts/jquery.validate.unobtrusive.min.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// logged in, carry on&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Analysis&lt;/h2&gt;&lt;p&gt;The first obvious thing we can see is that the UnobJS code is slightly  lighter, even with strict formatting turned it generally produces less LOC's.   But that's not really that important in the grand scheme of things, but is  important is the whole unobtrusiveness of the UnobJS source.&lt;/p&gt;&lt;p&gt;In the old MVC2 code look at those inline event handlers at lines 14 and 15,  look at that huge code island at line 33, smack bang in the middle of our HTML.   It's not wrong per say but it's certainly not best practise.  No it's always  best to keep your View (HTML) and your Code (JavaScript) separated as much as  possible.  Imagine if some wayward script was added between line 61 and 62 did  something like this&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mvcClientValidationMetadata&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forms&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;onclick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forms&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;onsubmit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;OHT3HNOES END OF THE WORLD!&lt;/p&gt;&lt;p&gt;Or what if your CDN that served your scripts was down?  You are actually  going to get JavaScript errors during form submission which can, in various  browsers, prevent the form being submitted.&lt;/p&gt;&lt;p&gt;OHT3HNOES END OF THE WORLD AGAIN!&lt;/p&gt;&lt;p&gt;MVC 3/UnobJS on the other hand isn't going to error out and will fall back to  a normal form submission - progressive enhancement or just expected  behaviour?&lt;/p&gt;&lt;p&gt;On other thing... those inline event handlers pretty much need to have the MS  Ajax Library available to work (and not cause errors).  What if I was already  using jQuery for my project?  I'd still need to include MS Ajax on the page even  though only generated code required it (well I could write my own API mimicking  the required MS Ajax API but why should I?).&lt;/p&gt;&lt;p&gt;Now Unobtrusive JavaScript isn't without it's issues.  Using HTML5's data-  attributes can invalidate your HTML which can be a showstopper for some  projects.  One other issue that we may start to see is that EVERYONE is starting  to use data- attributes (e.g. jQuery Mobile) I wonder if we are going to start  seeing collisions between different libraries?&lt;/p&gt;&lt;p&gt;All in all I don't think there should be an option to turn  Unobtrusive JavaScript off, it should just be the only way of doing things :-)  but thats just me.&lt;/p&gt;</content></entry><entry><title>Developing a jQuery Plugin</title><updated>Fri Oct 22 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="/presentations/developing-a-jquery-plugin.pdf"></link><id>urn:yobriefca-se:feed:post:Developing a jQuery Plugin</id><content type="html"></content></entry><entry><title>Introduction to Ajax</title><updated>Fri Oct 22 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="/presentations/introduction-to-ajax.pdf"></link><id>urn:yobriefca-se:feed:post:Introduction to Ajax</id><content type="html"></content></entry><entry><title>JavaScript Performance Consideration</title><updated>Fri Oct 22 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="/presentations/javascript-performance-considerations.pdf"></link><id>urn:yobriefca-se:feed:post:JavaScript Performance Consideration</id><content type="html"></content></entry><entry><title>A Brief Introduction to jQuery</title><updated>Fri Oct 22 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="/presentations/brief-introduction-to-jquery.pdf"></link><id>urn:yobriefca-se:feed:post:A Brief Introduction to jQuery</id><content type="html"></content></entry><entry><title>jQuery and YUI: A Comparison</title><updated>Fri Oct 22 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="/presentations/jquery-yui-a-comparison.pdf"></link><id>urn:yobriefca-se:feed:post:jQuery and YUI: A Comparison</id><content type="html"></content></entry><entry><title>Practical jQuery Mobile with ASP.NET MVC</title><updated>Thu Oct 21 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/21/practical-jquery-mobile-with-asp-dot-net-mvc/"></link><id>urn:yobriefca-se:feed:post:Practical jQuery Mobile with ASP.NET MVC</id><content type="html">
&lt;p&gt;That's a bit of a mouthful.  I wanted to write a post about creating a basic  jQuery Mobile app but as I started putting the code together MVC became more and  more involved so I combined the 2.&lt;/p&gt;&lt;p&gt;The end solution?  Lets create a phone directory with 2 main views,&lt;/p&gt;&lt;ol&gt;&lt;li&gt;A filterable list of all people with quick info (telephone extension and  name) that is grouped and sorted alphabetically, and,&lt;/li&gt;&lt;li&gt;A disclosure view of a selected person showing more details including a  photo&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;[[posterous-content:FddxCHuIgArIuezHEbcn]]&lt;/p&gt;&lt;h2&gt;MVC (The Server Side)&lt;/h2&gt;&lt;p&gt;jQuery Mobile works by progressive enhancement and uses Ajax to load and  parse external links so it has more control over page transitions and Ajax  history.  This means that we create a plain old website that will work without  jQuery Mobile, Ajax or any JavaScript.  So I started with the ASP.NET MVC 2 Web  Application Visual Studio Template and ripped out everything bar the Home  Controller and the 2 views.  I also stripped the Site.Master down to the bare  bones.&lt;/p&gt;&lt;p&gt;Next I created my model with data access methods,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Id&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Title&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;FirstName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Surname&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;InternalNo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Room&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;ExternalNo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;PhotoLocation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;GetAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// get all entries&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Entry&lt;/span&gt; &lt;span class="nf"&gt;GetById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// get a specific entry&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can implement your own data access there or just hard code some values.   Next I updated the HomeController to return the right models  to the views&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="k"&gt;orderby&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Surname&lt;/span&gt;
                &lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Surname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;
                &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;About&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;I updated the views to display the information in a straightforward way. &lt;/p&gt;&lt;p&gt;Index.aspx&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;IEnumerable&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;IGrouping&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;string&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="na"&gt;Kas&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;JQueryMobile&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;WhosWho&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Entry&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&amp;gt;&amp;gt;" %&amp;gt;
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Import Namespace="Kainos.JQueryMobile.WhosWho.Models" %&amp;gt;
&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content1"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Whos Who&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content2"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;   
            &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Whos Who&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
                &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% foreach (IGrouping&lt;span class="nt"&gt;&amp;lt;string&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="na"&gt;Entry&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; group in Model){%&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=group.Key%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% foreach (Entry item in group){%&amp;gt;
                        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Action("&lt;/span&gt;&lt;span class="na"&gt;About&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="na"&gt;Home&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="na"&gt;new&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id =&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="s"&gt;item.Id&lt;/span&gt; &lt;span class="err"&gt;})&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;"&amp;gt;
                                &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=item.Title %&amp;gt;
                            &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
                                &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=item.InternalNo %&amp;gt;&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;       
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;
                &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;                               
            &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;        
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;About.aspx&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;Kas&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;JQueryMobile&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;WhosWho&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Entry&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" %&amp;gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content1"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Title%&amp;gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content2"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;   
            &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Title%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"background: url(&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Images&lt;/span&gt;&lt;span class="err"&gt;/"&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="na"&gt;Model&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PhotoLocation&lt;/span&gt;&lt;span class="err"&gt;)&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;) no-repeat top right"&amp;gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Title%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"mailto:&amp;lt;%=Model.Email%&amp;gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Email%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.InternalNo%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.ExternalNo%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Room%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;                       
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This leaves us with a normal, not so pretty but fully functioning site. &lt;/p&gt;&lt;p&gt;[[posterous-content:DblJukiumuFlvvqlHokq]]&lt;/p&gt;&lt;p&gt;Now lets inject some mobile niceness into it,&lt;/p&gt;&lt;h2&gt;jQuery Mobile&lt;/h2&gt;&lt;p&gt;First things first lets put reference to jQuery and jQuery Mobile  (script  and css) into our site master and update our DOCTYPE to the HTML5 DOCTYPE  leaving us with,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %&amp;gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;asp:ContentPlaceHolder&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;    
        &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;ClientBin&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mobile-1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;0a1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;css&lt;/span&gt;&lt;span class="err"&gt;")&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" rel="stylesheet" type="text/css" /&amp;gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;ClientBin&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery-1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;4&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;3&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;ClientBin&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mobile-1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;0a1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;" type="&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;asp:ContentPlaceHolder&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now we need to tell jQuery Mobile how to layout the pages and mobilise.  This  isn't necessarily done in script as you might think.  jQuery Mobile makes use of  HTML5's data- attributes to identify how the page should be laid out and mark  areas to specific roles.&lt;/p&gt;&lt;p&gt;Index.aspx&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;IEnumerable&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;IGrouping&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="na"&gt;string&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="na"&gt;Kas&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;JQueryMobile&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;WhosWho&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Entry&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&amp;gt;&amp;gt;" %&amp;gt;
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Import Namespace="Kainos.JQueryMobile.WhosWho.Models" %&amp;gt;
&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content1"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Whos Who
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content2"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;   
            &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Whos Who&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"listview"&lt;/span&gt; &lt;span class="na"&gt;data-filter=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% foreach (IGrouping&lt;span class="nt"&gt;&amp;lt;string&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="na"&gt;Entry&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; group in Model){%&amp;gt;
                    &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"list-divider"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=group.Key%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% foreach (Entry item in group){%&amp;gt;
                        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%=Url.Action("&lt;/span&gt;&lt;span class="na"&gt;About&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="na"&gt;Home&lt;/span&gt;&lt;span class="err"&gt;",&lt;/span&gt; &lt;span class="na"&gt;new&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id =&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt;&lt;span class="s"&gt;item.Id&lt;/span&gt; &lt;span class="err"&gt;})&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;"&amp;gt;
                                &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=item.Title %&amp;gt;
                            &lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"ui-li-aside"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                                &lt;span class="nt"&gt;&amp;lt;strong&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=item.InternalNo %&amp;gt;&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt;
                            &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
                        &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;       
                    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;
                &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;                               
            &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;        
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;About.aspx&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;span class="nt"&gt;&amp;lt;Kas&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;JQueryMobile&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;WhosWho&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Entry&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;" %&amp;gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content1"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"TitleContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Title%&amp;gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;asp:Content&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"Content2"&lt;/span&gt; &lt;span class="na"&gt;ContentPlaceHolderID=&lt;/span&gt;&lt;span class="s"&gt;"MainContent"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;   
            &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Title%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"background: url(&amp;lt;%=Url.Content("&lt;/span&gt;&lt;span class="err"&gt;~/&lt;/span&gt;&lt;span class="na"&gt;Images&lt;/span&gt;&lt;span class="err"&gt;/"&lt;/span&gt; &lt;span class="err"&gt;+&lt;/span&gt; &lt;span class="na"&gt;Model&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;PhotoLocation&lt;/span&gt;&lt;span class="err"&gt;)&lt;/span&gt; &lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;) no-repeat top right"&amp;gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Title%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"mailto:&amp;lt;%=Model.Email%&amp;gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Email%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.InternalNo%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.ExternalNo%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Model.Room%&amp;gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
                       
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/asp:Content&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So what have we added here? &lt;/p&gt;&lt;ul&gt;&lt;li&gt;data-roles - telling jQuery Mobile what each div actually represents e.g.     &lt;ul&gt;&lt;li&gt;page - a single view (a single html page can have multiple views)&lt;/li&gt;&lt;li&gt;header - the header of a page&lt;/li&gt;&lt;li&gt;content - the pages content&lt;/li&gt;&lt;li&gt;footer - the footer of a page&lt;/li&gt;&lt;li&gt;list-view - a special role to specifying  that the content is a list&lt;/li&gt;&lt;li&gt;list-divider - a divide for a list that doesn't do anything but look  different&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;data-filter - telling jQuery Mobile that it should provide filtering on this  view&lt;/li&gt;&lt;li&gt;ui-li-aside class identifies that this is some aside information for this  list item&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;And that is it.  We have taken a static site and without writing any code  (bar markup) created a mobile app.  Probably might be a good idea to include an  application cache manifest file as well so there is some semblance of offline  capability provided.  But that's for another day.&lt;/p&gt;&lt;p&gt;[[posterous-content:pJHwwsHHmrfAkJIJFkrp]]&lt;/p&gt;</content></entry><entry><title>ASP.NET MVC3 JsonValueProviderFactory</title><updated>Wed Oct 20 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/20/asp-dot-net-mvc3-jsonvalueproviderfactory/"></link><id>urn:yobriefca-se:feed:post:ASP.NET MVC3 JsonValueProviderFactory</id><content type="html">
&lt;p&gt;&lt;a target="_blank" href="http://www.asp.net/mvc/mvc3"&gt;ASP.NET MVC 3&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Anyone who's been involved in an ASP.NET MVC project that is quite Ajax heavy  will probably have noticed that something was always missing.  Imagine this  front end scenario,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% using (Html.BeginForm()) { %&amp;gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.LabelFor(m =&amp;gt; m.Username)%&amp;gt;
        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.TextBoxFor(m =&amp;gt; m.Username)%&amp;gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.LabelFor(m =&amp;gt; m.Password)%&amp;gt;
        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.TextBoxFor(m =&amp;gt; m.Password)%&amp;gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%} %&amp;gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"form"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   
        &lt;span class="c1"&gt;// extract values to submit         &lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nx"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"[name=Username]"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"[name=Password]"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
            &lt;span class="nx"&gt;json&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="nx"&gt;Username&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;Password&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"action"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'POST'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;contentType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'application/json; charset=utf-8'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                
            &lt;span class="nx"&gt;dataType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'json'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                
            &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;handleLogin&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="c1"&gt;// stop form submitting&lt;/span&gt;
        &lt;span class="nx"&gt;evt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Which posts to the following action&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[HttpPost]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LoginModel&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do login&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;We have a login screen that is submitted via ajax.  Now this is quite a  contrived example (ideally you'd be performing a normal post via ajax in  this situation) but there are many instances where this sort practise would  apply (ExtJS' RESTful DataWriters for example). &lt;/p&gt;&lt;h2&gt;MVC 2&lt;/h2&gt;&lt;p&gt;In MVC 2 this wouldn't work immediately.  The default model binder in MVC 2  uses Request parameters to bind to model properties but in this case there are  none as the ajax content is the body of the request.&lt;/p&gt;&lt;p&gt;To accommodate this sort of request in MVC 2 we had to provide a custom model  binder that knows how to deal with JSON requests,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;JsonModelBinder&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IModelBinder&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="nf"&gt;BindModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ControllerContext&lt;/span&gt; &lt;span class="n"&gt;controllerContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ModelBindingContext&lt;/span&gt; &lt;span class="n"&gt;bindingContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;HttpRequestBase&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;controllerContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;jsonStringData&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;StreamReader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InputStream&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;ReadToEnd&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;JsonConvert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DeserializeObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonStringData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bindingContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelType&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And attribute our actions model argument telling it to use this binder,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[HttpPost]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;ModelBinder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;JsonModelBinder&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;&lt;span class="n"&gt;LoginModel&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It'll do the job but it's incredibly messy. &lt;/p&gt;&lt;h2&gt;MVC 3&lt;/h2&gt;&lt;p&gt;MCV3 fills this gap thanks to the JsonValueProviderFactory.  The JVPF  operates at a higher level than a model binder.  Basically what it does when a  JSON request is received is that it pulls the values out of the JSON body as key  value pairs which means they are available to the model binders including the  default model binder.  No special wiring required, no custom model binders  (unless of course you want one) just out-of-the-box workingness!&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="na"&gt;[HttpPost]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;ActionResult&lt;/span&gt; &lt;span class="nf"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LoginModel&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do login&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; 
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Success&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;I know most of this post was taken up by MVC2 specific implementation but  isn't that really the point?  MVC3 is a nice refinement of MVC2 there isn't  anything new exactly but the core stuff that is there has been made easier and  more configurable.&lt;/p&gt;</content></entry><entry><title>Client Side: Processing vs Responsiveness</title><updated>Mon Oct 18 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/18/client-side-processing-vs-responsiveness/"></link><id>urn:yobriefca-se:feed:post:Client Side: Processing vs Responsiveness</id><content type="html">
&lt;p style="padding-left: 30px;"&gt;&lt;em&gt;The examples below are probably best run in a browser that has a relatively slow script engine (IE for example)&lt;/em&gt;&lt;/p&gt;&lt;p&gt;One of the problem with the whole Web2.0/Rich Client concepts is that people seem to think it's OK to shove processing that should be done on the server on to the client. This is never the answer - you can control resources on the sever but never on some random browser half way across the world/country/street/room. There are, however, situations where client side processing is going to be mandatory (think Complex Grids and Calculations etc). This obviously leads to a loss of responsiveness on the client (to varying degrees). One of the big problem with large processing is that it obviously takes time. Imagine this scenario...&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doProcessing&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
 
  &lt;span class="cm"&gt;/* get the progress indicator element */&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"progress-meter"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 
  &lt;span class="cm"&gt;/* update element */&lt;/span&gt;
  &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Processing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
  &lt;span class="cm"&gt;/* do long running processing */&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;999999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
 
  &lt;span class="cm"&gt;/* update element */&lt;/span&gt;
  &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Done!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="nx"&gt;doProcessing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You'd expect that this code updates the "progress-meter" element with "Processing", does some long computation and then updates the "progress-meter" again to indicate it's done. But it doesn't. Screen repaints/updates do not happen until calls complete. So what is actually happening? Well the expected outcome DOES happen it's just both updates happen after the call completes so the first update occurs so fast you never see it. &lt;a href="http://jsfiddle.net/kouphax/VtaHg/" target="_blank"&gt;See It In Action&lt;/a&gt;. You'll also notice that the entire screen is locked while this processing occurs and that just leads to a bad user experience. So what can be done?&lt;/p&gt;&lt;h2&gt;Use of setTimeout()&lt;/h2&gt;&lt;p&gt;Javascript is single threaded which leads to the issues above but there is a way create pseudo-threading using setTimeout. The setTimeout function executes code after a set time in milliseconds (what a surprise!). So delaying this execution (even by 0 miilliseconds) allows screen updates to occur before the processing begins. This requires a small change to the code.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doProcessing&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"progress-meter"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Processing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;9999999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Done!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="nx"&gt;doProcessing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So if you &lt;a href="http://jsfiddle.net/kouphax/MJFFX/" target="_blank"&gt;give this a go&lt;/a&gt; you will see there is a much better feedback response compared to the first. This approach solves our initial problem but we still have the issue that the screen is locking up. At least now the users knows something is happening. Another issue this creates is that some browsers prompt users if scripts take to long allowing the user to cancel potentially important tasks.&lt;/p&gt;&lt;p&gt;[[posterous-content:jcdDmwFBuEneeneritwd]]&lt;/p&gt;&lt;h2&gt;Asynchronous Processing&lt;/h2&gt;&lt;p&gt;Now we are getting into the good stuff. To prevent the screen locking up during any complex processing activity it is possible to apply the setTimeout technique but only over a smaller subset of the processing activity each time. This will achieve a sort of asynchronous effect allowing the user to do other stuff while the processing happens in the background. This is a basic example,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Processing Complete"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doProcessing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
 
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;el&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"progress-meter"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Processing"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
  &lt;span class="cm"&gt;/* setup iteration */&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;iterations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9999999&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;iterations&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="cm"&gt;/* each chunk is 1% of the overal processing count*/&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
  &lt;span class="cm"&gt;/* self executing anonymous function */&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 
    &lt;span class="cm"&gt;/* process chunk */&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;99999&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 
    &lt;span class="cm"&gt;/* more to process */&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="cm"&gt;/* update screen */&lt;/span&gt;
      &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;iterations&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s1"&gt;'% Complete'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
      &lt;span class="cm"&gt;/* recurse for next segment */&lt;/span&gt;
      &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;callee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="cm"&gt;/* update screen */&lt;/span&gt;
      &lt;span class="nx"&gt;el&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Done!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
      &lt;span class="cm"&gt;/* call optional callback */&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;doProcessing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;You can &lt;a href="http://jsfiddle.net/kouphax/CKALu/" target="_blank"&gt;see this in action&lt;/a&gt; for yourself. If you run the example you will see that there is a constant update (after each chunk is processed) and that screen control is still available. So this has solved both our issues but has introduced new considerations.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Executes asynchronously therefore we need to support a callback mechanism as it's not procedural (see code)&lt;/li&gt;&lt;li&gt;Processing time increases (see below)&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;You will see that the length of time it take to process the complete task is a lot longer than the other 2 examples.. this is due to the increase in complexity of the code (recursion, another function call etc) and this is the trade off. Processing time is inversely proportional to the size of each processing chunk&lt;/p&gt;&lt;div&gt;[[posterous-content:ImAAruEoruBBGBGeBetI]]&lt;p&gt;Chunk Size (y) vs Response Time (x)&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;As the chunk sizes get smaller you get a more responsive UI but the processing time can get scarily long. The example chunks at about 1% of the total task size and that seems to be a good balance (for this task at least) between responsiveness and processing time. I threw together a little suite of yielding techniques with various update times etc and you can play around with that &lt;a href="http://jsfiddle.net/kouphax/Kkhc9/" target="_blank"&gt;here&lt;/a&gt;&lt;/p&gt;</content></entry><entry><title>&lt; HEAD &gt;ache: Including JavaScript in ASP.NET Master Pages</title><updated>Mon Oct 18 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/18/&lt;-head-&gt;ache-including-javascript-in-asp-dot-net-master-pages/"></link><id>urn:yobriefca-se:feed:post:&lt; HEAD &gt;ache: Including JavaScript in ASP.NET Master Pages</id><content type="html">
&lt;h2&gt;Problem&lt;/h2&gt;&lt;p&gt;Include a Javascript file in the head of of an ASP.NET WebForms Master Page whose src attribute should contain an absolute (from Application Root) path e.g.http://localhost:8080/js/jquery-1.3.2.js (single application server) or &lt;a href="http://localhost:8080/MyProject/js/jquery-1.3.2.js"&gt;http://localhost:8080/MyProject/js/jquery-1.3.2.js&lt;/a&gt; (multi application server).  Sounds simple enough but it's surprisingly difficult.&lt;/p&gt;&lt;h3&gt;Attempt 1: Use a fixed path. (Partial Success)&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;?/js/jquery-1.3.2.js?&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/NIApp/js/jquery-1.3.2.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;While this "works" it isn't exactly the most resilient to server changes - what if we move to/from a single/multi application server?  We need to re-write all our URL's&lt;/p&gt;&lt;h3&gt;Attempt 2: Use Application Root Reference (~) (Fail)&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;?~/js/jquery-1.3.2.js?&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This just plain doesn't work because script tags do not get parsed by ASP page renderer so the URL doesn't get converted and what you see here is what you end up with on the page which obviously isn't correct.&lt;/p&gt;&lt;h3&gt;Attempt 3: Use Server Code (&amp;lt;%= %&amp;gt;) (Partial Success/Fail)&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;?&amp;lt;%=Page.ResolveUrl(?~/js/jquery-1.3.2.js?)%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=?&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="o"&gt;?&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again this is going to fail in certain situations (but not all).  This will fail when your head tag has a runat="server" attribute present.  In some cases you can simply remove the attribute but if you are using the "out-of-the-box" CSS Themes/Skins you NEED this.&lt;/p&gt;&lt;h3&gt;Attempt 4: Use a Second &amp;lt;HEAD&amp;gt; Element (Partial Success)&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;?&amp;lt;%=Page.ResolveUrl(?~/js/jquery-1.3.2.js?)%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=?&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="o"&gt;?&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;head&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;?server?&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; ? &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will work as you would expect - the only problem is that it isn't valid Markup and will fail if you throw it through W3C Validators.&lt;/p&gt;&lt;h3&gt;Attempt 5: Add script tags "Outside" Head Element (Partial Success)&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;?server?&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; ? &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;?&amp;lt;%=Page.ResolveUrl(?~/js/jquery-1.3.2.js?)%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=?&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;javascript&lt;/span&gt;&lt;span class="o"&gt;?&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Again this will work the only problem is you can never guarantee another developer won't stick code (which is dependant on your global script) BEFORE your inclusion of said script which is a bit of a &amp;lt;head&amp;gt;ache (i.e. in the &amp;lt;head&amp;gt; element - get it?).&lt;/p&gt;&lt;h3&gt;Attempt 6: Insert Script Tags via MasterPage Page_Load Event (Success)&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Page_Load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;HtmlGenericControl&lt;/span&gt; &lt;span class="n"&gt;script&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;HtmlGenericControl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;javascript&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Attributes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ResolveUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;~/&lt;/span&gt;&lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;jquery&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1.3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="m"&gt;2.&lt;/span&gt;&lt;span class="n"&gt;js&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="n"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Controls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;script&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I suppose this is the first real successful attempt.  It does what it says by dynamically creating new Script tag element and inserting them to the top of &amp;lt;head&amp;gt;.  I inserted everything at the top of the head so no matter where people put their scripts it's going to have all global resources available.  The only problem with this is that EVERY Page_Load event is going to dynamically inject new Elements into the head and it's not exactly a very clean separation of mark-up and code.&lt;/p&gt;&lt;h3&gt;Attempt 7/Solution 1: Use Data Binding to Evaluate src Attribute (AKA The Holmes Technique)&lt;/h3&gt;&lt;p&gt;ASPX&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;head&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;    
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;?&amp;lt;%#&lt;/span&gt; &lt;span class="na"&gt;ResolveUrl&lt;/span&gt;&lt;span class="err"&gt;("~/&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;/&lt;/span&gt;&lt;span class="na"&gt;jquery-1&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;3&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;2&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;js&lt;/span&gt;&lt;span class="err"&gt;")%&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;?&amp;gt;&lt;/span&gt;   &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;asp:ContentPlaceHolder&lt;/span&gt; &lt;span class="na"&gt;ID=&lt;/span&gt;&lt;span class="s"&gt;"head"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/asp:ContentPlaceHolder&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Jonny Holmes &lt;a href="http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/"&gt;uncovered&lt;/a&gt; a more elegant solution to this issue and it is described above.  It gets around the issue of not being able to use &amp;lt;%= %&amp;gt; tags by using a Data Binding Expression.  And all you have to do is perform a DataBind() during the Page_Load event and you're sorted like &lt;a href="http://www.youtube.com/watch?v=e0Mzr_A-Q0I"&gt;Ebenezer&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Page_Load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//Called for the javascript references in the header of the master page&lt;/span&gt;
    &lt;span class="n"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DataBind&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Anyone got any other methods to do this?&lt;/p&gt;</content></entry><entry><title>JavaScript Proxy Pattern</title><updated>Mon Oct 18 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/18/javascript-proxy-pattern/"></link><id>urn:yobriefca-se:feed:post:JavaScript Proxy Pattern</id><content type="html">
&lt;p&gt;&lt;/p&gt;&lt;p&gt;More often than not when using 3rd party Javascript widgets/plugins/frameworks is that they don't behave in exactly the way that you want forcing you to change tactics, write some horribly messy boilerplate code, hack the original code or look for alternatives. The proxy pattern is a useful pattern that provides a sort of AOP-lite technique that can be used to transparently manage 3rd party code without having to make breaking changes or repeating horrible code from a single point in the code.&lt;/p&gt;&lt;p&gt;Functions are 1st class objects in JavaScript and this is the feature that powers this pattern. Imagine the setup below,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;// AJAX Object&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;XHR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;get&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="c1"&gt;// perform ajax call to url&lt;/span&gt;
        &lt;span class="c1"&gt;// call the callbakc with the returned data&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="c1"&gt;// specific internal ajax functions.  &lt;/span&gt;
&lt;span class="c1"&gt;// These are 3rd party code that shouldn't be touched&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;parsedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parsedData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;GetData2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="cm"&gt;/* similar thing to GetData*/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;GetDataN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="cm"&gt;/* similar thing to GetData*/&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now imagine there are a set of URLs that require some sort of authentication token passed along with the request. How do we deal with that? Well some people would just hack the internal functions but that's a bad idea as it probably breaks other parts of the system. Another way to do it would be to write your own Ajax framework for the new calls you want to make but that's overkill. The proxy pattern can help here.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;// THE PROXY BLOCK&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
 
    &lt;span class="c1"&gt;// create empty list of secured URLs&lt;/span&gt;
    &lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;securedURLs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
 
    &lt;span class="c1"&gt;// point to original function&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;_get&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
    &lt;span class="c1"&gt;// create proxy function&lt;/span&gt;
    &lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="c1"&gt;// is url a secured url?&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;securedURLs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="c1"&gt;// append authentication token&lt;/span&gt;
            &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s2"&gt;"?authToken="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
 
        &lt;span class="c1"&gt;// call original get function with potentially updated url&lt;/span&gt;
        &lt;span class="nx"&gt;_get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
 
&lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"AUTH_TOKEN"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;XHR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;securedURLs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://secured.kainos.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 
&lt;span class="nx"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://secured.kainos.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="cm"&gt;/* code */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// via proxy&lt;/span&gt;
&lt;span class="nx"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"http://notsecured.kainos.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="cm"&gt;/* code */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// via original&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;So basically if the URL is a secured URL (present in the XHR.securedURLs array) then there will be a bit of pre-processing done to the URL before the function is called. Otherwise the normal function is called. That is the primary aim of the proxy pattern - to enhance existing functionality - at the very least the proxied function should fall back to the original function otherwise other parts of the system could be come affected (unless you know what you are doing of course - cancelling illegal calls etc).&lt;/p&gt;</content></entry><entry><title>Webforms &amp; jQuery: A Comparison</title><updated>Sun Oct 17 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/17/webforms-and-jquery-a-comparison/"></link><id>urn:yobriefca-se:feed:post:Webforms &amp; jQuery: A Comparison</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;em&gt;This one has been pulled from the archives but I still think it is quite relevant&lt;/em&gt;&lt;/p&gt;&lt;p&gt;I tend to be a bit picky when it comes to seeing what is generated by server-side technologies. The first thing I do when I write a page (JSP, ASPX – whatever) that uses some sort of tag library or user control is check the resultant HTML/CSS/Javascript. When it comes to ASPX's this has always been a major issue for me – the code it generates, the JS it pulls down, the amount of redundant traffic, the actual quality of the code – all of it annoys me. Just out of curiosity I wanted to do a “contrast and compare” study to see the differences between the DIY approach and using Webforms…&lt;/p&gt;&lt;p&gt;The use case for the study is simple&lt;/p&gt;&lt;ul&gt;&lt;li&gt;A simple screen with a star rating (1-5)&lt;/li&gt;&lt;li&gt;User selects a rating by clicking a star&lt;/li&gt;&lt;li&gt;The result is posted back, via ajax, to the server&lt;/li&gt;&lt;li&gt;The front end is updated with message indicating the selection and the rating system is disabled&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Technologies Used&lt;/h2&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;h3&gt;Webforms&lt;/h3&gt;&lt;/td&gt;&lt;td&gt;&lt;h3&gt;    jQuery&lt;/h3&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td valign="top"&gt;&lt;p&gt;ASP.NET ScriptManager Control&lt;/p&gt;&lt;p&gt;ASP.NET Update Panel Control&lt;/p&gt;&lt;p&gt;ASP.NET Ajax Toolkit Star Rating Control&lt;/p&gt;&lt;p&gt;ASP.NET Label Control&lt;/p&gt;&lt;/td&gt;&lt;td valign="top"&gt;&lt;p&gt;    ASP.NET MVC Framework&lt;/p&gt;&lt;p&gt;    jQuery v1.3.2&lt;/p&gt;&lt;p&gt;    &lt;a href="http://www.fyneworks.com/jquery/star-rating/"&gt;jQuery Star Rating&lt;/a&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p&gt;There is not much really to draw from this table except for Webforms the majority of the technology is server-side whereas the jQuery stuff is more client side focused. This is one of the big arguments that people use when arguing about webforms. It's all server side work which reduces client side scripting – but that's a bit blind. It's server-side for the developer but the end code isn't any less client side (it's simply auto-generated).&lt;/p&gt;&lt;h2&gt;Development Effort Required&lt;/h2&gt;&lt;p&gt;I'm going to end up being biased on this. I am 10,000 times more familiar with the MVC paradigm and jQuery than I am with Webforms. Ultimately it took me about the same amount of time to knock up both applications. But here are some points to note.&lt;/p&gt;&lt;h3&gt;Webforms&lt;/h3&gt;&lt;p&gt;Drag and drop of all the controls to knock up a simple page. The bulk of the work came from my lack of understanding I guess. I initially stuck the Star Rating control on the page and set up the CSS Classes, this failed as I didn't have the ScriptManager control added to the page. The next issue I faced was that my server side code wasn't actually having any effect – I had to turn AutoPostBack on for it to work but this wasn't Ajax-y. I got around this by wrapping the code in an UpdatePanel.&lt;/p&gt;&lt;p&gt;[[posterous-content:bBdcwIItewquatsyfJyv]]&lt;/p&gt;&lt;p&gt;Default.aspx&lt;/p&gt;&lt;p&gt;[[posterous-content:vfJuAxpqzbFsFoFBFwAE]]&lt;/p&gt;&lt;p&gt;Default.aspx.cs  (Code Behind)&lt;/p&gt;&lt;h3&gt;jQuery&lt;/h3&gt;&lt;p&gt;I used ASP.NET MVC framework to generate my empty page, and controller. I then added a simple one liner Action to my controller to handle the Ajax response. I included jQuery and Star Rating (JS &amp;amp; CSS) to my page and 5 radio buttons that act as the star rating (automatic progressive enhancement) and wrote the initializer code for the star rating stuff. For me that was it – completely straight forward with little configuration.&lt;/p&gt;&lt;p&gt;[[posterous-content:xqpnygHdsdJiruvDqkpf]]&lt;/p&gt;&lt;p&gt;Index.aspx&lt;/p&gt;&lt;p&gt;[[posterous-content:kgpqwhjHGDbiqHJCCvCA]]&lt;/p&gt;&lt;p&gt;RatingController.cs&lt;/p&gt;&lt;h2&gt;Resultant Page Weight &amp;amp; Render Time&lt;/h2&gt;&lt;h3&gt;Webforms&lt;/h3&gt;&lt;p&gt;[[posterous-content:uCAmokcAGoyHstBqGxsj]]&lt;/p&gt;&lt;p&gt;Page Weight&lt;/p&gt;&lt;p&gt;[[posterous-content:nDxjAJoJDbwurDtdAnvx]]&lt;/p&gt;&lt;p&gt;Render Time&lt;/p&gt;&lt;h3&gt;jQuery&lt;/h3&gt;&lt;p&gt;[[posterous-content:cHfxHIJCwxwiGDCgCIuw]]&lt;/p&gt;&lt;p&gt;Page Weight&lt;/p&gt;&lt;p&gt;[[posterous-content:zsChEvClgasifCgquBpo]]&lt;/p&gt;&lt;p&gt;Render Time&lt;/p&gt;&lt;p&gt;Now the real differences are starting to show. The jQuery Solution is light years ahead when it comes to page weight (with marginally faster render times). It's also worth noting here that the Webform solution automatically used GZIP compression for it's scripts whereas the MVC didn't so the results are actually skewed in favour of Webforms (not that it makes a difference).&lt;/p&gt;&lt;p&gt;Here in lies one of my issues with the Webform solution – it pulls down the massive ASP.NET JavaScript framework even though it doesn't use 99.9% of it. jQuery is a complete framework but it's ethos is that it gives you the bare minimum set of tools to get the job done. Look at my code (both server side and client side) – is it at all complex? Not really.&lt;/p&gt;&lt;p&gt;One argument I guess you could use here is that as the page grows the Webform solution, because it has most of the scripts it needs, shouldn't grow much bigger, where as the jQuery solution will. I haven't tested this so I could be wrong but it's a valid argument. That said given any situation I'd be willing to bet I could at the very least match page weight and render time against any Webform solution using jQuery (and MVC).&lt;/p&gt;&lt;h2&gt;Server Responses&lt;/h2&gt;&lt;h3&gt;Webforms&lt;/h3&gt;&lt;p&gt;[[posterous-content:rChlEcegkzzzEgEkxiAc]]&lt;/p&gt;&lt;p&gt;Request Parameters&lt;/p&gt;&lt;p&gt;[[posterous-content:gIgansGldxpvrcfvkapa]]&lt;/p&gt;&lt;p&gt;Response Body&lt;/p&gt;&lt;h3&gt;jQuery&lt;/h3&gt;&lt;p&gt;&lt;img src="http://blogs.kainos.com/jameshu/files/2009/04/jquery-postparams.png" height="21" alt="jquery-postparams" width="70" /&gt;&lt;/p&gt;&lt;p&gt;Request Parameters&lt;/p&gt;&lt;p&gt;&lt;img src="http://blogs.kainos.com/jameshu/files/2009/04/jquery-postresponse.png" height="21" alt="jquery-postresponse" width="70" /&gt;&lt;/p&gt;&lt;p&gt;Response Body&lt;/p&gt;&lt;p&gt;Again there is a big difference here. Because a lot of stuff is automated on the Webform side there is a lot of configuration (Viewstate etc) that needs to be passed about. Even for this simple solution the overhead is considerable – Firefox is giving me traffic (response only sans headers) of 1 byte for jQuery and 3k for Webforms – now multiply that by 2 to roughly include the request as well.&lt;/p&gt;&lt;p&gt;Which is easier to understand here?&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;A lot of the arguments and observations brought up here aren't purely Webform vs jQuery related – a lot have to do with Client vs Server programming but they are still valid. I admit I have a firm grasp of client side coding so the choice for me is obvious but the results here speak for themselves – there is a considerable overhead involved in the Webform world that must be accepted if you want to be able to drag and drop controls and save possibly a small amount of development time.&lt;/p&gt;&lt;p&gt;I am in no way a veteran Webform developer and I am sure there are folks out there that could strip my example down to the bare minimum and get similar results and to them I open floor – bring it – I haven't begun!&lt;/p&gt;</content></entry><entry><title>Strong Typing vs. Magic Strings (Part II)</title><updated>Sun Oct 17 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/17/strong-typing-vs-magic-strings-part-ii/"></link><id>urn:yobriefca-se:feed:post:Strong Typing vs. Magic Strings (Part II)</id><content type="html">
&lt;p&gt;One of the problems with ASP.NET MVC is it's use of magic strings in almost every facet of the architecture.  I've mentioned this &lt;a href="http://blogs.kainos.com/jameshu/2010/03/19/avoiding-magic-strings-in-mvc2/"&gt;before&lt;/a&gt; but I have a few other things I'd like to mention.  In my last post I used T4MVC to generate static classes that layer on top of the magic strings but if these don't generate when you want them to they become out of sync and break compile time checking.  One solution is to generate the "proxy" files one every save however if you have a large project this could be a bit of a pain.&lt;/p&gt;&lt;p&gt;Other solutions exist already.  MVC2 comes with strongly typed HtmlHelpers so which turns&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.TextBox("FullName")%&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Into,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.TextBoxFor(m =&amp;gt; m.FullName)%&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Great - compile time checking and the ability to reflect things such as attribute for generating validation etc.  Problem is they didn't really do much else.  Controller methods still suffered from Magicstringitis.  RedirectToAction still needed magic strings for controller and action names and ModelState.AddModelError still needed string for property names.&lt;/p&gt;&lt;h2&gt;MvcContrib&lt;/h2&gt;&lt;p&gt;&lt;a href="http://mvccontrib.codeplex.com/Wikipage"&gt;MvcContrib&lt;/a&gt; offers a load of non-official but excellent extensions to MVC and one of the things that comes bundled with the build is a strongly typed RedirectToAction extension method.  This lets change this,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RedirectToAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Index"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Home"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Into&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RedirectToAction&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;HomeController&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Index&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Yet again  - compile time checking, refactoring support and no magic strings - Win.  But that leaves one last thing that still bugs me - ModelState.AddModelError.  To add an error for a particualr property of your model you still need to do something like this...&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;ModelState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddModelError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Username"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Username is already in use"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;YAMS! Yet another magic string!&lt;/p&gt;&lt;h2&gt;DIY&lt;/h2&gt;&lt;p&gt;Seeing as I couldn't find a solution from my good friend Google I decided to see if I could do it myself.  Turns out it's bloody easy.  This extension method will allow us to get rid of magic strings when expecting to map to a property of a simple class (such as a model or DTO)&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;/// Adds a model error using strongly typed lambda expressions&lt;/span&gt;
&lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="n"&gt;AddModelError&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TModel&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;ModelStateDictionary&lt;/span&gt; &lt;span class="n"&gt;modelState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="n"&gt;Expression&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Func&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentNullException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"method"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 
    &lt;span class="n"&gt;MemberExpression&lt;/span&gt; &lt;span class="n"&gt;mce&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;MemberExpression&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;property&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Member&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;modelState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddModelError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;property&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And you can now call it like this...&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;ModelState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AddModelError&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Username already in use"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Neat!&lt;/p&gt;&lt;p&gt;One more bit of proof that lambda expressions are awesome - true there is reflection involved so it's going to be slower than magic strings but in my daily use I haven't really noticed any performance hits so (thanks to Jeff Atwood @ &lt;a href="http://www.codinghorror.com"&gt;codinghorror.com&lt;/a&gt;) I can give it a "Works on my Machine" badge.&lt;/p&gt;&lt;p&gt;&lt;img title="6a0120a85dcdae970b0128776ff992970c-pi[1]" src="http://codinghorror.typepad.com/.a/6a0120a85dcdae970b0128776ff992970c-pi" border="0" height="193" alt="6a0120a85dcdae970b0128776ff992970c-pi[1]" width="200" /&gt;&lt;/p&gt;</content></entry><entry><title>Multi-Touch Reactive Extensions</title><updated>Sun Oct 17 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/17/multi-touch-reactive-extensions/"></link><id>urn:yobriefca-se:feed:post:Multi-Touch Reactive Extensions</id><content type="html">
&lt;p&gt;Link: &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;Reactive Extensions for .NET&lt;/a&gt;&lt;/p&gt;&lt;p&gt;It's been a while since I've played with MultiTouch functionality so I decided it was time to dust off the old TouchSmart and put it to good use.  The purpose wasn't to play with MultiTouch functionality but more to see how we could simplify managing isolated events using &lt;a href="http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx"&gt;RX&lt;/a&gt;.  The MultiTouch API is good for this because by its very nature MultiTouch requires us to collect multiple events simultaneously and wire them up to create gestures.  Unfortunately the current iteration of MultiTouch API wrappers for Silverlight and WPF (&lt;a href="http://touch.codeplex.com/"&gt;touch&lt;/a&gt;, &lt;a href="http://miria.codeplex.com/"&gt;MIRIA&lt;/a&gt; etc) are quite basic and when working with custom gestures you are forced to introduce a lot of global code such as flags etc.  Not pretty!&lt;/p&gt;&lt;p&gt;Imagine the following scenario.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;2 touch points on screen (let call them HoldRight and HoldLeft).  Both expose 2 events Hold and Release&lt;/li&gt;&lt;li&gt;When a person presses and hold a finger over BOTH points trigger an event&lt;/li&gt;&lt;li&gt;If only one is being pressed and held do not fire the event&lt;/li&gt;&lt;li&gt;If the user lifts their finger or fingers off the points and repeats the process the event should fire again.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;This is a fairly simple example but actually wiring this up requires the use of nasty global variables and a bit of repetition.  Here is a very quick implementation of this.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;partial&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainPage&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UserControl&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;_rightHeld&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;_leftHeld&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MainPage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;HoldLeft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hold&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;HoldLeft_Hold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;HoldRight&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hold&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;HoldRight_Hold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;HoldLeft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Release&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;HoldLeft_Release&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;HoldRight&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Release&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;HoldRight_Release&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HoldRight_Release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_rightHeld&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;DoCheck&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HoldLeft_Release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_leftHeld&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;DoCheck&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HoldRight_Hold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;GestureHoldEventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_rightHeld&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;DoCheck&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;HoldLeft_Hold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;GestureHoldEventArgs&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_leftHeld&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;DoCheck&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
 
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;DoCheck&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_rightHeld&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;_leftHeld&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Fire the event&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;As you can see I have made use of 2 global variables a DoCheck method that checks if the 2 canvases are currently in a "held" state and fires the event if they are.  All the event handlers do nearly the same thing.  Sure I could refactor this to use maybe 2 event handlers and inspect the sender but that starts getting messy.&lt;/p&gt;&lt;h2&gt;Reactive Extensions to the Rescue!&lt;/h2&gt;&lt;p&gt;Reactive extensions let us "compose" events so we can pass them around and filter them like first class citizens.  Because of this we can actually create custom events by combining.  Tackling the same issue as above we can achieve the same functionality without the need for dodgy global vars and boilerplate code.  Lets look at the code first.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;partial&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainPage&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;UserControl&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MainPage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// compose events&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;leftHold&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FromEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;GestureHoldEventArgs&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
            &lt;span class="n"&gt;HoldLeft&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hold"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;rightHold&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FromEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;GestureHoldEventArgs&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
            &lt;span class="n"&gt;HoldRight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hold"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;rightRelease&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ObservableEx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FromMultiTouchReleaseEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HoldRight&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;leftRelease&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ObservableEx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FromMultiTouchReleaseEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HoldLeft&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 
        &lt;span class="c1"&gt;// subscribe to dual hold event&lt;/span&gt;
        &lt;span class="n"&gt;leftHold&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rightHold&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;// listen until either release triggered&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TakeUntil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leftRelease&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Amb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rightRelease&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="c1"&gt;// trigger the event&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* trigger event */&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ObservableEx&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IObservable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;EventArgs&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;FromMultiTouchReleaseEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;TCanvas&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FromEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;TCanvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReleaseHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EventArgs&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;
                &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;TCanvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ReleaseHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
                &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Release&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Release&lt;/span&gt; &lt;span class="p"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;
            &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First things first for some random reason the implementers of MIRIA decided to create en event the actually passed NO event args - this is not normal and could easily be considered and anti-pattern.  Because of this I had to use the long winded Observable.FromEvent which is created as a helper method.&lt;/p&gt;&lt;p&gt;So what are we doing here?&lt;/p&gt;&lt;ol&gt;&lt;li&gt;Compose our events into first class citizens - Observables&lt;/li&gt;&lt;li&gt;Zip left and right hold events.  Zipping basically combines 2 Observables and publishes or emits a value when both observables have emitted a value.  The second argument in Zip is the transform function that converts the 2 emitted values into 1 value.  We don't care about this value so just return some arbitrary value.&lt;/li&gt;&lt;li&gt;Take the Zip generated observable and keep publishing it's values until Either the left or right release events are triggers (Amb = most ambitious - publishes first value to appear)&lt;/li&gt;&lt;li&gt;Subscribe to this super composed event.  When this happens we can fire our event&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;What actually happens on the front end is irrelevant (the solution looks like this and will spin when the two Thumb areas are pressed and held)&lt;/p&gt;&lt;p&gt;[[posterous-content:ECxarBiHmvIvJutnbJaJ]]&lt;/p&gt;&lt;p&gt;But the fact we have managed to combine 4 isolated events without having to use boilerplate is very nice.  This gives us a lot of power to create and control complex gestures - not limited to MultiTouch but any sort of UI interactions (Mouse events, Web service calls etc).&lt;/p&gt;&lt;p&gt;&lt;em&gt;* I'll keep saying this.  RX doesn't do anything new or solve any unsolvable problems but what it does do is allow us to do things in a neater way.  Now there is a bit of learning curve in it - you really need to start thinking in RX but once you wrap your head around RX is a nice tool to have on your tool belt.  No it's not complexity for complexity's sake - I honestly believe the code above, when used in the real world, will hep reduce complexity and make maintenance easier.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Good Fortune Awaits!&lt;/p&gt;</content></entry><entry><title>T4MVC: Strong Typing vs Magic Strings</title><updated>Sun Oct 17 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/17/t4mvc-strong-typing-vs-magic-strings/"></link><id>urn:yobriefca-se:feed:post:T4MVC: Strong Typing vs Magic Strings</id><content type="html">
&lt;p&gt;Magic strings are everywhere in ASP.NET MVC 2.  Less so than in version 1 and some of the Beta and RC releases but there are still some kicking around.  The problem with literals is that they don't give you any compile time error checking or refactoring ability.  Say for example you have an action link that points you to the Edit action of the Person controller - you write it like this.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%=Html.ActionLink("Edit", "Person", new { id = 12 }) %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now image you've written something like this over numerous pages and all of sudden you change the name of the Action or the Controller.  These are literal strings so you can't use the refactor tools in VS or get any compile time error when you build.  The only real way to ensure everything has been changed would be to do a quick regression test.  Nightmare.&lt;/p&gt;&lt;h2&gt;&lt;a href="http://mvccontrib.codeplex.com/wikipage?title=T4MVC&amp;amp;referringTitle=Documentation"&gt;T4MVC&lt;/a&gt;&lt;/h2&gt;&lt;p&gt;T4MVC makes use of the Visual Studio bundled T4 Text Template Transform Toolkit (Template based code generation tool) to generate invisible classes that give you strongly typed links to Controllers, Actions, Views and even JavaScript, CSS, Images and other static links.&lt;/p&gt;&lt;p&gt;T4MVC is part of the MVCContrib project (though completely standalone).  You simply download the zip with the 2 T4 files and add them to the root of your MVC project and you are done.  Here are some examples of it in action.&lt;/p&gt;&lt;h3&gt;VIEW NAMES&lt;/h3&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% Html.RenderPartial("DinnerForm"); %&amp;gt;
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;% Html.RenderPartial(MVC.Dinners.Views.DinnerForm); %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And within a controller&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"InvalidOwner"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InvalidOwner&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;CONTROLLER ACTIONS&lt;/h3&gt;&lt;p&gt;&lt;span style="font-weight: normal;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%= Html.ActionLink("Delete Dinner", "Delete", "Dinners", new { id = Model.DinnerID }, null)%&amp;gt;
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;%= Html.ActionLink("Delete Dinner", MVC.Dinners.Delete(Model.DinnerID))%&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
 &lt;h3&gt;STATIC FILES&lt;/h3&gt;&lt;p&gt;You also get the ability to reference static files so that if they are moved or renamed you'll not be missing images etc. - something that is a pain to test most times.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Content/nerd.jpg"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%= Links.Content.nerd_jpg %&amp;gt;"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Another nicety is that you also get this for JavaScript files but without having to do anything it will swap out debug (uncompressed) versions of you files for minified versions when you move into production.  As long as you have &amp;lt;filename&amp;gt;.js and &amp;lt;filename&amp;gt;-min.js side by side it will determine which one to use based on the build environment.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/Scripts/Map.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;%= Links.Scripts.Map_js %&amp;gt;"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
 &lt;h3&gt;Summing Up&lt;/h3&gt;&lt;p&gt;T4MVC is a nice tool to get around magic strings.  There are other things out there that give "fluent" syntax to methods that use literals which use delegates instead of generating any sort of proxy classes.  Which is better is a question I still can't answer confidently.  I have had some minor issues using T4MVC (mostly my fault) and I am a bit dubious about the syntax for static files but it's a decent starter for 10 and doesn't need any thought to implement and use.  Of course you could also roll own specific T4 template (or extend T4MVC) but that obviously requires a bit more work.&lt;/p&gt;</content></entry><entry><title>jQuery Mobile Quick Look</title><updated>Sun Oct 17 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/17/jquery-mobile-quick-look/"></link><id>urn:yobriefca-se:feed:post:jQuery Mobile Quick Look</id><content type="html">
&lt;p&gt;jQuery Mobile was &lt;span style="color: #1f497d;"&gt;&lt;a href="http://jquerymobile.com/2010/10/jquery-mobile-alpha-1-released/"&gt;released&lt;/a&gt;&lt;/span&gt; over the weekend or at least the alpha version of it was (full release scheduled for some time on 2011). &lt;/p&gt;&lt;p&gt;So what is it like?  Well first things first it's early days, there are certainly more than few rough edges but given the development timeline (~2 months) it is quite an impressive effort put in by the &lt;span style="color: #1f497d;"&gt;&lt;a href="http://www.filamentgroup.com/"&gt;Filament Group&lt;/a&gt;&lt;/span&gt; and others.&lt;/p&gt;&lt;h2&gt;iThings&lt;/h2&gt;&lt;p&gt;The first question most people will ask (or the first thing they will try and do) will probably be&lt;/p&gt;&lt;blockquote&gt;&lt;p style="padding-left: 30px;"&gt;So how does it look on the various iThings (iPad, iPhone, iPhone 4)? &lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Well it looks good for the most part.  There are some issues around page transitions and displaying dialogs (especially on the iPad) - things like page flickering, hidden content becoming visible after the page has loaded etc. and while they are a bit annoying they don't "break" anything.&lt;/p&gt;&lt;p&gt;As the framework isn't targeting iOS specifically there are some behaviours that would wouldn't necessarily expect if you used this as a native app (using PhoneGap or whatever).  Toolbars aren't explicitly fixed and when they are fixed disappear on scroll and repositioned after scrolling has finished.  Select boxes don't display the normal pickers found in native iOS but instead use a custom dialog for selection.  Again these aren't criticisms, they work well, but I have heard people already complaining because they expect the framework to produce a fully native looking app.&lt;/p&gt;&lt;h2&gt;Sencha Touch&lt;/h2&gt;&lt;p&gt;When comparing frameworks many peoples first thoughts will be&lt;/p&gt;&lt;blockquote&gt;&lt;p style="padding-left: 30px;"&gt;How does it compare to &lt;span style="color: #1f497d;"&gt;&lt;a href="http://www.sencha.com/products/touch/"&gt;Sencha Touch&lt;/a&gt;&lt;/span&gt; &lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;To be honest  the comparison is like comparing apples to oranges, or jQueryUI to ExtJS.  Both projects have also different goals.  While Sencha currently aims to provide a rich native like experience on a small subset of mobile browsers (specifically iOS and Android), jQuery Mobile aims to create a consistent, mobile/touch capable experience across a much &lt;span style="color: #1f497d;"&gt;&lt;a href="http://jquerymobile.com/gbs/Touch"&gt;wider range of mobile browsers&lt;/a&gt;&lt;/span&gt;.  This is by no means easy.  An interaction style one device may not be suitable for another.&lt;/p&gt;&lt;h2&gt;FOUC&lt;/h2&gt;&lt;p&gt;One of the goals of jQuery Mobile is to progressively enhance the base page to become more mobile capable.  Because of this the base page is very basic with jQuery Mobile specific stylers applied when the DOM is ready.  This can lead to some initial FOUC (Flash of Unstyled Content).  We can get around this by applying some of the basic jQuery Mobile styles to begin with and I suspect we will see this tactic being used on future releases.&lt;/p&gt;&lt;p&gt;[[posterous-content:EeHJJwvCqFJrzcdxeEEb]]&lt;/p&gt;&lt;h2&gt;Accessibility&lt;/h2&gt;&lt;p&gt;One thing I never really though of in a Mobile Framework is accessibility.  This is just short sightedness on my part as it is as relevant in the mobile world as it is in the desktop world.  jQuery Mobile aiming to be fully accessible through it's use of ARIA roles and progressive enhancement.&lt;/p&gt;&lt;h2&gt;The Technicals&lt;/h2&gt;&lt;p&gt;From a technical/developer stand point jQuery Mobile is quite nice.  Lets look at a few of the highlights,&lt;/p&gt;&lt;h3&gt;Pages&lt;/h3&gt;&lt;p&gt;Sites can be built as a single page with sub sections being marked as "pages" that jQuery Mobile can navigate.  External pages are load using ajax (rather than simply navigating to them), this give jQuery Mobile better control over transitions, and history management.  A simple page looks like this,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt; 
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt; 
        &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;jQuery Mobile&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt; 
        &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"Styles/jquery.mobile-1.0a1.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"Scripts/jquery-1.4.3.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"Scripts/jquery.mobile-1.0a1.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt; 
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt; 
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;   
                &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 1&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 2&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 1 Content&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;   
                &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 2&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 2 Content&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 2 Footer&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So lets break this down and pick out the interesting parts,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;HTML5 DocType which older browsers can handle gracefully&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"Styles/jquery.mobile-1.0a1.css"&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/css"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"Scripts/jquery-1.4.3.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"Scripts/jquery.mobile-1.0a1.js"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Scripts and styles included.  Now from a mobile perspective this is still quite a bit of script especially over some flaky 3G connections but in comparison to some of the frameworks out there it is still quite small.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;   
        &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 1&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 2&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 1 Content&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"page"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"about"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"header"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;   
        &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Page 2&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 2 Content&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"footer"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Page 2 Footer&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;This section describes 2 pages.  A page is marked as a page by its `data-role` attribute and can contain 3 elements marking a header, the page content and the footer.  Unless stipulated the first page will be first page in the DOM.  Navigation between pages is handled using location.hash changes (page 1s button links to Page 2 in the example).&lt;/p&gt;&lt;h3&gt;Themeing&lt;/h3&gt;&lt;p&gt;Themeroller will see an overhaul on the official release which will allow you to create custom themes a la jQueryUI.  Currently jQuery Mobile comes with 2 themes both of which have a number of swtaches.  This is intended to create contrast between UI elements (e.g. for marking mandatory fields, or highlight primary buttons on a form etc.)&lt;/p&gt;&lt;h3&gt;Dialogs&lt;/h3&gt;&lt;p&gt;Dialogs in jQuery Mobile are simply pages with a `data-rel="dialog"` added to them so the same rules apply as above.&lt;/p&gt;&lt;h3&gt;Form Elements&lt;/h3&gt;&lt;p&gt;Forms in jQuery Mobile are plain old HTML(5) forms which is actually a VERY nice feature.  Sencha/ExtJS forms are nice but when using SELECTs etc. can become a real pain to manage.  jQuery Mobile uses a much lighter approach.  Some screenshots (iPhone 4)&lt;/p&gt;&lt;p&gt;[[posterous-content:qipBwFyayBhuztrHFpJI]] So thats a very quick overview of jQuery Mobile.  I encourage you to play around with and get those bugs filed.  As I've said it's early days but it's a big goal they are aiming for and they are currently well on track to produce a very nice framework come the official release.&lt;/p&gt;</content></entry><entry><title>jQuery 1.4.3 &amp; jQuery Mobile Released</title><updated>Fri Oct 15 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/15/jquery-1-dot-4-3-and-jquery-mobile-released/"></link><id>urn:yobriefca-se:feed:post:jQuery 1.4.3 &amp; jQuery Mobile Released</id><content type="html">
&lt;p&gt;As expected jQuery 1.4.3 and jQuery Mobile (alpha) have dropped today. The official announcements are here&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://blog.jquery.com/2010/10/16/jquery-143-released/"&gt;jQuery 1.4.3&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://jquerymobile.com/2010/10/jquery-mobile-alpha-1-released/"&gt;jQuery Mobile&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Having had the chance to play around with these already here are a few of the highlights that I think are worth mentioning.  This first post will talk about jQuery Core as I need a bit more time with jQuery Mobile to get a better feel for it.&lt;/p&gt;&lt;h2&gt;jQuery 1.4.3&lt;/h2&gt;&lt;h3&gt;.data()&lt;/h3&gt;&lt;p&gt;One of the first cool things that 1.4.3 brings with it is a tweak to the .data() method to allow it to work with not only DOM objects but plain old JavaScript objects as well. So what? Well it opens the doors for the recently announced &lt;a href="http://github.com/jquery/jquery-datalink"&gt;Data Linking&lt;/a&gt; plugin from Microsoft (&lt;a href="http://yobriefcase.posterous.com/microsofts-trio-of-jquery-plugins"&gt;blogged about it before&lt;/a&gt;) until now ran on a patched version of jQuery Core. Essentially this give us the ability to perform data binding between UI elements and a model object. This will be familiar to people who use binding in Silverlight. This is all made possible via the newly introduced changeData event that will get fired when .data() is used to modify a data value.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'jameshu'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;fullName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'James Hughes'&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"changeData"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" changed to "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"username"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"jameshu2"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// =&amp;gt; username changed to jameshu2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Another nice new feature rolled into .data() is that all HTML data attributes are automatically parsed as data on initial selection of the element(s). This behaviour is similar to the metadata plugin that has been around for some time. In fact data values are coerced into the applicable types ("true" becomes Boolean, "{o:1}" becomes a JavaScript object etc.). This is a nice feature to have but I wonder how resilient it is to weird strings.&lt;/p&gt;&lt;p&gt;So for,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-role=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt; &lt;span class="na"&gt;data-hidden=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="na"&gt;data-options=&lt;/span&gt;&lt;span class="s"&gt;'{"name":"James Hughes"}'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The following will be true&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"container"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hidden"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"options"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"James Hughes"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This is going to play very nice with the Unobtrusive JavaScript/Ajax features of the ASP.NET MVC3 (which already employs something similar)&lt;/p&gt;&lt;h3&gt;Modularity&lt;/h3&gt;&lt;p&gt;One of the issues many people had with jQuery was it was "all or nothing".  This was fine at the start but as the file size started to grow people felt that they should be able to only use a small part of jQuery without having to pull the whole thing down.  This release sees a fairly big refactoring to try and make this wish a reality.  jQuery Core has been broken up into a number of sub modules which gives us the ability to load modules on demand (see &lt;a href="http://yobriefcase.posterous.com/30487860"&gt;Modernizr and Require.JS&lt;/a&gt;) or simply use the modules we need.&lt;/p&gt;&lt;h3&gt;jQuery.type()&lt;/h3&gt;&lt;p&gt;There are plenty of little cross browser differences in the typeof operator and as such it's not always reliable.  The jQuery type method abstracts out all those little quirks to correctly determine arrays etc.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'boolean'&lt;/span&gt;
&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'number'&lt;/span&gt;
&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'test'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;
&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){})&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'function'&lt;/span&gt;
&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'array'&lt;/span&gt;
&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'date'&lt;/span&gt;
&lt;span class="nx"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/test/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'regexp'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;h3&gt;And The Rest&lt;/h3&gt;&lt;p&gt;There are a load more improvements and tweaks in 1.4.3 that are fully explained in the original announcement including,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Performance tweaks across CSS and Traversal modules&lt;/li&gt;&lt;li&gt;Tweak to allow pausing the DOM Ready event until explicitly specified&lt;/li&gt;&lt;li&gt;Event helpers can now be passed data objects&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Now onto jQuery Mobile....&lt;/p&gt;</content></entry><entry><title>Microsoft's Trio of jQuery Plugins</title><updated>Fri Oct 15 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/15/microsofts-trio-of-jquery-plugins/"></link><id>urn:yobriefca-se:feed:post:Microsoft's Trio of jQuery Plugins</id><content type="html">
&lt;p&gt;Microsoft's 3 jQuery plugins have been accepted as official plugins (with templating being absorbed into the core in jQuery 1.5).  Lets have a quick look at what they provide.&lt;/p&gt;&lt;p&gt;There are a ton of blog posts and documentation out there describing what these plugins do,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://blog.jquery.com/2010/10/04/new-official-jquery-plugins-provide-templating-data-linking-and-globalization/"&gt;Official Post&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2010/10/04/jquery-templates-data-link-and-globalization-accepted-as-official-jquery-plugins.aspx"&gt;Scottgu's Annoucement&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://github.com/jquery/jquery-tmpl/"&gt;Template Git Repo&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://github.com/jquery/jquery-tmpl/"&gt;Data Link Git Repo&lt;/a&gt; &lt;/li&gt;&lt;li&gt;&lt;a href="http://github.com/jquery/jquery-global"&gt;Globalisation Git Repo&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I am going to ignore Globalisation for now as it takes too long to put a worthwhile demo together and it's alreaqdy heavily explained on the GitHub page linked above.  So lets take the other plugins and use them to create "the best todo app evar"&lt;/p&gt;&lt;h2&gt;ToDo App&lt;/h2&gt;&lt;p&gt;Lets take a simple to do app the you can add Tasks to (along with associated comments) then save them to the server.  Simple design like so,&lt;/p&gt;&lt;p&gt;[[posterous-content:iesdxxncdohkDnbunIad]]&lt;/p&gt;&lt;h2&gt;HTML&lt;/h2&gt;&lt;p&gt;So first things first lets get the HTML out of the way,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;ToDo List&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"new-task-form"&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"\"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"task"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Task:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"task"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"task"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"note"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Note:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;textarea&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"note"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"note"&lt;/span&gt; &lt;span class="na"&gt;rows=&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt; &lt;span class="na"&gt;cols=&lt;/span&gt;&lt;span class="s"&gt;"30"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/textarea&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Add"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"add"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Save"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"save"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Current ToDos&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"todo-list"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;JavaScript&lt;/h2&gt;&lt;p&gt;Next up is the interesting part - JavaScript,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="c1"&gt;// empty model object&lt;/span&gt;
        &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#todo-list"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'#new-task-form'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;addBtn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#add"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;saveBtn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#save"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// link ui to model&lt;/span&gt;
    &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// handle onclick&lt;/span&gt;
    &lt;span class="nx"&gt;addBtn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// clone the data model&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// generate template&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tmpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;li&amp;gt;${task}&amp;lt;/li&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;appendTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// handle save&lt;/span&gt;
    &lt;span class="nx"&gt;saveBtn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// pull back the data&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"li"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;tmplItem&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// save the object&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/Home/Save"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Todos Saved"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That's it.  So lets break it down and see exactly what is happening.&lt;/p&gt;&lt;h3&gt;Variable Declarations&lt;/h3&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="c1"&gt;// empty model object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First thing we do is declare a model object.  Simply an empty object for our purposes but could be pre-populated with data that can be bound.&lt;/p&gt;&lt;h3&gt;Model Linking&lt;/h3&gt;&lt;p&gt;Model linking is effectively 2 way data binding between a data context and a form (1 way optional and configurable mappings possible see API). &lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;    &lt;span class="c1"&gt;// link ui to model&lt;/span&gt;
    &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We just want simple 2 way binding so that is all we need.  This sets up the 2 form inputs so they are bound to our model object so any changes will be reflected accordingly.&lt;/p&gt;&lt;h3&gt;Add Functionality&lt;/h3&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;    &lt;span class="c1"&gt;// clone the data model&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tmpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;li&amp;gt;${task}&amp;lt;/li&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;appendTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;First things first we need to take a clone of the current object as this is bound/linked to the form and if we don't clone it we will be referencing the same object over and over.  This wont affect the UI but templating has some nice data centric features to pull back our data later and we need this.&lt;/p&gt;&lt;p&gt;Next we create a very simple template passing the cloned model into the template call.  Finally we append this new LI to the UL list. &lt;/p&gt;&lt;h3&gt;Save Functionality&lt;/h3&gt;&lt;p&gt;The interesting part of the save functionality is how we actually pull back the list of data to be sent to the server.&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;    &lt;span class="c1"&gt;// pull back the data&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"li"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;tmplItem&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Templates are data centric.  That is, when they are created the original data that was used to generate them is stored in the jQuery data cache and can be retrieved using tmplItem.  So rather than having to scrape the data back from UI (&lt;em&gt;where are you going to get the comment value from?&lt;/em&gt;) you can simply ask for the elements data context.&lt;/p&gt;&lt;h2&gt;So...&lt;/h2&gt;&lt;p&gt;That's it.  Some very nice data centric (3rd time in as many paragraphs) plugins.  Sure you can do this stuff without these plugins but it would certainly not be as elegant.  This is only skimming the surface of the data linking and templating plugins, there is a heap of stuff that I didn't cover,&lt;/p&gt;&lt;ul&gt;&lt;li&gt;One way binding&lt;/li&gt;&lt;li&gt;Value converters for binding&lt;/li&gt;&lt;li&gt;Programatically change data values&lt;/li&gt;&lt;li&gt;Adding custom linking behaviour&lt;/li&gt;&lt;li&gt;Generate templats from embedded script templates&lt;/li&gt;&lt;li&gt;Complex and nested templating&lt;/li&gt;&lt;li&gt;Statically define a reusable template&lt;/li&gt;&lt;/ul&gt;</content></entry><entry><title>Modernizr &amp; RequireJS</title><updated>Wed Oct 13 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/13/modernizr-and-requirejs/"></link><id>urn:yobriefca-se:feed:post:Modernizr &amp; RequireJS</id><content type="html">
&lt;p&gt;I've been pulling a lot of JavaScript libraries and utilities together to build up a NuPack server as it's by far one of the best ways to distribute client side resources during development.  Some of these libraries I have used myself and thought it might be worth mentioning some of them.&lt;/p&gt;&lt;h2&gt;Modernizr&lt;/h2&gt;&lt;p&gt;Modernizr is a feature detection library that allows us to test the existence of certain HTML5 features within the browser.  This is not only useful when constructing a page and applying styling of JS widgets but also for detecting things such as WebWorkers, Web DB, application cache etc.  It supports many things,&lt;/p&gt;&lt;blockquote class="posterous_medium_quote"&gt;&lt;p&gt;@font-face, Canvas, Canvas Text, HTML5 Audio, HTML5 Video, rgba(), hsla(), border-image:, border-radius:, box-shadow:, opacity:, Multiple backgrounds, CSS Animations, CSS Columns, CSS Gradients, CSS Reflections, CSS 2D Transforms, CSS 3D Transforms, CSS Transitions, Geolocation API, localStorage, sessionStorage, SVG, SMIL, SVG Clipping, Drag and Drop, hashchange, X-window Messaging, History Management, applicationCache, Web Sockets, Web Workers, Web SQL Database, IndexedDB, Input Types, Input Attributes&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;In most cases native support for a feature is always better than an alternative CSS hack or JS script.  Rounded Corners are a good example of this.  A lot of, but not all, browsers already support rounded corners natively but in a world where people think a site should be &lt;a href="http://dowebsitesneedtolookexactlythesameineverybrowser.com/"&gt;identical&lt;/a&gt; in all browsers it is often necessary to provide your own solution.  This means adding extra work for all browsers.  This is where Modernizr comes in.  Rather than applying non-native rounded corners to browsers that support them nativley we can do this,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;Modernizr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;borderradius&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;".borderradius"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;roundedcorners&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;That way we can always defer to the browser and remove unnecessary processing.  But lets take it step further.  Obviously for the majority of browsers pulling down a rounded corner plugin is completely pointless as it will never be used.  What can we do about that?&lt;/p&gt;&lt;h2&gt;Require.JS&lt;/h2&gt;&lt;p&gt;Require.JS is one of many module/script loaders available but the good thing about Require.JS is that is extremely simple.  Essentially it injects a script file into the head of the document and fires a callback when ready (there is a bit more configurability held within but that's the gist of it).  So lets take the above example and see what we can do about it.  We don't want to load scripts unnecessarily (lets leave that up to ASP.NET WebForms, ugh.... ) so lets only load the rounded corner script when needed,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;Modernizr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;borderradius&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"scripts/jquery-corners.js"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
            &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;".borderradius"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;roundedcorners&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;        
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So basically we only get the extra script pulled down in the few browsers that don't support rounded corners, less processing, less traffic (as long as require.js file size &amp;lt; total size of optional scripts). &lt;/p&gt;&lt;p&gt;Nice.&lt;/p&gt;&lt;p&gt;Obviously you need to be a bit more strategic about where you place this code as you can get FOUC if placed after document has fully loaded.  Another issue is that Require.JS intentionally doesn't support CSS as there is no guaranteed way to ensure it has loaded across all browser.  Thats an easy fix though,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;loadCss&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"link"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"text/css"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"stylesheet"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"head"&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;link&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As always I've put together a nice wee demo app that makes use of these technologies.... coming to a NuPack server near you too.&lt;/p&gt;&lt;p&gt;&lt;a href="svn://subversion/Playground/trunk/jameshu/Src/Kainos.Internal.RequireAndModernizr" title="svn://subversion/Playground/trunk/jameshu/Src/Kainos.Internal.RequireAndModernizr"&gt;svn://subversion/Playground/trunk/jameshu/Src/Kainos.Internal.RequireAndModernizr&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Let me know if that works as it uses NuPack (but I don't think that is a requirement to open the project).&lt;/p&gt;</content></entry><entry><title>Outside Events jQuery Plugin</title><updated>Mon Oct 11 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/10/11/outside-events-jquery-plugin/"></link><id>urn:yobriefca-se:feed:post:Outside Events jQuery Plugin</id><content type="html">
&lt;p&gt;&lt;a href="http://benalman.com/projects/jquery-outside-events-plugin/"&gt;Outside Event Plugin&lt;/a&gt; doesn't have a great deal of uses but it does solve one very important issue in client side code - Diminishing Responsibility.  Before I explain how it does this I need to explain what it does.&lt;/p&gt;&lt;p&gt;Outside events are a new set of events provided by the plugin taht are fired on the registered element when the event happens OUTSIDE that element.  Imagine a click event&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#somelement"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"click"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// this element was clicked&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Everytime that #somelement gets clicked the event fires.  An outside event is the opposite of that so,&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#somelement"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"outsideclick"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="c1"&gt;// some other element was clicked&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This event fires when any other element is clicked that WASN'T #somelement (or whatever query you use - we are not stuck to a single element).  Outside events that are supported include (some more useful than others),&lt;/p&gt;&lt;blockquote class="posterous_medium_quote"&gt;&lt;p&gt;&lt;code&gt;clickoutside&lt;/code&gt;, &lt;code&gt;dblclickoutside&lt;/code&gt;, &lt;code&gt;focusoutside&lt;/code&gt;, &lt;code&gt;bluroutside&lt;/code&gt;, &lt;code&gt;mousemoveoutside&lt;/code&gt;, &lt;code&gt;mousedownoutside&lt;/code&gt;, &lt;code&gt;mouseupoutside&lt;/code&gt;, &lt;code&gt;mouseoveroutside&lt;/code&gt;,&lt;code&gt;mouseoutoutside&lt;/code&gt;, &lt;code&gt;keydownoutside&lt;/code&gt;, &lt;code&gt;keypressoutside&lt;/code&gt;, &lt;code&gt;keyupoutside&lt;/code&gt;, &lt;code&gt;changeoutside&lt;/code&gt;, &lt;code&gt;selectoutside&lt;/code&gt;, &lt;code&gt;submitoutside&lt;/code&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Great, cool but what use is this?  I've already said it helps solve the Diminished Responsibility issue but what does that mean?&lt;/p&gt;&lt;h2&gt;Diminished Responsibility&lt;/h2&gt;&lt;p&gt;Imagine an example where there a popup notification on a web page.  It's not obtrusive but should go away when the user clicks off it.  How do you do this?  The simple answer would be to add a load of (or delegate) click events and hide the element so your handlers would start to look like this [very contrived BTW],&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#somelement"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"click"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#dialog"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;is&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":visible"&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
       &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#dialog"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;hide&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// perform function&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Starts to get a bit messy if we have a lot of handlers and is actually bad practise as the popup notification should be as self contained/self managing as possible.  Outside events solve this by allowing you to push responsibility back onto the widget/element that needs to "do something".  Neat.&lt;/p&gt;&lt;h2&gt;The Magic&lt;/h2&gt;&lt;p&gt;Funny thing is while this looks like it may be complicated to implement it's really not.  Thanks to the power of event delegation and event bubbling all the plugin does is attach the inverse of the outside event (outsideclick becomes click) to the document element and when a click event is fired the handler checks if the clicked element is an element registered with outsideclick and if not fires the event. &lt;/p&gt;&lt;p&gt;Easy peasy though it does open up the possibility of infinite event triggers but that's not the fault of the plugin :-P. &lt;/p&gt;</content></entry><entry><title>ASP.NET MVC 3 Preview 1 First Look</title><updated>Tue Jul 27 23:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/07/27/asp-dot-net-mvc-3-preview-1-first-look/"></link><id>urn:yobriefca-se:feed:post:ASP.NET MVC 3 Preview 1 First Look</id><content type="html">
&lt;p style="margin-right: 0cm; margin-bottom: 16.5pt; margin-left: 0cm; line-height: 16.5pt; vertical-align: baseline;"&gt;&lt;span style="font-size: 11.5pt; font-family: Georgia,serif; color: #333333;"&gt;[[posterous-content:6xLmqhke9CMHGNqiaUG7]]&lt;/span&gt;&lt;span style="font-size: 11.5pt; font-family: Georgia,serif; color: #333333;"&gt; &lt;/span&gt;&lt;/p&gt;&lt;p&gt;A much better read than the ramblings below:&lt;br /&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx"&gt;http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;So I had a chance to take ASP.NET MVC 3 Preview 1 out for a test drive today to see what was new and interesting.  For a Preview release it seems fairly solid - there is a lot of low level stuff introduced (new Interfaces etc) which is, I assume, going be used to add new features in Preview 2, 3, 4 whatever.  There is nothing MASSIVE per say in fact it really feels like the next logical step in the MVC roadmap rather than some crazy reimagining.  That's good by the way - backwards compatibility maintained and life goes on as normal.  The most obvious "issue" I have with this release is that it's NET4/VS2010 only which means if it came out tomorrow we'd probably not have the choice of making use of it due to the snails pace of the "enterprise".  Anyways enough preamble, down to the juicy stuff - what's this release giving us?&lt;/p&gt;&lt;h2&gt;Razor&lt;/h2&gt;&lt;p&gt;Preview 1 introduces Microsofts new ViewEngine, Razor, to the MVC world (WebMatrix aside).  Currently there is no syntax highlighting/intellisense for it but it'll still compile and generate views just fine.  I don't want to dive into Razor in this post but it's a weird syntax.  It's certainly feels much neater than the WebForms View Engine and for a weird reason it felt quite natural to use it - want to embed some serverside code?  Just type @ and you drop into serverside mode but with the advantage of actually still being able to embed HTML content without having to escape it.  The render just knows.  It's strange but good.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;@inherits System.Web.Mvc.WebViewPage&lt;span class="nt"&gt;&amp;lt;Models&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Person&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
@{
    View.Title = "Home Page";
    LayoutPage = "~/Views/Shared/_Layout.cshtml";
}

&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    @Html.ValidationSummary()
    @{Html.EnableClientValidation();}
    @using(Html.BeginForm()){
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Name:&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
        @Html.TextBoxFor(p =&amp;gt; p.Name)
        &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;@Html.ValidationMessageFor(p =&amp;gt; p.Name)&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Submit"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    }
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Read more here: &lt;a href="http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx"&gt;http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;On a side note when you create  a new view MVC 3 and VS2010 detects installed view engines so you can EASILY pick and choose which is very nice - though no ViewEngine currently supports this e.g.&lt;/p&gt;&lt;p&gt;[[posterous-content:Ryck0mqEkX92dSBGu1n4]]&lt;/p&gt;&lt;p&gt;Model Validation&lt;/p&gt;&lt;p&gt;There is some nice stuff in this area.  In MVC you where restricted to validating properties of a model (by default using data annotations) and if you wanted to validate entire models or perform validation in model scope (e.g. compare 2 fields) you had to write custom code.&lt;/p&gt;&lt;p&gt;MVC3 provides model-level validation in 2 ways,&lt;/p&gt;&lt;p style="" class="MsoListParagraph"&gt;&lt;span style=""&gt;1.&lt;span style="font: 7.0pt Times New Roman;"&gt;       &lt;/span&gt;&lt;/span&gt;Using Validation Attributes&lt;/p&gt;&lt;p style="" class="MsoListParagraph"&gt;&lt;span style=""&gt;2.&lt;span style="font: 7.0pt Times New Roman;"&gt;       &lt;/span&gt;&lt;/span&gt;Using the new data annotations interface IValidateObject&lt;/p&gt;&lt;p&gt;Validation attributes are the same attributes that the default validation strategy currently uses except you now have access to the model and ValidationResult has been tweaked to allow broken rules to be bound to multiple fields e.g.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PasswordsMustMatchAttribute&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ValidationAttribute&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;ValidationResult&lt;/span&gt; &lt;span class="nf"&gt;IsValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ValidationContext&lt;/span&gt; &lt;span class="n"&gt;validationContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;validationContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ObjectInstance&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Password&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PasswordConfirm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;ValidationResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ValidationResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"Password and Password Confirmation must match"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Password"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"PasswordConfirm"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;IValidateObject interface is the new way to provide validation of a Model object across all MS technologies (EntityFramework, DynamicData, Silverlight and ADO Data Services) and it behaves much better than the decoupled attribute stuff - at least in the preview.&lt;/p&gt;&lt;p&gt;If model implement the IValidateObject interface they are expected to implement the validate(ValidationContext context) method which returns an enumerable of ValidationResults.  The Person model in my sample app gives an example of this,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IValidatableObject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Nickname&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Password&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;PasswordConfirm&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;ValidationResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;ValidationContext&lt;/span&gt; &lt;span class="n"&gt;validationContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Nickname&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ValidationResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="s"&gt;"Name and Nickname cannot be the same"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Nickname"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Global Filters&lt;/h2&gt;&lt;p&gt;More of a convenience than anything, global filters allow you to apply the same Action Filters to EVERY controller action within an application.  Useful for all tha AOP style stuff like logging etc.  Taking the new KDF Action Logger we can log every action invocation by pushing the attribute type into the global filters via global.asax like so,&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="n"&gt;GlobalFilters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;LoggingFilter&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;Thats it really.  Nice and convenient.&lt;/p&gt;&lt;h2&gt;Json Model Binding&lt;/h2&gt;&lt;p&gt;Typically model bindng is done through creating an new instance of the model and attempting to bind up properties against the names of the values submitted by the client.  This doesn't really work in the Ajax world because the body of a POST request is generally a single unnamed parameter which represents a JSON encoded string of the client request data.  In MVC2 we had to write custom binders to parse the string and bind to a model but in MVC3 this is provided automatically.  Again another nice to have.&lt;/p&gt;&lt;h2&gt;Other stuff&lt;/h2&gt;&lt;p style="" class="MsoListParagraph"&gt;&lt;span style="font-family: Symbol;"&gt;&lt;span style=""&gt;·&lt;span style="font: 7.0pt Times New Roman;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Dynamic ViewModel  - Simply a dynamic bag allowing us to use ViewModel.SomeProperty vs ViewData["SomeProperty"]&lt;/p&gt;&lt;p style="" class="MsoListParagraph"&gt;&lt;span style="font-family: Symbol;"&gt;&lt;span style=""&gt;·&lt;span style="font: 7.0pt Times New Roman;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;New hooks for IoC/Dependency Injection through the lifecycle&lt;/p&gt;&lt;p style="" class="MsoListParagraph"&gt;&lt;span style="font-family: Symbol;"&gt;&lt;span style=""&gt;·&lt;span style="font: 7.0pt Times New Roman;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;New ways to expose Client Validation and Model Metadata which "should" allow an easier way to create a single point of validation that can be used across both client and server&lt;/p&gt;&lt;p style="" class="MsoListParagraph"&gt;&lt;span style="font-family: Symbol;"&gt;&lt;span style=""&gt;·&lt;span style="font: 7.0pt Times New Roman;"&gt;         &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;New view results - HttpStatusCodeResult and Permanent Redirects&lt;/p&gt;&lt;h2&gt;Summing up&lt;/h2&gt;&lt;p&gt;MVC3 Preview 1 is a solid enough release but ultimately it seems to be a foundation, albeit a solid one.  Given the previous release schedule I expect another release within the next few months.  What can we expect for future releases?  See &lt;a href="http://aspnet.codeplex.com/wikipage?title=Road%20Map&amp;amp;referringTitle=MVC"&gt;here&lt;/a&gt;.&lt;/p&gt;</content></entry><entry><title>CSS Hacks for IE - Unnecessary</title><updated>Wed Jan 13 00:00:00 UTC 2010</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2010/01/13/css-hacks-for-ie-unnecessary/"></link><id>urn:yobriefca-se:feed:post:CSS Hacks for IE - Unnecessary</id><content type="html">
&lt;p&gt;There are lots of CSS tricks floating aroud to accomodate IE quirks and more specifically in most cases IE6 the problem is these hacks - which is exactly what they are - invalidate your CSS and break future-proofing (what if a another browser learns to understand this hack with different rendering effects?). These things are parsed by every browser that comes across them - the browser then has to attempt to parse it, fail and then carry on - not exactly the most performant styling mechanism! They are useful and often quite nice but aren't exactly self explanatory making them a maintenance nightmare if someone doesn;t understand that order can be very important in these cases.&lt;/p&gt;&lt;p&gt;So - Why hack when a system already exists?&lt;/p&gt;&lt;p&gt;Hello &lt;a href="http://www.quirksmode.org/css/condcom.html"&gt;Conditional Comments&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Conditional Comments are a feature implemented in IE (though other browsers COULD implement them if they wanted to but none have). It's a specially marked up HTML comment node (so you include it in your HTML not CSS) that is only parsed by IE. A few examples are below....&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--[if IE 6]&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;IE specific Styling&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;![endif]--&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!--[if IE 6]&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;IE6 Styling&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;![endif]--&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!--[if gt IE 6]&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;IE7 + Styling&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;![endif]--&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;So basically you have you all normal CSS link tag then you can include a conditional comment to cater for specifics to IE or some specific version or rnge of versions. OK you would still need hacks for the other browsers but NIWater only had to use IE6 conditional comments (no hacks for other browsers) and looks fine in Chrome/Safari/FF - plus it means you CSS is valid and IE specific CSS is only pulled down by IE slightly reducing page weight (very slightly most likely!!!).&lt;/p&gt;</content></entry><entry><title>.less - Dynamic CSS for .NET</title><updated>Mon Nov 23 00:00:00 UTC 2009</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2009/11/23/dot-less-dynamic-css-for-net/"></link><id>urn:yobriefca-se:feed:post:.less - Dynamic CSS for .NET</id><content type="html">
&lt;p&gt;Link: &lt;a href="http://www.dotlesscss.com/"&gt;http://www.dotlesscss.com/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;There is a &lt;a href="http://lesscss.org/"&gt;Ruby library (LESS)&lt;/a&gt; that gets a fair bit of buzz in Ruby circles.  It's purpose is to introduce the &lt;a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself"&gt;DRY principle&lt;/a&gt; into CSS but also adds a lot of other useful features.  Well now there is a &lt;a href="http://www.dotlesscss.com/"&gt;.NET port&lt;/a&gt; of it - so what can it do (ripped from the original page as this pretty much sums it up)?&lt;/p&gt;&lt;h2&gt;Variables&lt;/h2&gt;&lt;p&gt;Variables allow you to specify widely used values in a single place, and then re-use them throughout the style sheet, making global changes as easy as changing one line of code.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;@brand_color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;#4D926F&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;#header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;brand_color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;brand_color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;And variables are a god-send in CSS world though other people disagree (&lt;a href="http://www.w3.org/People/Bos/CSS-variables" rel="nofollow"&gt;http://www.w3.org/People/Bos/CSS-variables&lt;/a&gt;) - but at least this is still generating plain old CSS for consumption&lt;/p&gt;&lt;h2&gt;Mixins&lt;/h2&gt;&lt;p&gt;Mixins allow you to embed all the properties of a class into another class by simply including the class name as one of its properties. It's just like variables, but for whole classes.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nc"&gt;.rounded_corners&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;moz&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;border&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;webkit&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;border&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;border&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rounded_corners&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#footer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rounded_corners&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Operations&lt;/h2&gt;&lt;p&gt;Are some elements in your style sheet proportional to other elements? Operations let you add, subtract, divide and multiply property values and colors, giving you the power to do create complex relationships between properties.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;@the-border&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;1px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;@base-color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;#111&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;#header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;color&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;border-left&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;the&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;border&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;border-right&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;the&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;border&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;#footer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;base&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;color&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;#111&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Operations are "OK" i guess but I am not sold on them.&lt;/p&gt;&lt;h2&gt;Nesting&lt;/h2&gt;&lt;p&gt;Rather than constructing long selector names to specify inheritance, in Less you can simply nest selectors inside other selectors. This makes inheritance clear and style sheets shorter.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nf"&gt;#header&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;color&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
       &lt;span class="k"&gt;font-weight&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="k"&gt;text-decoration&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;I like the nesting as it moves the structure closer to the nexted DOM tree format that you are actually trying to map against&lt;/p&gt;&lt;h2&gt;Compression &amp;amp; Caching&lt;/h2&gt;&lt;p&gt;Now you can configure if you enable Caching and minifying or the CSS output.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dotless&lt;/span&gt; 
    &lt;span class="na"&gt;minifyCss=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt; 
    &lt;span class="na"&gt;cacheEnabled=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It's still in beta and I haven't had a chance to properly tinker with it but it's a great idea as CSS bloat is something that is often overlooked and can make rebranding a nightmare if not carefully managed.  I might post more on this when I get a chance to have proper play with it.&lt;/p&gt;</content></entry><entry><title>Lazy Function Definition Pattern</title><updated>Sun Oct 18 23:00:00 UTC 2009</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2009/10/18/lazy-function-definition-pattern/"></link><id>urn:yobriefca-se:feed:post:Lazy Function Definition Pattern</id><content type="html">
&lt;p&gt;Have a look at this..&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;scripts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;scripts&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="nx"&gt;sid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Fairly simple function that loads the contents of a specific script tag (based on id) so that it can be used as a template (for something). &lt;span&gt; &lt;/span&gt;The reasons why you would do this is not important but the implementation of this function is. Every time you call this function you are re-selecting all the tag elements on the page which is just plain wrong. A quick fix for this would be to cache the results after the initial call and use the cache.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="nx"&gt;sid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This would allow the the costly getElementsByTagName to be called once and the results reused. This however starts polluting the global namespace with unnecessary variables plus even if the getTemplate function is never used you still have the cost of one call to getElementsByTagName which is pretty poor. You can get around these issues fairly easily by extending the actual function object itself&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="nx"&gt;sid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The benefits of this approach are that you aren't polluting the global namespace and you are only making the call to getElementsByTagName ONLY when it is needed but it does still have an issue. The cache should only be available to the getTemplate functions internal logic but it is currently exposed allowing other functions to remove or update it.&lt;/p&gt;&lt;p&gt;Introducing the Lazy Function Definition Pattern. Thanks to the dynamic nature of JavaScript functions can actually redefine themselves at runtime and this allows us to provide private, lazy, one time processing without polluting the global namespace. Lets see it in action...&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"script"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
 
  &lt;span class="nx"&gt;getTemplate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;script&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="nx"&gt;sid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;script&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHtml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;    
  &lt;span class="p"&gt;}&lt;/span&gt;
 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;getTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;The big change here is the middle statement. getTemplate is actually redefining itself. So what happens here?&lt;/p&gt;&lt;ol&gt;&lt;li&gt;On the first call getElementsByTagName is called and stored in the cache variable&lt;/li&gt;&lt;li&gt;The function then redefines itself as simple function that simply loops through the already defined cache&lt;/li&gt;&lt;li&gt;The function then calls itself (the new self!)&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Thanks to the creation of a &lt;a href="http://www.jibbering.com/faq/faq_notes/closures.html"&gt;closure&lt;/a&gt; the redefined function actually has access to the variables of the old definition - cache. This means that cache is private to the new function, only called once and not part of the global namespace. Exactly what I wanted. Programatically as well it's a better solution than the ones above because there are no checks to see if cache is defined saving a few cycles each call.&lt;/p&gt;</content></entry><entry><title>JavaScript Workshop</title><updated>Thu Oct 01 00:00:00 UTC 2009</updated><author><name>James Hughes</name></author><link href="/presentations/javascript-workshop.pdf"></link><id>urn:yobriefca-se:feed:post:JavaScript Workshop</id><content type="html"></content></entry><entry><title>LINQPad: Don't let the name fool you</title><updated>Tue Aug 25 23:00:00 UTC 2009</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2009/08/25/linqpad-dont-let-the-name-fool-you/"></link><id>urn:yobriefca-se:feed:post:LINQPad: Don't let the name fool you</id><content type="html">&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.linqpad.net/"&gt;LINQPad Website&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I've been playing with this little app for a few days now and it's exactly what I was looking for. For the last few years I was knee deep in the crazy world of Java and in my never ending attempt to be different I researched Groovy quite a bit. One of the things that came with the installation of Groovy was a console that let me write off the cuff Groovy or Java statements without having to fire up an editor and stick all that public static void main(string[] args) boilerplate around my code, compile it and run it. I used this console a lot when debugging little bits of code, testing theories, seeing a dump of an object etc and it sped up my development no end. Since getting involved in the .NET world any time I've wanted to write small test apps I've had to either create a new Unit Test or new Console Application and it's a lot slower to do achieve the same results.&lt;/p&gt;&lt;p&gt;This is where LINQPad comes in. LINQPad lets you write C# (and VB) statements, expressions, programs etc and evaluates them as they are without having to write a lot of boilerplate. If your code returns an Object it can be dumped in a very neat fashion into the output window along with the generated IL and some other stuff. LINQPad, as the name suggests also lets you hook up to SQL Server, the Entity Framework, Attachable Databases etc and perform LINQ Queries, showing you the generated SQL as well.&lt;/p&gt;&lt;blockquote&gt;&lt;ul&gt;&lt;li&gt;LINQPad reports the execution time in the status bar, so you won't have to manually create a Stopwatch class for performance testing.&lt;/li&gt;&lt;li&gt;Want to test a variation of your snippet? Ctrl+Shift+C instantly clones your snippet so you can run another version side-by-side.&lt;/li&gt;&lt;li&gt;You can return to saved queries in single click, thanks to the My Queries treeview. Some people are using LINQPad as a scripting tool!&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;p&gt;The licensed version gives you full Intellisense for your code plus 2 books worth (C# in a Nutshell and LINQ in Action) of working samples. Anyways here are a few screenshots of it in action. Like I said the name nearly made me skip over it but luckily I decided to try it and found what I was after!&lt;/p&gt;&lt;p&gt;[[posterous-content:HEtroelnwcgbGuFkaJnG]]&lt;/p&gt;</content></entry><entry><title>JavaScript Method Overloading</title><updated>Mon May 18 23:00:00 UTC 2009</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2009/05/18/javascript-method-overloading/"></link><id>urn:yobriefca-se:feed:post:JavaScript Method Overloading</id><content type="html">
&lt;p&gt;I was recently asked a question if method overloading in JavaScript was good practise, or even possible, so I thought I'd spread share my thoughts on this to anyone who bothers to read my ramblings. JavaScript doesn't offer method overloading directly but it does offer features that can be used to facilitate overloading.&lt;span&gt; &lt;/span&gt; Imagine these C# method signatures,&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;// Joins 2 strings&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;+&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="c1"&gt;// Joins an array of strings&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- 'ab'&lt;/span&gt;
&lt;span class="n"&gt;Join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- 'ab'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So depending on the arguments passed to the function the correct method will be called. Now this style of overloading isn't possible in JavaScript.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="c1"&gt;// Joins 2 strings&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="c1"&gt;// Joins an array of strings&lt;/span&gt;
&lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- 'ab'&lt;/span&gt;
&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- ERROR! (join is not a function of string)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As I have said JavaScript has no notion of method overloading so all that happens here is that the join function get redefined with the last occurrence of the function definition. However thanks to JavaScript's typeless nature and optional arguments overloading can be achieved (or at least simulated depending on your viewpoint).&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
 
&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- 'ab'&lt;/span&gt;
&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- 'ab'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Fixed! OK so that may seem simple enough and in this case it is but there are 2 things you need to consider before actually putting this into practise.&lt;/p&gt;&lt;h3&gt;Signature Detection&lt;/h3&gt;&lt;p&gt;Obviously the first thing you need to do when overloading is detect the intention of the user - what method signature are they using. This is the biggest issue you'll probably come across, especially when dealing with arguments of type&lt;code&gt;function&lt;/code&gt; or &lt;code&gt;array&lt;/code&gt;. &lt;a href="http://thinkweb2.com/projects/prototype/category/isarray/"&gt;These&lt;/a&gt; &lt;a href="http://bytes.com/groups/javascript/746441-isfunction-code-worth-recommending-project"&gt;posts&lt;/a&gt; explain the various issues with type detection and functions/arrays but to summarise&lt;/p&gt;&lt;ul&gt;&lt;li&gt;typeof returns "object" for Arrays&lt;/li&gt;&lt;li&gt;An array created in a frame/iframe does not share the same prototype as another Array in the parent document or another frame/iframe which make instanceof, typeof and [].constructor fail&lt;/li&gt;&lt;li&gt;Attempting to detect features such as methods (splice, join) and properties (length) can fail if the a non-array has matching methods/properties&lt;/li&gt;&lt;li&gt;Certain browsers report objects as functions making typeof == function fail&lt;/li&gt;&lt;li&gt;Host methods (browsers provided methods) sometimes do not report themselves as functions&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The list goes on! So you need to be aware of the target browsers quirks and work with them. Another way to detect signature is to use the &lt;code&gt;arguments&lt;/code&gt; psuedo-array but this is only useful in situations where your overloaded methods accepts a varying number of arguments e.g.&lt;/p&gt;&lt;p&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;&lt;p&gt;But then this is open to abuse by ignorant/stupid/malicious coders as it doesn't really imply how the function should behave unless commented well (yeah right!!).&lt;/p&gt;&lt;h3&gt;Code Bloat&lt;/h3&gt;&lt;p&gt;You need to be sure you aren't abusing this overloading strategy as it can lead to spaghetti code. Functions that attempt to do many things with similar signatures result in loads of argument testing and lot's of indented code. jQuery code is riddled with this due to the way almost every method acts as either a setter, bulk setter or getter method and it's run into bugs in the past due to this. Also the jQuery (or $) function is overloaded to do 5 different things so looking at the init function you'll see that it is a big if..elseif..elseif..elseif..else block which is pretty gruesome and breaks extensability and modularity of the code base.&lt;/p&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;p&gt;Overloading is possible in JavaScript but needs to be used with caution. Not only can it lead to very hard to find bugs due to type detection in different browsers but it can also make code harder to understand and extend, not to mention that amount of boilerplate code you need to write to detect the correct method signature etc.&lt;/p&gt;</content></entry><entry><title>UpdatePanels &amp; Alternatives</title><updated>Tue Apr 14 23:00:00 UTC 2009</updated><author><name>James Hughes</name></author><link href="https://yobriefca.se/blog/2009/04/14/updatepanels-and-alternatives/"></link><id>urn:yobriefca-se:feed:post:UpdatePanels &amp; Alternatives</id><content type="html">
&lt;p&gt;&lt;/p&gt;&lt;p&gt;I'm not sold on ASP.NET UpdatePanels. Conceptually they are quite nice but their implementation is nasty. So much so I'd be happy to say the should be considered an anti-pattern. My reasons? Well...&lt;/p&gt;&lt;ul&gt;&lt;li&gt;They generate too much JavaScript&lt;/li&gt;&lt;li&gt;It's too "development focused" - it puts saving time ahead of a clean performant solution&lt;/li&gt;&lt;li&gt;Ajax calls should be data focused (returning XML, JSON etc), UpdatePanels are content focused&lt;/li&gt;&lt;li&gt;Generates a massive amount of network traffic in comparison to alternative solutions (including ViewState and markup)&lt;/li&gt;&lt;li&gt;UpdatePanels use innerHTML to replace contents which orphans bound events (leakage) that need to be rebound each time (extra work).&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;But what are the alternatives? Page Methods and Scripted Services are a good starting point. The good things about these alternatives is that they are lightweight - they send JSON data to and from the server so your network traffic is greatly reduced. Here is a simple example of using a page method - I'll not cover Scripted Services as they are very similar and you can find info about them on the net.&lt;/p&gt;&lt;p&gt;In your code behind you can add a [WebMethod] attributed method&lt;/p&gt;&lt;div&gt;&lt;span style="font-size: small;"&gt;&lt;span&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Web.Services&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 
&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;WebformControls&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;partial&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PageMethods&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Web&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UI&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;        
&lt;span class="na"&gt;        [WebMethod]&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetCurrentDateTime&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;And turn on EnablePageMethods attribute of the pages ScriptManager&lt;/p&gt;&lt;div&gt;&lt;span style="font-size: small;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/1999/xhtml"&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form1"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;asp:ScriptManager&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt; &lt;span class="na"&gt;EnablePageMethods=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/asp:ScriptManager&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;What happens next is that when the page is rendered a JavaScript Proxy Class will be generated called PageMethods that contains a function call GetCurrentDateAndTime. It'll look something like this.&lt;/p&gt;&lt;div&gt;&lt;span style="font-size: small;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;PageMethods&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;PageMethods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;initializeBase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_userContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_succeeded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;PageMethods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;_get_path&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get_path&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;PageMethods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_staticInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get_path&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nx"&gt;GetCurrentDateTime&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;succeededCallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;failedCallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;/// &lt;/span&gt;
        &lt;span class="c1"&gt;/// &lt;/span&gt;
        &lt;span class="c1"&gt;/// &lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_get_path&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s1"&gt;'GetCurrentDateTime'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="nx"&gt;succeededCallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;failedCallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;PageMethods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;set_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/PageMethods.aspx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;PageMethods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GetCurrentDateTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onFailed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;/// &lt;/span&gt;
    &lt;span class="c1"&gt;/// &lt;/span&gt;
    &lt;span class="c1"&gt;/// &lt;/span&gt;
    &lt;span class="nx"&gt;PageMethods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_staticInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GetCurrentDateTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;onSuccess&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onFailed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;userContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Now all we have to do is use it. This is easy...&lt;/p&gt;&lt;div&gt;&lt;span style="font-size: small;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;xmlns=&lt;/span&gt;&lt;span class="s"&gt;"http://www.w3.org/1999/xhtml"&lt;/span&gt; &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getDate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;PageMethods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;GetCurrentDateTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"results"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;})&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form1"&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;asp:ScriptManager&lt;/span&gt; &lt;span class="na"&gt;runat=&lt;/span&gt;&lt;span class="s"&gt;"server"&lt;/span&gt; &lt;span class="na"&gt;EnablePageMethods=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/asp:ScriptManager&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"#"&lt;/span&gt; &lt;span class="na"&gt;onclick=&lt;/span&gt;&lt;span class="s"&gt;"getDate()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Get Date!&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"results"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;That wasn't too difficult now was it? But we aren't done yet. Sure this solution works but there is still one problem - We are including the ScriptManager in our page which means event though all we are doing is getting a date back from the server we are still required to pull down at least 160k (in it's most compressed &amp;amp; gzipped form) of framework 90% of which we aren't even going to use.&lt;/p&gt;&lt;h3&gt;Calling PageMethods and ScriptedServices using jQuery&lt;/h3&gt;&lt;p&gt;Obviously a request from client to server is always done through HTTP it doesn't actually matter how it is handled on the client. So it's easy to swap out the ASP.NET Ajax framework for jQuery. You'll probably be asking why? Well the long and short of it is that regardless of what the requirements say you (or someone else on the project) WILL end up writing Javascript and jQuery is the better choice...&lt;/p&gt;&lt;ul&gt;&lt;li&gt;It's a better, smaller, compact and faster framework&lt;/li&gt;&lt;li&gt;It's much better documented and easier to learn&lt;/li&gt;&lt;li&gt;It's extensible via plugins so you don't need the whole "kitchen sink" to do simple things&lt;/li&gt;&lt;li&gt;It can do everything ASP.NET Ajax can do&lt;/li&gt;&lt;li&gt;It's technology agnostic so you can jump to a Java project and use the same solutions&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I could go on, honestly I could, but that's not what this post is about. There are a few nuances in calling PageMethods and ScriptedServices, namely&lt;/p&gt;&lt;ul&gt;&lt;li&gt;The request content-type must be &lt;code&gt;application/json; charset=utf-8&lt;/code&gt;&lt;/li&gt;&lt;li&gt;The request must be a POST request&lt;/li&gt;&lt;li&gt;Data sent to the server must be encoded as a JSON string or "{}" if empty&lt;/li&gt;&lt;li&gt;Standard ASP.NET JSON serialization doesn't convert DateTime to a proper JSON date&lt;/li&gt;&lt;li&gt;The JSON data in the request must map directly to arguments of the function&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Failure to comply with any of the above will result in either a security error or, usually, the entire page being rendered back to the response. So calling the above PageMethod using jQuery is as simple as this....&lt;/p&gt;&lt;div&gt;&lt;span style="font-size: small;"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ajax&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"PageMethods.aspx/GetCurrentDateTime"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;contentType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/json; charset=utf-8"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;dataType&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"json"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;success&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'#result'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Not bad, but there is a lot of boiler plate code there. Thankfully it's easy to write a jQuery plugin to abstract out most of this and make the calls simple. So all in all even with the 2 plugins (JSON and .Net Services) and the jQuery framework we are only forced to pull down 60k of scripts (minified but NOT gzipped) so you could see this reduced to 25-30k all in with gzipping turned on.&lt;/p&gt;</content></entry></feed>