You can catch lots in your .NET

Pro DLR in .NET 4 by Chaur Wu

I found this book in the book store at Tech Ed. The DLR is a really interesting area of .NET 4, though aside from a few articles in MSDN magazine, I’ve never really seen any in-depth information about how it works and the reasons behind the design. This was exactly the complaint of the author of IronJs in a recent podcast. However, dynamic objects are finding a place in the .NET world, including active record implementations like Simple.Data that was recently featured on Herding Code.

I didn’t find this the easiest book to read. Sometimes, the author includes a lot of tightly packed text without inline code examples, which made it quite hard to follow. I decided to give the book a chance by reading it twice, and this certainly seemed to help as the second time through I found the material much easier to digest.

The book covers several areas. The first, and most interesting aspect to me, was the support the DLR offers for efficient execution of late bound operations. This includes the DLR expression trees which offer variable binding, lambdas, labels and goto statements, giving a very expressive language, and the multi-level cache design in the notion of call sites and language binders. Having looked at this as a means of understanding the implementation of the dynamic type in C#, I didn’t fully understand how these ideas fitted together to give the interoperability story between C# and dynamic languages such as Ruby and Python. This book covers this really well, showing examples of interworking between all of these languages. The other part of the interoperability story I neglected to study in the past is the set of hosting APIs that the DLR offers. The idea here is that global information can be set up for a language, before any code executes in that language. This is needed to bootstrap one language into the world of another. Again the book offers numerous examples of the languages working together.

Part two of the book covers the uses of the DLR. The first chapter gives a brief introduction to aspect oriented programming (AOP), which I thought was good, and then goes on to demonstrate how you could add aspects to objects in C#. The difficulty here is that the C# classes have to implement IDynamicMetaObjectProvider which they need to implement to return an AopMetaObject which can be implemented in a library to add the necessary point-cut processing. When compared against’s way to doing AOP by intercepting object creation and returning a dynamic proxy, this need to modify the classes looked less than convincing to me.

The second use of the DLR is in a chapter on meta-programming, where the author writes a class which handles dynamic data access. Dynamic objects can be effectively extended to add fields corresponding to columns in a dynamically provided data table. This chapter had some great code examples of manipulating expression trees to get powerful results, and covers the kind of ground where Simple.Data is making a name for itself.

Other uses of the DLR are in implementing a scripting language that processes the definition of a DSL for hosting (and running) multiple source languages, and a very interesting chapter on using the DLR from Silverlight. This latter chapter covers a project named Gestalt which allows script blocks written in multiple languages to be embedded inline into an HTML page. Gestalt takes care of finding these blocks and feeding them into an instance of the DLR which then runs them. This allows the user to write a single HTML page that contains script written in a number of languages. The examples of this were very cool, though Gestalt doesn’t appear to have much development for some time.

Even through Microsoft is no longer actively developing Iron Python and Iron Ruby, at least the DLR has been included into the latest .NET to support the dynamic type that was added to C#. It isn’t clear if C# is going to get any more dynamic features in the future, but this book serves as a good introduction to the kinds of things that you can do with dynamic objects and how they can be efficiently implemented.

This entry was posted in Books. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s