Skip to content

Commit 8439d63

Browse files
authored
Improve samples for serialization customization and pagination (Azure#33053)
* Improve samples for serialization customization and pagination
1 parent 67c09d9 commit 8439d63

File tree

5 files changed

+158
-15
lines changed

5 files changed

+158
-15
lines changed

sdk/tables/Azure.Data.Tables/samples/README.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,7 @@ description: Samples for the Azure.Data.Tables client library
1212

1313
# Azure Tables client SDK Samples
1414

15-
Description of Azure Tables. Covers following functions:
16-
17-
* Create and delete a table
18-
* Includes error handling
19-
* Query tables
20-
* Create and delete entities
21-
* Upsert and update entities
22-
* Query entities
23-
* Authenticating the client
24-
* Transactional batches
25-
26-
You can find samples for each of this main functions below.
15+
You can find samples for the most common features of the tables SDK below.
2716
To get started you'll need an Azure Tables endpoint and credentials. See Azure Tables Client Library [Readme](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/tables/Azure.Data.Tables/README.md) for more information and instructions.
2817

2918
* [Create/delete tables](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/tables/Azure.Data.Tables/samples/Sample1CreateDeleteTables.md)
@@ -33,3 +22,4 @@ To get started you'll need an Azure Tables endpoint and credentials. See Azure T
3322
* [Query table entities](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/tables/Azure.Data.Tables/samples/Sample4QueryEntities.md)
3423
* [Auth](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/tables/Azure.Data.Tables/samples/Sample0Auth.md): Authenticate with connection strings, shared keys, and shared access signatures0.
3524
* [Transactional batches](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/tables/Azure.Data.Tables/samples/Sample6TransactionalBatch.md)
25+
* [Customizing serialization](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/tables/Azure.Data.Tables/samples/Sample7Serialization.md)

sdk/tables/Azure.Data.Tables/samples/Sample4QueryEntities.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,34 @@ foreach (Page<TableEntity> page in queryResultsMaxPerPage.AsPages())
9696
}
9797
}
9898
```
99+
100+
In more advanced scenarios, it may be necessary to store the continuation token returned from the service so your code controls exactly when the next pages is fetched.
101+
Below is a simple example that illustrates how the token can be fetched and applied to paginated results.
102+
103+
```C# Snippet:TablesSample4QueryPagination
104+
string continuationToken = null;
105+
bool hadResults = true;
106+
while (hadResults)
107+
{
108+
Page<TableEntity> page = tableClient
109+
.Query<TableEntity>()
110+
.AsPages(continuationToken, pageSizeHint: 10)
111+
.FirstOrDefault(); // Note: Since the pageSizeHint only limits the number of results in a single page, we explicitly only enumerate the first page.
112+
113+
if (page == null)
114+
break;
115+
116+
// Get the continuation token from the page.
117+
// Note: This value can be stored so that the next page query can be executed later.
118+
continuationToken = page.ContinuationToken;
119+
120+
IReadOnlyList<TableEntity> pageResults = page.Values;
121+
hadResults = pageResults.Any();
122+
123+
// Print out the results for this page.
124+
foreach (TableEntity result in pageResults)
125+
{
126+
Console.WriteLine($"{result.PartitionKey}-{result.RowKey}");
127+
}
128+
}
129+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Transactional Batches
2+
3+
This sample demonstrates the approaches available for customizing model serialization.
4+
5+
## Ignoring and renaming model properties for serialization
6+
7+
Decorating a model property with the `[IgnoreDataMember]` attribute will ignore it on serialization and the `[DataMember(Name = "some_new_name")]` will rename the property.
8+
Below is an example model class utilizing these attributes.
9+
10+
```C# Snippet:TablesSample7ModelPropertiesClass
11+
// Define a strongly typed entity by implementing the ITableEntity interface.
12+
public class CustomSerializationEntity : ITableEntity
13+
{
14+
public string Product { get; set; }
15+
public double Price { get; set; }
16+
public int Quantity { get; set; }
17+
public string PartitionKey { get; set; }
18+
public string RowKey { get; set; }
19+
public DateTimeOffset? Timestamp { get; set; }
20+
public ETag ETag { get; set; }
21+
22+
[IgnoreDataMember] public string IgnoreMe { get; set; }
23+
24+
[DataMember(Name = "rename_me")] public string RenameMe { get; set; }
25+
}
26+
```
27+
28+
After defining our custom model, let's use it and inspect how it transforms the properties.
29+
30+
```C# Snippet:TablesSample7ModelProperties
31+
// Construct a new TableClient using a TokenCredential.
32+
var client = new TableClient(
33+
new Uri(storageUri),
34+
tableName,
35+
new DefaultAzureCredential());
36+
37+
// Create the table if it doesn't already exist.
38+
client.CreateIfNotExists();
39+
40+
// Create a new entity with our customization attributes.
41+
var entity = new CustomSerializationEntity
42+
{
43+
PartitionKey = "CustomInventory",
44+
RowKey = "special stock",
45+
Product = "Fancy Marker",
46+
Price = 1.00,
47+
Quantity = 42,
48+
IgnoreMe = "nothing to see here",
49+
RenameMe = "This property will be saved to the table as 'rename_me'"
50+
};
51+
52+
// Add the entity to the table. It will be serialized according to our customizations.
53+
await client.AddEntityAsync(entity);
54+
55+
// Fetch the entity as a TableEntity so that we can verify that things were serialized as expected.
56+
var fetchedEntity = await client.GetEntityAsync<TableEntity>(entity.PartitionKey, entity.RowKey);
57+
58+
// Print each property name to the console.
59+
foreach (string propertyName in fetchedEntity.Value.Keys)
60+
{
61+
Console.WriteLine(propertyName);
62+
}
63+
/*
64+
Console output:
65+
66+
odata.etag
67+
PartitionKey
68+
RowKey
69+
Timestamp
70+
Product
71+
Price
72+
Quantity
73+
rename_me
74+
*/
75+
}
76+
```

