views:

267

answers:

1

I'm considering embedding a scripting language into one of my software projects and have identified two options: compiling C# at run-time via CodeDOM and embedding a DLR-based scripting language. Both options would give me full access to the .NET Framework.

The operation that I'd be scripting would be a user-defined transformation of a DataRow and a set of metadata resulting in a modified DataRow. I expect these transforms will be composable and frequently invoked. Of course, I expect the transforms to be provided and modifiable by the end-user.

With this workload in mind, are there any clear advantages to using one approach over another?

+3  A: 

For users it's usually better to use languages with a more forgiving syntax, for obvious reasons. So I would recommend using a DLR based-language. If you have the time and resources, a specialized DSL is the best choice, because you can offer a small and easy-to-learn syntax, and it's easier to keep the user from doing things they should not be doing (like accessing the filesystem, for instance...)

I can't speak from experience, but, from what I've seen, the DLR can be quite fast (IronPython does better than native Python!). But dynamic dispatch always entails a slight overhead. On the gripping hand, cross-AppDomain calls are pretty expensive. While the dynamic dispatch cost is paid everywhere inside the script, the cross-AppDomain cost is paid only once per script call. Which one does better depends on how much your scripts will do.

Embedding a DLR Scripting Host is not difficult at all. What's difficult is to roll your own DSL, if you choose to go that way.

You could also look into boo. It's a static CLI language that looks like Python, thanks to type inference. Its compiler is highly extensible, and I've had some success writing some small DSLs on it. You could also look into Oren's book Writing DSLs with boo.

Martinho Fernandes
Do you have any thoughts about the relative performance of the two approaches? Or about the AppDomain management issues that arise with the C# option? Or about the difficulty of embedding a DLR scripting host? (Good point though, I did upvote it).
Dave
"But it will never be faster..." Are you saying that the penalty for the cross-AppDomain method calls will be less than the DLR dynamic dispatch mechanism? Do you have any sources for that? (In the C# implementation, I'll need to be able to unload the compiled assemblies, hence the AppDomain isolation.)
Dave
@Dave: Oh, there's that issue... That penalty is paid when you cross the AppDomain. The slight dynamic dispatch cost, you pay it everywhere inside the script. I'm not sure how these will balance out in your use case.
Martinho Fernandes
You should check this session from this PDC, which describes how to use IronPython/IronRuby to enable scripting in an application: http://ecn.channel9.msdn.com/o9/pdc09/wmv/FT30.wmv
Mathias
Yes, it's a great demo.
Alexandra Rusina
@Martin: Again, good point. Time to test.
Dave