The WebBuddy Manifesto

(or "What is WebBuddy"?)
Face it, getting your Silverlight Application talking with your web servers can be a bit of a pain. You have to muck with WebClient, build your REST query strings by hand, handle System.IO.Stream's containing JSON strings, and basically have a bad day. Sure it might be easy for those guys running ASP.NET--they just do some magic in Visual Studio and everybody is happy, but your application stack is Ruby, Perl, or PHP. Visual Studio gives you no love, the .NET library has nothing for you, and every wiz-bang example you find assumes you are talking with ASP.NET. It's high time calling your web services with Silverlight is as easy as it is with JavaScript!
Now with Dog
WebBuddy offers a growing plethora of quick-and-dirty utilities to make AJAX easy. UriBuddy makes manipulating query strings easy with a Dictionary. AjaxBuddy lets you make AJAX calls using strongly typed callbacks which will pass back your deserialized data.

Get Started

WebBuddy Overview

  • AjaxBuddy - A library that attempts to make AJAX calls feel more like jQuery or Prototype.js.
  • UriBuddy - Makes handling query parameters easy. No more string concatenation!
  • JsonBuddy - (comming soon) Paste a JSON string, click some buttons, and get a bunch of strongly typed C# classes to use for serialization.

AjaxBuddy

Call your backend and AjaxBuddy will call you back with a strongly typed result!
View a Full Sample
public void MakeQuery()
{
    string requestUrl = "http://search.yahooapis.com/ImageSearchService/V1/imageSearch";
    // Ask Yahoo about snow in Seattle and call us back with the strongly typed results...
    Dictionary<String, String> query = new Dictionary<string, string>
    {
        {"appid","YahooDemo"},
        {"query","Snow In Seattle"},
        {"results","23"},
        {"output","json"}
    };
    AjaxBuddy buddy = new AjaxBuddy();
    // Notice how this looks kinda like jQuery?  Notice how there isn't any fuss with WebClient?
    buddy.Get<YahooImageResult>(
        new Uri(requestUrl),
        query,
        new AjaxOptions<TResponseType>
        {
            Callback = queryCallback,
            State = "my user state",
            FailureCallback = failureCallback
        });
}
// This is our callback when the AJAX request has completed.  Notice it passes in a strongly
// typed result?  Nice, huh?
private void queryCallback(YahooImageResult queryResults, object userState)
{
    Console.WriteLine("Got back {0} results", queryResults.Results.totalResultsReturned);

}
// This is our callback when the AJAX request has barfed on our shoes.  Exception contains
// the exception passed in by WebClient
private void failureCallback(Exception exception, object userState)
{
    Console.WriteLine("Barf: {0}", exception);

}

UriBuddy

Use a Dictionary<> to hold your parameters. Let us turn them into a properly escaped string.
// Take a base url
Uri sample = new Uri("http://www.nytimes.com");

// some highly useful parameters
Dictionary<String, String> query = new Dictionary<string, string>
{
    {"param1","nice"},
    {"param2","code man"}

};


// create a new url using a chained style of coding
Uri newSample = sample
    .AppendPath("/pages/world")
    .AppendQueryValues(query);


// or do something like this
Uri somewhereElse = sample.AppendPath("pages")
    .AppendPath("world")
    .AppendQueryString("param1=nice&param2=code%20man");


// prints: http://www.nytimes.com/pages/world?param1=nice&param2=code%20man
Console.WriteLine(newSample);


// turn the dictionary into a string with our famous extension method
String queryString = query.ToQueryString();


// prints: param1=nice&param2=code%20man
Console.WriteLine(queryString);


Notice: Project is under construction. Code may not work as intended--patches are welcome. No purchase necessary. Actual cash value: $0.01. Rules and free game pieces available on the web. Offer void where prohibited by law. May contain peanuts.

Last edited Mar 31, 2009 at 9:02 PM by coryking, version 22