Searching Public Domain UK Postcodes

Tuesday, 28 July 2009

Download Released:
26th July, 2009

Postcodes divide the United Kingdom into logical groups for addresses, averaging 15 properties per postcode. Postcodes have rough latitude and longitude coordinates associated with them, which makes them useful for location aware applications such as tracking and social networking. Unfortunatley the Postcode Address File (PAF) is owned by the Royal Mail, who charge for access and generate revenues of £14.9m every year.

Postbox Postbox by asplosh.

Enter the new service Ernest Marples (named after the postmaster general who invented the postcode) that aims to overcome this restriction by offering the public a postcode API free of charge. Operating in a legal grey area, they don't host or cache the information (it is derived from undisclosed sources) but offer a public API to access it.

Of course, alternatives do exist. Numerous services offer public domain postcodes - postcodes that have been submitted by members of the public using GPS devices. The drawback of these public domain services is that they are largely incomplete, and the accuracy of each postcode is not guaranteed to be correct.

Free The Postcode

Free the postcode operate a site that allows users to submit postcodes and their corresponding coordinates, as well as download the results collected so far (7557 at the time of writing). If you feel like adding the database, it is made even easier with the introduction of an iPhone application.

New Popular Edition Maps

Based upon out of copyright Ordnance Survey maps, New Popular Edition Maps provide a web based interface to view the UK as it was 50 years ago. In addition to this, it is also possible to submit the location of your postcode through the web interface. This information is then available to download (43,887 postcodes at the time of writing) and use for your own purposes, as long as your not making a profit from them (see the licence)

Locating Postboxes

Ever found yourself needing to post a letter but not know where the nearest postbox is? This is a question that Matthew Somerville's Locating Postboxes service aims to answer. Using data derived from a freedom of information request, the service can look-up and find your nearest postbox. A full list of all the available information (20,076 complete and partial postcodes so far) is available to download.

Accessing Public Domain Postcodes

If you want to integrate postcode information into your .NET application, retrieving and parsing the information can be a pain. Because of this I've written an open source postcode library that supports all the main providers and provides a simple interface for your to query. The library also includes handy functions to validate and calculate the distance between two postcodes.

namespace Flipbit.Postcodes
{
    /// <summary>
    /// Postcode Library Demo Program
    /// </summary>
    public class PostcodeDemo
    {
        /// <summary>
        /// Gets or sets the ernest marples service.
        /// </summary>
        public IPostcodeService ErnestMarplesService { get; set; }

        /// <summary>
        /// Gets or sets the free the postcode service.
        /// </summary>
        public IPostcodeService FreeThePostcodeService { get; set; }

        static void Main()
        {
            var buckinghamPalace = FreeThePostcodeService.Lookup("SW1A 1AA");
            var downingStreet = ErnestMarplesService.Lookup("SW1 2AA");

            var distance = new Distance(buckinghamPalace.Coordinates, 
                                        downingStreet.Coordinates);

            Console.WriteLine("Buckingham Palace to 10 Downing St:");
            Console.WriteLine("{0} miles", distance.Miles);            
        }
    }
}

You can see an online demo of the library, or download the source here.

Writing iPhone Sites with ASP.NET MVC

Wednesday, 22 July 2009

Update: Scott Hanselman has an elegant solution supporting multiple devices by creating a new mobile view engine.

Download Released:
22nd July, 2009

Whilst working on a recent project I noticed that I was getting some traffic from Apple iPhones and iPod Touches. The site used some pretty complex jQuery for user interaction - this didn't translate into a workable user experience for mobile users.

The iPhone displaying a site with regular HTML

Writing a Mobile Safari site is made easy with some of ASP.NET MVC's Aspect Orientated Programming (AOP) features. These allow you to address cross-cutting concerns with the use of filters. A filter can be attached to a controller or controller action, and run before or after the action is executed.

Processing iPhone Requests

To get a website to display a Mobile Safari version of the HTML for the iPhone, you can simply write a filter to switch between the standard HTML and the mobile version. Using the filter is as simple as decorating the controller with the MobileFilter attribute:

using System.Web.Mvc;
using Flipbit.Web.Filters;

