SubSonic LoadFromPost

I think SubSonic is simply amazing. It has saved me so much time and effort of writing. One little trick that it's got is the LoadFromPost method the Models inherit from the SubSonic.AbstractRecord<T> class.

What LoadFromPost does is iterate over the the Request.Forms collection and matches the name of the form element with the corresponding  property for your Model. So what that means is no more of this.

 1: Product prod = new Product();
 2: prod.Name = name.Text;
 3: ....//More code setting the properties based on the form
 4: prod.Save

So all you now have to do is this

 1: Product prod = new Product
 2: prod.LoadFromPost();//this loads up all the properties.
 3: prod.Save();

Anything that saves time is a good thing, and nothing sucks more than writing tedious databinding code. Another advantage is that you don't need to use Server controls just plain html tags so you don't have to worry about ASP.Net adding all the extra naming container bits as well and that'll play nicely with any JavaScript or CSS that uses the id of an html tag.

kick it on DotNetKicks.com

8 Comments

  • Interesting to say the least. How does it handle enum properties?

  • mxmissile,

    Well it would ignore it. It only works for generated properties and unless you know something I don't, SubSonic doesn't generate enums for model properties.
    So if you've created a property for an enum that is just using a generated prop as a backing field then it would set the backing field from the int based property.

  • Hmm very nice, I just love all the goodness that comes from SubSonic :)

  • common man, this feature is since ages in castle monorail with activerecord. open source. www.castleproject.org.
    This hype with Subsonic is like kids loving mozart only if it played in a rapper song.
    Get real.

  • liviu.
    What's your point? It's been there for ages in SubSonic too. Nobody was talking about it(kinda like monorail) so I mentioned it. Don't blame us for monorail not gettting any hype.
    I like the concepts behind monorail, just not the actual implementation of ActiveRecord on NHibernate. Not to mention that it didn't fit into the existing infrastructure as well as SubSonic did. And come on. it's not like monorail didn't borrow from ruby on rails.....

  • Is there any performance penalty for this ?

  • Julian,

    I can't imagine that there would be for most cases. If anything it may be faster cause you could use straight up html and not even need to have the controls run on the server, or disable view state if you wanted. The only way I could see it being an performance issue is if you had massive amounts of items being posted back to the server and you loaded up multiple models from that.

    For Instance, if you had 50 form elements for 3 models, each model would loop through all 50 form elements each and then have to loop through the table columns for that model to match them up.
    But even then I can't see it being a performance issue.


  • I am working with the march prerelease of the MS MVC Framework. I Like LoadFromPost, but I want to do my mapping and validation in the Model class - will LoadFromPost still work there? Also, the Model may shunt the input to another external validation layer, is there a way to do LoadFromPost so it takes a collection other than the Request.Form one?

    Thanks.

Comments have been disabled for this content.