Wednesday, June 05, 2013

Scripting .NET Applications with IronPython

The idea for this article is to add IronPython to existing enterprise .NET software systems. There are a few reasons why I wanted to do this.

First, I wanted to provide other developers the ability to query the data store using the existing business logic and data access libraries, with the convenience of writing SQL queries.  I had this inspiration while writing queries against a SQL Server database, because the existing application didn't support the queries I needed to write. As the queries get more complex, I wished I could just write LINQ statements, since managing large SQL queries quickly gets out of hand. And adding these types of one off queries  to the existing application seemed like a waste. I had read an article years ago about the benefits of scripting, and I started forming the idea for this article in the back of my mind.

Secondly, I was mulling around the idea of a redesigned API for the legacy API I've been maintaining. I was thinking along the lines of using the Unit of Work pattern using either LINQ to SQL, NHibernate, or the Entity Framework, but abstracting away the choice of ORM. The API I had in mind would be accessed by writing LINQ queries, just like querying the underlying ORM . What I wanted was a way to quickly test out the API as I went along, without having to compile my queries. I was also thinking that having other developers writing queries against the API would help with the design of the API, as new ideas for the types of queries are thought up.


Configuring IronPython 

First, you'll need to install IronPython here (The current version is 2.7.3). You'll need to add references to Microsoft.Scripting.dll, Microsoft.Dynamic.dll, and IronPython.dll from the Iron Python installation folder.

Below is a partial code sample that creates a Python engine and execute a script.  

using System;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

string script = ""; // TODO - get Iron Python script
var engine = Python.CreateEngine();
var scope =  engine.CreateScope();
var source = engine.CreateScriptSourceFromString(script, SourceCodeKind.Statements); 
var compiled = source.Compile();
var result = compiled.Execute(scope);   

The Python engine  and scope should be initialized once and program startup. I haven't tried to see what happens if multiple instances are created and I don't want to know

Read more: Codeproject
QR: Inline image 1