namespace Flipbit.Web.Controllers
{
  [MobileFilter]
  public class HomeController : Controller
  {
    /// <summary>
    /// Shows the homepage.
    /// </summary>
    public ViewResult Index()
    {
      return View();
    }
  }
}

The MobileFilter executes after the action has executed, and simply decides whether to show the mobile version of the site or not.

/// <summary>
/// Called when the controller action is executed.
/// </summary>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
  // Only process when request is from mobile Safari
  if (!IsMobileSafari(filterContext.RequestContext.HttpContext)) return;

  // Only process Views
  if (filterContext.Result.GetType() != typeof (ViewResult)) return;

  // Get the name of the view
  var viewName = GetViewName(filterContext);

  var mobileViewName = viewName + ".iPhone";

  // Ensure that a mobile view has been defined
  if (!ViewExists(mobileViewName, filterContext)) return;

  // Assign the new mobile view
  var result = (ViewResult)filterContext.Result;
  result.ViewName = mobileViewName;
}

Detecting a Request from an iPhone or iPod

The first thing the filter needs to do is determine if the user is accessing the site through an iPhone or iPod touch. We can do this by examining the UserAgent string in the incoming HTTP request. If the device is an iPhone, the UserAgent will look like this:

Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C28 Safari/419.3

Meanwhile, the iPod touch looks like this:

Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3

In order to detect this, you can simply examine the UserAgent for an instance of the text "iPod" or "iPhone". This should provide some protection for changes to the UserAgent over time due to upgrades etc...

/// <summary>
/// Determines whether this request was made from a mobile safari device
/// </summary>
/// <param name="context">The context.</param>
/// <returns>
/// <c>true</c> if request is from mobile safari; otherwise, <c>false</c>.
/// </returns>
public bool IsMobileSafari(HttpContextBase context)
{
  var isMobileSafari = false;

  if (context != null)
  {
    var userAgent = context.Request.UserAgent;

    if (!string.IsNullOrEmpty(userAgent))
    {
      var ipodIndex = userAgent.IndexOf("iPod");
      var iphoneIndex = userAgent.IndexOf("iPhone");

      if (iphoneIndex + ipodIndex > -1) isMobileSafari = true;
    }
  }

  return isMobileSafari;
}

Dynamically Switching the View

If the request is from an iPhone or iPod touch, we need to use the HTML for Mobile Safari. This is stored along side the regular view, however has ".iPhone" appended to the view file name. This allows us to maintain two separate versions of the HTML and keep the same controller logic.

/// <summary>
/// Views the exists.
/// </summary>
/// <param name="viewName">Name of the view.</param>
/// <param name="filterContext">The filter context.</param>
private bool ViewExists(string viewName, ActionExecutedContext filterContext)
{
  // get the controller name
  var controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;

  // get the path to the view & map to IIS location
  var path = string.Format("~/Views/{0}/{1}.aspx", controller, viewName);
  path = filterContext.RequestContext.HttpContext.Server.MapPath(path);

  // see if the view exists
  return File.Exists(path);
}

The filter checks to see if the mobile view exists. If it does not, then the normal view is used by default, allowing the site to gracefully degrade if the mobile view is missing for some reason.

Mobile views along side regular ones in Visual Studio

Writing and Testing Mobile Safari HTML

With the filter in place it's time to start writing some HTML for Mobile Safari. There are numerous sites to help get you started and test the result without having to do a full deploy and test using the iPhone.

The iPhone displaying a site with Mobile Safari HTML

Once you've finished, you should have a dynamically switching, mobile compatible version of your site. A sample of this code is available to download.

Visual Studio Dark Theme

Tuesday, 21 July 2009

I have never been much of a fan of changing the color schemes in Visual studio, however whilst working on a Ruby on Rails site for a month or so, I came to enjoy using the e editor's cappuccino scheme.

Loosely based upon Textmate's Vibrant Ink scheme, I found it hard to adjust to the dark background at first, however after a few days I'd find it hard to go back.

Using Oren Ellenbogen's dark theme as a base, I ported the cappuccino theme to Visual Studio 2008:

Visual Studio Dark Theme

I'd recommend trying a dark theme for a few days if you haven't before. It's easier on your eyes if your programming in the evening, especially if you set up a light behind your monitor.

