Sunday 10 July 2011

ASP.NET MVC

Hi all, I'm back after a long time off. What I would like to discuss is ASP.NET MVC in comparison to ASP.NET.

Let’s give a quick introduction ASP.NET (Active Server Pages) is a server side coding language used for rendering HTML (Hyper-Text Markup Language). This is achieved by using the notoriously infamous server tags <% code goes here %>.

The ASP.NET runtime was built as an abstraction to allow developers to code in their native .Net languages, e.g. C# to create powerful web applications. The problem (amongst other things) was that the level of abstraction meant that coding “simple straight-forward” applications was quite easy and efficient, however coding custom or complex applications became quite difficult, e.g. two form tags on the same page.
The main problem was that through this level of abstraction some granular control was taken away from the developer in order to have a windows forms style development experience. Even controlling Ids on a particular HTML element became quite a laborious and painful task.

With ASP.NET version 4.0 some vast improvements had been introduced to make most coding scenarios easier, however with the revolution of test driven development, the improvements (in my opinion) still weren’t enough – introducing ASP.NET MVC.
MVC (Model View Controller) for those who do no t know is a design pattern used throughout all kinds of programming languages. It essentially boils down to one primary concept “separation of concerts”, meaning everyone has a job and they should only be concerned with fulfilling the requirements of their job. Fox example, in a typical restaurant theirs a chef, a waiter, and a manager. It’s not chef’s responsibility to take your jackets and make sure your comfortable in your seat, just like it’s not the waiter’s responsibility to ensure that the restaurant’s targets are met and that everyone shows up to work on time.

The MVC design pattern has been so successful in coding web applications that Microsoft and the ASP.NET team decided to introduce their own implementation. What this means is that we now have a design pattern directly “embedded” into the .NET framework, and used correctly can lead to power web applications as with ASP.NET but also powerful unit testing. ASP.NET MVC gives some of the control back to the developer and allows you to deepen your understanding of ASP.NET runtime.

With ASP.NET MVC 3.0 there is a new syntax introduced for creating ASPX pages called Razor. It is what I would describe as very clean and concise. The reason being that a lot of the repetitive tags and directives are no longer needed and writing C# inline with HTML (or even JavaScript) looks a lot nicer to read! Which for some developers such as myself is very important. What this means that less keystrokes are now required to produce the same content with ASP.NET thus allowing the developer to get on with other important tasks, such as, poking people of Facebook.