Do you twig?–Full source code
Published on Friday, April 1, 2011 11:47:00 AM UTC in Programming
No, this is not an April Fools' joke :). As announced in the post where I explained how I created and optimized the dynamic trees of my Eco Contest 2011 entry "Do you twig?", I'm releasing the full source code of the project here. The entry was chosen for the first runner-up award by the judges, which I was and still am very happy about. In the following, I explain some details of the application. You can find the source code at the end of the post.
General application structure
The whole application is not overly complex and only consists of a hand full of elements. The general design follows the MVVM pattern, which means individual elements on the screen are grouped and encapsulated as views, each with a corresponding view model that implements some logic and data transformation.
The tree rendering is somewhat different, as various performance optimizations made it necessary to let go of best design principles and some of the concepts you'd use in a normal application. This doesn't mean the code is a mess; it's still cleanly structured, and I've added a lot of inline comments to explain what's happening and why some decisions have been made. If you want to learn more about the tree rendering and how it works in detail, my previous post on it has some good explanations in it too.
Of course, just like with any other application, I'm sure a lot of things can be made differently and better, and I'm also sure that when I look back at the code in a few years I will shake my head in disbelief over some details :).
The data source
At first, the application used RIA Services to retrieve the quiz data from an SQL Server database. When I looked into the submission process though and realized that I would have to host the service indefinitely myself, I decided to create a self-contained version instead. Because the quiz data does not dynamically change once it is created, this was a practicable solution.
The repository design for the data part of the application helped a lot with this. I simply added a second repository implementation that uses embedded XML files instead of a web service to retrieve the data. With SQL Server's XML support extracting the data from the database as XML was a matter of writing a simple SQL statement. Combined with Silverlight's LINQ to XML capabilities implementing this whole alternate data source took less than two hours, which once again proved the amazing flexibility and power of Silverlight to me.
To switch between the two implementations, you can pass a certain init parameter to the Silverlight application (the included hosting page contains a comment on this).
Source code and license
The package you can download here is the unaltered archive that I sent in for the contest, with the exception that I have added a text file which contains the license information. The source code is published using the Microsoft Public License (Ms-PL). Important key points of the license are that you are free to take the code, use it for your own projects if you like and publish derivative work without any restrictions, also for commercial purposes. But I also give no warranty or guarantee whatsoever, the package comes "as-is". If you come home from work and realize my source code has eaten your cat it is not my problem.
Have fun with it, and feel free to leave any comments and/or provide feedback.