InfoQ

News

ASP.NET MVC and Codebehind Files

Posted by Hartmut Wilms on Sep 23, 2008 03:33 PM

Community
.NET
Topics
.NET Framework
Tags
ASP.NET MVC

The ASP.NET MVC community discusses, if codebehind files are still needed when using the WebFormsViewEngine and whether they are a benefit or a disadvantage or even a problem.

ASP.NET MVC still uses the WebFormsViewEngine by default. When adding a WebForms view to an ASP.NET MVC application Visual Studio adds a codebehind file and a designer file automatically. Many people think that codebehind files are obsolete and that their usage is rather confusing, since developers might tend to use views like traditional all-in-one ASP.NET web forms.

Steve Smith goes a bit further and proclaims that Codebehind Files in ASP.NET MVC are Evil, because  they tempt developers to add logic to views, which should stay “dumb” by design:

Having a codebehind file is a temptation.  Developers who are new to ASP.NET MVC (and who isn't - it's not even a year old and not released yet) but who have a background with web forms (as most will) are going to have to resist a natural inclination to put code into their codebehind files like they always have.  This will make the logic in the View more difficult to test and at worst might even involve logic in the codebehind making calls directly to a database or web service and thus completely bypassing the separation of Model from View as well.

Steve explains that in some cases codebehind files are a necessary evil, e.g. if you want to have a strongly typed view in order to reference the model in a strongly typed way. Some argue that codebehind pages might also be used to store some view logic, which otherwise would clutter the ASPX file. And last but not least they are technically needed in order to get intellisense on the ASPX files. Regarding the first issue, Tim Barcz shows how to use Strongly-Typed ViewData Without A Codebehind by incorporating the CLR notation for generics instead of the C# or VB notation:

Inherits="System.Web.Mvc.ViewPage`1[[ABCCompany.MVC.Web.Models.LoginData, ABCCompany.MVC.Web]]"

The CLR notation defines the generic parameters count after the apostrophe followed by the parameters and the assembly containing the parameters’ types.

Luis Abreu disagrees with Steve and responds in a post. He is “not sure that removing the codebehind file will be enough for stopping a ‘bad’ programmer”, because logic could still be placed within script blocks in the ASPX file. Luis also disagrees with the general ideas of “dumb” views. In his view views may incorporate “presentation related code” and this code should be placed in codebehind files rather than in controllers or in the ASPX file directly, which would destroy the clear separation between markup and code.

Its optional -anyway- by Francois Ward Posted Sep 23, 2008 9:35 PM
Re: Its optional -anyway- by Marcelo Lopez Posted Sep 24, 2008 1:13 PM
Re: Its optional -anyway- by chris barrow Posted Sep 24, 2008 2:15 PM
  1. Back to top

    Its optional -anyway-

    Sep 23, 2008 9:35 PM by Francois Ward

    Even in Webforms the code behind file is completly optional, so both camps can be happy.

    That said, those MVC views are cute and pretty in demos and prototypes, but when you start going hardcore and make full scale applications, even when using partial views, it will get cluttered REALLY fast... code behind will help in that situation... So the option should stay there IMO.

  2. Back to top

    Re: Its optional -anyway-

    Sep 24, 2008 1:13 PM by Marcelo Lopez

    Absolutely. When you're dealing with demo-lets ( applets not good for much more than a fly-by-the-seat demo ), MVC views are all well and good. However, when you start to put together an system of any reasonable scale that not all necessary decision-behavior options be restricted to the view.

  3. Back to top

    Re: Its optional -anyway-

    Sep 24, 2008 2:15 PM by chris barrow

    There will be situations where code behind files are necessary. Since “presentation related code” exists in every web application, placing this code in code behind files rather than in controllers or in the ASPX file directly is not a bad design decision.

Educational Content

Bindings, Platforms, and Innovation

This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.

Orchestrating Long Running Activities with JBoss / JBPM

This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.

Neo4j - The Benefits of Graph Databases

This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.

Realistic about Risk: Software development with Real Options

This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.

Communication Flexibility Using Bindings

This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.

Writing DSLs in Groovy

After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.

Scaling Agile with C/ALM (Collaborative Application Lifecycle Management)

IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.

Concurrent Programming with Microsoft F#

Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.