Skip to main content

Roslyn shopping cart DSL - Part 1


Lately, a lot of people where asking on mailing lists and users groups, how to dynamically load and execute C# code. This code maybe be stored as expressions in the database or will be entered by the user in some sort editor or whatever they want to use, but the important thing is that the app should compile and execute the code at runtime. Unlike Ruby or Python, C# does not support this kind of stuff right out of the box. Fortunately, the Redmond guys had implemented a nice library called Roslyn, which allows us to do this and a lot of other crazy stuff.

Whereas this was possible in the past by using reflection or the CodeDomProvider, it were a lot of work (complex and error prone work), so, very few people did it.

In this post I’m going to show a little app that uses Roslyn to compile and execute C# code at runtime.

Instead of work with expressions like 2+2 or a>b (a.k.a. the classic “hello world!” example), I’ll work with something more close to a DSL. This DSL will evaluate expressions against business objects and it'll move values back and forth. The idea is provide a more real world example, not just a lab test.

Now suppose we are working on a shopping cart DSL that allows sales people enter business rules to modify the order processing pipeline. In order to keep things consistent, it would make sense to use the same domain objects that the company’s ERP uses. As it turns out, that is a really easy thing to do with Roslyn.


This is how it looks the syntax of our DSL:





Something that worth to mention is with Roslyn we must provide valid C# or VB code. Unlike the Boo compiler, Roslyn does not allow us to extend the language syntax (and this is by design). Obviously this is no good enough for sales people, who ain't gonna learn C# in order to work with our DSL, but it’s a great starting point for us, because with a little bit of source to source translation we can get the job done (We don’t have to worry about semantic analysis, operator’s precedence, etc., etc… all the heavy lifting will done by the Roslyn compiler).

You can get the source code from github using this link

By reading the unit tests you will understand how it works, there are just eleven of them, so is not a big deal. In future post I’ll cover in more detail some aspects of the Roslyn API internals, but I think this example will give you an easy to read overview, on how to build a DSL on top of the Roslyn APIs.



In the next post I'll be revising some design considerations that you may want to have in mind while designing your DSL syntax as well as the business rule editor.

Comments

Popular posts from this blog

Migrating an ASP.NET MVC 4 app from Azure websites to WinHost

About a week ago I've to migrate an ASP.NET MVC 4/EF5 application from Azure websites to WinHost. While the process was really smooth, there were some caveats related to database connections that I want to share with you. Create and setup the ftp profile on VS and configure the connection string was really easy, WinHost provide you those values and there is nothing special here. But once you deploy your website and try to see it online, you may get the “yellow screen of dead” with the message: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)" Assuming you wrote the connection string properly, this happens because you cannot use the default connection name in your web.c

How to show excel files inside the .NET Webbrowser Control

If you are reading this, chances are you been banging your head against the wall for a couple of hours (or even days) trying to show excel files inside the WinForms webbrowser control. Possible reasons you ended up in here: You had working code that got broke after upgrading from Win 7. Your code doesn’t work the same way between machines running different (newer) versions of IE. A download box pops up every time your app tries to show an excel file inside the webbrowser control (you wanna show the actual content). You just have no clue on how to get excel working into the .NET embedded webbrowser control. You are trying to implement IInternetSecurityManager and don’t know where to start. (Or how don’t know how to delegate calls to your security manager). Among many other, maybe….. Yes, COM is a PITA, so is ActiveX and IE (Embedded or full for that matter). And no, showing excel files inside the webbrowser control shouldn’t be that hard, but sometimes we have

How to replace text with images on Word documents using C#

This post it’s a reply to a question I got from a previous post that shows how to work with Word templates from C# code . If you haven’t read it, I recommend to do so because I’m not going into details here. But basically it was about how to create a Word document from a template and perform same text manipulation. So, the question from Marcel Kieboom  was “Do you know if it would be also possible to replace one of the words with an image which is locally stored?” The answer is yes, and this is how you can do that. Based on the same convention I had used in the previous post, I should have a template like this: What I’m trying to do here is replace the text [angus-young] for an actual pic of Angus. The technique I’m using it’s pretty common on web sites and basically consist in have an image with a matching name for each keyword I want to replace and then create the image path dynamically. This is the C# code to do that. * When I execute the f