got net?

Kevin Hazzard's Brain Spigot

About the author

Welcome to Kevin Hazzard's blog.
E-mail me Send mail

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Mixing Static & Dynamic .NET Languages - November 2008 Update

This is the November 2008 update of my now famous "Mixing Static & Dynamic .NET Languages" presentation. OK, so it's not a famous talk. But it still rocks and the folks at the code camps love it. This is the material I will be using at my talk at the upcoming Raleigh Code Camp on 11/15/2008. The source code and slides are attached below. When you run the demo code, you'll see a dialog that looks like this: 

The application, written primarily in C#, implements a shopping cart to which various products can be added. The shopping cart isn't the cool part, of course. What's interesting is the use of Python code to implement the discounting/marketing rules within the cart. If you press the Show Rules Editor button at the top of the form, you'll see the rule editor dialog: 



Python code is pretty easy to read isn't it? You can probably follow the logic to see that if certain counts of Entertainment products are added, they get discounted at various levels. And a trigger is set on the total count of items in the cart, too. There's a bug in the code though. But that's part of the demo. Can you find the bug?

Hopefully, if you run this code and understand it, you mind will be expanded a bit. One of the most often asked questions I get when talking about IronPython is, "How do I convince my boss to let me write that application in Python?" My answer is, "You don't have to write the whole thing in Python. With the fantastically rich hosting APIs in the .NET Dynamic Language Runtime (DLR), you can write the portions that need dynamism in Python and write the rest in C# or VB.NET." In fact, for most applications, the portion that can benefit from dynamic behavior is usually fairly small as a percentage of the application's total surface area. So this model of mixing static and dynamic languges is a nice approach to those kinds of problems.

Slides for MixingStaticAndDynamicDotNETLanguages 20081115.pptx (251KB)

Source Code for MixingStaticAndDynamicLanguagesInDotNet 20081109 (43KB)


Posted by kevin on Friday, November 07, 2008 5:17 PM
Permalink | Comments (3) | Post RSSRSS comment feed

Comments

Roberto Bonvallet Chile

Thursday, November 27, 2008 2:59 PM

Roberto Bonvallet

I think the bug is that lineItem.DiscountPercentage isn't initialized when the product's category isn't 'Entertainment'.  What did I win? Smile

I find it cool that one can mix static and dynamic languages in order to write more succint code by using the idioms of the more expressive languages.  I don't know anything about IronPython or .NET, so I was wondering whether there is any reason in the code above for not using the more pythonic:

totalItemCount = sum(i.Quantity for i in cart.LineItems)
entertainmentItemCount = sum(i.Quantity for i in cart.LineItems if i.Product.Category == 'Entertainment')

Cheers.

Kevin Hazzard, MVP United States

Friday, November 28, 2008 11:00 AM

Kevin Hazzard, MVP

You got it right, Roberto! OK, so it wasn't tough. And I avoided making the script more "pythonic" specifically because I was teaching to an audience that had never seen Python before. Playing with Python and C# is fun, isn't it? So many things we can do by integrating the right languages for each problem.

As a prize, I suppose I'll send you a free copy of my eBook on Dynamic Language Integration when it's done in a few weeks. Smile

Cheers.

vardis United Kingdom

Tuesday, December 02, 2008 4:59 PM

vardis

Can we all have a copy of the eBook, Kevin.

Comments are closed