« May 2008 | Main | July 2008 »

June 2008

June 11, 2008

IronRuby and ASP.NET MVC

At Tech Ed last week, I showed a prototype of integration between IronRuby and ASP.NET MVC. I was really happy about how straightforward this turned out to be; it’s a strong testament to the robustness of our hosting API that Levi on the ASP.NET MVC team was able to add IronRuby integration with only a simple example and the existing docs.

Let’s walk through a simple example. Keep in mind that this is a prototype, and that many things will likely change before we’re done with it (particularly some of things we do today with globals and instance variables).

First, let’s look at how you can define some default routes via routes.rb:

image

Next, let’s look at the Controller code for the HomeController:

image

The index action renders a view using a trivial Model which lives in models\HomeModel.rb:

image

This model is used by the index action View, which lives in views\home\index.rhtml:

image

Next, let's look at a slightly more sophisticated example using a Products Controller that renders a list of categories that looks like:

clip_image002

It also renders a list products for a category when you click on it:

clip_image002[5]

This is what the controller looks like:

image

There are two actions in this controller: index and list. The index action returns a list of the categories, and the list action returns a list of products for that category.

Note that we're using instance variables on the controller to communicate with the view - this is likely going to change in the future, but it was something we hacked in to see what it would look like.

If we look at ProductsModel, you'll see that it contains a simple adapter around a Visual Studio generated wrapper for the Northwind database:

image

We had to write our own adapter to work around a few bugs that we have in our .NET interop story as of right now, as well as adding a few model-esque methods. Here's what the NorthwindDatabase adapter looks like:

image

Notice how we can use LINQ to SQL to write our queries to the database, and the funky C# 3.0 anonymous delegate syntax that we use for GetCategory().

The interesting code in the view looks like:

image

Today we're using the MVC template engine, but there isn't any reason why we couldn't enable erb / haml etc. in the future.

How can I get it?

You can download a ZIP of the entire project from here if you want to take a closer look. Note that you can't run these bits yet, since I demo'd it using a private build of ASP.NET MVC from mid-way through their current milestone. That said, when Preview 4 of ASP.NET MVC ships you'll be able to run IronRuby on top of it (their current release is Preview 3).

Look for a blog post here, and from the folks like Phil over in MVC-land when we're ready to ship you some bits to build MVC apps using IronRuby.

June 10, 2008

IronRuby at Tech Ed 2008

image

I’d like to start out by thanking everyone who came up to talk to me at the conference. We had a Dynamic Languages booth at the conference, and it was great to talk to all of the folks who took the time out to drop by for a visit.

A big thanks goes out to our booth staffers – Dave Fugate, Curt Hagenlocher, Jimmy Schementi, Bill Chiles and Mahesh Prakriya who helped to keep things running smoothly. Dave somehow managed to grab an enormous whiteboard from somewhere and used it to help answer many of the FAQs for folks who were hanging around waiting for a blue-shirted guy to become available.

image

Tech Ed is (not surprisingly) a very enterprisey conference. We spent at least as much time telling folks what Dynamic Languages were (or correcting their misperceptions) as we did talking about our team’s progress. We also had a whole bunch of folks come up to ask us questions about F# because, apparently, F# is now a dynamic language :)

By far the most frequently asked question was why dynamic languages? The advice that resonated the most with the folks that I talked to was about using it to help them build internal DSLs for their applications. This is the idea that you can use DSLs for part of your app, dynamic languages to help faciliate those DSLs, and statically typed languages for the foundational pieces. You'll find that this idea is often called polyglot programming.

The Talk

The IronRuby talk was the very last talk of the conference. Apparently I was one of the ‘big guns’ that would convince folks to stay all the way to the very end of the conference. While I prefer being at the start of the conference so that my talk can help drive further discussions with folks while they’re still there, it is what it is. Oh well, let’s see if the tubes can help start a conversation :).

My talk focused on three things. First up was an introduction to Ruby since half of the folks in the room hadn’t used Ruby before. I wrote a simple unit-testing framework live on stage. Each new feature in the framework helped to introduce a different feature of Ruby. At the very end of the talk I showed how that same framework could be used to test .NET code. Here it is in its entirety:

image

And here is some .NET code that we tested using this framework:

image

It worked rather well, and I think I’ll continue to use it at talks where I need to introduce Ruby to the audience.

Next, I talked about our Silverlight integration. I showed the excellent set of demos that Jimmy created for RailsConf last week, including his client-side Try IronRuby demo:

image

and the lovely Silverlight watch demo:

image

I also got a chance to sneak in some of the code I was hacking on in the evenings – an adapter that maps the HTML 5 Canvas API to Silverlight. I ran a few of the examples from the excellent Mozilla Canvas tutorials using a my adapter. I pasted some code (with minor rubification) from the Mozilla tutorial into the text box:

image

and hit run - the colored boxes with the translucent circles appeared below: image

There's no reason why we couldn't do this with managed JavaScript either using the cross-language interop features of the DLR.

Oh yeah, there was one more thing that I demo'd - more on that tomorrow.

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from John Lam. Make your own badge here.

May 2009

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Blog powered by TypePad