Part of the reason I choose C# for larger web projects, and a big reason I point to when people ask me about my tech preferences is the nice, clean, managed strongly-typed objects. "I want to know if I'm using my variables correctly at compile time, not after users start getting errors". Is this short sided of me? Am I being a code-purist, or prude?
About 6 months ago I started to let my guard down. There was a really great article written by Alexandra Rusina in the February 2011 issue of MSDN that gives you the C# skinny on var, dynamic, and object:
Understanding the Dynamic Keyword in C# http://msdn.microsoft.com/en-us/magazine/gg598922.aspx
Having read that, I get the difference, but why do I want to dance around without the fences? Why treat the standard int the same I would a string / value pair? It's chaos. It's all "var", it's groovy, right? No. What is this, PHP?
My "ah ha" moment was a couple weeks ago at the Mix11 conference in Las Vegas. I do a lot of Facebook development, and a lot of pulling my hair out because of the world of pain that is Facebook development. With facebook, you can get at user data using their old REST apis, their FQL queries, FQL multi-queries, and now their Graph APIs (and GraphAPIs with queries). The strongly typed control freak in me was declaring objects like "FriendLinks", "Friend", "FamilyConnection", "PhotoTag", etc. It got out of control. At Mix, I was really eager to hear from the creator of one of the C# libraries for Facebook, Jim Zimmerman about the status of his library, Facebook C# SDK. If you do Facebook development I highly recommend watching his talk in its entirety: http://channel9.msdn.com/Events/MIX/MIX11/OPN06
What struck me the most of Jim's talk is that Jim feels my pain. For as much as Facebook can give a developer, they hide it in cracks and corners of their API. Sometimes you have to call their Graph API, get a result, then call an FQL query. Sometimes the result changes with new data because they've introduced a new feature. Deprecation of older APIs is quick. It's a very dynamic API. Which is exactly what led Jim to create the SDK the way he did. "I'm not writing a wrapper API around Facebook", he says with absolute conviction. I don't blame him. Instead, this SDK gives developers the tools we need.
I decided to give it a whirl. Here's the easy one, the Graph API for getting "me" (this user):
var client = new FacebookWebClient();
dynamic me = client.Get("me");
ViewBag.Name = me.name;
ViewBag.Id = me.id;
Nice! Now, no matter what Facebook adds to the "user" object, I can take advantage of it. if they added a profile property favorite_candy, it's just me.favorite_candy, and I've got it implemented. OK, but I might not mind adding a property to a Facebook.User object. So this is kinda cool, but I'm not totally sold.
Next, I'm going to commit to start poking around at some of the FQL (Facebook Query Language) tables I can query: http://developers.facebook.com/docs/reference/fql/, specifically, I want to query for all the photos I am tagged in from the photo_tag table: http://developers.facebook.com/docs/reference/fql/photo_tag/
var query0 = "SELECT pid FROM photo_tag WHERE subject=me()"; //photos I am tagged in
try
{
var result = (IList<object>)client.Query(query0);
}
catch (FacebookApiException ex)
{
throw;
}
Here's the data just back from Facebook's servers (I'm tagged in 356 photos):

Nice! So, I can make up queries and get back dynamic objects. OK, so that's cool, but even these smaller name/value pair type queries STILL hadn't sold me on ditching my safe zone of inteli-sense warm fuziness. Here's the clincher. I want all friends who are tagged with me in photos, and which photo we were tagged, as well as when that tag happened. Yes, you read that correctly. This scenario gave me pause and excitement. I don't want a strongly typed object anymore. Also, I need an FQL Multi-query to make this happen, starting with the query above where I select the photo ids of all the photos I am in. Here's that using the C# SDK:
var query0 = "SELECT pid FROM photo_tag WHERE subject=me()"; //photos tagged in
var query1 = "SELECT pid,subject,created,text FROM photo_tag WHERE pid IN (SELECT pid FROM #query0)"; //friends tagged in photos with me
try
{
var result = (IList<object>)client.Query(query0, query1); //multi-query
var resultPhotosUserIn = ((IDictionary<string,object>)result[0])["fql_result_set"];
var resultTagsOfPeopleInPhotos = ((IDictionary<string,object>)result[1])["fql_result_set"];
}
catch (FacebookApiException ex){
// Note: make sure to handle this exception.
throw;
}
And here's my new frankenstein object. What's nice is if I didn't qet the FQL query right, or those data fields returned data differently than I expected, I can preview it in the debugger.

This makes a lot of sense now. If you're doing a lot of interesting Facebook queries, dynamic variables are your friends. Jim's approach also makes sense. As the Facebook API changes, he doesn't have to rush to his computer to update wrapper classes that we all have to wait to download.
Oh, and to sweeten the deal, this Facebook C# SDK is available as a NuGet package. Check out the install guides. Using NuGet you can have all this magic in your project in 5 minutes.