« IronRuby at Tech Ed 2008 | Main | IronRuby at OSCON »

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.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2419952/30055680

Listed below are links to weblogs that reference IronRuby and ASP.NET MVC:

Comments

Don't forget to encode that output!

You don't want the category "Shoes & Trainers" producing invalid HTML... or leading to injection/XSS from the inside.

[)amien

What about the possibility of using ActiveRecord with an MVC project?

Hi John,

Congrats. Good job getting done, under your expert guidance.

What about the possibility of using ActiveRecord / DataMapper with an MVC project?

Since Data mapper will be out very soon, i am also considering it.

SoftMind

I think I just peed in my pants a little bit.

Very, very cool :o)

I'm really, really, really, really looking forward to doing ASP.NET development with IronRuby (and any DLR-basd language). Really. Keep up the great work! :)

What's with the require "HomeModel" stuff? Give us the snake_case back!

Nice sample... not far away to use scaffold and ActiveRecord. can't wait to see it.

Hey John,

Do you have any idea when ASP.NET MVC Preview 4 is likely to be released ?

Also, do you have any take on whether there might be other reasons for people to use IronRuby for developing ASP.NET MVC applications rather than coming from Ruby background? Any specific Ruby benefit for this exact scenario ?

@Mohamed: I would expect about 6 weeks after their last release - which is mid-July by my calculations?

It's the idea about being able to define your business problems in an internal DSL. Ruby excels at doing this, and offers some pretty compelling advantages over other languages.

I want to know if ASP.NET MVC Preview 4 is released or not? if not then when is the date for the release?

@Ravindra: Yes it is - Google is your friend here :)

John is there a very simple way of doing this with just http word paragraphs. I wish that microsoft would provide some more examples on how to do this....

Post a comment

If you have a TypeKey or TypePad account, please Sign In

Photos

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

October 2008

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