Repository Pattern for Azure Table Storage, Blob Storage, Queues and AnalyticsI 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.
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:
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:
Also inserting the data can be achieved by InsertAsync method. Which executes:
so does filtering:
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