You can download the colour scheme here.

Announcing SEO grep

Monday, 20 July 2009

When working on public facing websites for clients, they often ask me to optimize their site for search engines. Most developers I’ve worked with will just hold up their hands at this point and do one of the following:

  • Get a dedicated SEO consultant in.
  • Start expressing the virtue of meta keywords and description tags.
  • Submit the website to lots of online directories using automated tools.

Whilst the only sure-fire way of getting a complete SEO strategy is to hire an SEO professional, there are a few simple rules that you can apply to your site in order to avoid some common pitfalls.

I listed these metrics for on-site SEO and automated the process that I would perform manually through a free online service, SEO grep.

SEO grep logo

The site analyses your website using a web-crawler and calculates a score out of 100 for each page. The name comes from the Unix command to search, grep. More truthfully, this is also a reflection on the sparsity of good, short and meaningful .com domain names!

The report produced by SEO grep includes metrics from on-page SEO (HTML structure), site structure (folder layout, page parameters) and site-wide metrics on your domain (domain age, server location).

SEO grep is currently limited to analysing the first 25 pages on your site – this is due to bandwidth and server restrictions I currently have. I’m working to increase this cap, and adding 20+ more metrics to the results.

You can try SEO grep here and leave feedback on the UserVoice account.

Anemic Domain Models vs the Single Responsibility Principle

Thursday, 16 July 2009

One discussion I keep hearing come up again and again in the blogosphere, as well as in the pub, is that of the anemic domain model as an anti-pattern.

Whilst I can see the benefits of having a "full-fat" domain model, it seems to go against the Single Responsibility Principle (SRP). This is wonderfully illustrated by Robert Martin on the Hanselminutes podcast, about 3 minutes in. In this podcast Martin argues against having an object having multiple reason to change.

A rich domain model

In this example, the employee class knows too much about it's construction and persistence. This can be easily addressed using standard Domain Driven Design (DDD) Factory and Repository patterns:

A domain driven design inspired domain model

However, we're still left with the employee object having multiple responsibilities, and multiple reasons to change. Some argue that this is a good thing, however I think that particular example is more an illustration of the Liskov Substitution Principle.

Following Robert Martin's example to its logical conclusion, we end up with an service-oriented architecture (SoA) approach, and an Anemic Domain Model.

An anemic domain model

So which approach would I use? A rich domain model or an anemic one, with an SoA and SRP? In my opinion it is easier to write a loosely coupled, testable, anemic domain with an SoA, than it is to write a full interactive domain. By using the SoA approach, you can essentially defer the question, "when should I split this functionality into another class?" - it is answered for you by the SRP.

With a greater level of skill required to write a good rich domain model, I tend to go for the anemic option when possible - it is easier to point to the SRP than it is to count on good OO design acumen. Of course, this depends on the project your working on and the people your working with.

Introducing Hudson Monitor

Wednesday, 15 July 2009

The Hudson Continuous Integration server has been my continuous integration platform of choice for about a year now, mainly because of it’s ease of use and configurability.

Whilst working on a project for a client last year, I needed to monitor the status of the build whenever someone checked in. There are numerous ways of achieving this already, however we wanted a separate PC running next to the team with a full-screen display letting everyone know the build status.

Accessing Hudson with .NET

To achieve this task I wrote a .NET wrapper for the Hudson Remote API. This RESTful API enables a program to query the status and history of the builds running on the Hudson server. An ASP.NET MVC application then takes this information and displays it on a webpage that can be run full-screen in a browser.

The Hudson Web Monitor running fullscreen

It is much easier to quickly see the status of the build on the 40 inch display rather than through the default web interface or a tray icon!

The monitor displays information such as build number, subversion revision and the check-in comments. If an image with the same name as the user checking in the code exists on the server, their avatar is displayed. This obfuscates who did the build to the outside world.

We found that displaying the avatar of the last user also was a strong encouragement for developers to check that the build wasn’t broken before checking in their own code.

Downloading the Monitors and Source Code

I’ve recently revisited the code and re-written it from scratch, adding more robust unit tests and support for a WPF monitor running in the system tray or Windows 7 Superbar. All the code is open sourced on Google code.

Screen shots and downloads are available here.