InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

ASP.NET MVC and Codebehind Files

Posted by Hartmut Wilms on Sep 23, 2008

Sections
Development
Topics
.NET Framework ,
.NET
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
Re: Its optional -anyway- by Marcelo Lopez Posted
Re: Its optional -anyway- by chris barrow Posted
  1. Back to top

    Its optional -anyway-

    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-

    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-

    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

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.