Very amusing - “.. it runs 10x slower than PHP, but you get an architecture you can blog about”

Postcodes

I was talking to Phil (the creator of carspy.co) a while ago, and suggesting that he should look at using geolocation on his site to work out where you are and thus find cars near you.

Once you have the lat lon, I said, it shouldn’t be too hard to get the UK postcode for that location, and then pre-fill the user’s location in. Well, as it turns out there are a few services that will give you the lat lon for a given postcode, but none that will take a lat lon and try to guess your nearest postcode for you. Well, none that I could find anyway (for free).

So, that was my excuse for writing a simple app in node.js to do this for you :) The idea behind it is a simple API that you can pass a lat,lon, and you get back a postcode. The postcodes are all available from http://data.gov.uk/dataset/os-code-point-open, albeit without Northern Ireland and a few other places.

I’m not far from finishing this, so I’ll post about it when I get it done. I’m going to be using Node.JS and MongoDb to store the postcodes and associated lat/lon.

tumblrbot said: WHAT IS YOUR FAVORITE INANIMATE OBJECT?

My Mac of course! Currently an iMac, but one day I hope to have a mac book air, just as soon as I can convince my wife how important it is to get one :)

String.IsNullOrWhiteSpace

I came accross String.IsNullOrWhiteSpace recently, and thought I’d pass this on, it’s one of those simple methods that once you notice you find yourself using more and more. I won’t tell you what it does, I think the clue is in the name ;)

On the subject of strings, I also found this extension method which I find myself using a lot:

        
/// 
/// replacement for String.Format
/// 
public static string With(this string format, params object[] args)
{
    return string.Format(format, args);
}


It means I can write code like

 var myString = "Hello there {0} and welcome to {1}".With(User.Name,Site.Name);


Which I find a whole lot easier to manage than ugly error-prone string concatenation and slightly more convenient than using String.Format. I came across that on Suteki Shop when I was browsing the source. I’m finding I use that a lot!

Simple way to unit test your WCF duplex services (and avoid that dependency on OperationContext)

I’m building a set of secure WCF services to communicate with a bunch of client machines. Because the communication is duplex, you need to use a Callback in the service method:

OperationContext.Current.GetCallbackChannel<IClientCallBack>()

But the problem with this is that if you write any unit tests against this method, it will fail because there is no OperationContext! So what do you do?

Well, there are a few blogs out there with various suggestions, and there’s WCFMock which I did initially use and allowed me to mock out the OperationContext, but what I couldn’t suss out with WCFMock was how to specify my own call back handler for my tests - so that I could record when the callback was being called by the service.

In the end, I came up with a solution that works well for me. Basically, I create a constructor overload for my service that accepts a Func<IClientCallBack> - I am abstracting out the code that instantiates a callback to this function. 

Now, when running inside IIS as normal, WCF isn’t going to call my constuctor, so I just modify my default constructor so that it uses OperationContext ONLY if nothing else has been set. Here’s the code:

    public class ScannerManagerService : IScannerManager
    {
        private readonly Func<IClientCallBack> _createCallbackChannel;        

        public ScannerManagerService() : this(null)
        {
        }

        /// 
        /// If you pass in a callBack, messages will be sent to that. Otherwise, as is the case in IIS, null will be passed in which will result
        /// in the OperationContext being used.
        /// 
        /// 
        public ScannerManagerService(Func<IClientCallBack> callBack)
        {
            if (callBack != null)
            {
                this._createCallbackChannel = callBack;
            }
            else
            {
                this._createCallbackChannel = () => OperationContext.Current.GetCallbackChannel<IClientCallBack>();
            }
        }

        private void DoSomeWork()
        {
            var callBack = _createCallbackChannel(); 
            ...
        }

And here’s how I use that service in my unit tests:

        [SetUp]
        public void Setup()
        {
            _scannerManager = new ScannerManagerService(() => new SimpleClientCallBack());
        }

SimpleClientCallBack is just a simple implementation of IClientCallBack which allows me to test the call backs, it looks like this:

    public class SimpleClientCallBack : IClientCallBack
    {
        public static int messageCount;
        public void SendClientMessage(ScannerMessageWrapper message)
        {
            messageCount++;
        }
    }

And that’s that. It’s pretty simple, and does the trick for me. 

Hope it helps someone else out there…

Manage your database with Visual Studio’s “Database Project”

It was with some trepidation that I set about looking into Visual Studio’s Database projects, but it looks like an awesome tool that you really should be aware of if you do a lot of work with SQL Server databases in your project..

The background

We needed some way to track changes to our database for our startup. The database was evolving all the time, and while all the code was being tracked in svn, the database was just being hacked at, with nothing more than a daily backup. We needed something (a tool/process) that we could use to:

  • Get the database into source control so we could track schema changes
  • "Deploy" the database - generate a "new install" script to create an instance of the database
  • Generate a “test database” - something we can use to fire databasey tests against, where we know the state of the database and the data in it.

Enter “Database Project”

So, in Visual Studio 2010 (and in 2008 “Database Edition”), you now have the option of creating a database project. You can then import an existing schema into your database project, which creates sort of mini-scripts for all the objects that make up your database. You can “deploy” the database from here, which depending on how you’ve configured it will either install the database for you, or will just generate a SQL script for the schema install.

Once you’ve imported a schema, you can then make changes by editing the files corresponding to your change - so if you want to change a table, you edit the MyTable.table.sql file, make your change, save, and “deploy”. One clever thing is that you can target a specific database instance for the deployment, and if that database already exists, then only the changes required to bring it upto-date with the latest schema changes will be generated.

It gets much much better

Ok so I can keep track of schema changes, generate new install scripts that will deploy my database, all good stuff. But the really useful stuff is yet to come (and you can only do this in the Ultimate or Premium version btw)

Schema Comparisons

You can compare 2 database schemas, or a database schema with your database project, to see if they are synced. If they are not synced, you can generate a script to bring the “target” in line with the “source”. That’s pretty awesome. Using this, we can for example easily make sure that our production instances are upto-date, and if not quickly bring them upto-date without losing the data on them. You could also use this for example at the end of a release cycle, to generate a migrate script, which you could then tweak and include with your release for existing clients.

Data Comparisons

As the name suggests, you can also perform data comparisons between 2 database, to see what is different about the contents of the 2 databases. You can then choose which of the tables you want to generate an update script for - really useful stuff. Even better is the fact that the scripts generated maintain the actual primary key identity value, rather than just generating new id’s.

So, In Summary..

I like it. A lot. I’m going to be using this to track database changes in svn, generate test databases with test data pre-loaded, and to automate the deployment process so that the target database gets updated automatically.

If you want to find out more and get started, then I found the best information to be the collection of walkthroughs available on MSDNs site, starting from here: http://msdn.microsoft.com/en-us/library/aa833292.aspx

So, this is my new blog.. for now.

Quick “hello world” post to test out my new blog. I’ve moved from my old blog (www.geekswithblogs.net/mattrobertsblog), it’s feeling a bit out-dated on there, and I wanted to be somewhere more hip ;)

Hopefully I’ll be stepping up the game a bit and blogging about some of the interesting things I’m working on at the moment.

The idea is that I’m going to eventually roll out my own blogging engine, just to say I’ve done it, but that’s not going to happen anytime soon, so for now tumbl looks like a nice place to be. 

TTFN