Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/StevenThuriot/TableStorage
Streamlined way of working with Azure Data Tables that mimics Entity Framework's way of working with DbSets. Source Generators for the Table Entities to avoid any reflection calls being made, making it easier to use Azure Data Tables in a NativeAOT project.
https://github.com/StevenThuriot/TableStorage
azure storage table
Last synced: about 1 month ago
JSON representation
Streamlined way of working with Azure Data Tables that mimics Entity Framework's way of working with DbSets. Source Generators for the Table Entities to avoid any reflection calls being made, making it easier to use Azure Data Tables in a NativeAOT project.
- Host: GitHub
- URL: https://github.com/StevenThuriot/TableStorage
- Owner: StevenThuriot
- License: mit
- Created: 2022-07-16T13:33:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-25T12:14:36.000Z (5 months ago)
- Last Synced: 2024-11-05T14:07:16.595Z (about 1 month ago)
- Topics: azure, storage, table
- Language: C#
- Homepage: https://www.nuget.org/packages/TableStorage/
- Size: 52.7 KB
- Stars: 4
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/StevenThuriot/TableStorage
README
# TableStorage
Streamlined way of working with Azure Data Tables## Installation
```bash
dotnet add package TableStorage
```## Usage
Create your own TableContext and mark it with the `[TableContext]` attribute. This class must be partial.
```csharp
[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.
```csharp
[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.```csharp
[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.```csharp
[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.
```csharp
[TableContext]
public partial class MyTableContext
{
public TableSet Models1 { get; set; }
public TableSet Models2 { get; set; }
}
```Register your TableContext in your services. An extension method will be available specifically for your context.
```csharp
builder.Services.AddMyTableContext(builder.Configuration.GetConnectionString("MyConnectionString"));
```Optionally, pass along a `Configure` method to adjust some configuration options.
```csharp
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.
```csharp
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:
```csharp
var tableSet = context.GetTableSet("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.