Azure Table Storage with Xamarin.Forms
This is the second post of the series on Azure Storage & Xamarin. Besides, this entry is part of Xamarin Month, which is an initiative by Luis Matos. Read other interesting publications and learn from the experts in several topics related with Xamarin and other technologies!
Azure Storage, one of the services included in the Azure offer, can be used to store information at a really low cost. You can use it to store NoSQL data, which means that the data not necessarily contains relationships or is modeled “the same way” a SQL table is. Microsoft provides SDKs for our apps in several languages and technologies (C# and .NET included of course) or we can also use the REST API to implement it.
In this post you’ll learn how to develop a mobile application which stores information on the cloud by using the WindowsAzure.Storage SDK! The source code is available on GitHub, of course, download it from here.
Steps 1-3: Firstly, if you don’t have an Azure Storage Account created, follow Steps 1-3 from my previous publication, Azure Blob Storage with Xamarin.Forms.
After completing Step 3, you get both the key and the connection string. We’ll use the latter to connect to the Azure Storage Account, so copy it and keep it somewhere.
Step 4. Now open Visual Studio and create a Mobile App (Xamarin.Forms) project with the name MusicApp.
Step 5. The code sharing strategy is .NET Standard.
Step 6. After it is created, right click on the solution and select Manage Nuget Packages for Solution
Step 7. Search and install the WindowsAzure.Storage package in all the projects included in the solution. This is actually the Storage SDK which will allow us to include cloud storage support in our mobile apps seamlessly.
Step 8. In the .NET Standard project MusicApp (which by the way shares the main functionality across platforms), create the TableStorageService class (inside a Services folder). This one handles the Table Storage functionality. You can identify three main elements in the code:
- A CloudStorageAccount instance which uses the CloudStorageAccount.Parse method to set up a connection to the storage account that you just created in Azure (the parameter is actually the Connection String from Step 3 -which is included in the Helpers/Constants.cs file in the source code-).
- A CloudTableClient, which is a client connected to the table storage account. It uses the connection string from the CloudStorageAccount.
- A CloudTable, which is a direct pointer to an AzureTable in our cloud Storage Account (think of it as a table). This table will be created from our application (although it is also possible to create it in the portal).
These elements are used to list all the entities (GetSongs method), insert or update a new entry (SaveSong method), and remove an element (DeleteSong) on the table:
- To list all the songs you use the ExecuteQuerySegmentedAsync method of the CloudTable. You use a continuation token because the information is retrieved in pages (segments).
- To add a new song to the table or update one, the TableOperation.InsertOrMerge method is used. A 204 Status Code is retrieved if the operation was successful.
- To remove a song from the table, the TableOperation.Delete method is used. A 204 Status Code is retrieved if the operation was successful
This is the actual code that implements what was just explained in this step.
Step 9. The rest of the code is really easy:
- There are two Content Pages:
- SongListView contains a ListView which shows all the songs saved on the table. This list is filled up with the collection of entities retrieved by the TableStorageService.GetSongs method. The user can select a song and go to the second page.
- SongView is a page which allows the user to save a new song, edit the one the one previously selected or delete it from the table storage.
Step 10. You can see the app working after you run and deploy it on an emulator or device. This is a test on Android Emulator.
First, click on the Add button:
Now fill-in the information. For instance:
After clicking on the Save button, you should receive a Success message.
And when you go back to the list, you’ll see the entity on the list:
Click on it and you’ll be able to edit it. For instance, let’s change the song name:
Clicking on the Save button should retrieve a Success message again:
And we see the song is updated.
Clicking on the image again will take us back to the edit page. Now if we click on Delete, a warning message should appear.
Should the user click on Yes, the entity will actually be removed from the table.
And, since it was the only entry, the table is empty (but it exists, of course):
As you just saw, it is really easy to store cloud database-like information from our Xamarin mobile apps. If you want to learn more about it, just remember that the documentation is always your friend.
Thanks for your visit and I hope this publication was useful for you. Don’t forget to share it with your peeps and colleagues ?
See you next time,
-Luis