Skip to content

.NET Standard 2.0 library with helpers to convert your POCO objects to Azure Table Storage objects of type ITableEntity or DynamicTableEntity and vice versa.

License

Notifications You must be signed in to change notification settings

rgbweb/AzureTableStoragePocoConverter

Repository files navigation

Azure Table Storage POCO Converter

Build status Release version NuGet version License: MIT

.NET Standard 2.0 library with helpers to convert your POCO objects to Azure Table Storage objects of type ITableEntity or DynamicTableEntity and vice versa.

It supports saving complex classes by converting not natively supported property types into JSON strings.

There is no need to have dedicated properties called PartitionKey and RowKey. Just decorate your properties with the [PartitionKey] and [RowKey] attributes. There is also an optional [ETag] attribute to read the ETag and ensure optimistic concurrency when writing it back. You can decorate a property with the [Timestamp] attribute to read the auto-generated time stamp from your table entity.

NuGet Package

https://www.nuget.org/packages/AzureTableStoragePocoConverter/

Install the package AzureTableStoragePocoConverter

How to use

You can find a working example application in the AzureTableStoragePocoConverter.Sample project in this repository.

Create your POCO class

Create your POCO classes and decorate properties with the required [PartitionKey] and [RowKey] attributes and optionally with [ETag] and [Timestamp] attributes from AzureTableStoragePocoConverter.Attributes namespace. You can use the [IgnoreProperty] from Microsoft.WindowsAzure.Storage.Table namespace to exclude properties from being added to the Table Storage entity.

// Simple object - no base class needed
public class Customer
{
    // PartitionKey must be of type string and exist exactly one time per class
    [PartitionKey]
    public string LastName { get; set; }

    // PartitionKey must be of type string and exist exactly one time per class
    [RowKey]
    public string FirstName { get; set; }

    // ETag is optional and can safely have a private setter
    [ETag]
    public string ETag { get; private set; }

    // Timestamp is optional and can safely have a private setter
    [Timestamp]
    public DateTimeOffset Timestamp { get; private set; }

    // Ordinary string field will be stored as string in the entity
    public string Email { get; set; }

    // Complex type will be converted to JSON ans stored as string
    public Address Address { get; set; }

    // IgnoreProperty marks a property to be ignored when creating the TableEntity
    [IgnoreProperty]
    public string TemporaryLoginHash { get; set; }
}

Save Data to Table Storage

Convert your data object to ITableEntity in order to save your data to Azure Table Storage using an operation like InsertOrReplace.

// Convert POCO to ITableEntity object using TableEntityConvert.ToTableEntity()
var tableEntity = TableEntityConvert.ToTableEntity(customer);

// Save the new or updated entity in the Storage
var operation = TableOperation.InsertOrReplace(tableEntity);
await _cloudTable.ExecuteAsync(operation);

Read Data from Table Storage

Read your data from Azure Table Storage by using the ordinary Retrieve operation without providing a Type to it. Than convert it back to your POCO with a single method call.

// Load the DynamicTableEntity object from Storage using the keys
var operation = TableOperation.Retrieve(lastName, firstName);
var result = await _cloudTable.ExecuteAsync(operation);

// Convert into the POCO using TableEntityConvert.FromTableEntity<T>()
var customer = TableEntityConvert.FromTableEntity<Customer>(result.Result);

Releases

Have a look at the Releases page

Ask, Help, Contribute

Feel free to open an issue if you have questions ore want to help in some way.

License

The MIT license can be found here

About

.NET Standard 2.0 library with helpers to convert your POCO objects to Azure Table Storage objects of type ITableEntity or DynamicTableEntity and vice versa.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages