Jaa


3 Steps for Developing Against Windows Azure Tables

This blog post is not intended to explain how to design
Windows Azure Tables. This will be discussed in a later topic.

The whole purpose of this blog is to explain to developers an easy way to build and interact with Windows Azure Tables.

Thinking about this it is very easy to implement it in a few
easy Steps

 

1)     
Create the Service Entity
Class

2)     
Create the Table Context
Class

3)     
Start Manipulation

 

We will discuss these Classes in Details with Examples

The Service Entity Class:

For the sake of this blog I will use the famous blog sample the
blog Entity is meant to define the data that is required to be stored within
the sample

    public class BlogEntity : Microsoft.WindowsAzure.StorageClient.TableServiceEntity

    {

        public BlogEntity()

        {

            RowKey = String.Format("{0:10}_{1}", (DateTime.MaxValue.Ticks - DateTime.Now.Ticks), Guid.NewGuid());

            PartitionKey = DateTime.Now.ToString("ddmmyyy");

        }

        public String Title { get; set; }

        public String Id { get; set; }

        public String Body { get; set; }

        public String ImagePath { get; set; }

    }

 

The above class inherits from TableServiceEntity which is
part of the Microsoft.WindowsAzure.StorageClient;

All the data that is required is stored in as properties in
this class

Notice the PartitionKey and the RowKey defined in the class
you can think of them as the Table Name and the Row Number in a Relational
Database

The PartitionKey is the Table Name that contains all the
data that we would like to group together. In the Scinario above each day we
get a new Partition

The RowKey is a unique identifier within the Partition.

This class is used as a representation of each Row within
the partition

Table Context Class:

    public class BlogContext : TableServiceContext

    {

        public BlogContext(string baseAddress, StorageCredentials credentials)

            : base(baseAddress, credentials)

        {

        }

        public IQueryable<BlogEntity> BlogEntities

        {

            get

            {

                return this.CreateQuery<BlogEntity>("BlogEntities");

            }

        }

        public void InsertEntry(BlogEntity entity)

        {

            this.AddObject("BlogEntities", new BlogEntity { Title = entity.Title, Body = entity.Body, Id = entity.Id, ImagePath = entity.ImagePath });

            this.SaveChanges();

        }

        public void RemoveEntry(string id)

        {

            BlogEntity res = (from r in BlogEntities where r.Id == id select r).First();

            this.DeleteObject(res);

            this.SaveChanges();

        }

        public void ModifyEntry(BlogEntity entity)

        {

            BlogEntity res = (from r in BlogEntities where r.Id == entity.Id select r).First();

            res.Title = entity.Title;

            res.Body = entity.Body;

            res.ImagePath = entity.ImagePath;

            this.UpdateObject(res);

            this.SaveChanges();

        }

    }

 

The Table Context Class the is the class required to
Manipulate the data within the table it inherits from TableServiceContext

This enables any client application to interact with the
table it includes. This interaction is the same style you would use when
interacting with

Relational tables.

This is all you need as a developer to use tables as storage
entities.