Skip to content

Commit c41ad86

Browse files
committed
Add missing documentation on DynamicTableEntity support
1 parent a59e86e commit c41ad86

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

readme.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ it with `[Browsable(false)]` and it will be skipped when persisting and reading
222222
Since these repository APIs are quite a bit more intuitive than working directly against a
223223
`TableClient`, you might want to retrieve/enumerate entities just by their built-in `ITableEntity`
224224
properties, like `PartitionKey`, `RowKey`, `Timestamp` and `ETag`. For this scenario, we
225-
also support creating `ITableRepository<TableEntity>` and `ITablePartition<TableEntity>`
225+
also support creating `ITableRepository<ITableEntity>` and `ITablePartition<ITableEntity>`
226226
by using the factory methods `TableRepository.Create(...)` and `TablePartition.Create(...)`
227227
without a (generic) entity type argument.
228228

@@ -236,10 +236,26 @@ var repo = TablePartition.Create(storageAccount,
236236
partitionKey: "Region");
237237

238238
// Enumerate all regions within the partition as plain TableEntities
239-
await foreach (TableEntity region in repo.EnumerateAsync())
239+
await foreach (ITableEntity region in repo.EnumerateAsync())
240240
Console.WriteLine(region.RowKey);
241241
```
242242

243+
Moverover, the returned `ITableEntity` is actually an instance of `DynamicTableEntity`, so
244+
you can downcast and access any additional stored properties, which you can persist by
245+
passing a `DynamicTableEntity` to `PutAsync`:
246+
247+
```csharp
248+
await repo.PutAsync(new DynamicTableEntity("Book", "9781473217386", "*", new Dictionary<string, EntityProperty>
249+
{
250+
{ "Title", EntityProperty.GeneratePropertyForString("Neuromancer") },
251+
{ "Price", EntityProperty.GeneratePropertyForDouble(7.32) },
252+
}));
253+
254+
var entity = (DynamicTableEntity)await repo.GetAsync("Book", "9781473217386");
255+
256+
Assert.Equal("Title", entity.Properties["Neuromancer"].StringValue);
257+
Assert.Equal(7.32, entity.Properties["Price"].DoubleValue);
258+
```
243259

244260
## Installation
245261

src/Tests/RepositoryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ await CloudStorageAccount.DevelopmentStorageAccount.CreateCloudTableClient().Get
317317
await repo.PutAsync(new DynamicTableEntity("Book", "1234", "*", new Dictionary<string, EntityProperty>
318318
{
319319
{ "Status", EntityProperty.GeneratePropertyForString("OK") },
320+
{ "Price", EntityProperty.GeneratePropertyForDouble(7.32) },
320321
}));
321322

322323
var entity = (DynamicTableEntity)await repo.PutAsync(new DynamicTableEntity("Book", "1234", "*", new Dictionary<string, EntityProperty>
@@ -326,6 +327,7 @@ await CloudStorageAccount.DevelopmentStorageAccount.CreateCloudTableClient().Get
326327

327328
Assert.Equal("OK", entity.Properties["Status"].StringValue);
328329
Assert.Equal("Done", entity.Properties["Reason"].StringValue);
330+
Assert.Equal(7.32, entity.Properties["Price"].DoubleValue);
329331

330332
await CloudStorageAccount.DevelopmentStorageAccount.CreateCloudTableClient().GetTableReference(nameof(CanMergeDynamicEntity))
331333
.DeleteIfExistsAsync();

0 commit comments

Comments
 (0)