Binder for ironruby mvc

Binder for ironruby mvc
by Ivan Porto Carrero
Posted April 8th, 2009 at 7:07 pm

I’ve progressed far enough with IronRuby MVC to start thinking about a sample and I’ve decided to write 2 separate samples both on the same technologies. As an OR/M I will be using LightSpeed from Mindscape, just because it’s an awesome piece of technology.

At this moment I’m looking at creating a binder for objects that takes a hash as input. The default model binder of ASP.NET MVC doesn’t quite fit all that well with the ironruby implementation so I’m creating a ruby binder implementation. This stuff is just something that makes ruby shine.

While doing so I came up with this little tidbit of code:

class System::Object

  class << self

    def create_from_hash(options)
      result = self.new
      result.populate_from_hash options
      result
    end

  end

  def populate_from_hash(options)
    options.each do |k, v|
      mn = "#{k}=".to_sym
      self.send(mn, v) if self.respond_to?(mn)
    end
  end
end

This code allows you to populate any .NET object from a hash.

So doing Person.create_from_hash(:username => “joe”) would create a user instance with the property username set to joe.

You do need to provide a hash where the values have the correct type.

Technorati Tags: ,
2 ResponsesLeave a comment
  • ASP.NET MVC Archived Blog Posts, Page 1
    April 13, 2009 at 06:14

    [...] to VoteBinder for ironruby mvc (4/8/2009)Wednesday, April 08, 2009 from flanders.co.nzAt this moment I’m looking at creating a binder for [...]

  • Ryan Riley
    December 4, 2009 at 21:44

    Have you looked at hashie [1] for creating objects from hashes? I don’t know that it actually works for this, but it looks like another good alternative.

    [1] http://github.com/intridea/hashie

Add a commentGet a Gravatar

* Name

* Email Address

Website Address

You can usethese tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Around The Site
Categories
Archives
Tags