Archive for category Software Development

Inversion of Control, Two Ways

One of the reasons I like to play with other languages from time to time is to find new ways of solving problems. This is why, while learning Ruby on Rails, I am making a conscious effort to do things the Ruby way, as opposed to writing C# style code in Ruby.

While working on my new blog page, I encountered a problem that was identical to one I had on a previous project but using ASP.Net MVC. In both projects (but for different reasons) I made a design decision to use Amazon’s S3 web service for storing images. However, I wanted the option of switching to an alternative local (file system based) implementation at configuration/deploy time.

The ASP.Net MVC way

The C# solution to this problem was obvious to me at the time, because I was already using an Inversion of Control container. I created two implementations of my image repository and set up the IoC container to use whatever implementation I wanted based on some configuration setting (note: In this extract I am also setting up local/remote message queue implementations):

if (bool.Parse(ConfigurationManager.AppSettings["RunLocal"]))
   _container.RegisterType<IBinaryRepository, Local.BinaryRepository>(
       new InjectionConstructor("F:/TadmapLocalData/LocalBinaryFolder")

   _container.RegisterType<IMessageQueue, Local.MessageQueue>(
      new InjectionConstructor("F:/TadmapLocalData/LocalImageMessageFolder")
   _container.RegisterType<IMessageQueue, Local.MessageQueue>(
      new InjectionConstructor("F:/TadmapLocalData/LocalCompleteMessageFolder")

   _container.RegisterType<FileUploaderAdapter, LocalUploadAdapter>();
   _container.RegisterType<IBinaryRepository, Amazon.BinaryRepository>(
      new InjectionConstructor(

   _container.RegisterType<IMessageQueue, Amazon.MessageQueue>(
      new InjectionConstructor(ConfigurationManager.AppSettings["CompleteMessageQueue"])
   _container.RegisterType<IMessageQueue, Amazon.MessageQueue>(
      new InjectionConstructor(ConfigurationManager.AppSettings["ImageMessageQueue"])

   _container.RegisterType<FileUploaderAdapter, DirectAmazonUploader>(
      new InjectionProperty("BucketName", ConfigurationManager.AppSettings["S3BucketName"]),
      new InjectionProperty("AccessKey", ConfigurationManager.AppSettings["S3AccessKey"]),
      new InjectionProperty("SecretKey", ConfigurationManager.AppSettings["S3SecretAccessKey"]),
      new InjectionProperty("FileAccess", com.flajaxian.FileAccess.Private)

The Ruby on Rails way?

While looking for an IoC container or equivalent Dependency Injection framework, I started to realise that although DI frameworks exist for Ruby they are normally not used. One of the articles I read suggested reopening the class and mixing in a module. So I gave this a try.

Most of my models are including a module to define the persistence implementation, for example my Post class is stored as as mongoDB document:

class Post
  include MongoMapper::Document
  # ...

So my first step was to move all image persistence code into two different modules, one for S3 storage and one for local file storage.

Then inside an initializer file, I check my configuration and based on this, I reopen the class and include the appropriate persistence module:

require 'image'

IMAGE_STORE_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/image_store.yml")[RAILS_ENV]


  class Image
    include S3ImageStore


  class Image
    include FileImageStore


This solution works for now, although I am not entirely happy with it. My problem is that my Image model now looks like the following:

class Image

   attr :name


This is very simple (which is good) and there are no details about it’s persistence (which is also good), but it doesn’t give the reader any clue that it can persist itself. For example, in C# I would have to have a placeholder (interface) for my implementation that would indicate to the reader that there is some stuff missing and this is what the missing stuff would look like if you want to go looking for it.

I am happy now that I can easily switch between cloud services and local implementations, but I can’t help thinking there is a better way of achieving this…

Tags: , , ,

A new blog and learning Ruby on Rails

My blog has been a Word Press application for a while now but two things have been bothering me.

  1. I spent ages playing with the application settings and server settings to try and get the URLs the way I wanted, they are still not what I want. Nothing complex, I justed wanted a little more control.
  2. There are thousands of plugins that do almost what I want, but none that do exactly what I want. Most are customizable but sometimes you end up having to hack some PHP.

As a web developer, I am used to having full control over my URLs from within the code and would like to tweak the behaviors using the tools and languages I am familiar with.

My original idea was to write a new blog application using ASP.Net MVC, but have since decided to use this as an opportunity to learn something new.

I’ve been spending a lot time recently learning a new language and framework, namely Ruby on Rails. At first it was just to see what all the hype was about but I eventually got hooked. Right now I am in the middle of developing my new blog/website using this fantastic framework.

I’ve already blogged about some of the new stuff I’m learning (Checking page titles with Cucumber) and I will probably have some more posts about it before it goes live.

Tags: , , ,

Checking page titles with Cucumber

As a way of learning a new framework I started to write my own blog application using Ruby on Rails. I’ve also taken the opportunity to try a bit of BDD using Cucumber, which I’m finding is a lot of fun. One feature that I have written recently and particularly like is:

Feature: Main Title
  In order to improve SEO and help readers know what they are looking at
  The reader or search engine spider will need the name of the page
  and the author's name in the title of all pages

    Given I have a post with title "Why I really like Ruby"

  Scenario Outline: Pages contain name and author in the title
    Given I am on the <Page>
    Then I should see "<Full Title>" within "title"
    | Page                          | Full Title                            |
    | home page                     | Home - Trevor Power                   |
    | about page                    | About - Trevor Power                  |
    | contact page                  | Contact - Trevor Power                |
    | blog page                     | Blog - Trevor Power                   |
    | "Why I really like Ruby" post | Why I really like Ruby - Trevor Power |

This test is very simple, it visits each page in the listed examples and checks that the title is as expected. For me it is important that this is tested automatically because as developer/tester of the site I never really pay attention to the text that appears in the window title. But this text is important for search engines as well as appearing in browser history and book marks.

Another thing I like about this test is the cross cutting nature of it. The titles of all pages from different parts of the website are listed together and any inconsistencies are made obvious.

I don’t have syntax highlighting on this blog for gherkin (the language used in the above spec) but here is a screen shot of the results which might be more readable:

Screen shot of the Cucmber results for the above feature

Tags: , , ,

Podcast List

I listen to mostly programming podcasts, namely:

Non programming related include:

Tags: , , ,

Displaying the workspace name in Visual Studio 2008

Switching between Visual Studio instances with different workspaces can be very frustrating as it is hard to tell which workspace solution is open. I was planning on writing a plug-in to display the workspace name in the title bar when I found this question on Stackoverflow by someone with a similar problem, Working with different versions/branches of the same Visual Studio 2005 solution.

One of the comments there suggested the use of ‘hard links’ to the solution file. I have given it a go and it has been working quite well for me for that last few days. Here’s how to do it.

A normal windows shortcut will not suffice, you need to create a symbolic or hard link to the solution file. In Windows 7 you can do this via the command line tool mklink. If you have a workspace called ‘Merge’ and a solution called ‘MyApp.sln’ the the following command will create a shortcut that you can use. (You will need to run this as an administrator).

C:\MyApp>mklink MyApp.Merge.sln MyApp.sln

Multiple Visual Studio Instances

Now use the new link to open you solution file. Then when you have multiple VS instances open and you click on the Visual Studio task bar icon, you will see the name of your new link (including workspace name or branch name). It also appears in the main title bar.

Have you come across any better solutions? Or easier ways to create links?

Update: I wasn’t sure at first what the difference would be between a hard link and a symbolic link but it turns out that using a hard link (a ‘/H’ switch on the above command) helps when you need to save changes to the solution file. But I suggest opening the solution file normally when you need to modify it as saving the solution from the hard link will insert its name into the file.

Tags: , ,

Sharing configuration settings in Visual Studio

Correctly managing your systems settings during both development and deployment can save you a lot of time. When ever you have to manually change settings there is the opportunity to mess up, so I try to automate any repetitive configuration tasks.

It often happens that application settings or connection strings are needed by more than one component in your system. The solution I use is a variation of this method of sharing app.config files between applications, except that it allows each project to have its own app.config or web.config files which reference the common settings.

Creating shared settings

The first step is to make a settings class that acts as a wrapper around the values in the configuration files. This is a very simple class inheriting ApplicationSettingsBase and looks like this:

using System.Configuration;

namespace TP.Example.Configuration
    public class Settings : ApplicationSettingsBase
        private static Settings _defaultInstance =
                (Settings)(ApplicationSettingsBase.Synchronized(new Settings()));

        public static Settings Default { get { return _defaultInstance; } }

        string ServiceName
            get { return ((string)(this["ServiceName"])); }
            set { this["ServiceName"] = value; }

        string EventLogName
            get { return ((string)(this["EventLogName"])); }
            set { this["EventLogName"] = value; }

        string ConnectionString
            get { return ((string)(this["ConnectionString"])); }
            set { this["ConnectionString"] = value; }

The next step is to create the files that contain the settings. I usually have two, one for application settings and one for connection strings. The application settings file consists of a single node matching the full name of my settings class:

  <setting name="ServiceName" serializeAs="String">
    <value>My Example Service
  <setting name="EventLogName" serializeAs="String">

The connection strings file consists of one ‘connectionStrings’ node. Note the name of the connection string is qualified with the full name of my settings class.

  <add name="TP.Example.Configuration.Settings.ConnectionString" connectionString="[...]" />

These files should exist outside any of your projects but be under source control. I usually put them in a solution folder ‘Configuration’ that contains a simple project with the above settings class:
'Configuration' solution folder

Referencing shared settings

FilesAsLinksThese common settings are now ready to be used by any project in the solution by following these steps:

  1. Add a reference to the settings project.
  2. Add a link to settings files and turn on ‘copy to output directory’
  3. Include settings files in the app.config/web.config

To link to the settings files you should Add -> Existing Item -> Add As Link, they will then appear with a shortcut icon. This means that they only point to the original file, so there is no duplication.

Now that the files are in our project we just need to get our App.config file to include them. A simplified App.config would look like this:

<?xml version="1.0" encoding="utf-8" ?>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup" >
      <section name="TP.Example.Configuration.Settings" type="System.Configuration.ClientSettingsSection" />
  <connectionStrings configSource="TP.Example.Configuration.ConnectionStrings.config" />
    <TP.Example.Configuration.Settings configSource="TP.Example.Configuration.AppSettings.config" />

Now when you modify one of these files the change will be picked up by all other projects in the solution.

Websites and Web.config

Using these settings in a website requires one extra step. As before, the settings file will be linked to by the project file and the ‘Copy to Output Directory’ turned on. The problem is that the web.config file is usually at the root while the output directory is ‘bin’ or ‘bin/Debug’ depending on your setup. Because the output folder can depend on the build configuration, I have found the best solution is to copy the config files from the target directory to the project directory. This is just one line in the build events.COPY $(TargetDir)*.config $(ProjectDir)
Writing it out like this sounds like a lot of work but it is easy to setup and easy to maintain. If you have any question or have a better solution, please leave a comment.

Tags: , , ,

Enumerations and global constants in SQL Server

Everybody knows that you shouldn’t put lots of business logic into stored procedures, right? Well maybe not, but whether or not you agree with this statement you’ll always find some cases where it just makes things easier or more efficient to have your business rules close to the data. Often business rules mean dealing with record types, flags and other magic numbers. These numbers normally correspond to enumerations or constants in our favourite high level language (for me C#).

So what do we do with all these magic numbers? Of course, we being good programmers, put them into well named variables that makes the code easier to read and the values easier to maintain. But what happens when we have multiple stored procedures using the same values?

I didn’t know the answer to this, so I put a question on Stack Overflow, What are the different ways of handling ‘Enumerations’ in SQL server? I got some good answers, but most did some mapping between VARCHARs and INTs. Hard coded strings are better than hard coded numbers because they are easier to read, but they have other problems such as hard to find typos.

One answer that wasn’t given, and hasn’t been suggested by any other developers I’ve talked to, is the use of scalar functions as constants. I stumbled on this solution today while refactoring some existing functions and ended up with a function that did little more than call another function with a hard coded int value. You can create a simple function that just returns a number:


This is quite a lot of code for just one constant but it is available to all stored procedures. Maybe they could be generated automatically?

As for performance, I haven’t been able to write a test with any big difference in execution time, but I’m sure there must be some hit. Regardless it is bound to perform better than most of the other answers I got for the initial question.

One problem I have found is that you cannot pass them directly to other stored procedures, you need to introduce an intermediate variable, a bit annoying but not a show stopper.

Maybe you’ve being doing this all the time? If there’s something I’m missing, please leave a comment.


Using the IIS SEO Toolkit

Was at a great talk by Scott Guthrie today in Dublin. One thing he talked about near the end was the new IIS SEO Toolkit, this I was able to try as soon as I came home.

It was very easy to install and can be pointed at any website (not just your own). To install:

  • If you haven’t already, get the Web Platform Installer.
  • Select and install the ‘Search Engine Optimization Toolkit’.

Then, to examine a site:

  • Start ‘Internet Information Services (IIS) Manager’.
  • Go to Search Engine Optimization -> Site Analysis.
  • Point it at any website.

Starting a Site Analysis of my blog
It then gives you a list of of rule violations, 405 for my blog. The details of  each violation include a description of the rule with recommended action.
SEO Toolkit results for my website
As you can see, rule violations include broken links, missing description text and non-relevant link text (as in click here). Most of these rules are obvious but they are hard to spot without looking at the generated HTML of the page.

Tags: , ,

Software Craftsmanship

Most developers I’ve worked with would have come to me at some point and asked questions like:

  • What is the best way to implement this new feature?
  • What do you think of this design, is it good enough?

This to me indicates that software developers in general do take pride in their work and try to achieve the first part of the Manifesto for Software Craftsmanship:

Not only working software, but also well-crafted software

Given that most developers are competent enough to come up with good designs and implement them well with nice clean code and generally have the desire to do so, why is this rarely reflected in the resulting code?

The reason, I believe, is related to the second line from the same manifesto:

Not only responding to change, but also steadily adding value

The key word here being change. In contrast to my first observation, many developers when working with existing code will try and squeeze in their fix/feature and get out as quickly as possible. If they do come to me to talk about that change it’s just to grumble and complain about how hard it is to work with that code or how buggy it is.

The next time someone grumbles about some code they are working on I will remind them (or myself) that because they are now working on that code it is now their responsibility, it is up to them to make it better. If you’re not happy with the code you are working with, think about how the existing code could be changed to make your fix/feature easier to add.

If you’re not making the code base better, you are making it worse!

Tags: ,

A limitation of shared hosting and a push toward cloud computing

Today I ran into one of the limitations of shared hosting.

I’ve been struggling for the last few weeks to handle large files in my application. I published to the server earlier this week but with no success. All my tests failed after 10 minutes with a ‘Request timed out’ error. I was testing with an 80mb file but with a slow broadband connection (thanks to Ice). I had increased the timeout and max request length for the upload handler like so in the web.config:

   <location path="Upload.mvc/Upload">
         <httpRuntime  maxRequestLength="2097151" executionTimeout="10800" />

I ruled out any setting that I had control over, so the next step was to talk to my hoster, Blacknight, to see if they could help me out. They replied saying that they could not support the uploading of large files stating that:

It ties up too much system resources to upload huge files like that through asp and would cause problems for other users on the shared server.

Of course they’re right and I can’t really complain but it’s bitterly disappointing none the less.

For the foreseeable future, a couple of file uploads a month is all I require so I don’t want to pay for more expensive hosting packages. So back to the drawing board…

Cloud computing

I’m already using the Amazons S3 service for the storage of the images and I think I can upload directly from the client to S3. So I just need a high spec PC for a few minutes to process the files after the user has uploaded them. Once processed I will only ever have to deal with smaller versions or tiled sections. This looks like an ideal candidate for the Amazon Elastic Compute Cloud (EC2). Where I can pay by the hour for a high spec machine.

I was hoping to dip my toe into some cloud computing at some point but I didn’t want to be pushed. This is going to be a lot more work than I hoped and will need some effort just to cordinate between the web server, client machine and a cloud computer.

I haven’t decided on the final architecture yet, just thinking out loud, but looks like a lot of fun (and hard work) ahead…

Tags: , , , , ,