Code behind vs. code inside
Should I use Code-behind or Code-Inside (or Code-beside)?
I’ve never heard that term, “Code Inside”, before. I always called it inline, even though it really isn’t. The biggest problem I have with code-behind doesn’t have anything to do with the separation of View and Controller objects (let’s be honest, ASP.NET no matter how you slice it is just an MVC pattern. You really have to work to break an application out of that pattern). It has to do with the compilation of the assembly. All of the code behind classes get compiled, by Visual Studio, into one big assembly. Whidbey changes some of the logical errors that can occur when you have two separate assemblies representing the UI and the code behind classes, but it still, as far as I know, doesn’t address the ability to compile multiple assemblies or to compile to a module and then create your assembly from the multiple modules. Whidbey does, as Andy Smith points out, at least check the code behind code against the declared aspx file and combines them into a single class contained in the overall assembly. This is the way JSP has been handled since it’s inception I believe.
Scott Mitchell points out the DataBinding syntax still forces you to mix up controller code with view code. That’s true and there is no way to get around that. Whidbey will still force us to use the DataBinding syntax in our UI in templated controls. I guess their way of dealing with that is to move the data connecting code into the UI with the new “DataSource” tags.
What should we do with our code until Whidbey comes out? My suggestion would be to use the Code Inside model, but to create as much separation between your business/data logic and your UI as you can.
update It would be groovy to have a bindable repeater type control that supported the following syntax:
<asp :GroovyRepeater id="foo" runat="server" dataSource="Bar">
<itemtemplate>
<asp :Label id="lbl" runat="server" datafield="hootie"/>
</itemtemplate>
</asp:GroovyRepeater>
Which still kind of sucks, but it’s better than embedding alligators everywhere.
