共用方式為


Introducing “ActiveRecord for Azure”

DISCLAIMER: This is a personal spare time experiment.

The Windows Azure Storage system is very capable and will probably fit your and your application needs in a lot of scenarios. I feel that, especially Windows Azure Tables, is far too often forgotten when considering object persistence in the cloud. This post was supposed to show off  Windows Azure Tables together with the WCF Data Services client in order to try to change the perception of "SQL Azure is the only option".

But the accompanying code examples evolved, so..

Please say welcome to “ActiveRecord for Azure”

“ActiveRecord for Azure” (or just AR4A) is what its name implies; a sample implementation of the ActiveRecord  pattern for the Windows Azure Storage system. Its main objective is to make Windows Azure data access really easy, yet powerful.

AR4A is built on top of the WCF Data Services client and the managed Windows Azure Storage API - utilizing the great work that's already been put into those libraries. AR4A just adds more simplicity to them.

To get started you inherit your entities from the ActiveRecord base class followed by telling the ActiveRecord environment to create the required tables. AR4A then handles the persistence of entities, retrieval of entities, continuation tokens, table partitions, among other things for you.

Feature overview (at the point of writing this)

  • CRUD operations
  • Partition management
  • Table generation
  • Id generation
  • Stubs

To give you a peek of what it looks like, I've revamped the RdChat application that comes with the Windows Azure Training Kit using AR4A.

“ActiveRecord for Azure” sneak peek

List all messages

Training Kit style
 var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
var context = new MessageDataServiceContext(account.TableEndpoint.ToString(), account.Credentials);

... (non-relevant code)

this.messageList.DataSource = context.Messages;
this.messageList.DataBind();
AR4A style
 this.messageList.DataSource = Message.All();
this.messageList.DataBind();

Add message

Training Kit style
 var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
var context = new MessageDataServiceContext(account.TableEndpoint.ToString(), account.Credentials);

context.AddMessage(this.nameBox.Text, this.messageBox.Text);
 public void AddMessage(string name, string body)
{
    this.AddObject("Messages", new Message { Name = name, Body = body });
    this.SaveChanges();
}
AR4A style
 //just create the message..
var message = new Message() {
    Body = messageBox.Text,
    Name = nameBox.Text,
};

//..and save it
message.Save();

Entity

Training Kit style
 public class Message : TableServiceEntity
{
    public string Name { get; set; }
    public string Body { get; set; }

    public Message()
    {
        PartitionKey = "a";
        RowKey = string.Format("{0:10}_{1}", DateTime.MaxValue.Ticks - DateTime.Now.Ticks, Guid.NewGuid());
    }
}
AR4A style
 public class Message : ActiveRecord<Message> {

    public string Name { get; set; }
    public string Body { get; set; }

}

Startup

Training Kit style

First about ten lines of configuration change handling code. Then:

 var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

// dynamically create the tables
CloudTableClient.CreateTablesFromModel(typeof(MessageDataServiceContext),
                                       account.TableEndpoint.AbsoluteUri, account.Credentials);
AR4A style
 //initialize the AR4A environment
ActiveRecordEnvironment.Start(this);