Table of Contents

1. Prerequisites

This tutorial assumes that you are familiar with the Slazure prerequisites, C# programming, and the Microsoft Azure Table Storage.

1.1 Installing the Microsoft Azure Storage Emulator

Slazure uses .NET libraries available in the Microsoft Azure SDK for .NET 2.7, which needs to be downloaded and installed in your development environment.

First, download the Microsoft Azure Storage Emulator package in Microsoft Azure SDK for .NET 2.7 from the official Microsoft download web site or by using this link. Make sure that the MicrosoftAzureStorageEmulator.msi is checked for download before you click the 'Next' button to download the installer file to your development environment.

Unspecified
Download Microsoft Azure Storage Emulator package from the Microsoft Azure SDK

Run the the MicrosoftAzureStorageEmulator.msi file once it has downloaded. The installer process itself is pretty straight forward, and selecting all the default set options and features is usually what you want. Once it is installed you will have a Microsoft Cloud Emulator installed which will allow you to develop locally on your workstation without having to upload & install anything in the Microsoft Cloud before it is ready.

1.2 Creating a cloud project with Visual Studio

Now that the Microsoft Azure Storage Emulator package is installed we can create a new Microsoft Visual Studio project to host your Microsoft Cloud code.

Once Visual Studio is started, create a new project. As Slazure requires at least .NET Framework 4.5 you should select '.NET Framework 4.5' — or a newer version like 4.5.2 in your screenshot below — in the 'New Project' dialog box. Then select to create a 'Cloud' project of type 'Azure Cloud Service'.

Unspecified
Create a Azure Cloud Service to host your code

1.3 Installing Slazure

Adding Slazure to your Visual Studio is done using NuGet.

Right-click your Visual Studio solution and select 'Manage NuGet Packages for Solution' in the context menu. Alternatively, you can type 'Install-Package Azure.Storage.Slazure' in the NuGet Package Manager Console.

Unspecified
Open the NuGet Package Manager

Make sure that the package source is set to 'nuget.org', then Search for 'Azure.Storage.Slazure' in the NuGet Package Manager. Select the appropriate Slazure package and version according to your preferences once the query result set has returned. After you click the 'Install' button you must agree to some license agreements, after which Slazure is added to your Visual Studio solution.

Unspecified
Install Slazure using NuGet

2 Getting started

A programmer develops Slazure-based software using Visual Studio and a .NET compatible programming language like C# or VB.NET.

2.1 Adding Slazure namespaces

After the Slazure NuGet package has been installed you should add the Slazure namespaces shown below.

using SysSurge.Slazure.AzureTableStorage;
using SysSurge.Slazure.AzureTableStorage.Linq;
using SysSurge.Slazure.Core.Linq.QueryParser;

2.2 Connecting to your database server

You connect to your Azure Table Storage using Slazure by specifying the Azure connection string in the constructor like below. This will return a dynamically typed object which you can use to access your database server. The following example uses a connection string that opens a Salzure connection the the Azure Storage Emulator in your development environment:

dynamic storage = new DynStorage("UseDevelopmentStorage=true");

Alternatively, if you plan to use the proprietary Slazure Linq provider, then you should rather use the QueryableStorage<TData> class constructor since this class includes the Linq provider - it includes all the functionality of DynStorage class as it inhertits from it. Example follows below:

dynamic storage = new QueryableStorage<DynEntity>("UseDevelopmentStorage=true");

2.3 Tables

2.3.1 Accessing a table

You access Azure Table Storage tables using either DynStorage or QueryableStorage<TData>. All work with Slazure tables is done through the DynTable class.

If you know the name of the table when you write your code you can access it directly using a dynamic method with the table name, in the example below we access a table named "ExampleMessagesTable":

dynamic messagesTable = storage.ExampleMessagesTable;

Alternatively, if you don't know the name at runtime then you can access the table using the DynTable class constructor and specify the table name there; example follows below:

DynStorage storage = new DynStorage("UseDevelopmentStorage=true");
dynamic messagesTable = new DynTable(storage.StorageAccount, "ExampleMessagesTable");

2.3.2 Creating a new table

With Slazure, you automatically create Azure Table Storage tables when you access them if the table does not already exists.

2.3.3 Deleting a table

You delete an existing Azure Table Storage table by using the DynStorage.Delete() method while specifying the table name of the table that you want to delete from the database. Nothing happens if the table does not exists. In the follwing example we delete a table named "OldTable" from the Azure Table Storage database:

dynamic storage = new DynStorage("UseDevelopmentStorage=true"); 
storage.Delete("OldTable");

2.3.4 Listing tables

You can list all Azure Table Storage tables using the DynStorage.Tables property, this will return tables as CloudTable objects. In the example implementation that follows, we first need to add a new namespace:

