Home > .NET, Azure, C# > Introducing WACEL: how to use Cloud Tables

Introducing WACEL: how to use Cloud Tables

12/11/2013

WACEL, Windows Azure Cache Extension Library, is the newcomer among Azure tools. As we can read on the CodePlex project site:

WACEL provides implementation of high-level data structures that can be shared among your services and application. You can use WACEL data structures just as if you were using local data structures such as arrays, tables, circular buffers and OLAP cubes, and these data structures are backed by Windows Azure Cache, Windows Azure Table Storage, or both.

Integrating WACEL in our application is straightforward. The library is available on NuGet:

Installing WACEL from NuGet

Installing WACEL from NuGet

At this moment, WACEL depends on the libraries System.Spatial 5.2.0 and Microsoft.Emd.Data 5.2.0, that aren’t compatible with Windows Store and Phone apps. So, for now we can install it only on .NET applications.

Using a Console Application as example, let’s see how to use WACEL Cloud Table, one of the feature that I like much. A Cloud Table provides an abstraction layer over Windows Azure Table Storage: it allows to access tables using POCO entities, without using specialized types such as TableEntity, TableOperation and TableQuery.

So, first of all, let’s create the following POCO classes:

public class Student
{
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
    public List<Exam> Exams { get; set; }

    public Student()
    {
        Exams = new List<Exam>();
    }
}

public class Exam
{
    public string Subject { get; set; }
    public DateTime Date { get; set; }
    public int Vote { get; set; }
}

In order to save a Student class and related exams on a Table Storage using WACEL Cloud Tables, we need the following code:

CloudTableStorage<Student> students =
    new CloudTableStorage<Student>("ID", "UseDevelopmentStorage=true", "students");

var student1ID = Guid.NewGuid().ToString();
var student1 = new Student
{
    ID = student1ID,
    FirstName = "Marco",
    LastName = "Minerva",
    BirthDate = new DateTime(1981, 4, 2),
    Exams = new List<Exam>()
    {
        new Exam
        {
            Subject = "Operating Systems",
            Date = new DateTime(2002,7,4),
            Vote = 28
        },
        new Exam
        {
            Subject = "Compure Architecture",
            Date = new DateTime(2005, 5,13),
            Vote = 30
        }
    }
};

var student2ID = Guid.NewGuid().ToString();
var student2 = new Student
{
    ID = student2ID,
    FirstName = "Mario",
    LastName = "Rossi",
    BirthDate = new DateTime(1976, 12, 10),
};

students.Insert(student1);
students.Insert(student2);

// Retrieves the inserted students.
var existingStudent = students.Read(student1ID.ToString());

The CloudTableStorage constructor requires the following parameters:

  • The name of the field in the POCO entity that holds the primary key of the item;
  • The connection string of the Storage Account. In this example, we use the connection string that refers to the Storage Emulator, but of course we can use any existing Azure Storage Account;
  • The name of the table in which to store data. It will be automatically created if it doesn’t exist.

Then, we create two instances of the Student class and, finally, we call the Insert method, that actually adds the complete entities to the Table Storage.

The last instruction shows how to use the Read method to retrieve an element using its primary key. If the specified key doesn’t exist, an ArgumentOutOfRangeException error is raised.

There is another way to get entities, through the List method, that allows to use query predicates. For example, if we want to retrieve all the students whose last name starts with M, we can write:

var lastNameWithM = students.List(s => s.LastName.StartsWith("M"));

The predicate can be any valid lambda expression. So, to get the students who have taken at least a 30 on an exam, we just need the following code:

var bestStudents = students.List(s => s.Exams.Any(e => e.Vote == 30));

Of course, the library provides also methods to update and delete entities:

// Deletes the student with the given ID.
students.Delete(student1ID);

// Retrieves a student and updates it.
var existingStudent = students.Read(student2ID);
existingStudent.FirstName = "Riccardo";
existingStudent.Exams.Add(new Exam
{
    Subject = "Languages",
    Date = new DateTime(2006, 10, 29),
    Vote = 26
});

students.Update(existingStudent);

In conclusion, as we have seen in this post, Cloud Tables provide the following features:

  • use of POCO types instead of TableEntity
  • Direct CRUD operations without TableOperation
  • Query using predicates instead of TableQuery

For more information and examples, we can refer to the WACEL CodePlex project site.

Categories: .NET, Azure, C#
Comments are closed.
%d bloggers like this: