https://github.com/loresoft/tablestorage.abstracts
Azure Table Storage Abstracts library defines abstract base classes for repository pattern.
https://github.com/loresoft/tablestorage.abstracts
azure-storage azure-table-storage repository-pattern storage
Last synced: 5 months ago
JSON representation
Azure Table Storage Abstracts library defines abstract base classes for repository pattern.
- Host: GitHub
- URL: https://github.com/loresoft/tablestorage.abstracts
- Owner: loresoft
- License: mit
- Created: 2024-04-08T15:37:20.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-06T07:36:43.000Z (5 months ago)
- Last Synced: 2025-05-07T04:47:09.279Z (5 months ago)
- Topics: azure-storage, azure-table-storage, repository-pattern, storage
- Language: C#
- Homepage:
- Size: 133 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# TableStorage.Abstracts
Azure Table Storage Abstracts library defines abstract base classes for repository pattern.
[](https://github.com/loresoft/TableStorage.Abstracts/actions)
[](https://www.nuget.org/packages/TableStorage.Abstracts/)
[](https://coveralls.io/github/loresoft/TableStorage.Abstracts?branch=main)
## 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
```powershell
Install-Package TableStorage.Abstracts
```More information about NuGet package available at
## 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
```c#
services.AddTableStorageRepository("AzureStorage");
```Example appsettings.json file
```json
{
"AzureStorage": "UseDevelopmentStorage=true"
}
```Register with the Azure storage connection string passed in
```c#
services.AddTableStorageRepository("UseDevelopmentStorage=true");
```Resolve `ITableRepository`
```c#
var repository = serviceProvider.GetRequiredService>();
```Resolve `TableServiceClient`
```c#
var tableServiceClient = serviceProvider.GetRequiredService();
```### Custom Repository
Create a custom repository instance by inheriting `TableRepository`
```c#
public class UserRepository : TableRepository
{
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.
```c#
var repository = serviceProvider.GetRequiredService>();
var readResult = await repository.FindAsync(rowKey, partitionKey);
```Find all by filter expression
```c#
var queryResults = await repository.FindAllAsync(r => r.IsActive);
```Find all by filter expression
```c#
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.
```c#
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
```c#
var createdItem = await repository.CreateAsync(item);
```Update an item
```c#
var updatedItem = await repository.UpdateAsync(item);
```Save or Upsert an item
```c#
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
```c#
await repository.BatchAsync(items);
```Batch Merge data
```c#
await repository.BatchAsync(items, TableTransactionActionType.UpdateMerge);
```