OdeToCode IC Logo

Code-behind for Me

Friday, June 3, 2005

In ASP.NET 2.0, I still prefer using code-behind over the single-file code model for web forms – but it is a tough choice.

In ASP.NET 1.x the choice was easy. Using a code-behind file was the only way to have intellisense when writing code, was the only way to catch syntax errors at build time, and was the only way to ‘pre-compile’ your code into an assembly before deployment.

All these reasons above go away in 2.0. In the single-file model your C# / VB.NET code exists inside a <script runat=”server”> tag, and within the same file as the ASPX / HTML markup for the web form. You’ll have intellisense, and the refactoring menu works, too. You can catch syntax errors by requesting a build, and still pre-compile the entire web application. In short, you won’t lose any productivity using a single-file model.

There are other advantages to the single-file model. There is only one file to check into source control. Only one file to diff when looking for changes between builds. Only one file to deploy, and only one file to copy when you want to sell company secrets to the highest bidder.

I like the simplicity of the single-file model. There are certainly fewer moving pieces. I just don’t like code intermingled with so much declarative markup – neither in ASP.NET or some of the Avalon samples I look at. I like to see a full class definition and not headless, floating method definitions. The mixture just isn't pretty.

I like to look at pretty code.

Perhaps this dislike could pass in time, but for now I’m putting code in a separate file.