Skip to content

Azure Table Storage Abstracts library defines abstract base classes for repository pattern.

License

Notifications You must be signed in to change notification settings

loresoft/TableStorage.Abstracts

Repository files navigation

TableStorage.Abstracts

Azure Table Storage Abstracts library defines abstract base classes for repository pattern.

Build status

NuGet Version

Coverage Status

Download

The TableStorage.Abstracts library is available on nuget.org via package name TableStorage.Abstracts.

To install TableStorage.Abstracts, run the following command in the Package Manager Console

Install-Package TableStorage.Abstracts

More information about NuGet package available at https://nuget.org/packages/TableStorage.Abstracts

Features

  • find one, many and by paged results
  • create, update and delete pattern
  • batch processing for bulk insert/update
  • table initialization on first use

Usage

Dependency Injection

Register services with the Azure storage connection string named 'AzureStorage' loaded from configuration

services.AddTableStorageRepository("AzureStorage");

Example appsettings.json file

{
  "AzureStorage": "UseDevelopmentStorage=true"
}

Register with the Azure storage connection string passed in

services.AddTableStorageRepository("UseDevelopmentStorage=true");

Resolve ITableRepository<T>

var repository = serviceProvider.GetRequiredService<ITableRepository<Item>>();

Resolve TableServiceClient

var tableServiceClient = serviceProvider.GetRequiredService<TableServiceClient>();

Custom Repository

Create a custom repository instance by inheriting TableRepository<T>

public class UserRepository : TableRepository<User>
{
    public UserRepository(ILoggerFactory logFactory, TableServiceClient tableServiceClient)
        : base(logFactory, tableServiceClient)
    { }

    protected override void BeforeSave(User entity)
    {
        // use email as partition key
        entity.PartitionKey = entity.Email;

        base.BeforeSave(entity);
    }

    // uses typeof(TEntity).Name by default, override with custom table name
    protected override string GetTableName() => "UserMembership";
}

Query Operations

Find an entity by row and partition key. Note, table storage requires both.

var repository = serviceProvider.GetRequiredService<ITableRepository<Item>>();
var readResult = await repository.FindAsync(rowKey, partitionKey);

Find all by filter expression

var queryResults = await repository.FindAllAsync(r => r.IsActive);

Find all by filter expression

var itemResult = await repository.FindOneAsync(r => r.Name == itemName);

Find a page of items by filter. Note, Azure Table storage only supports forward paging by Continuation Token.

var pageResult = await repository.FindPageAsync(
    filter: r => r.IsActive,
    pageSize: 20);

// loop through pages
while (!string.IsNullOrEmpty(pageResult.ContinuationToken))
{
    // get next paging using previous continuation token
    pageResult = await repository.FindPageAsync(
        filter: r => r.IsActive,
        continuationToken: pageResult.ContinuationToken,
        pageSize: 20);
}

Update Operations

Create an item

 var createdItem = await repository.CreateAsync(item);

Update an item

 var updatedItem = await repository.UpdateAsync(item);

Save or Upsert an item

 var savedItem = await repository.SaveAsync(item);

RowKey and PartitionKey

Azure Table Storage requires both a RowKey and PartitionKey

The base repository will set the RowKey if it hasn't already been set using the NewRowKey() method. The default implementation is Ulid.NewUlid().ToString()

If PartitionKey hasn't been set, RowKey will be used.

Batch Operations

Bulk insert data

await repository.BatchAsync(items);

Batch Merge data

await repository.BatchAsync(items, TableTransactionActionType.UpdateMerge);

About

Azure Table Storage Abstracts library defines abstract base classes for repository pattern.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors 3

  •  
  •  
  •  

Languages