Skip to main content

TableStorage by Steven Thuriot

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: TableStorage

Package Description

Author: Steven Thuriot

NuGet: https://www.nuget.org/packages/TableStorage/

You can find more details at https://github.com/StevenThuriot/TableStorage

Source : https://github.com/StevenThuriot/TableStorage

Original Readme

note

TableStorage

Streamlined way of working with Azure Data Tables

Installation

dotnet add package TableStorage

Usage

Create your own TableContext and mark it with the [TableContext] attribute. This class must be partial.

[TableContext]
public partial class MyTableContext;

Create your models, these must be classes and have a parameterless constructor. Mark them with the [TableSet] attribute. This class must be partial.

[TableSet]
public partial class Model
{
public string Data { get; set; }
public bool Enabled { get; set; }
}

Properties can also be defined using the [TableSetProperty] attribute. This is particularly useful if you are planning on using dotnet 8+'s Native AOT, as the source generation will make sure any breaking reflection calls are avoided by the Azure.Core libraries.

[TableSet]
[TableSetProperty(typeof(string), "Data")]
[TableSetProperty(typeof(bool), "Enabled")]
public partial class Model;

Some times it's also nice to have a pretty name for your PartitionKey and RowKey properties, as the original names might not always make much sense when reading your code, at least not in a functional way. You can use the [PartitionKeyAttribute] and [RowKeyAttribute] attributes to create a proxy for these two properties.

[TableSet]
[PartitionKey("MyPrettyPartitionKey")]
[RowKey("MyPrettyRowKey")]
public partial class Model;

Place your tables on your TableContext. The sample below will create 2 tables in table storage, named Models1 and Models2.

[TableContext]
public partial class MyTableContext
{
public TableSet<Model> Models1 { get; set; }
public TableSet<Model> Models2 { get; set; }
}

Register your TableContext in your services. An extension method will be available specifically for your context.

builder.Services.AddMyTableContext(builder.Configuration.GetConnectionString("MyConnectionString"));

Optionally, pass along a Configure method to adjust some configuration options.

builder.Services.AddMyTableContext(builder.Configuration.GetConnectionString("MyConnectionString"), Configure);

static void Configure(TableOptions options)
{
options.AutoTimestamps = true;
options.TableMode = TableUpdateMode.Merge;
}

Inject MyTableContext into your class and use as needed.

public class MyService(MyTableContext context)
{
private readonly MyTableContext _context = context;

public async Task DoSomething(CancellationToken token)
{
var entity = await _context.Models1.GetEntityOrDefaultAsync("partitionKey", "rowKey", token);
if (entity is not null)
{
//Do more
}
}
}

For some special cases, your table name might not be known at compile time. To handle those, an extension method has been added:

var tableSet = context.GetTableSet<Model>("randomname");

Linq

A few simple Linq extension methods have been provided in the TableStorage.Linq namespace that optimize some existing LINQ methods specifically for Table Storage.

Since these return an instance that implements IAsyncEnumerable, System.Linq.Async is an excellent companion to these methods. Do keep in mind that as soon as you start using IAsyncEnumerable, any further operations will run client-side.

Note: Select will include the actual transformation. If you want the original model, with only the selected fields retrieved, use SelectFields instead. If you are using Native AOT, you will need to use SelectFields as Select will not work.

About

note

Generate resources for accessing Azure Table Storage

How to use

Example ( source csproj, source files )

This is the CSharp Project that references TableStorage

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.13.1" />
<PackageReference Include="Azure.Storage.Files.Shares" Version="12.1.0" />
<PackageReference Include="Azure.Storage.Queues" Version="12.11.1" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.5.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="TableStorage" Version="4.2.1" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX


using System;

namespace TableStorage
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class TableContextAttribute : Attribute
{
}
}

Usefull

Download Example (.NET C# )

Share TableStorage

https://ignatandrei.github.io/RSCG_Examples/v2/docs/TableStorage

In the same category (Database) - 3 other generators

Breezy

Dapper.AOT

Gedaq