using Microsoft.WindowsAzure.Storage.Table;

, before we start accessing CloudTable objects:

List<string> GetListOfTableNames()

    List<string> tableNames = new List<string>();
    DynStorage storage = new DynStorage("UseDevelopmentStorage=true");

    foreach (CloudTable table in storage.Tables)
    {
        tableNames.Add(table.Name);
    }

    return tableNames;
}

2.3.5 Table batch updates

To execute Azure Table Storage batch updates using Slazure you need to use the TableBatchOperation class on the Windows Azure table using the CloudTable object; you can get a reference to the Windows Azure table using the DynTable.Table property. Example implementation follows; first we need to add a new namespace:

using Microsoft.WindowsAzure.Storage.Table;

, to be able to work with TableBatchOperation:

// Get a reference to the Azure Table Storage
dynamic storage = new DynStorage("UseDevelopmentStorage=true");

// Get a reference to the exampleEmployees table, it's created if it doesn't already exist.
dynamic employeesTable = storage.exampleEmployees;

// Create the batch operation.
var batchOperation = new TableBatchOperation();

// Create an entity in the Employees table for John Doe using his surname as PartitionKey and first name as RowKey.
var employee1 = employeesTable.Entity("Doe""John");
employee1.Salary = 59000; // John earns $59k/yr.
employee1.Birthdate = new DateTime(1985, 8, 18); // John was born 08/18/1985.

// Create an entity in the Employees table for Jane Doe using her surname as PartitionKey and first name as RowKey.
var employee2 = employeesTable.Entity("Doe""Jane");
employee2.Salary = 61000; // Jane earns $61k/yr.
employee2.Birthdate = new DateTime(1990, 1, 10); // Jane was born 01/10/1990.

// Add both employees entities to the batch insert operation.
batchOperation.Insert(employee1);
batchOperation.Insert(employee2);
// -> Add more employees here if needed

// Execute the batch operation.
employeesTable.Table.ExecuteBatch(batchOperation);

2.4 Enitites

2.4.1 Accessing an entity

You access Azure Table Storage entities by either using DynTable.Entity() method, or querying for entities using the Slazure Linq Provider; both returns a DynEntity class which is also dynamically typed. When you use the DynTable.Entity() method you identify an entity by its PartitionKey alone or both its PartitionKey and RowKey. The following example shows how to query for an entity using both PartitionKey and RowKey:

// Get a reference to the Table Service storage
dynamic storage = new DynStorage("UseDevelopmentStorage=true");

// Get reference to the ExampleMessagesTable table, it's created if it doesn't already exist
dynamic employeesTable = storage.exampleEmployees;

// Get a reference to an  entity using PartitionKey to identify it
var employee = employeesTable.Entity("Doe""Jane");

// Write Jane Doe's salary to the console
Console.WriteLine(employee.FullName + "'s salary is $" +  employee.Salary + " USD/yr");

The following example does the same as the one above only this time using the Slazure Linq provider:

// Get a reference to the Table Service storage
dynamic queryableStorage = new QueryableStorage<DynEntity>("UseDevelopmentStorage=true");

// Get reference to the ExampleMessagesTable table, it's created if it doesn't already exist
QueryableTable<DynEntity> queryableEmployeesTable = queryableStorage.exampleEmployees;

// Get a reference to an  entity using PartitionKey and RowKey to identify it
var employeeQuery =
    queryableEmployeesTable.Where("PartitionKey = @0 and RowKey = @1""Doe""Jane")
    .Select("new(FullName, Salary)");

dynamic employee = employeeQuery.Cast<dynamic>().First();

// Write Jane Doe's salary to the console
Console.WriteLine(employee.FullName + "'s salary is $" +  employee.Salary + " USD/yr");

2.4.2 Creating a new entity

With Slazure, you create Azure Table Storage entities when you call the DynEntity.Save() method on the entity object in question. The following example shows how we create a new employee entity for John Doe by calling the DynEntity.Save() method:

// Get a reference to the Table Service storage
dynamic storage = new DynStorage("UseDevelopmentStorage=true");

// Get reference to the ExampleMessagesTable table, it's created if it doesn't already exist
dynamic employeesTable = storage.exampleEmployees;

// Define a new entity using PartitionKey and RowKey
var employee = employeesTable.Entity("Doe""John");

// Add some values to the employee entity
employee.FullName = "John Doe";
employee.Salary = 60000;

// Save the new entity to the Azure Table Storage
employee.Save();

2.4.3 Deleting an entity

You delete enitites using the DynTable.Delete() method

2.4.4 Listing enitites

You list enitites using the Slazure Linq provider and the SysSurge.Slazure.AzureTableStorage.Linq namespace.