- Posted by liammclennan on July 30, 2010
I am writing this post in response to David Burela’s ‘What is your preferred technology stack?’ meme.
As an independent developer I generally have a lot of flexibility to chose the tools that I think will deliver the best result, with the restriction that my customer is generally looking for a .NET based solution. I favour tools which:
- are open source. Open source libraries are often designed to facilitate quality practices, not to sell tools. Also, access to the source and the price are bonuses.
- support the software craftsmanship ideology. Small, sharp tools that stay out of the way and don’t force any design decisions.
Here is my current preferred technology stack, from the outside in:
- jQuery + lots of plugins on the client. I hope to introduce CoffeeScript on a project soon.
- I don’t have a firm opinion on view engines. nHaml is great in theory, but poor in execution. Spark is also fantastic, but I might switch razor in the future if I like it.
- Asp.NET MVC. Leaving webforms behind was the best technology decision I have ever made.
- Structure Map for IoC. I don’t have a strong opinion on this. I continue to use Structure Map because it is familiar.
- ELMAH for logging exceptions.
- Moq for mocking. AutoMockContainer for auto mocking.
- StoryQ for BDD style testing.
- NHibernate + FluentNHibernate for data access. The more I use these tools that more I favour explicit mapping over auto mapping. I don’t like NHibernate but at the moment it seems to be the best of a bad bunch.
- A custom framework for constructing objects for use in testing.
- SVN for version control. I have used mecurial for some projects but svn is familiar and does everything that I need.
- Pivotal Tracker, Green Hopper or Unfuddle for project management
- Rake + albacore for build scripting. I have a screencast on this that will hopefully one day see the light of day.
- Teamcity for continuous integration.
- Posted by liammclennan on July 13, 2010
I spent some time this afternoon testing lenses, just to see what my cheap DSLR lenses are capable of. I also wanted to know if my 50mm 1.8 prime lens produced sharper images than my 18-55mm kit lens.
My test subject was a book on a shelf. The cover features small writing with sharply defined edges. From where I was sitting the writing was too small to read, and my eyesight is ok, as far as I know. I shot one image with each lens both using the following settings: 1.3” ISO 400, f7.1.
Here are the results at full size:

The first is the 50mm prime and the second is the kit zoom.
At this size there is very little difference. For some reason the prime lens produced an image with higher contrast and more saturation.
The following screenshot shows both images at 100% ie one pixel on the screen for each pixel in the photo. Click to view full size.
The first thing to note is that they are both incredibly detailed. Remember that from where the camera was I could not even read the small text. At this magnification it is clear that the $150 prime lens is significantly sharper that the kit zoom. Of course, both are extremely sharp and the difference is inconsequential to an amateur photographer like myself.
While the prime lens does create sharper images I rarely use it because of its practical disadvantages. It is fixed at 50mm (obviously) so composition is more challenging, and at large apertures it is difficult to focus due to the extremely limited depth-of-field.
- Posted by liammclennan on June 28, 2010
I read somewhere that Kent Beck likes to apply a strategy of taking a technique that works and exaggerating it, to see if the results are likewise exaggerated. He found that specifying the behaviour of components prior to implementation, and in small increments, produced good design, so he exaggerated it and now we have TDD. He found that strong engineering practices, rich communication and delayed decision making was a good way to run projects, so he exaggerated it and now we have Extreme Programming.
In homage to Kent Beck and his work I decided to apply his technique to web frameworks, taking the things that I like and exaggerating them. Here are some of the things that I value in a web framework:
- a rich language
- simplicity
- minimalism
- expressiveness
- opinionatedness
Using the above guidelines I sketched out an idea for a server-side framework for CoffeeScript. It is heavily inspired by Sinatra, with a few minor tweaks and coverted to CoffeeScript. Here is what a controller might look like:
# The first parameter is the route. Like sinatra, there is no route abstraction.
# The second parameter is a method to execute when the route is matched.
# Enforced one-model-in. No need for params[]
get '/', (input_model) ->
"Hello World"
# Views are found based on the name of the model passed to the view method.
# ? here is the CoffeeScript existential operator.
get 'address/:slug', (address_request) ->
if address_request?
@address = address_finder.find_by_slug address_request.slug
view @address
# View model objects are automatically given bind_to() and update()
# methods for binding to and from domain objects.
post 'address/create', (address_model) ->
@address: address_model.update new Address()
# do something with @address
@address_list = address_finder.all()
view @address_list
As promised the implementation described has some strong opinions: one-model-in, binding methods on view models and no route abstraction.
One-Model-In
One-model-in is a rule that no action may have more than one parameter. All request values (querystring, form, route) are mapped onto the input view model.
Binding Methods on View Models
The input view model objects are automatically given bind_to() and update() methods. By extending the base view model class developers can create more sophisticated view models.
view_model_object.bind_to(domain_object)
Bind to copies the attributes of the input domain object to the view model object.
view_model_object.update(domain_object)
Update copies the attributes of the view model object to the domain model object.
No Route Abstraction
Most MVC web frameworks have an abstraction for routes, meaning that a url is mapped to a route and the route is mapped to a controller action.
Url <—> Route <—> Action <—>
Sinatra simplifies by omitting the route abstraction, and I have too. The advantage is simplicity, the disadvantage is that refactoring an application’s URL scheme might be more difficult.
- Posted by liammclennan on June 23, 2010
Here are the original mockups for KeyRef


