Simon Miller Team : Web Development Tags : Web Development

The Power of MVC Compels You!

Simon Miller Team : Web Development Tags : Web Development

Recently I've been delving into the wonderful world of MVC. MVC for the uninitiated stands for Model –View-Controller and is a modern design pattern in software design. To say that this pattern architecture - specifically, Microsoft ASP.NET version 3 - is a vast improvement to how we code over traditional .NET Webforms, does the implementation a huge disservice. The design, while not enforcing proper separation of concerns, very loudly suggests it at every opportunity. I simply feel unclean at the thought of putting controller code into my model classes. It really does that to you.

The first thing I noticed about MVC that did take some getting used to was relearning my understanding of how web pages work back in the pre-.NET days. It was a shock to me how much I had forgotten about how Forms worked after dealing with the ‘magical’ implementation seen in Webforms with its Viewstate and Page life cycles. Here’s a basic rundown on how it works:

MODELS: This is the part that is most familiar to me. Models are simply classes with the purpose of storing data for display on Views and passing to the Controller. They enforce the public-facing structure and integrity (including validation if you like) of the site structure.

VIEWS: Bundled with MVC 3 is the view engine called Razor. You can swap in and out other view engines however they are beyond the scope of this discussion; plus, I’ve come to really enjoy working with Razor. At first glance it looked like “Classic” ASP to my eyes – and that’s not a compliment – but I soon saw the advantages of having some (basic) page logic directly in the HTML of a page e.g. loops. Views are similar to regular .NET front-end pages and can be used similarly as MasterPages or UserControls (a Partial View).

CONROLLER: The real work now happens in Controllers. Before you would have a ‘code behind’ on a page where the page logic would go, or a separate class entirely. A controller lets you expose methods that are called by the Views by GET, POST etc. With each return type from your Controller method being a generic ActionResult, it can be cast simply into modern outputs like Json.

So put simply, Views contains your forms which post Models to your Controllers.

Microsoft has become very friendly with the JQuery library, so much so that it is now bundled with MVC ASP.NET. I absolutely love how painless it is to interface a Jquery AJAX post from your View with a method in your Controller, and return the Action Result as a JSON object. It’s highly integrated as to become second nature. You’ll want to AJAX-ify everything, and unlike the old Webforms AJAX Panels, this isn’t a bad thing at all!

Bundling all of the above with other fairly recent Microsoft technologies like LINQ and the Entity Framework allows the creation of faster applications with cleaner and easily testable code. I can’t wait to keep learning more MVC.