Written by Irmak Tevfik on 18 - Oct - 2014

Repository Pattern for Azure Table Storage, Blob Storage, Queues and Analytics

I have gone through Azure Storage services this weekend and thought it would be a good idea to create a basic repository pattern for all of the services, which currently we can work on Blob/Table Storage, Queues and Analytic. Before we begin, I have uploaded the project to GitHub, where you can find from the link below:


Idea is simple. I have started by adding a new Cloud Service with a Web Role Project (MVC). After the project creation you realize a class inherit from RoleEntryPoint has been added to the MVC project. 

On next step, I have created a new Library to hold our Repository classes. Created project should have Windows Azure Storage and Windows Azure Configuration Manager Nuget packages installed like below.

Ok. Created project has a class called AzureStorageRepository which all of the repositories are derived from. In this class I have created our CloudStorageAccount object to be initialized on constructor. If you are willing to use the project, please make sure to change the connection string referenced on this initialization.

public class AzureStorageRepository : RoleEntryPoint
    internal CloudStorageAccount storageAccount;
    public AzureStorageRepository()
        // Retrieve the storage account from the connection string.
        storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureStorageConnectionString"));
    public override bool OnStart()
        return base.OnStart();

In this case, AzureStorageConnectionString is located under properties of our Web role project.

Usage is really simple. Once you referenced the repository project to your Web role project, just create an instance of the repository class you would like to use. Lets say, if you are willing to work on Table storage and create a simple table just execute:

AzureStorageRepository.TableRepository rep = new AzureStorageRepository.TableRepository();
var result = await rep.CreateTableAsync("test");

Please note that most of the methods are async, so can be awaited. Anyway, the codes above simply calls CreateTableAsync method on our TableRepository to execute:

public async Task<bool> CreateTableAsync(string tableName)
    // Create the table if it doesn't exist.
    table = tableClient.GetTableReference(tableName);
    return await table.CreateIfNotExistsAsync();

Also inserting the data can be achieved by InsertAsync method. Which executes:

public async Task<bool> InsertAsync<T>(T tableModel, string tableName)
    // Create the table if it doesn't exist.
    table = tableClient.GetTableReference(tableName);
    TableOperation insertOperation = TableOperation.Insert(tableModel as ITableEntity);
    var result = await table.ExecuteAsync(insertOperation);
    return HttpStatusCodeHelper.GetResultFromTableResult(result);

so does filtering:
public async Task<IEnumerable<T>> GetAllAsync<T>(string tableName, string filter)
    where T : ITableEntity, new()
    table = tableClient.GetTableReference(tableName);
    TableQuery<T> query = new TableQuery<T>().Where(filter);
    var result = table.ExecuteQuery(query);
    if (result != null)
        return result as IEnumerable<T>;
        return null;

Go ahead and play around with the project using your Azure account. You can find plenty more stuff on four different repositories. 
BTW, some of the the methods are not async and needs to have a Task wrapper to be awaited.
Next topic we will work on "How to create and publish a Nuget Package" by using this project.

Enjoy coding:)
comments powered by Disqus