ASP.Net MVC 4 all - E J Hernández Valdelamar
ASP.Net MVC 4 all
Martial coding for rapid web development.
Eugenio Jacobo Hernández Valdelamar
México. Version 3. August, 2009
August, 2009
ASP.Net MVC 4 all - E J Hernández Valdelamar
1 Introduction
So you want to make a Web application. Well, there are tons of information around the Internet and so many languages and tools.
However, when you choose one, there is a learning curve to climb, so this decision is crucial for the next weeks, months or years of your pro life.
But, what do you have to consider?
Language must be something known to you or easy to learn if it´s something new. Maybe, you don't want to pay for a tool because you want to evaluate it. Web infrastructure must be at hand, so your computer must be able to host a web server. Maybe you read some articles and top 10 lists to make sure you are on the right track or just because you want to be part "on the right side" according to statistics.
While you analyze and decide what is the best choice for your career, I pick Microsoft web platform. I know VB.Net for a while (desktop flavor), I have the tool (Visual Studio 2008) and the environment (virtual machine server with Internet information server), and because I don´t want to start with 1990's ASP, I want to start with the most recent incarnation of MS Web development alternatives: ASP.Net MVC framework.
In this document I´m planning to expose the main concepts and techniques to develop a Web application using this platform.
So let´s start.
1.1 About this document
This document started as a compilation of references to try to understand ASP.Net MVC framework and how to develop an application with it. However there were lots of details about web applications, the ASP.Net framework and design criteria that I wanted to unify to have a clear vision about this technology. Another reason is that many articles in the web (some dated from 2007 or earlier) show code snipplets that don´t apply to the current release of the framework; decorating action methods with attributes or changing views code behind are deprecated, so its important to have an actual version of this code. It's a "follow the breadcums" exercise and once I had a bunch of them, it was time to order all this jigsaw pieces to get the big picture. That's how these notes began to grow, and some readings about martial arts gave me the idea of organizing the contents: theory, techniques and applied sets of techniques to solve problems (katas).
August, 2009
ASP.Net MVC 4 all - E J Hernández Valdelamar
1.2 About the author
Well, I've been teaching since 1993 and programming since 1990. I started with Turbo Pascal, then Turbo C++, had some courses of mystic languages (Smalltalk, Prolog), had a taste of Java in the server side and I met Visual Basic in 1996. Since then I've been developing solutions with MS technologies, most for desktop and C/S. Even when today I am more involved in software processes and methods, I like to be aware of technology evolution, and I really think that web applications are about to make a great leap. I still enjoy a lot programming and learning something new, and I believe this is a good opportunity to share the experience. Please, send any comment and suggestion that help me improve this work to: jack_hv@yahoo.com
1.3 Licence
This work is licensed under the terms of the Creative Commons Attribution- Noncommercial-No Derivative Works 3.0 License.
1.4 Document versions
Version 3. August, 2009. Update on views realm. One more basic kata (sending e-mail), fixing tips and new PDF document format. Version 2. June, 2009. Format revision. File uploader sample. RSS sample. Basic techniques updated. Initial structure of intermediate techniques (app design & testing). Initial structure of the intermediate techniques chapter. Version 1. May, 2009. Basic document structure (key concepts, basic techniques and code katas) and initial content.
2 ASP.Net MVC: key concepts
2.1 Web pages are not enough
When you start dealing with web development, the basic thing you start playing with is HTML.
August, 2009
ASP.Net MVC 4 all - E J Hernández Valdelamar
All you have to do is creating a file with .html extension, write some tags and the result in a browser will be a web page.
Lets put something clear: writing HTML is NOT programming. HTML is a way to format content that will be displayed on a browser.
You can be a masochist and write your pages in a text editor like Note Pad, or have a life and use a Web page editor. Even you can use Office Word and then export the document to a Web page. Of course, this is only about contents; the aesthetic part still remains in the graphic artists realm, and because I don´t want to mess with color theory I'll keep it as Spartan as possible.
As long as your content remains static, this is the way to go. But if you want to add some notes every day, display a calendar, have a list of your visitors or any functionality that implies new data, making it by hand is a bad idea (unless you want to be buried alive in a server farm).
Interaction, nice look & feel and fresh data implies programming and something to put the text, numbers or images you want to use.
2.2 Web applications
See document: smli_tr-2007-166.pdf
So you want to make an application. Well, then you have to write some code to make a program. But where is this programs is going to live? In your PC (win or *nix), in a big machine, in your cell phone? Well the answer tends to be everywhere, and that means that your environment is the World Wide Web.
Current web applications rely extensively on a number of technologies that are fundamental components of the web browser. These include the HTML markup language, Cascading Style Sheets (CSS), the JavaScript scripting language, and the Document Object Model (DOM).
In a way, these technologies serve as the logical equivalent of “binary code” of traditional software applications, and are intended for execution in any web browser regardless of the underlying hardware or operating system.
2.2.1 Database driven Web sites
A data-driven Web site draws its content from external dynamic data sources. The data sources may come from:
* Files (text or XML) * Database (SQL Server, Access, Oracle)
August, 2009
ASP.Net MVC 4 all - E J Hernández Valdelamar
* XML Web services * Other websites
When you have a data-driven site, you are collecting data on the fly and then building the pages from that data.
A well-architected, data-driven Web site is much easier to maintain than a static Web site because most content changes require no change to the HTML behind the Web site.
Instead, changes are made to the data source that drives the Web site, and the Web site automatically adjusts to reflect those changes. With a data-driven Web site, you connect to and present live data from a variety of data sources.
Now the question is: how to do it?
2.2.2 Spaghetti a la Web See document: Spaghetti_code
Many applications on the web today have a complex and tangled structure – just like spaghetti programs in the 1960s and 1970s. Spaghetti code is a pejorative term for source code that has a complex and tangled control structure, especially one using many gotos, exceptions, threads, global variables, or other "unstructured" constructs.
It is also used in pejorative sense to imply that a given piece of work is difficult to understand.
So, where can you find spaghetti on the Web apps? Well, there are 3 primary scenarios:
1. Mixed code to the browser. Usually, HTML definitions, style sheets, and JavaScript code are not represented separately. Rather, in most web sites, HTML definitions, style sheets and JavaScript functions are interspersed and mixed in no specific order – other than the order that was established by the tools that were used to generate the web page. The source code of the web documents is not very easy to read and the actual behavior of the document is even harder to understand. This is because the typical user interaction model of the web is such that a new web page is generated and sent to the browser each time the user clicks on a link or a button on a page.
August, 2009
ASP.Net MVC 4 all - E J Hernández Valdelamar
2. Creative freedom over software engineering. As long as the primary purpose of web development was the creation of web sites consisting of documents, pages and forms, there was little reason to apply established software engineering principles to web development. The web browser, with its original design dating back to 1990, is quite well-suited to displaying documents and supporting simple navigation from page to page.
3. Zero structure programming models. The old "spaghetti" code model of web developing platforms as ASP, where actual code with application logic (VBScript, Jscript or whatever) was mixed with markup UI elements (HTML) is a good example. Trying to maintain this applications today is really complicated because of a simple question: where is it? So, it seems that we need to ´put some order in here, but where to start?
2.2.3 Separate the code, please See document: 112916.aspx
The most simple way to put some order to this mess is to separate. Let's put the put code in a separate script block element and let the markup in the rest of the page. Even when the organization unit is still the page, the page now have a structure. Most often the ASP pages are HTML pages with embedded VBScript or JScript, but there is no problem to implement ASP pages that generate plain text, XML or any other textual content. It is possible, of course, certain ASP page to contain only script and no static non-script content at all. This is especially useful when the application implements techniques such as page templates and all the content is dynamically generated, or when the generated content is not textual. So, to separate code from markup ASP provides: * Script code in <% %> and <%= %> tags * Script code in