and the implemented screens look like (apologies for the ugliness):


both the application search, and the shortcut search, are working.
I tweaked the application shortcuts page url, so now the url for visual studio shortcuts is:
http://localhost:3000/app/show/4c19e8a5cfbfb01794000001/Visual%20Studio-shortcuts
Also, I have a page for entering new shortcuts that looks like:

Now I can start adding shortcuts, which will be a big job. I’d like to get it online so that people can add shortcuts for their favourite applications.
- Posted by liammclennan on June 22, 2010
My rule of thumb for storing configuration settings is:
- if it does not need to change hard code it
- if it needs to change but the user of the application can’t change it, store it in the configuration file
- if the user can change it, store it in a database
This post discusses configuration data stored in the configuration file. For the examples that follow the configuration data might look like:
<appSettings>
<add key="ForumURLRoot" value="http://someurl" />
<add key="EmailSendingRetries" value="3" />
</appSettings>
To make my applications robust and testable I like to write a service that wraps appSettings. What I mean by robust is that it provides useful error data in the event that the application depends upon configuration data that is missing. My configuration service throws the following exception if an appSetting is missing:
A nice clear error message helps me to rapidly diagnose the problem.
This service is something that I have written many times. To be honest, there is often some copy and paste involved. Here is what the most recent incarnation looks like:
public interface IConfigService
{
string ForumURLRoot();
int EmailSendingRetries();
}
public class ConfigService : IConfigService
{
// an example configuration element
public string ForumURLRoot()
{
return ReadSetting("ForumURLRoot");
}
// an example with a cast
public int EmailSendingRetries()
{
return (int)ReadSetting("EmailSendingRetries");
}
private string ReadSetting(string key)
{
CheckForKey(key);
return ConfigurationManager.AppSettings[key];
}
private void CheckForKey(string appSettingKey)
{
if (ConfigurationManager.AppSettings[appSettingKey] == null
|| ConfigurationManager.AppSettings[appSettingKey].Equals(string.Empty))
{
throw new ConfigurationException("Missing appsetting: " + appSettingKey);
}
}
}
Providing a method for each configuration setting gives me:
- the ability to cast if the setting needs to be something other than string
- the ability to easily mock configuration settings for testing
Stubbing a setting, such as ForumURLRoot, is easily accomplished with Moq:
[Test]
public void ConfigMock()
{
var configMock = new Mock<IConfigService>();
configMock
.Expect(config => config.ForumURLRoot())
.Returns("http://google.com.au");
Assert.AreEqual("http://google.com.au", configMock.Object.ForumURLRoot());
}
I write this post to expose my ignorance. If I am doing to wrong, let me know.
- Posted by liammclennan on June 17, 2010
Just because I like to build things, and I like to learn, I have been working on a keyboard shortcut reference site. I am using this as an opportunity to improve my ruby and rails skills.
The first few days were frustrating. Perhaps the learning curve of all the fun new toys was a bit excessive. Finally tonight things have really started to come together. I still don’t understand the rails built-in testing support but I will get there.
Interesting Things I Learned Tonight
RubyMine IDE
Tonight I switched to RubyMine instead of my usual Notepad++. I suspect RubyMine is a powerful tool if you know how to use it – but I don’t. At the moment it gives me errors about some gems not being activated. This is another one of those things that I will get to. I have also noticed that the editor functions significantly differently to the editors I am used to. For example, in visual studio and notepad++ if you place the cursor at the start of a line and press left arrow the cursor is sent to the end of the previous line. In RubyMine nothing happens.

Haml
Haml is my favourite view engine. For my .NET work I have been using its non-union Mexican CLR equivalent – nHaml.
Multiple CSS Classes
To define a div with more than one css class haml lets you chain them together with a ‘.’, such as:
.span-6.search_result contents of the div go here

