Share on FacebookI wrote a blog post about using Subsonic to speed up Silverlight 2 projects. This is important in Silverlight 2 because of it’s limited (make that non-existent) support for working with already existing business objects. Thankfully, this problem will evaporate in SLV3 – or at least that’s my understanding.
In the mean time, I’ve written a lot of code to help me get my data securely from server to client and back again. To reduce duplication of code, I used Subsonic project and some custom templates to generate my Silverlight business objects (ok, property bags), my Silverlight Xaml and Xaml code behind, and also to generate my serverside BO/Property bags.
This blog post is written to provide details on how I incorporated my customized templates into the Subsonic project that I have. And no, sorry, this isn’t the latest version of Subsonic. And Oops. I don’t know if these procedures will need modified for the latest Subsonic. Probably. Anyway, here’s a link to the finished Subsonic project, 17 mb download, just so you can see how the code went together.
There’s a web app that comes with Subsonic, called SubSonicCentral. Here is where you can play with the code gen features of SubSonic, and this is where I placed my controlling code to be able to generate SL templates. The page, Generators.aspx, has a new usercontrol tag:

<%@ Register src="inc/SilverlightXAMLGenerator.ascx" TagName="XAMLGenerator" TagPrefix="uc3" %>
<uc3:XAMLGenerator ID="SilverlightXAMLGenerator1" runat="server" />
SilverlightXAMLGenerator.ascx is where the entry point is for this code. It started as a clone of ClassGenerator.ascx which is a SubSonic file, really the only differences are in the identity of the templates used and the code gen type.
The key function is the click event handler for btnGo.
protected void btnGo_Click(object sender, EventArgs e)
I have no clue where to start to explain this. So all I’ll add here are the names of items added to this project to support one new template. You can download the SubSonic project above and use search to find the specific spots.
I added to the project:
- A template file for each language supported (not all finished). These files go in folder: SubSonic\CodeGeneration\Templates. CS_SilverlightXAML.CodeBehind.aspx and VB_SilverlightXAML.CodeBehind.aspx
- FileExtension.XAML: this is a constant which identifies the output filename
- CodeService.BuildSilverlightCodeBehindTemplate(item.Value, className, language, provider);
- TemplateType.SilverlightCodeBehind
- this was modified to add the new template: private static string GetTemplateText(TemplateType t, ICodeLanguage language)