Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code behind in ASP.NET MVC

Tags:

asp.net-mvc

What is the purpose of the code behind view file in ASP.NET MVC besides setting of the generic parameter of ViewPage ?

like image 859
alex Avatar asked Sep 20 '08 14:09

alex


1 Answers

Here's my list of reasons why code-behind can be useful taken from my own post. I'm sure there are many more.

  • Databinding legacy ASP.NET controls - if an alternative is not available or a temporary solution is needed.
  • View logic that requires recursion to create some kind of nested or hierarchical HTML.
  • View logic that uses temporary variables. I refuse to define local variables in my tag soup! I'd want them as properties on the view class at the very least.
  • Logic that is specific only to one view or model and does not belong to an HtmlHelper. As a side note I don't think an HtmlHelper should know about any 'Model' classes. Its fine if it knows about the classes defined inside a model (such as IEnumerable, but I dont think for instance you should ever have an HtmlHelper that takes a ProductModel. HtmlHelper methods end up becoming visible from ALL your views when you type Html+dot and i really want to minimize this list as much as possible.
  • What if I want to write code that uses HtmlGenericControl and other classes in that namespace to generate my HTML in an object oriented way (or I have existing code that does that that I want to port).
  • What if I'm planning on using a different view engine in future. I might want to keep some of the logic aside from the tag soup to make it easier to reuse later.
  • What if I want to be able to rename my Model classes and have it automatically refactor my view without having to go to the view.aspx and change the class name.
  • What if I'm coordinating with an HTML designer who I don't trust to not mess up the 'tag soup' and want to write anythin beyond very basic looping in the .aspx.cs file.
  • If you want to sort the data based upon the view's default sort option. I really dont think the controller should be sorting data for you if you have multiple sorting options accessible only from the view.
  • You actually want to debug the view logic in code that actuallky looks like .cs and not HTML.
  • You want to write code that may be factored out later and reused elsewhere - you're just not sure yet.
  • You want to prototype what may become a new HtmlHelper but you haven't yet decided whether its generic enough or not to warrant creating an HtmlHelper. (basically same as previous point)
  • You want to create a helper method to render a partial view, but need to create a model for it by plucking data out of the main page's view and creating a model for the partial control which is based on the current loop iteration.
  • You believe that programming complex logic IN A SINGLE FUNCTION is an out of date and unmaintainable practice.
  • You did it before RC1 and didn't run into any problems !!

Yes! Some views should not need codebehind at all.

Yes! It sucks to get a stupid .designer file created in addition to .cs file.

Yes! Its kind of annoying to get those little + signs next to each view.

BUT - It's really not that hard to NOT put data access logic in the code-behind.

They are most certainly NOT evil.

like image 145
Simon_Weaver Avatar answered Sep 28 '22 16:09

Simon_Weaver