Indent Consistency
I also learnt tonight that both haml and nhaml complain if you are not consistent about indenting. As a consequence of the move from notepad++ to RubyMine my haml views ended up with some tab indenting and some space indenting. For the view to render all of the indents within a view must be consistent.
Sorting Arrays
I guessed that ruby would be able to sort an array alphabetically by a property of the elements so my first attempt was:
Application.all.sort {|app| app.name}
which does not work. You have to supply a comparer (much like .NET). The correct sort is:
Application.all.sort {|a,b| a.name.downcase <=> b.name.downcase}
MongoMapper Find by Id
Since document databases are just fancy key-value stores it is essential to be able to easily search for a document by its id. This functionality is so intrinsic that it seems that the MongoMapper author did not bother to document it. To search by id simply pass the id to the find method:
Application.find(‘4c19e8facfbfb01794000002’)
Rails And CoffeeScript
I am a big fan of CoffeeScript so integrating it into this application is high on my priorities. My first thought was to copy Dr Nic’s strategy. Unfortunately, I did not get past step 1. Install Node.js. I am doing my development on Windows and node is unix only. I looked around for a solution but eventually had to concede defeat… for now.
Quicksearch
The front page of the application I am building displays a list of applications.

When the user types in the search box I want to reduce the list of applications to match their search. A quick googlebing turned up quicksearch, a jquery plugin. You simply tell quicksearch where to get its input (the search textbox) and the list of items to filter (the divs containing the names of applications) and it just works. Here is the code:
$('#app_search').quicksearch('.search_result');
Summary
I have had a productive evening. The app now displays a list of applications, allows them to be sorted and links through to an application page when an application is selected. Next on the list is to display the set of keyboard shortcuts for an application.
- Posted by liammclennan on June 17, 2010
Visual studio supports relatively advanced string manipulation via the ‘Quick Replace’ dialog.
Today I had a requirement to modify some html, replacing line breaks with unordered list items. For example, I need to convert:
Infrastructure<br/>
Energy<br/>
Industrial development<br/>
Urban growth<br/>
Water<br/>
Food security<br/>
to:
<li>Infrastructure</li>
<li>Energy</li>
<li>Industrial development</li>
<li>Urban growth</li>
<li>Water</li>
<li>Food security</li>
This cannot be done with a simple search-and-replace but it can be done using the Quick Replace regular expression support. To use regular expressions expand ‘Find Options’, check ‘Use:’ and select ‘Regular Expressions’
Typically, Visual Studio regular expressions use a different syntax to every other regular expression engine. We need to use a capturing group to grab the text of each line so that it can be included in the replacement. The syntax for a capturing group is to replace the part of the expression to be captured with { and }. So my regular expression:
{.*}\<br/\>
means capture all the characters before <br/>. Note that < and > have to be escaped with \.
In the replacement expression we can use \1 to insert the previously captured text. If the search expression had a second capturing group then its text would be available in \2 and so on. Therefore, the required replacement expression is:
<li>\1</li>
Visual Studio’s quick replace feature can be scoped to a selection, the current document, all open documents or every document in the current solution.
- Posted by liammclennan on June 14, 2010
After more time than I care to admit I have finally released a rudimentary Http Handler for serving compiled CoffeeScript from Asp.Net applications.
It was a long and painful road but I am glad to finally have a usable strategy for client-side scripting in CoffeeScript.
Why CoffeeScript?
As Douglas Crockford discussed in detail, Javascript is a mixture of good and bad features.
The genius of CoffeeScript is to treat javascript in the browser as a virtual machine.
By compiling to javascript CoffeeScript gets a clean slate to re-implement syntax, taking the best of javascript and ruby and combining them into a beautiful scripting language. The only limitation is that CoffeeScript cannot do anything that javascript cannot do.
Here is an example from the CoffeeScript website. First, the coffeescript syntax:
reverse: (string) ->
string.split('').reverse().join ''
alert reverse '.eeffoC yrT'
and the javascript that it compiles to:
var reverse;
reverse = function(string) {
return string.split('').reverse().join('');
};
alert(reverse('.eeffoC yrT'));
Areas For Improvement ;)
The current implementation is deeply flawed, however, at this point I’m just glad it works. When the server receives a request for a coffeescript file the following things happen:
- The CoffeeScriptHandler is invoked
- If the script has previously been compiled then the compiled version is returned.
- Else it writes a script file containing the CoffeeScript compiler and the requested coffee script
- The process shells out to CScript.exe to to execute the script.
- The resulting javascript is sent back to the browser.
This outlandish process is necessary because I could not find a way to directly execute the coffeescript compiler from .NET. If anyone can help out with that I would appreciate it.
- Posted by liammclennan on June 12, 2010