Monday, October 29, 2007 12:01 PM kevinisom

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 Filed under: ,

Comments

# SubSonic LoadFromPost Method

Sunday, October 28, 2007 11:14 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: SubSonic LoadFromPost

Monday, October 29, 2007 9:34 AM by mxmissile

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

# re: SubSonic LoadFromPost

Monday, October 29, 2007 8:40 PM by kevinisom

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.

# re: SubSonic LoadFromPost

Tuesday, October 30, 2007 11:31 AM by Mischa Kroon

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

# re: SubSonic LoadFromPost

Tuesday, October 30, 2007 7:43 PM by liviu

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.

# re: SubSonic LoadFromPost

Tuesday, October 30, 2007 11:04 PM by kevinisom

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.....

# re: SubSonic LoadFromPost

Wednesday, October 31, 2007 2:29 AM by Julian

Is there any performance penalty for this ?

# re: SubSonic LoadFromPost

Wednesday, October 31, 2007 3:55 AM by kevinisom

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.

# re: SubSonic LoadFromPost

Friday, March 14, 2008 10:53 AM by SubC

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.

# re: SubSonic LoadFromPost

Thursday, May 22, 2008 5:53 PM by ESICO

Leave a Comment

(required) 
(required) 
(optional)
(required)