sdk/tables/Azure.Data.Tables/tests/samples/Sample4_QueryEntities.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using Azure.Core.TestFramework;
6-
using NUnit.Framework;
7-
using Azure.Data.Tables.Tests;
85
using System.Collections.Generic;
96
using System.Linq;
7+
using Azure.Core.TestFramework;
8+
using Azure.Data.Tables.Tests;
9+
using NUnit.Framework;
1010

1111
namespace Azure.Data.Tables.Samples
1212
{
@@ -109,6 +109,35 @@ public void QueryEntities()
109109
}
110110
#endregion
111111

112+
#region Snippet:TablesSample4QueryPagination
113+
114+
string continuationToken = null;
115+
bool hadResults = true;
116+
while (hadResults)
117+
{
118+
Page<TableEntity> page = tableClient
119+
.Query<TableEntity>()
120+
.AsPages(continuationToken, pageSizeHint: 10)
121+
.FirstOrDefault(); // Note: Since the pageSizeHint only limits the number of results in a single page, we explicitly only enumerate the first page.
122+
123+
if (page == null)
124+
break;
125+
126+
// Get the continuation token from the page.
127+
// Note: This value can be stored so that the next page query can be executed later.
128+
continuationToken = page.ContinuationToken;
129+
130+
IReadOnlyList<TableEntity> pageResults = page.Values;
131+
hadResults = pageResults.Any();
132+
133+
// Print out the results for this page.
134+
foreach (TableEntity result in pageResults)
135+
{
136+
Console.WriteLine($"{result.PartitionKey}-{result.RowKey}");
137+
}
138+
}
139+
#endregion
140+
112141
serviceClient.DeleteTable(tableName);
113142
}
114143
}

sdk/tables/Azure.Data.Tables/tests/samples/Sample8_CustomizingSerialization.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public async Task CustomizeSerialization()
2020
string storageUri = StorageUri;
2121
string tableName = "OfficeSupplies" + _random.Next();
2222

23+
#region Snippet:TablesSample7ModelProperties
2324
// Construct a new TableClient using a TokenCredential.
2425
var client = new TableClient(
2526
new Uri(storageUri),
@@ -59,7 +60,22 @@ public async Task CustomizeSerialization()
5960
{
6061
Console.WriteLine(propertyName);
6162
}
63+
/*
64+
Console output:
65+
66+
odata.etag
67+
PartitionKey
68+
RowKey
69+
Timestamp
70+
Product
71+
Price
72+
Quantity
73+
rename_me
74+
*/
6275
}
76+
#endregion
77+
78+
#region Snippet:TablesSample7ModelPropertiesClass
6379

6480
// Define a strongly typed entity by implementing the ITableEntity interface.
6581
public class CustomSerializationEntity : ITableEntity
@@ -76,5 +92,6 @@ public class CustomSerializationEntity : ITableEntity
7692

7793
[DataMember(Name = "rename_me")] public string RenameMe { get; set; }
7894
}
95+
#endregion
7996
}
8097
}

0 commit comments

Comments
 (0)