Skip to content

Commit 1c6839e

Browse files
committed
Support Timestamp property in POCO entities
It's sometimes useful to be able to read the Timestamp value managed by Table Storage, so we just allow POCOs to be able to read it by just declaring a property with that name. Fixes #60
1 parent b23679d commit 1c6839e

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ var repo = TableRepository.Create<Product>(...,
185185
> NOTE: when using alternative serializers, entities might need to be annotated with whatever
186186
> attributes are required by the underlying libraries.
187187
188+
> NOTE: if access to the `Timestamp` managed by Table Storage for the entity is needed, just declare a property
189+
> with that name with either `DateTimeOffset`, `DateTime` or `string` type.
190+
188191
### Attributes
189192

190193
If you want to avoid using strings with the factory methods, you can also annotate the

src/TableStorage/TableRepository`1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ T ToEntity(DynamicTableEntity entity)
192192
if (rowKeyProperty != null)
193193
writer.WriteString(rowKeyProperty, entity.RowKey);
194194

195+
writer.WriteString(nameof(ITableEntity.Timestamp), entity.Timestamp);
196+
195197
foreach (var property in entity.Properties)
196198
{
197199
switch (property.Value.PropertyType)

src/Tests/RepositoryTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ public void CanSpecifyRowKeyLambda()
159159
TablePartition.Create<EntityNoRowKey>(CloudStorageAccount.DevelopmentStorageAccount, e => e.Id ?? "");
160160
}
161161

162+
[Fact]
163+
public async Task CanReadTimestamp()
164+
{
165+
await TablePartition
166+
.Create<TimestampedEntity>(
167+
CloudStorageAccount.DevelopmentStorageAccount,
168+
"Timestamped", "Timestamped", e => e.ID)
169+
.PutAsync(
170+
new TimestampedEntity("Foo"));
171+
172+
Assert.NotNull((await TablePartition
173+
.Create<TimestampedEntity>(
174+
CloudStorageAccount.DevelopmentStorageAccount,
175+
"Timestamped", "Timestamped", e => e.ID)
176+
.GetAsync("Foo"))!.Timestamp);
177+
178+
Assert.NotNull((await TablePartition
179+
.Create<StringTimestampedEntity>(
180+
CloudStorageAccount.DevelopmentStorageAccount,
181+
"Timestamped", "Timestamped", e => e.ID)
182+
.GetAsync("Foo"))!.Timestamp);
183+
184+
Assert.NotNull((await TablePartition
185+
.Create<TimestampedDateTimeEntity>(
186+
CloudStorageAccount.DevelopmentStorageAccount,
187+
"Timestamped", "Timestamped", e => e.ID)
188+
.GetAsync("Foo"))!.Timestamp);
189+
}
190+
162191
[Fact]
163192
public void DefaultTableNameUsesAttribute()
164193
{
@@ -385,5 +414,20 @@ record AttributedRecordEntity([PartitionKey] string Kind, [RowKey] string ID)
385414
public string? Status { get; set; }
386415
public string? Reason { get; set; }
387416
}
417+
418+
record TimestampedEntity(string ID)
419+
{
420+
public DateTimeOffset? Timestamp { get; set; }
421+
}
422+
423+
record StringTimestampedEntity(string ID)
424+
{
425+
public string? Timestamp { get; set; }
426+
}
427+
428+
record TimestampedDateTimeEntity(string ID)
429+
{
430+
public DateTime? Timestamp { get; set; }
431+
}
388432
}
389433
}

0 commit comments

Comments
 (0)