Udostępnij za pośrednictwem


CRUDing with “ActiveRecord for Azure”

In my previous blog post I introduced you to my little experiment – a sample implementation of the ActiveRecord pattern for the Windows Azure Storage system which I call “ActiveRecord for Azure”. (It’s easier to refer to something it it has a name – right?). In this post I want to elaborate a little bit further on the features previously mentioned. Since most of us associate the ActiveRecord pattern with MVC style apps I’m going to show the Create, Read, Update and Delete (CRUD) support by implementing a simple Task List application using ASP.NET MVC.

Wire it up

First I’ve to set up the “ActiveRecord for Azure” environment – that’s; create tables, wire up configuration change handlers etc. I’ve put all this kind of logic into the ActiveRecordEnvironment class. All I have to do is to call its Start- method. Probably the best place to do this is in the RoleInstance startup event. To do that I just create a class which derives from RoleEntryPoint and overrides its OnStart –method.

 public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {
        //start the ActiveRecord environment
        ActiveRecordEnvironment.Start(this);

        return base.OnStart();
    }
}

The Model

I’m now ready to implement my Model. Since it’s a Task List application I’m building the Model at least  needs to have a Task entity. To store an entity in Windows Azure Tables it has to have three properties called RowKey, PartitionKey and Timestamp .  You could certainly write these yourself (or derive from TableServiceEntity) but you have to handle the generation and associations of RowKeys and PartitionKeys “manually”.  The ActiveRecord base class takes care of this for me and also equips the entity with the CRUD operations.

 public class Task : ActiveRecord<Task>
{
    [Required]
    public string Title { get; set; }
    
    public DateTime Due { get; set; }

    public bool Complete { get; set; }
}

As you probably already recognized I’m using the validation attribute Required from the System.ComponentModel.DataAnnotations namespace to mark the Title property as mandatory. ASP.NET MVC 2 now knows how to handle these validation attributes. (No more validation logic scattered through out the app – yay!).

The Controller

Listing all Tasks is as simple as calling the All-method on the Task class (provided by the ActiveRecord base class)

 public ActionResult Index() {
    return View(Task.All());
}

As you can see in the below Edit Action Method the data access code is minimal. Allowing you to focus on the business problem while leveraging the performance of Windows Azure Table Storage. The “ActiveRecord for Azure” specific code rows are commented with “AR4A” – all other rows are just plain vanilla ASP.NET MVC.

 [HttpPost]
public ActionResult Edit(string id, Task task) {
    var existingTask = Task.Find(id); //AR4A

    try {
        UpdateModel(existingTask);
        existingTask.Save(); //AR4A

        return RedirectToAction("Index");
    }catch {
        return View(task);
    }
}

To delete an entity I just call (you probably already figured this one out – didn’t you?) – Delete.

 [HttpPost]
public ActionResult Delete(string id) {
    Task.Delete(id); //AR4A
    return RedirectToAction("Index");
}

In the above example I called the static Delete- method and passed in the Id (RowId) of the Task to delete. I could also have called the Delete method on the specific Task instance to delete it.

Wrapping it up

So in just a few minutes I built myself a Task List application using Windows Azure and ASP.NET MVC 2. It’s so nice not having to spend hours on infrastructure code that in itself doesn’t provide any business value – isn’t it?

image

You can download the full example below. Please remember – this comes as-is and should be considered example code only.