Monday, October 05, 2009

Using IronPython as a Scripting Engine

IronPython makes adding Python scripting to .NET applications very simple. .NET applications interact with Python code through the DLR hosting API which is easy to use once you get used to the basic principles. You can find a guide to working with the DLR hosting API in IronPython in Action or my series of articles and examples on embedding IronPython.

A blogger called rune has been experimenting with embedding IronPython in C# business applications.
In this post I will look into the subject of extending an application written in a traditional .NET language like C# using IronPython. I am not talking about authoring assemblies by writing them in IronPython, though: I’m talking about letting some third party create plug-ins in Python which my application will load and execute at runtime.

Let’s say we are C# programmers developing some LOB application for processing orders from a fixed set of customers. Since these are trusted customers, they all receive special treatment: Some receive a discount on all their orders, for others we want to always add a “URGENT”-headline to their order, and yet others receive other kinds of special treatment.

Even if no third party will ever be creating plug-ins for our application, the support for plug-ins will greatly increase the application’s flexibility and reduce our maintenance costs.
To enable our order processing system to accomodate these and future cases, we have considered various options:
  • Implement all cases directly in the system using a lot of if-then-else and switches. When requirements are added or changed we implement, test and redeploy the application.
  • Implement a set of (parameterized) templates for handling cases like those described above. When requirements are added or changed, pray that they can be covered by one or more of the existing templates. Otherwise, implement a new template, test and redeploy the application.
  • Allow the application to execute plug-ins at certain stages in the process. Make the plug-ins execute in a sandboxed environment, thereby restricting the amount of havoc they can cause. When requirements are added or change, write a new snippet of code, test just the snippet (as opposed to the whole application) and deploy it.
Obviously, this post will explore the third option, using IronPython as an embedded scripting engine. I will not be presenting an implementation of the imagined order processing application but only showing code snippets illustrating the points of interacting with IronPython from C#.

So, the first thing to do is to be able to execute Python code from a C# application. This isn’t as difficult as it might sound...
In my last post, I gave an example of how to execute a Python script from within a C# application and I tried to justify why you might want to do something like that. In this post I will elaborate on the very simple example from the previous post by passing data to and from the Python script being executed. If you haven’t read the previous post, you should at least skim it.

The data we will be passing to IronPython will represent an order in an order processing application. To this end I have created two very simple classes...

No comments:

Post a Comment

Note: only a member of this blog may post a comment.