in

ASP.NET Weblogs

A Recipe for New Media

April 2009 - Posts

  • ASP.NET Async Controllers Rock!!

    I've been watching videos from MIX 09 and following along and doing the examples presented (love Phil Haack's Ninja on Fire Black Belt Tips for ASP.NET MVC).  There are a lot of good things coming out in regards to ASP.NET and Silverlight that I will talk about later, but there is one thing that I'm really excited about in ASP.NET MVC Futures that i found as a result of my digging and that is Aysnc Controllers.  For everyone who has used ASP.NET WebForms async support, you basically know what I'm talking about.  For those who don't here is the run down:

    Every action done in code carries with it an inherent time to complete, some are so quick they are imperceptible, others like web services access and database access (or anything requiring disk or network access for that matter) have a noticeable delay at times (especially in data mining and OLAP scenarios).  In many web application systems you are forced to simply run each action synchronously and wait for each event to complete before going onto the next.  So if you are accessing a web service and doing analytical processing, it could take quite a bit of time.  But what if you could run the activities in parallel?  It would greatly decrease the wait time for the end user.  That is where Async mode comes in.  Here is an example of an async action on a MVC Controller:

    public Func<ActionResult> Foo(int id) {

         AsyncManager.RegisterTask(

            callback => BeginGetPersonById(id, callback, null),

            ar => {

                ViewData["p"] = EndGetPersonById(ar);

            });

        AsyncManager.RegisterTask(

            callback => BeginGetTotalUsersOnline(callback, null),

            ar => {

                ViewData["numOnline"] = EndGetTotalUsersOnline(ar);

            });

     

        return View;

    }

    What is happening here is that the MVC system executes both BeginGetPersonById and BeginGetTotalUsersOnline in parallel and when both complete, it will then process the View (display the web page).  Simple, clean, efficient. 

    I used to play around with Ruby on Rails, and basically bought their: "The real problem is development time not runtime.  Just throw more hardware at it if it gets slow."  But there are some things you simply cannot speed up no matter how much hardware you throw at it (especially when you are making calls out into the cloud to services you don't control).  To be able to cut the execution time via async callbacks will make a huge difference in client perception of speed.  Couple that with the fact that ASP.NET MVC beats the pants off of RoR speedwise and basically provides for all the same scenarios (I will admit that ActiveRecord is a hard act to follow, but LinqToEntities is close and gives you data model visualization as an added bonus.  Personally I'm using FluentNHibernate with LinqToNHibernate as I am a control freak and don't like the baggage that gets added to each class).

    EDIT (Aug 17, 2009):  LinqToEntities V4 is doing something about the "baggage" and allowing POCO (Plain Old C# Objects) to be used, with supporting classes to handle the mapping.  I'm still using NHibernate, especially with all the coolness that is baked into NHibernate 2.1.

    So for me, and especially because we build Facebook applications where how fast we can service the clients translates into customer satisfaction, the speed, the control, and the support we get from Microsoft for ASP.NET MVC makes it a no brainer for me to go with ASP.NET MVC.

    So if you want to join the ASP.NET MVC camp, just go to http://www.asp.net/mvc and you will find everything you need to get started, including great tutorials and example applications.  If you are already using ASP.NET MVC and want to see what I'm talking about with Async Controllers, then go to http://www.codeplex.com/aspnet and download ASP.NET MVC v1.0 Futures and Using the AsyncController.docx to get started (It does assume some understanding of asynchronous methods and how they work).

More Posts