I’ve had another chance to play around with Silverlight 2 over the last two days. The result: a new version of my utility to convert numbers to words. I’ve now added the ability to convert to and from Roman Numerals, making it substantially more useful – if you’re a Roman citizen caught in a time warp. Go try it.
I love the way Silverlight lets you run standard C# in the browser – generics, LINQ and all. The code for the conversion algorithms comes straight from two of my Project Euler posts, Converting Numbers to Words and Converting to and from Roman Numerals. From my limited experiment, the Model-View-View Model pattern also appears to work pretty much as it does in WPF, though data binding in Silverlight is not so sophisticated. For example, there is no UpdateSourceTrigger mode in Silverlight, so when you want a TextBox to update its data source every time the text changes you have to resort to a hack (switching focus away from the TextBox, then back again).
Unit Testing Silverlight
I’ve become quite a fan of unit testing over the last 9 months or so of my latest project, and I wanted to make sure we’ll be able to carry on the practice in Silverlight. Turns out it can be done, but choices are more limited than on the full CLR – mostly due to Silverlight only running in the browser (unless you are devious). There’s SilverUnit, but that’s built on top of Typemock Isolator which commercial users have to pay for. Or there’s Silverlight Unit Testing framework from Microsoft, which you can get free as part of the Silverlight Toolkit on Codeplex. I went with the free option.
Creating tests with Silverlight Unit Testing Framework is straightforward - you just decorate your classes and test methods with attributes, just as with other Unit Testing frameworks. At the moment though, options for running the tests are limited: you pretty much have to use the provided method, that runs through all the tests in a browser window. My colleague is currently trying out a technique using a powershell script to automate Internet Explorer so that we can include the Unit Tests on our Continuous Integration Server; also check out Odin, a promising looking project on CodePlex that aims to get the tests running from Resharper.
Oh, and one last thing. Did you know that Microsoft provide free hosting for Silverlight applications – 10 whole Gigabytes? And with Office Live you can get a website hosted for you, but using your own domain name – again for free. That’s how I’m hosting the utility.
Get the Code
I’ve put all the code for this little utility on MSDN Code Gallery for your hacking pleasure.