Archive for July, 2010

27 Jul 2010

ASP.NET MVC 3 Preview 1 Released

No Comments Uncategorized

The title says it all, so go get your copy today and check out all the new features.  You can download it here.

New features include:

  • Razor View Engine
  • Dynamic View and ViewModel Properties
  • “Add View” Dialog Box Supports Multiple View Engines
  • Service Location and Dependency Injection Support
  • Global Filters
  • New JsonValueProviderFactory Class
  • Support for .NET Framework 4 Validation Attributes and IValidatableObject
  • New IClientValidatable Interface
  • Support for >NET Framework 4 Metadata Attributes
  • New IMetadataAware Interface
  • New Action Result Types (HttpNotFoundResultAction and HttpStatusCodeResultAction)
  • Permanent Redirect Support in the controller (RedirectPermanent, RedirectToRoutePermanent, and RedirectToActionPermanent)

Looks like this is going to be a very worth while upgrade, and a special thanks should be given to Phil and team for making ASP.NET MVC everything that ASP.NET WebForms isn’t.

19 Jul 2010

Turning JSON into a ExpandoObject

20 Comments Uncategorized

Recently I had the need for a web service of mine to take a JSON blob as an input.  This isn’t really exciting or all that interesting a problem, but I really didn’t enjoy the code smell that came from drilling in to the resulting Dictionary object that comes from desterilizing the JSON object into something that .NET understands.

public ActionResult Create()
{
    IDictionary<string, object> request;

    using (var bodyStream = new StreamReader(Request.InputStream))
    {
        var json = bodyStream.ReadToEnd();

        JavaScriptSerializer ser = new JavaScriptSerializer();
        request = ser.Deserialize<IDictionary<string, object>>(json);
    }

    var accountRequest = request["account"] as IDictionary<string, object>;
    var billingRequest = request["billing"] as IDictionary<string, object>;
    var billingInfoRequest = billingRequest["info"] as IDictionary<string, object>;
    var billingInvoiceRequest = billingRequest["invoice"] as IDictionary<string, object>;
    var billingItemsRequest = billingRequest["items"] as IDictionary<string, object>;

    // create account
    var account = new Account {
        CreatedOn = DateTime.UtcNow,
        Email = accountRequest["email"] as string,
        Name = accountRequest["company"] as string,
        UserName = request["user_name"] as string
    };

    // ... more code using the dictionary object
}

After remembering that the ExpandoObject was also based on IDictionary<string, object> I thought it might be a marriage met in heaven.

The ExpandoObject class enables you to add and delete members of its instances at run time and also to set and get values of these members. This class supports dynamic binding, which enables you to use standard syntax likesampleObject.sampleMember instead of more complex syntax like sampleObject["sampleMember"].

Since I won’t know what the JSON will actually look like until it is passed in to the web service, and I really don’t want to create different objects just for the sake of making a temp pass through object from JSON to my app code, the use of the ExpandoObject is great because it helps keep my code clean looking and more readable.  Because as a developer I don’t really care that the data is coming in as JSON, all that I really care about is the data it self.  So my aim is to simplify my life and the readability of my code.

The first thing I usually do when creating an API is write a reference application, or in other words use a test driven approach when starting to write a new API.  I started with the following reference code:

public ActionResult Create()
{
    dynamic request;    

    using (var bodyStream = new StreamReader(Request.InputStream))
    {
        var json = bodyStream.ReadToEnd();

        JavaScriptSerializer ser = new JavaScriptSerializer();
        var dictionary = ser.Deserialize<IDictionary<string, object>>(json);
        var request = dictionary.Expando();
    }

    var account = new Account {
        CreatedOn = DateTime.UtcNow,
        Email = request.account.email,
        Name = request.account.company,
        UserName = request.account.user_name
    };

    // ... more code using the expando object
}

This seems much cleaner doesn’t it?  Now let me so you the Expando extension method that I created to accomplish this.  It is relatively straight forward I take an IDictionary<string, object> object and copy it into an ExpandoObject object, which is also implements IDictionary<string, object>.  So the copying from one to the other is pretty straight forward for the most part.

public static ExpandoObject Expando(this IDictionary<string, object> dictionary)
{
    var expando = new ExpandoObject();
    var expandoDic = (IDictionary<string, object>)expando;

    foreach (var item in dictionary)
    {
        bool alreadyProcessed = false;

        if (item.Value is IDictionary<string, object>)
        {
            expandoDic.Add(item.Key, Expando((IDictionary<string, object>)item.Value));
            alreadyProcessed = true;
        }
        else if (item.Value is ICollection)
        {
            var itemList = new List<object>();
            foreach (var item2 in (ICollection)item.Value)
                if (item2 is IDictionary<string, object>)
                    itemList.Add(Expando((IDictionary<string, object>)item2));
                else
                    itemList.Add(Expando(new Dictionary<string, object> { { "Unknown", item2 } }));

            if (itemList.Count > 0)
            {
                expandoDic.Add(item.Key, itemList);
                alreadyProcessed = true;
            }
        }

        if (!alreadyProcessed)
            expandoDic.Add(item);
    }

    return expando;
}

Not as straight forward as a foreach loop, but not totally out of the question for something that you couldn’t read and understand in about 10 minutes.

This may add a slight bit of overhead between the coping of one dictionary into another, and the use of the dynamic runtime, but I think the over all ease in readability is more of a productivity gain than the slight bit of overhead that was gained.

12 Jul 2010

Uninstalling Windows Phone Developer Tools CTP

10 Comments Uncategorized

Today I decided to upgrade to the Beta of the Windows Phone Developer Toolkit, however the uninstall process wasn’t working.  It kept asking me what I wanted to install every time I choose the uninstall radio button.

So after a couple failed attempts at uninstalling in different ways, I decided to go to the source, in my case that was:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Microsoft Visual Studio 2010 Express for Windows Phone  CTP – ENU

After in the folder, I just had to right click on the vs_setup.msi file and select Uninstall, after that the process worked like a charm and I could then install the beta.  Hope this helps someone besides me.