In my previous post I talked about using Blobs for storage when using Windows Azure. In this article we will talk about table which are similar in many respects to blobs, though as you might expect, with a higher degree of organization.
Lets first try to understand how tables are organized. Many of you are thinking similar to SQL tables and you are partially correct, but its more of a bucket for storing entities with similar structure. So basically, your class definition defines the table structure, you are not explicitly creating a table with columns. That said, Azure does make a requirement of these classes, they must inherit from TableServiceEntity, the code for our PictureEntry object:
The key thing to notice with this snippet is the constructor which references the two properties given to you via the inheritance. Remember, Azure tables ARE NOT SQL tables. The partition key is used for partitioning your entities. Remember, you are not creating separate tables for data, you have a bucket so the PartitionKey is used to separate the entities for different application/uses so to speak; its a partitioning mechanism, similar to defining a table. The idea behind RowKey is to provide a way to uniquely reference a row, think auto-incrementing field.
The following is a picture, taken from Cloudy in Seattle
So to understand this, first lets talk about the setup. In my the previous post I noted, at the end, that you do have to modify the WebRole.OnStart event to get things to work because the Publisher settings must be established. Here is the code for the OnStart event:
Our next step is to initialize the storage location. Here we will provide a name for our bucket. Below is the code snippet:
This is sort of backwards considering the standards that Azure recommends, however, in the case of this experimental application I wanted to just remove the existing table and recreate it when this method is invoked. The table is, quite literally, a bucket for entities, as the diagram above shows. It is important to remember, however, that these tables ARE NOT relational and many of the aspects of TSQL programming we take advantage of are not supported here; that is what SQL Azure handles.
To actually add and remove PictureEntry instances from our table, we are going to employ a repository pattern using Azure’s managed library classes. Here is our context layer:
In this case, we are defining a context class inheriting from TableServiceContext, this class will give you many of the methods seen in the Entity Framework, such as SaveChanges. However, it is specifically for working with Azure and cloud-based tables. For the most part this code is fairly straightforward. What links this class to the Cloud is the construct which takes a CloudStorageAccount as a parameter; we have seen this class frequently between this entry and the last.
So lets table about how we would add a picture to our table. To be fair, I used webforms last entry this time I will use ASP .NET MVC. Here is my action receiving the form with the file upload:
I honestly didn’t know you could do this, I just decided to try it out of understanding the theory. Who knew you could specify the uploaded file in the parameter list as opposed to getting it from Request.Files. Our only goal here is to get the the binary stream into a byte array form so we can assign it to the PictureEntry instance we are creating. After that its just basic interaction with our context instance reference.
Like the Blob example, we would like to be able to show the user what we have stored in the table, however, in ASP .NET MVC we dont normally think of using ASHX, its kind of against the convention. The better method would be to call an action which returns binary data. After some looking I was surprised that this did not come built-in meaning I would have to write something myself. As it turns out people have run into this issue and a solution exists using BinaryResult (code is courtesy of Jim Guerts). Note: I am told that a version of this code exists in MvcContrib, but I couldnt find BinaryResult in the most recent release.
With this and my context I needed to only pass the RowKey and use it as an ID to extract the images binary stream:
If the image does not exist, I simply return an empty view which would result in a broken image being displayed. Here is the code for the view that uses GetImage:
I always hate how this code looks, soon I hope to make Spark my default ViewEngine for all things, but for now I am still using WebForms and getting ugly code like this which reminds me too much of ASP3.
To conclude, working with tables for this simple example is not that much different then working with Blobs. Tables do provide a bit more structure and replace metadata with the properties that most developers are intimately familiar with having worked with .NET in the past. I would prefer tables to blobs because of this structure.
Download the Code here (Visual Studio 2010)