Microsoft recently released their Roslyn Community Technology Preview. The Roslyn project gives developers access to the Visual Basic and C# compiler information through APIs.
As described by Microsoft, “the Roslyn CTP previews the next generation of language object models for code generation, analysis, and refactoring, and the upcoming support for scripting and interactive use of VB and C#.” While that statement may seem pretty innocuous, this project is actually a very big deal. This will give developers greater ways to dynamically analyze code, refactor and generate code on the fly.
Let’s say I have a Lead Receiver Service that accepts leads from a client. My Lead Receiver may accept SOAP or Restful data transfers from a client. The Receiver will process the Lead, store the Lead in the data store and send notifications that a Lead was received. When the service fails and logs exceptions, I will have to analyze the source of the exception and fix it. With tools like the Roslyn project, I will be able to write a Lead Receiver Monitor that can monitor the Lead Receiver, receive error notifications, analyze the failures and fix application errors when they occur. The Lead Monitor can change the structure of the application, recompile it with the code fix and publish the change. We will be one step closer to having self-healing application systems. As Neo eloquently put it in the Matrix trilogy; it will be Programs hacking Programs.
I have a product that I sell with options that need to be dynamically generated based on its current configuration. For example, I am a car dealer with certain models having only certain colors available. Certain packages will only apply if I have a 4 X 4 instead of a sedan or a compact. I can have the options dynamically configured at runtime by writing a C# macro to populate the list of options based on the code in the macro.
Let’s implement this scenario to see how it will look.
I created an ASP.NET/MVC3 Web Project and added references to the Roslyn CTP libraries. I added the following libraries:
View Model
Our Clickpoint Motors Data model will simply have a Car that will have the following fields:
OptionsMacroText : This will hold the code that we will use to generate the Available Options List Dynamically.
We will use an interface with a method signature as our contract for generating the vehicle options. Our interface will have one method that takes in a Car Model parameter named ‘car’ and returns a Generic List containing strings.
Here are the steps we will perform when generating options
I create my new car configurations and select my Vehicle Model, Color, Sports Package and Enter the code into my options macro. By convention, my GenerateOptions method in my Macro class will have a local variable named options, which is of type List<string>.
After creating the Car Configurations, we view the car inventory to see what we have available in our Showroom. When we select “Show Options” on the Inventory Page, we will dynamically generate the options available based on the car’s macro code.
If we look at the ShowOptions action in our CarController class, we can take a closer look at how the options are dynamically generated.
In our ShowOptions controller we do the following:
We implement the source code generation and compilation in our GenerateOptionsMacroFactory
Class. The GenerateOptionsMacroFactory class is where we do all the heavy lifting of creating the Source code, compiling it and then creating an instance of the Macro class from the compiled assembly.
Here is the interface signature.
And the Implementation
Once the instance of the IGenerateOptionsMacro object is created, all we have to do is call the GenerateOptions method and it will spit out our list. We use the Roslyn.Compilers.CSharp library to compile our source code into an assembly. Then we use the assembly to generate an instance of our GenerateOptionsMacro class. Once we have an instance of the class, we can easily generate the options list. All done dynamically at runtime!
So, we just showed how we could dynamically generate code using the Microsoft Roslyn CTP.
I’ve put up the source code for this demonstration on GitHub:
https://github.com/kmariano/ClickpointRoslynCTPDemo