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?

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Chad Myers

Chad Myers is the Director of Development for Dovetail Software, in Austin, TX, where he leads a premiere software team building complex enterprise software products. Chad is a .NET software developer specializing in enterprise software designs and architectures. He has over 12 years of software development experience and a proven track record of Agile, test-driven project leadership using both Microsoft and open source tools. He is a community leader who speaks at the Austin .NET User's Group, the ADNUG Code Camp, and participates in various development communities and open source projects.
This entry was posted in ASP.NET, MVC. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.rosscode.com Joel Ross

    Take a look at this post:

    http://devlicio.us/blogs/tim_barcz/archive/2008/08/13/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.

  • http://altinoren.com Gokhan

    @Joel, although it works, R# chokes on intellisense with CLR notationed inherits attribute (at least mine does)
    http://www.jetbrains.net/jira/browse/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.

  • http://www.tobinharris.com Tobin Harris

    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?

  • http://dirkdev.blogspot.com/ Dirk

    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!

  • http://colinjack.lostechies.com Colin Jack

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

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

    Seems OK to me.

  • http://www.lostechies.com/members/chadmyers/default.aspx chadmyers

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

  • http://www.lostechies.com/members/chadmyers/default.aspx chadmyers

    @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 tag so that jQuery validation picks it up. Same for max length and other such things.

  • Dave Cameron

    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.

  • http://weblogs.asp.net/bsimser Bil Simser

    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.

  • http://www.devlicio.us/blogs/tim_barcz Tim Barcz

    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.

  • http://www.lostechies.com/members/chadmyers/default.aspx chadmyers

    @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 :)

  • http://www.agilification.com Emilio Cavazos

    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.

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

  • http://pro-thoughts.blogspot.com/ Vladimir Kelman

    I immediately remembered articles/notes written by Rob Conery
    http://blog.wekeroad.com/blog/crazy-talk-inline-scripting-and-code-behind/
    http://blog.wekeroad.com/blog/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
    http://pro-thoughts.blogspot.com/2008/09/how-to-decoratively-set-attribute-of.html