LightSwitch and T4 code generation

If you find yourself writing ‘similar’ and ‘repetitive’ code, you might want to look into custom code generation.

Code generator tools generate their code based on some kind of meta-data.  LightSwitch is partly a code-generator tool itself: what you do as a developer in the Screen Designer, Query Designer or Entity Designer, is stored in LightSwitch Markup Language (lsml files, the meta-data that describes your application), and used to generate code for parts of your application at compile time, and to interpret other parts at compile time.

Because your LightSwitch application already has this meta-data-driven approach, it’s extremely easy to do additional code generation yourself!

I started by downloading some tools, Tangible T4 editor came highly recommended for code coloring and “File>New Item” templates.  After installation and VS reboot, I added a new file and from the Tangible section in the Add New File dialog, selected “Advanced” template.

This generates a sample tt file (the T4 template) which interprets a piece of xml to generate a .cs file (any file is supported).  In the tt file, just swap out the hardcoded xml to load the lsml files you need (the path to your lsml files might be different!):

	XNamespace  schema = "http://schemas.microsoft.com/LightSwitch/2010/xaml/model";
	string serviceLsml = System.IO.File.ReadAllText(
		System.IO.Path.GetDirectoryName(this.Host.TemplateFile) + "\\..\\TESS.DataWarehouse\\TESS.DataWarehouse.Server\\Properties\\Service.lsml");
	System.Xml.Linq.XDocument XmlDoc = System.Xml.Linq.XDocument.Parse(serviceLsml);

Then just use your XDocument wizardry to extract the information you need…

		foreach(var entityContainer in XmlDoc.Root.Descendants (schema + "EntityContainer"))
        {
			#>
				// Data source: <#= entityContainer.Attribute("Name").Value  #>
				<#

Practical example: although it was tempting to try to generate a WinForms application based on the client lsml, we found it more rewarding to use this in our reporting solution instead.  We have this one LightSwitch application which exposes a couple of views (BI sans BS).  Reports are ran from the server as well, using the ServerApplicationContext as a data source.  However, when an end-user wants to design a new, or alter an existing ‘report definition’, he/she can’t “access” the ServerApplicationContext in the end-user report designer.  As a workaround, we use T4 to generate a stand-alone class library with a simplified layout of our LightSwitch ServerApplicationContext, but which spits out random dummy data when accessed. This class library is then distributed alongside with an end-user report designer, so that they can design the reports.  When a report is uploaded, we simply swap out the ‘Dummy’ data source for one that accesses the ServerApplicationContext instead.  (Mega-Props to Paul Van Bladel for switching on the light here ;-) )

Here’s a fragment of the full T4 template, more likely to be used as an example than anything else.  As you can see, it generates a partial class, so that I can have some non-generated methods as well (like: CreateDummyData< T >).

The amount of LightSwitch and T4 resources online right now is … Well, just this post… Two: this post, and Paul Van Bladel’s material.  If you ever decide to wander on this lucrative T4+LightSwitch combination, please do share your LightSwitch love so that I and others can learn.

Keep rocking LS!

Jan

About these ads

7 thoughts on “LightSwitch and T4 code generation

  1. Pingback: Microsoft Fakes Framework - The Daily Six Pack: January 7, 2014

  2. Pingback: LightSwitch Community & Content Rollup- December 2013 - Beth Massi - Sharing the goodness - Site Home - MSDN Blogs

  3. Pingback: LightSwitch 社区和内容汇总-2013年12月份 - Beth Massi的中文博客 - Site Home - MSDN Blogs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s