Los Techies : Blogs about software and anything tech!

Look Ma, No Code-Behind!


I’ve been playing with some MVC-related stuff and one of the things I took a second to look at was not having to have a code behind in order to do the whole ViewPage<MODEL> business.

I came up with this:

no_code_behind

 

Thoughts?

Yes, it works.  Code forthcoming (it’s incubating right now – this is part of a larger chunk of code I do plan on releasing).

Do you like this approach?  Should I continue heading down this path?

Kick It on DotNetKicks.com
Posted Nov 08 2008, 10:08 PM by chadmyers
Filed under: ,

Comments

Joel Ross wrote re: Look Ma, No Code-Behind!
on 11-08-2008 11:59 PM

Take a look at this post:

devlicio.us/.../strongly-typed-viewdata-without-a-codebehind.aspx

This allows you to do the same thing in a declarative fashion. It's not exactly the prettiest, but it's another way of accomplishing the same thing, I think.

Gokhan wrote re: Look Ma, No Code-Behind!
on 11-09-2008 4:05 AM

@Joel, although it works, R# chokes on intellisense with CLR notationed inherits attribute (at least mine does)

www.jetbrains.net/.../RSRP-84168

@Chad, will you also (at least) demo your code generators and helpers for Thunderdome P. ?Generator for IndexViewModel and  this.TextBoxFor stuff etc.

Tobin Harris wrote re: Look Ma, No Code-Behind!
on 11-09-2008 4:52 AM

Thumbs up for finding new ways to lose the code behind :)

It raises a question though...

In Rails, you have Helper classes for encapsulating complex display logic, and lead to simpler views. One place to put these in ASP.NET MVC is in the code behind. Where would you put this stuff without a code behind?

Dirk wrote re: Look Ma, No Code-Behind!
on 11-09-2008 6:15 AM

Hi Chad,

yes, I like it and please continue!

And like Gokhan, I would appreciate if you share some more information of your code arround the "this.TextBoxFor(...)" stuff.

I like the approach you describe in "Opinionated ASP.NET MVC by Joshua Flanagan"!

Regards!

Colin Jack wrote re: Look Ma, No Code-Behind!
on 11-09-2008 9:12 AM

I work with Johnnie Hall and his approach has been to use the MVC Contrib support:

<% var model = this.ViewData.Get<User>(); %>

Seems OK to me.

chadmyers wrote re: Look Ma, No Code-Behind!
on 11-09-2008 10:21 AM

@Joel

Yeah, I've seen that before, but it gets messy and, like Gokhan said, you don't get intellisense (yet?).  At any rate, once the IntelliSense does work, either way will work and it becomes a matter of preference at that point.  Hurray choices!

@Gokhan, Dirk

I'm working on the code. This is a teaser for what I'm preparing. Please hold on a little longer.

chadmyers wrote re: Look Ma, No Code-Behind!
on 11-09-2008 10:23 AM

@Tobin:

The way we're doing it is with microcontrollers which serve a similar purpose.

We have things like:

<%= this.TextBoxFor(m=>m.Contact.FirstName).Width(100).Label("First Name:") %>

The TextBoxFor uses an expression builder under the hood and is also aware of our domain model attributes such that "Required" will add a CSS class to the <input> tag so that jQuery validation picks it up. Same for max length and other such things.

Dave Cameron wrote re: Look Ma, No Code-Behind!
on 11-09-2008 6:32 PM

The microcontroller approach looks great. I'm about to embark on a largish project using ASP.NET MVC. Simplicity of the final source code is a big selling point, so anything to cut-out extra files is great too.

Really looking forward to the code you are going to post.

Dew Drop - November 10, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - November 10, 2008 | Alvin Ashcraft's Morning Dew
on 11-10-2008 6:07 AM

Pingback from  Dew Drop - November 10, 2008 | Alvin Ashcraft's Morning Dew

Bil Simser wrote re: Look Ma, No Code-Behind!
on 11-10-2008 7:48 AM

Looks good. I'm doing something similar with Presentation Model and WPF which I'm working on a post. Will be interesting to see your full post and code and compare notes.

Tim Barcz wrote re: Look Ma, No Code-Behind!
on 11-12-2008 9:55 AM

This is cool and am looking forward to seeing the code...

That said, this strikes me as a possible, "oh crap I forgot to set my NHibernate mapping as an embedded resource" issue.  Where people don't get intended behavior until they remember to do some trivial step.  I've always found this to be a small gripe, but a gripe it is.

Overall though it's a step forward and and thankful you're blazing this trail.

chadmyers wrote re: Look Ma, No Code-Behind!
on 11-12-2008 10:03 AM

@Tim:  Although you wouldn't be able to use the Model field at all in your view code (to bind against, etc).  If the developer wants to use the Model field, they HAVE to do this.  If they DONT do it, they won't have access to the Model field. But it won't matter because they're not using it!  

There's no magic "surprise". You either need it and do it, or don't and don't :)

Emilio Cavazos wrote re: Look Ma, No Code-Behind!
on 11-14-2008 11:10 AM

I posted an example of how I work around the code-behind issue.  I can't wait to see the code that makes your example work.

www.agilification.com/.../Strongly-Typed-Model-Object-Without-Code-Behind-(Sort-of).aspx

Vladimir Kelman wrote re: Look Ma, No Code-Behind!
on 11-21-2008 1:04 PM

I immediately remembered articles/notes written by Rob Conery

blog.wekeroad.com/.../crazy-talk-inline-scripting-and-code-behind

blog.wekeroad.com/.../crazy-talk-inline-scripting-revisited-or-2003-bites-back

and David Safitech

http://david.safitech.com/?p=4

And also - my own struggle against setting server-side controls properties declaratively on ASPX page

pro-thoughts.blogspot.com/.../how-to-decoratively-set-attribute-of.html

Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems