ASP.NET MVC RULES SURPREME!

April 9th, 2009 | Tags:

Seldom do I use all-caps for titles. This time, it’s merited. I’ve been circling around ASP.NET MVC like a suspicious lion for months. Since I don’t work much with web projects, this has slipped down on my priority list in favor of technologies more relevant to my current projects. But since it recently hit official release, I made some time last weekend and got around to it.

The web is a fantastic tool. Plain and simple. With all the new technologies that have been elbowing their way into our lives to make them simpler, easier, faster and richer, the web has never before been such a valuable resource as it is today. One of those technologies that I really like is Silverlight. Another, is ASP.NET MVC.

I cannot lavish enough praise upon this new technology. Because I’d run out of disk space.

There are many levels of programming. You can code in machine-code, for the ultimate control over the computer, or you can point and click for a bit more productivity at the expense of control. ASP.NET MVC has poked up from nowhere right smack in the middle of the two.

To quote  The Gu:

MVC is a framework methodology that divides an application’s implementation into three component roles: models, views, and controllers.

  • "Models" in a MVC based application are the components of the application that are responsible for maintaining state.  Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL).
  • "Views" in a MVC based application are the components responsible for displaying the application’s user interface.  Typically this UI is created off of the model data (for example: we might create an Product "Edit" view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object).
  • "Controllers" in a MVC based application are the components responsible for handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI.  In a MVC application the view is only about displaying information – it is the controller that handles and responds to user input and interaction.
    One of the benefits of using a MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application.  Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated.

I’m a big fan of productivity. And ASP.NET MVC increases productivity like few other web technologies I’ve ever seen. It allows me to do complex things in very little time. It’s also very extensible and comes with full source code, so if I run into a wall, I can simply rewrite the wall to conveniently have a gate in it.

The first thing I did (after I downloaded the ASP.NET MVC kit), was to have a look at a video tutorial. Normally, I’m one of those who gleefully tears off the wrapping and plugs things in and starts pushing buttons without a single thought to the manual. But I was tired and wanted a 5 minute crash course in what it is, and what it does.

I highly recommend you watch that very same video, the “Creating a movie database tutorial” on www.asp.net. They say that a picture says more than a thousand words, and a movie with maybe 20 frames per second for twelve minutes and four seconds, must then (obviously) say more than 14,480,000 words. And since I would not think of clogging you down with 14.5 million words, I’ll simply leave you to watch that fantastic introduction to ASP.NET MVC and wait right here while you go have your socks knocked off.

Right, so after I had watched the tutorial (which I highly recommend you do before proceeding any further. Go on, I’ll still be here when you’re done!), I instantly fired up Visual Studio and created my very first MVC web application.

I started to do my own movie database like the tutorial, but quickly got a better idea and opted for a tiny bit of originality. I created a movie site ranking database. A site with a list of online movie sites with ranking. I also found the ASP.NET MVC design gallery and downloaded a nice enough looking theme to use.

The whole thing took about an hour.  Yep, that’s it, an hour. And that includes the time spent designing the database, going back to the tutorial and Googling for solutions, tweaking the style sheet as well as creating a few icons and graphical elements. And this was my first time using ASP.NET MVC! Imagine what you could be doing nine hours from now, after you have made nine complete and functional websites just like this one! Why … there’s no end to the possibilities!

dude-wheres-my-movie

The ASP.NET MVC application I made is complete with user registration, a little news feed (no RSS support), a list of sites with description, commenting, ratings and ranking based on their total scores.

commenting

It has a functional administrative back-end to create, update and delete sites, score categories, news and studios (which sites can be affiliated with). The create/edit movie site views have some interesting master-multiple-detail relations with synchronization to remove unselected items and add newly selected items in a many-to-many relationship (the checkboxes and the scores).

admin

When a site is affiliated with a list of studios, their logos appear beneath the site description like this:

studios 

All that, in an hour!

And it’s all so very easy. Each action you can take on the website, such as create a new movie site, add a comment, and so on are methods on the controller class. Here for instance is the code to route the visitor from the root URL ~/ to a view that will render the list of movie sites, sorted by total score:

public class HomeController : Controller
{
    private MovieSitesDbEntities mEntities =
        new MovieSitesDbEntities();

    //
    // GET: /
    public ActionResult Index()
    {
        var sites = mEntities.SiteSet.
                Include("Scores").
                Include("Scores.ScoreCategory").
                Include("Studios").
                Include("Comments").
            OrderByDescending(s =>
                s.Scores.Sum(score => score.Score)).ToList();

        ViewData.Model = sites;
        return View();
    }
}

And the contents of the /Views/Home/Index.aspx which contains the code to render that particular view contains simple asp.net syntax like this:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent"
    runat="server">

<h1>Movie Site Rankings</h1>

<% int rankNo = 1;
   foreach (var item in Model) { %>
  <div class="rating">
    <div class="siteDescription">
      <h2>#<%= rankNo++%>
      <a href="<%= item.URI %>"><%= item.Name %></a></h2>
      <p>
        <img class="float-left"
          src="/Content/Screenshots/<%= item.ScreenshotURI %>"
          alt="Site screenshot" />
        <%= item.Description %>
      </p>
...

 

I’m impressed.

Click here to view a live demo of the Movie Site Ranking demo! 

  1. June 22nd, 2010 at 07:15
    Reply | Quote | #1

    online movies are cool but i wish the quality were better..,: