Avoiding magic strings in LightSwitch (part 2): Getting strongly typed access to the LightSwitch choice lists

Introduction

In the entity designer you can for a field specify a choice list. This will generate automatically a dropdown list when the entity is consumed in a screen.

That’s cool, but we are missing one feature: strongly typed access to the different choice lists values.

We sometimes refer to choice lists values in code, e.g.:

 if (this.PurchaseRequests.SelectedItem.State == "Rejected")
            {
                //apply some logic   
            }

The value “Rejected” is what is commonly called a “Magic String”. What happens when tomorrow someone updates our choice list and changes the “Rejected” magic string to “Refused”?

Well,… unfortunately nothing happens !  The above code will still compile but the underlying logic will fail. That’s not what we want.

Solution 1 : create yourself a class with choice lists constants

We could simply write following classes:

namespace LightSwitchApplication
{
	public class CodeValues
	{
		public class PurchaseRequest
		{
			public class State
			{
				public const string Approved = "Approved";
				public const string Rejected = "Rejected";
				public const string ReadyForValidation = "Ready For Validation";
			}
		}
	}
}

That would allow us to change our sample code block as follows:

 if (this.PurchaseRequests.SelectedItem.State == CodeValues.PurchaseRequest.State.Rejected)
            {
                //apply some logic   
            }

Ok, cleaner, but it’s not solving our problem. What we want is that when an underlying code value changes, we get a compilation error.

Furthermore with the above classes, we need to update our choice list values in two places.

So, we are very much interested in the above CodeValues class, but we want that it is automatically generated. That would mean we would need access to these choice lists values. That’s possible: they are in the Service.lsml file.

Solution 2: use T4 for generating the above CodeValues class based on information from the lightswitch model file

In case you don’t know what T4 is, go to http://t4toolbox.codeplex.com/

T4 allows you to generate code based on a template file. The trick here is of course to incorporate the information from the LightSwitch model file in the processing. This can be done with XDocument.

I’ll not further elaborate on how this T4 is constructed, because that’s a pretty technical story. Bottom line is that the above CodeValue class will be automatically generated based on the information in the LightSwitch model file.

The magic is in a nuget package

Try it out and inject the new Nuget Package in the server project of a LightSwitch V3 project:

 

choice

 

The nuget package will inject following 2 files into your project:

stronglytyped

When you right-click on CodeValue.tt you can start the generation of the CodeValue class based on the model file:

rightclick

Currently I have no solution to get it automatically in the silverlight client project. But it only takes a few seconds to add CodeValues.generated.cs as an existing item in the Silverlight client project. Add it as a link. By doing so, it’s automatically updated from the server file.

Conclusion

T4 opens a completely different world, also for LightSwitch. Drop me a line if you see other use cases than the one I presented here.