IronRuby ASP.NET MVC With Filters

Last July, I blogged about an IronRuby ASP.NET MVC prototype Levi and I put together with John Lam and Jimmy Schementi of the DLR team. It was really rough around the edges (and still is!)

IronRuby on ASP.NET MVC DemoOne of the benefits of doing that prototype was that it inspired all the work around action and controller descriptors in ASP.NET MVC (something I need to write more about later) which decoupled us from exposing reflection in our public API and improved the overall design of ASP.NET MVC greatly. This had the nice side-effect of making the implementation of IronRuby on top of ASP.NET MVC much cleaner.

In this updated prototype, I’ve now implemented support for ASP.NET MVC filters. You can define action filters and authentication filters (I need to test the other filter types). Keep in mind, this is a very rough prototype code still. I’ve just been swamped up to my neck lately and this is a spare-time labor of love.

I’ve only implemented one type of filter so far. You can specify a class to apply to an action method and the class implements a specific filter interface. I haven’t done anything like the more rails-y filter_before and filter_after thing.

Here’s an example of an action filter in IronRuby. This one simply writes something to the response in the before method, and does nothing in the after method.

class MyFilter < IronRubyMvc::Controllers::RubyActionFilter
    def on_action_executing(context)
      context.http_context.response.write 'MyFilter '
    end
    
    def on_action_executed(context)
      # noop
    end
    
    def method_missing(name, *args)
        show_action_info name, args
    end
end

(Gee, I wish I had a ruby syntax highlighter plug-in for WLW)

And here’s the use of that filter within a controller.

require 'HomeModel'
require 'MyFilter'

class HomeController < Controller
  filter :index, MyFilter

  def index
    view nil, 'layout', HomeModel.new
  end  
end

Notice that the way you define a filter on the index action is:

filter :action_name, FilterClassName

In the sample code I uploaded, you can see the effects of the filter at the top of the page. :) Hopefully I’ll find more time to update this, but as I said, it’s a labor of love, but time is in short supply.

In the meanwhile, I also need to look into whether there’s enough interest to make this a CodePlex project. There’s a bit of due diligence I have to do before I put code up on CodePlex, which is why I haven’t done it already because I’ve been busy.

And before I forget, here’s the download location for the sample.

Technorati Tags: ,

What others have said

Requesting Gravatar... Rob Conery Feb 17, 2009 4:16 PM
# re: IronRuby ASP.NET MVC With Filters
Ohhhhh Ruuuuubyyyyyy! Yay! Nice work Phil :). I know Bertrand is going to LOVE seeing Ruby code in MVC. This is good stuff!
Requesting Gravatar... Saad Savari Feb 17, 2009 5:33 PM
# re: IronRuby ASP.NET MVC With Filters
Looking forward for Ruby MVC store front Rob Conery Style.
Love Ruby. Good stuff
Requesting Gravatar... Steve Feb 17, 2009 6:10 PM
# re: IronRuby ASP.NET MVC With Filters
Looking good!

Sure hope to see these efforts continue!
Requesting Gravatar... Sergio Pereira Feb 17, 2009 8:30 PM
# re: IronRuby ASP.NET MVC With Filters
Not to be "stealing" too much from RoR, but I think you can make the Ruby syntax work even more in your favor if you change the order of the parameters in the filter method:
filter FilterClassName, :action_name
Then you could take it further like:
filter FilterClassName, :only => [:action_1, :action_2]
# or
filter FilterClassName, :except => [:action_3, :action_4]

Requesting Gravatar... haacked Feb 17, 2009 9:52 PM
# re: IronRuby ASP.NET MVC With Filters
@Sergio, I like it! Thanks for the suggestion.
Requesting Gravatar... Joe Chung Feb 17, 2009 10:01 PM
# re: IronRuby ASP.NET MVC With Filters
Looking forward to seeing this as a CodePlex project! IronRuby + ASP.NET MVC = Crazy Delicious!
Requesting Gravatar... Jeffrey Richardson Feb 19, 2009 10:58 AM
# re: IronRuby ASP.NET MVC With Filters
I'm not much into Ruby myself, but this does seem interesting. The more interesting part, however, is how you mention action and controller descriptors. I had worked with them just before you mentioned them on this post, and recently I've made a post of my own about them ( http://wc4f.qsh.es/archive/2009/02/19/6.aspx ). I do look forward to more posts about the extensibility of ASP.NET MVC from both you and ScottGu.
Requesting Gravatar... Johan Danforth Mar 22, 2009 10:20 AM
# re: IronRuby ASP.NET MVC With Filters
Oh, this is so sweet. Now if I could just get one of my customers to start a new web project. Most things seems to be "brownfield" or (for me) WCF.

Please keep up the work on IronRuby + ASP.NET MVC, I seriously think it will be quite popular give it some time and PR :) What might be needed is some more work on the M in MVC, something that plugs in with ASP.NET MVC somehow?

What do you have to say?

(will show your gravatar)
Please add 2 and 6 and type the answer here: