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

Moving to Medium

It's been a long time since I want to give medium a try, and finally, I made some time to do it. To get started on the new platform, I'll be doing series on "Getting programming concepts, languages and tools". If it sounds interesting to you, please take a look at the first post  Getting AWK  and spread the word if you like it. I'm not going to migrate old entries to the new web site. They will remain here safe and sound! As usual, thanks for reading!